blob: d030bc3d458c2ee52b65a837bffd784c975974c6 [file] [log] [blame]
SiCong Lif44bbc52022-08-29 18:25:51 +01001/*
Anitha Raj43809ec2023-11-14 13:23:24 +00002 * Copyright (c) 2022-2023 Arm Limited.
SiCong Lif44bbc52022-08-29 18:25:51 +01003 *
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 Raj43809ec2023-11-14 13:23:24 +000024#ifndef ACL_SRC_DYNAMIC_FUSION_SKETCH_ARGUMENTPACK_H
25#define ACL_SRC_DYNAMIC_FUSION_SKETCH_ARGUMENTPACK_H
SiCong Lif44bbc52022-08-29 18:25:51 +010026
27#include "arm_compute/core/experimental/Types.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028
SiCong Lif44bbc52022-08-29 18:25:51 +010029#include <unordered_map>
30#include <vector>
31
32namespace arm_compute
33{
34namespace experimental
35{
36namespace 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 */
45template <typename T>
46class ArgumentPack
47{
48public:
Anitha Raj43809ec2023-11-14 13:23:24 +000049 /** @ref arm_compute::TensorType encodes the position of a tensor argument within the pack */
SiCong Lif44bbc52022-08-29 18:25:51 +010050 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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010056 PackElement() = default;
57 PackElement(const PackElement &elem) = default;
SiCong Lif44bbc52022-08-29 18:25:51 +010058 PackElement &operator=(const PackElement &elem) = default;
59 PackElement(PackElement &&elem) = default;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010060 PackElement &operator=(PackElement &&elem) = default;
61 PackElement(Id id, T *tensor) : id(id), tensor(tensor), ctensor(nullptr)
SiCong Lif44bbc52022-08-29 18:25:51 +010062 {
63 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010064 PackElement(Id id, const T *ctensor) : id(id), tensor(nullptr), ctensor(ctensor)
SiCong Lif44bbc52022-08-29 18:25:51 +010065 {
66 }
67
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010068 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 Lif44bbc52022-08-29 18:25:51 +010071 };
72
73public:
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010087 ArgumentPack(const std::initializer_list<PackElement> &l) : _pack{}
SiCong Lif44bbc52022-08-29 18:25:51 +010088 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010089 for (const auto &e : l)
SiCong Lif44bbc52022-08-29 18:25:51 +010090 {
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100132 if (it != _pack.end())
SiCong Lif44bbc52022-08-29 18:25:51 +0100133 {
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100169 for (int id = static_cast<int>(TensorType::ACL_SRC); id <= static_cast<int>(TensorType::ACL_SRC_END); ++id)
SiCong Lif44bbc52022-08-29 18:25:51 +0100170 {
171 auto tensor = get_tensor(static_cast<TensorType>(id));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100172 if (tensor != nullptr)
SiCong Lif44bbc52022-08-29 18:25:51 +0100173 {
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100186 for (int id = static_cast<int>(TensorType::ACL_SRC); id <= static_cast<int>(TensorType::ACL_SRC_END); ++id)
SiCong Lif44bbc52022-08-29 18:25:51 +0100187 {
188 auto tensor = get_const_tensor(static_cast<TensorType>(id));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100189 if (tensor != nullptr)
SiCong Lif44bbc52022-08-29 18:25:51 +0100190 {
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100203 for (int id = static_cast<int>(TensorType::ACL_DST); id <= static_cast<int>(TensorType::ACL_DST_END); ++id)
SiCong Lif44bbc52022-08-29 18:25:51 +0100204 {
205 auto tensor = get_tensor(static_cast<TensorType>(id));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100206 if (tensor != nullptr)
SiCong Lif44bbc52022-08-29 18:25:51 +0100207 {
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100220 for (int id = static_cast<int>(TensorType::ACL_DST); id <= static_cast<int>(TensorType::ACL_DST_END); ++id)
SiCong Lif44bbc52022-08-29 18:25:51 +0100221 {
222 auto tensor = get_const_tensor(static_cast<TensorType>(id));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100223 if (tensor != nullptr)
SiCong Lif44bbc52022-08-29 18:25:51 +0100224 {
225 dst_tensors.push_back(tensor);
226 }
227 }
228 return dst_tensors;
229 }
230
231private:
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 Raj43809ec2023-11-14 13:23:24 +0000237#endif // ACL_SRC_DYNAMIC_FUSION_SKETCH_ARGUMENTPACK_H