SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 1 | /* |
Anitha Raj | 43809ec | 2023-11-14 13:23:24 +0000 | [diff] [blame] | 2 | * Copyright (c) 2022-2023 Arm Limited. |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +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 | */ |
Anitha Raj | 43809ec | 2023-11-14 13:23:24 +0000 | [diff] [blame] | 24 | #ifndef ACL_SRC_DYNAMIC_FUSION_SKETCH_ARGUMENTPACK_H |
| 25 | #define ACL_SRC_DYNAMIC_FUSION_SKETCH_ARGUMENTPACK_H |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/experimental/Types.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 28 | |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 29 | #include <unordered_map> |
| 30 | #include <vector> |
| 31 | |
| 32 | namespace arm_compute |
| 33 | { |
| 34 | namespace experimental |
| 35 | { |
| 36 | namespace dynamic_fusion |
| 37 | { |
| 38 | /** This is a generic class that packs the arguments of an operator. For now, it is only used for tensor-related types |
| 39 | * Examples of "tensor-related types": @ref ITensorInfo, @ref ITensor, @ref ICLTensor |
| 40 | * |
| 41 | * The argument id is the position of the argument within the pack, and is represented by @ref TensorType |
| 42 | * |
| 43 | * @tparam T Tensor-related type |
| 44 | */ |
| 45 | template <typename T> |
| 46 | class ArgumentPack |
| 47 | { |
| 48 | public: |
Anitha Raj | 43809ec | 2023-11-14 13:23:24 +0000 | [diff] [blame] | 49 | /** @ref arm_compute::TensorType encodes the position of a tensor argument within the pack */ |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 50 | using Id = TensorType; |
| 51 | /** A single argument element within the pack |
| 52 | * It contains either a const pointer or a non-const pointer to the Tensor-related type T, but never at the same time |
| 53 | */ |
| 54 | struct PackElement |
| 55 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 56 | PackElement() = default; |
| 57 | PackElement(const PackElement &elem) = default; |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 58 | PackElement &operator=(const PackElement &elem) = default; |
| 59 | PackElement(PackElement &&elem) = default; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 60 | PackElement &operator=(PackElement &&elem) = default; |
| 61 | PackElement(Id id, T *tensor) : id(id), tensor(tensor), ctensor(nullptr) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 62 | { |
| 63 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 64 | PackElement(Id id, const T *ctensor) : id(id), tensor(nullptr), ctensor(ctensor) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 65 | { |
| 66 | } |
| 67 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 68 | Id id{ACL_UNKNOWN}; /**< Argument id within the pack */ |
| 69 | T *tensor{nullptr}; /**< Non-const pointer to tensor-related object */ |
| 70 | const T *ctensor{nullptr}; /**< Const pointer to tensor-related object */ |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | public: |
| 74 | /** Default constructor */ |
| 75 | ArgumentPack() = default; |
| 76 | /** Destructor */ |
| 77 | ~ArgumentPack() = default; |
| 78 | /** Allow instances of this class to be copy constructed */ |
| 79 | ArgumentPack<T>(const ArgumentPack<T> &other) = default; |
| 80 | /** Allow instances of this class to be copied */ |
| 81 | ArgumentPack<T> &operator=(const ArgumentPack<T> &other) = default; |
| 82 | /** Allow instances of this class to be move constructed */ |
| 83 | ArgumentPack<T>(ArgumentPack<T> &&other) = default; |
| 84 | /** Allow instances of this class to be moved */ |
| 85 | ArgumentPack<T> &operator=(ArgumentPack<T> &&other) = default; |
| 86 | /** Initializer list Constructor */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 87 | ArgumentPack(const std::initializer_list<PackElement> &l) : _pack{} |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 88 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 89 | for (const auto &e : l) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 90 | { |
| 91 | _pack[e.id] = e; |
| 92 | } |
| 93 | } |
| 94 | /** Add tensor to the pack |
| 95 | * |
| 96 | * @param[in] id ID of the tensor to add |
| 97 | * @param[in] tensor Tensor to add |
| 98 | */ |
| 99 | void add_tensor(Id id, T *tensor) |
| 100 | { |
| 101 | _pack[id] = PackElement(id, tensor); |
| 102 | } |
| 103 | /** Add const tensor to the pack |
| 104 | * |
| 105 | * @param[in] id ID of the tensor to add |
| 106 | * @param[in] tensor Tensor to add |
| 107 | */ |
| 108 | void add_const_tensor(Id id, const T *tensor) |
| 109 | { |
| 110 | _pack[id] = PackElement(id, tensor); |
| 111 | } |
| 112 | /** Get tensor of a given id from the pack |
| 113 | * |
| 114 | * @param[in] id ID of tensor to extract |
| 115 | * |
| 116 | * @return The pointer to the tensor if exist and is non-const else nullptr |
| 117 | */ |
| 118 | T *get_tensor(Id id) |
| 119 | { |
| 120 | auto it = _pack.find(id); |
| 121 | return it != _pack.end() ? it->second.tensor : nullptr; |
| 122 | } |
| 123 | /** Get constant tensor of a given id |
| 124 | * |
| 125 | * @param[in] id ID of tensor to extract |
| 126 | * |
| 127 | * @return The pointer to the tensor (const or not) if exist else nullptr |
| 128 | */ |
| 129 | const T *get_const_tensor(Id id) const |
| 130 | { |
| 131 | auto it = _pack.find(id); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 132 | if (it != _pack.end()) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 133 | { |
| 134 | return it->second.ctensor != nullptr ? it->second.ctensor : it->second.tensor; |
| 135 | } |
| 136 | return nullptr; |
| 137 | } |
| 138 | /** Remove the tensor stored with the given id |
| 139 | * |
| 140 | * @param[in] id ID of tensor to remove |
| 141 | */ |
| 142 | void remove_tensor(Id id) |
| 143 | { |
| 144 | _pack.erase(id); |
| 145 | } |
| 146 | /** Pack size accessor |
| 147 | * |
| 148 | * @return Number of tensors registered to the pack |
| 149 | */ |
| 150 | size_t size() const |
| 151 | { |
| 152 | return _pack.size(); |
| 153 | } |
| 154 | /** Checks if pack is empty |
| 155 | * |
| 156 | * @return True if empty else false |
| 157 | */ |
| 158 | bool empty() const |
| 159 | { |
| 160 | return _pack.empty(); |
| 161 | } |
| 162 | /** Get the ACL_SRC_* tensors |
| 163 | * |
| 164 | * @return std::vector<T *> |
| 165 | */ |
| 166 | std::vector<T *> get_src_tensors() |
| 167 | { |
| 168 | std::vector<T *> src_tensors{}; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 169 | for (int id = static_cast<int>(TensorType::ACL_SRC); id <= static_cast<int>(TensorType::ACL_SRC_END); ++id) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 170 | { |
| 171 | auto tensor = get_tensor(static_cast<TensorType>(id)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 172 | if (tensor != nullptr) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 173 | { |
| 174 | src_tensors.push_back(tensor); |
| 175 | } |
| 176 | } |
| 177 | return src_tensors; |
| 178 | } |
| 179 | /** Get the const ACL_SRC_* tensors |
| 180 | * |
| 181 | * @return std::vector<const T *> |
| 182 | */ |
| 183 | std::vector<const T *> get_const_src_tensors() const |
| 184 | { |
| 185 | std::vector<const T *> src_tensors{}; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 186 | for (int id = static_cast<int>(TensorType::ACL_SRC); id <= static_cast<int>(TensorType::ACL_SRC_END); ++id) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 187 | { |
| 188 | auto tensor = get_const_tensor(static_cast<TensorType>(id)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 189 | if (tensor != nullptr) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 190 | { |
| 191 | src_tensors.push_back(tensor); |
| 192 | } |
| 193 | } |
| 194 | return src_tensors; |
| 195 | } |
| 196 | /** Get the ACL_DST_* tensors |
| 197 | * |
| 198 | * @return std::vector<T *> |
| 199 | */ |
| 200 | std::vector<T *> get_dst_tensors() |
| 201 | { |
| 202 | std::vector<T *> dst_tensors{}; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 203 | for (int id = static_cast<int>(TensorType::ACL_DST); id <= static_cast<int>(TensorType::ACL_DST_END); ++id) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 204 | { |
| 205 | auto tensor = get_tensor(static_cast<TensorType>(id)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 206 | if (tensor != nullptr) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 207 | { |
| 208 | dst_tensors.push_back(tensor); |
| 209 | } |
| 210 | } |
| 211 | return dst_tensors; |
| 212 | } |
| 213 | /** Get the const ACL_DST_* tensors |
| 214 | * |
| 215 | * @return std::vector<const T *> |
| 216 | */ |
| 217 | std::vector<const T *> get_const_dst_tensors() const |
| 218 | { |
| 219 | std::vector<const T *> dst_tensors{}; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 220 | for (int id = static_cast<int>(TensorType::ACL_DST); id <= static_cast<int>(TensorType::ACL_DST_END); ++id) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 221 | { |
| 222 | auto tensor = get_const_tensor(static_cast<TensorType>(id)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 223 | if (tensor != nullptr) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 224 | { |
| 225 | dst_tensors.push_back(tensor); |
| 226 | } |
| 227 | } |
| 228 | return dst_tensors; |
| 229 | } |
| 230 | |
| 231 | private: |
| 232 | std::unordered_map<int, PackElement> _pack{}; /**< Container with the packed tensors */ |
| 233 | }; |
| 234 | } // namespace dynamic_fusion |
| 235 | } // namespace experimental |
| 236 | } // namespace arm_compute |
Anitha Raj | 43809ec | 2023-11-14 13:23:24 +0000 | [diff] [blame] | 237 | #endif // ACL_SRC_DYNAMIC_FUSION_SKETCH_ARGUMENTPACK_H |