Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 1 | /* |
Sang-Hoon Park | 7229182 | 2021-01-14 14:52:03 +0000 | [diff] [blame] | 2 | * Copyright (c) 2020-2021 Arm Limited. |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef ARM_COMPUTE_ITENSORPACK_H |
| 25 | #define ARM_COMPUTE_ITENSORPACK_H |
| 26 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/experimental/Types.h" |
| 28 | |
Sang-Hoon Park | 7229182 | 2021-01-14 14:52:03 +0000 | [diff] [blame] | 29 | #include <cstddef> |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 30 | #include <cstdint> |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 31 | #include <unordered_map> |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | // Forward declaration |
| 36 | class ITensor; |
| 37 | |
| 38 | /** Tensor packing service */ |
| 39 | class ITensorPack |
| 40 | { |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 41 | public: |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 42 | struct PackElement |
| 43 | { |
| 44 | PackElement() = default; |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 45 | PackElement(int id, ITensor *tensor) |
| 46 | : id(id), tensor(tensor), ctensor(nullptr) |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 47 | { |
| 48 | } |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 49 | PackElement(int id, const ITensor *ctensor) |
| 50 | : id(id), tensor(nullptr), ctensor(ctensor) |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 51 | { |
| 52 | } |
| 53 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 54 | int id{ -1 }; |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 55 | ITensor *tensor{ nullptr }; |
| 56 | const ITensor *ctensor{ nullptr }; |
| 57 | }; |
| 58 | |
| 59 | public: |
| 60 | /** Default Constructor */ |
| 61 | ITensorPack() = default; |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 62 | /** Initializer list Constructor */ |
| 63 | ITensorPack(std::initializer_list<PackElement> l); |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 64 | /** Add tensor to the pack |
| 65 | * |
| 66 | * @param[in] id ID/type of the tensor to add |
| 67 | * @param[in] tensor Tensor to add |
| 68 | */ |
| 69 | void add_tensor(int id, ITensor *tensor); |
| 70 | |
| 71 | /** Add const tensor to the pack |
| 72 | * |
| 73 | * @param[in] id ID/type of the tensor to add |
| 74 | * @param[in] tensor Tensor to add |
| 75 | */ |
| 76 | void add_tensor(int id, const ITensor *tensor); |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 77 | |
| 78 | /** Add const tensor to the pack |
| 79 | * |
| 80 | * @param[in] id ID/type of the tensor to add |
| 81 | * @param[in] tensor Tensor to add |
| 82 | */ |
| 83 | void add_const_tensor(int id, const ITensor *tensor); |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 84 | /** Get tensor of a given id from the pac |
| 85 | * |
| 86 | * @param[in] id ID of tensor to extract |
| 87 | * |
| 88 | * @return The pointer to the tensor if exist and is non-const else nullptr |
| 89 | */ |
| 90 | ITensor *get_tensor(int id); |
| 91 | /** Get constant tensor of a given id |
| 92 | * |
| 93 | * @param[in] id ID of tensor to extract |
| 94 | * |
| 95 | * @return The pointer to the tensor if exist and is const else nullptr |
| 96 | */ |
| 97 | const ITensor *get_const_tensor(int id) const; |
Sang-Hoon Park | 8b83d46 | 2021-05-25 13:12:47 +0100 | [diff] [blame] | 98 | /** Remove the tensor stored with the given id |
| 99 | * |
| 100 | * @param[in] id ID of tensor to remove |
| 101 | */ |
| 102 | void remove_tensor(int id); |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 103 | /** Pack size accessor |
| 104 | * |
| 105 | * @return Number of tensors registered to the pack |
| 106 | */ |
| 107 | size_t size() const; |
| 108 | /** Checks if pack is empty |
| 109 | * |
| 110 | * @return True if empty else false |
| 111 | */ |
| 112 | bool empty() const; |
| 113 | |
| 114 | private: |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 115 | std::unordered_map<int, PackElement> _pack{}; /**< Container with the packed tensors */ |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 116 | }; |
| 117 | } // namespace arm_compute |
| 118 | #endif /*ARM_COMPUTE_ITENSORPACK_H */ |