Compute Library
 22.08
ITensorDescPack.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 Arm Limited.
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 #ifdef ENABLE_EXPERIMENTAL_DYNAMIC_FUSION
25 #ifndef ARM_COMPUTE_EXPERIMENTAL_DYNAMICFUSION_ITENSORDESCPACK_H
26 #define ARM_COMPUTE_EXPERIMENTAL_DYNAMICFUSION_ITENSORDESCPACK_H
27 
28 #include <cstddef>
29 #include <unordered_map>
30 #include <vector>
31 
32 namespace arm_compute
33 {
34 namespace experimental
35 {
36 namespace dynamic_fusion
37 {
38 template <typename TDesc>
40 {
41 public:
42  struct PackElement
43  {
44  PackElement() = default;
45  ~PackElement() = default;
46  PackElement(const PackElement &) = default;
47  PackElement &operator=(const PackElement &) = default;
48  PackElement(PackElement &&) = default;
49  PackElement &operator=(PackElement &&) = default;
50  PackElement(int id, TDesc *tensor)
51  : id(id), tensor(tensor), ctensor(nullptr)
52  {
53  }
54  PackElement(int id, const TDesc *ctensor)
55  : id(id), tensor(nullptr), ctensor(ctensor)
56  {
57  }
58 
59  int id{ -1 };
60  TDesc *tensor{ nullptr };
61  const TDesc *ctensor{ nullptr };
62 
63  friend bool operator==(const PackElement &elem0, const PackElement &elem1)
64  {
65  const bool same_ctensor = (elem0.tensor == nullptr && elem1.tensor == nullptr && elem0.ctensor != nullptr && elem1.ctensor != nullptr && *elem0.ctensor == *elem1.ctensor);
66  const bool same_tensor = (elem0.ctensor == nullptr && elem1.ctensor == nullptr && elem0.tensor != nullptr && elem1.tensor != nullptr && *elem0.tensor == *elem1.tensor);
67 
68  return elem0.id == elem1.id && (same_ctensor || same_tensor);
69  }
70  };
71 
72 public:
73  /** Default Constructor */
74  ITensorDescPack() = default;
75  ~ITensorDescPack() = default;
76  ITensorDescPack<TDesc>(const ITensorDescPack<TDesc> &other) = default;
80  /** Initializer list Constructor */
81  ITensorDescPack(std::initializer_list<PackElement> l)
82  : _pack{}
83  {
84  for(auto &e : l)
85  {
86  _pack[e.id] = e;
87  }
88  }
89  /** Add tensor to the pack
90  *
91  * @param[in] id ID/type of the tensor to add
92  * @param[in] tensor Tensor to add
93  */
94  void add_tensor(int id, TDesc *tensor)
95  {
96  _pack[id] = PackElement(id, tensor);
97  }
98 
99  /** Add const tensor to the pack
100  *
101  * @param[in] id ID/type of the tensor to add
102  * @param[in] tensor Tensor to add
103  */
104  void add_const_tensor(int id, const TDesc *tensor)
105  {
106  _pack[id] = PackElement(id, tensor);
107  }
108  /** Get tensor of a given id from the pac
109  *
110  * @param[in] id ID of tensor to extract
111  *
112  * @return The pointer to the tensor if exist and is non-const else nullptr
113  */
114  TDesc *get_tensor(int id)
115  {
116  auto it = _pack.find(id);
117  return it != _pack.end() ? it->second.tensor : nullptr;
118  }
119  /** Get constant tensor of a given id
120  *
121  * @param[in] id ID of tensor to extract
122  *
123  * @return The pointer to the tensor if exist and is const else nullptr
124  */
125  const TDesc *get_const_tensor(int id) const
126  {
127  auto it = _pack.find(id);
128  if(it != _pack.end())
129  {
130  return it->second.ctensor != nullptr ? it->second.ctensor : it->second.tensor;
131  }
132  return nullptr;
133  }
134  /** Remove the tensor stored with the given id
135  *
136  * @param[in] id ID of tensor to remove
137  */
138  void remove_tensor(int id)
139  {
140  _pack.erase(id);
141  }
142  /** Pack size accessor
143  *
144  * @return Number of tensors registered to the pack
145  */
146  size_t size() const
147  {
148  return _pack.size();
149  }
150  /** Checks if pack is empty
151  *
152  * @return True if empty else false
153  */
154  bool empty() const
155  {
156  return _pack.empty();
157  }
158 
159  /** Get the ACL_SRC_* tensors
160  *
161  * @return std::vector<TDesc *>
162  */
163  std::vector<TDesc *> get_src_tensors()
164  {
165  std::vector<TDesc *> src_tensors{};
166  for(int id = static_cast<int>(TensorType::ACL_SRC); id <= static_cast<int>(TensorType::ACL_SRC_END); ++id)
167  {
168  auto tensor = get_tensor(id);
169  if(tensor != nullptr)
170  {
171  src_tensors.push_back(tensor);
172  }
173  }
174  return src_tensors;
175  }
176  /** Get the const ACL_SRC_* tensors
177  *
178  * @return std::vector<const TDesc *>
179  */
180  std::vector<const TDesc *> get_const_src_tensors() const
181  {
182  std::vector<const TDesc *> src_tensors{};
183  for(int id = static_cast<int>(TensorType::ACL_SRC); id <= static_cast<int>(TensorType::ACL_SRC_END); ++id)
184  {
185  auto tensor = get_const_tensor(id);
186  if(tensor != nullptr)
187  {
188  src_tensors.push_back(tensor);
189  }
190  }
191  return src_tensors;
192  }
193  /** Get the ACL_DST_* tensors
194  *
195  * @return std::vector<TDesc *>
196  */
197  std::vector<TDesc *> get_dst_tensors()
198  {
199  std::vector<TDesc *> dst_tensors{};
200  for(int id = static_cast<int>(TensorType::ACL_DST); id <= static_cast<int>(TensorType::ACL_DST_END); ++id)
201  {
202  auto tensor = get_tensor(id);
203  if(tensor != nullptr)
204  {
205  dst_tensors.push_back(tensor);
206  }
207  }
208  return dst_tensors;
209  }
210  /** Get the const ACL_DST_* tensors
211  *
212  * @return std::vector<const TDesc *>
213  */
214  std::vector<const TDesc *> get_const_dst_tensors() const
215  {
216  std::vector<const TDesc *> dst_tensors{};
217  for(int id = static_cast<int>(TensorType::ACL_DST); id <= static_cast<int>(TensorType::ACL_DST_END); ++id)
218  {
219  auto tensor = get_const_tensor(id);
220  if(tensor != nullptr)
221  {
222  dst_tensors.push_back(tensor);
223  }
224  }
225  return dst_tensors;
226  }
227 
228  friend bool operator==(const ITensorDescPack<TDesc> &pack0, const ITensorDescPack<TDesc> &pack1)
229  {
230  return pack0._pack == pack1._pack;
231  }
232 
233 private:
234  std::unordered_map<int, PackElement> _pack{}; /**< Container with the packed tensors */
235 };
236 
237 } // namespace dynamic_fusion
238 } // namespace experimental
239 } // namespace arm_compute
240 #endif //ARM_COMPUTE_EXPERIMENTAL_DYNAMICFUSION_ITENSORDESCPACK_H
241 #endif /* ENABLE_EXPERIMENTAL_DYNAMIC_FUSION */
const TDesc * get_const_tensor(int id) const
Get constant tensor of a given id.
std::vector< const TDesc * > get_const_src_tensors() const
Get the const ACL_SRC_* tensors.
void remove_tensor(int id)
Remove the tensor stored with the given id.
TDesc * get_tensor(int id)
Get tensor of a given id from the pac.
Copyright (c) 2017-2022 Arm Limited.
friend bool operator==(const ITensorDescPack< TDesc > &pack0, const ITensorDescPack< TDesc > &pack1)
std::vector< TDesc * > get_src_tensors()
Get the ACL_SRC_* tensors.
friend bool operator==(const PackElement &elem0, const PackElement &elem1)
ITensorDescPack(std::initializer_list< PackElement > l)
Initializer list Constructor.
void add_const_tensor(int id, const TDesc *tensor)
Add const tensor to the pack.
std::vector< TDesc * > get_dst_tensors()
Get the ACL_DST_* tensors.
void add_tensor(int id, TDesc *tensor)
Add tensor to the pack.
std::vector< const TDesc * > get_const_dst_tensors() const
Get the const ACL_DST_* tensors.