blob: 7b0fd1c2b70a539a724f5a362bb6b8967f1af3a3 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gunes Bayir3a1e1252023-01-03 21:26:09 +00002 * Copyright (c) 2016-2023 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_ITENSORINFO_H
25#define ARM_COMPUTE_ITENSORINFO_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
27#include "arm_compute/core/Coordinates.h"
28#include "arm_compute/core/Strides.h"
29#include "arm_compute/core/TensorShape.h"
30#include "arm_compute/core/Types.h"
31#include "arm_compute/core/Utils.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000032#include "arm_compute/core/utils/misc/Utility.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "support/ICloneable.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
35#include <cstddef>
36
37namespace arm_compute
38{
Mohammed Suhail Munshia1b1e412023-03-23 22:21:31 +000039// Note: Any changes to the fields of the class below that have setters should be mirrored
40// (if possible) in the auto_init_if_empty function in AutoConfiguration.h
41
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042/** Store the tensor's metadata */
Georgios Pinitas283c1792017-11-10 18:14:06 +000043class ITensorInfo : public misc::ICloneable<ITensorInfo>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044{
45public:
Giorgio Arena61b6e242021-09-23 12:40:39 +010046 using TensorDimsState = std::vector<int>;
SiCong Lif44bbc52022-08-29 18:25:51 +010047 /** An id that uniquely identifies an ITensorInfo within some domain (e.g. a workload)
48 */
49 using Id = int32_t;
50 /** An invalid tensor id within a domain */
Gunes Bayir3a1e1252023-01-03 21:26:09 +000051 static constexpr Id invalid_tensor_id = 0;
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +000052 /** Get the value representing dynamic dimension state
53 *
54 * @return Value representing dynamic dimension state
55 *
56 */
57 static constexpr int32_t get_dynamic_state_value()
58 {
59 return _dynamic_dimension;
60 }
61 /** Get the value representing static dimension state
62 *
63 * @return Value representing static dimension state
64 *
65 */
66 static constexpr int32_t get_static_state_value()
67 {
68 return _static_dimension;
69 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070 /** Default virtual destructor */
71 virtual ~ITensorInfo() = default;
72 /** Set the data type to the specified value.
73 *
74 * @warning This resets the format to UNKNOWN.
75 *
76 * @param[in] data_type The new data type.
Georgios Pinitas283c1792017-11-10 18:14:06 +000077 *
78 * @return Reference to this ITensorInfo object
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079 */
Georgios Pinitas283c1792017-11-10 18:14:06 +000080 virtual ITensorInfo &set_data_type(DataType data_type) = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010081 /** Set the number of channels to the specified value.
82 *
83 * @warning This resets the format to UNKNOWN.
84 *
85 * @param[in] num_channels New number of channels.
Georgios Pinitas283c1792017-11-10 18:14:06 +000086 *
87 * @return Reference to this ITensorInfo object
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088 */
Georgios Pinitas283c1792017-11-10 18:14:06 +000089 virtual ITensorInfo &set_num_channels(int num_channels) = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090 /** Set the format of an already initialized tensor.
91 *
92 * @note If the data type has already been configured (i.e. not UNKNOWN) it
93 * must match the new format. If data type hasn't been configured it will
94 * be based on the format.
95 *
96 * @param[in] format Single-plane format of the tensor.
Georgios Pinitas283c1792017-11-10 18:14:06 +000097 *
98 * @return Reference to this ITensorInfo object
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099 */
Georgios Pinitas283c1792017-11-10 18:14:06 +0000100 virtual ITensorInfo &set_format(Format format) = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101 /** Set the shape of an already initialized tensor.
102 *
103 * @warning Changing the shape requires to recompute the strides and is
104 * therefore only possible if the tensor hasn't been allocated yet.
105 *
106 * @param[in] shape New tensor shape.
Georgios Pinitas283c1792017-11-10 18:14:06 +0000107 *
108 * @return Reference to this ITensorInfo object
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109 */
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000110 virtual ITensorInfo &set_tensor_shape(const TensorShape &shape) = 0;
Georgios Pinitasb14a0f02021-01-08 03:14:31 +0000111 /** Set the state for each dimension of the tensor
112 *
113 * This sets the state of each dimension of the shape in terms of dynamic behavior using -1 where appropriate.
114 * The index in the state is a 1 to 1 mapping with the shape dimension index.
115 * For example if you want to express [?, 3, 3] as a dynamic input then [-1, 3, 3] has to be set as a state
116 *
117 * @param[in] state Tensor dimensions state
118 *
119 * @return Reference to this ITensorInfo object
120 */
121 virtual ITensorInfo &set_tensor_dims_state(const TensorDimsState &state) = 0;
Georgios Pinitas283c1792017-11-10 18:14:06 +0000122 /** Set the quantization settings (scale and offset) of the tensor.
Anthony Barbierf202e502017-11-23 18:02:04 +0000123 *
124 * @param[in] quantization_info QuantizationInfo containing the scale and offset
125 *
126 * @return Reference to this ITensorInfo object
127 */
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000128 virtual ITensorInfo &set_quantization_info(const QuantizationInfo &quantization_info) = 0;
Isabella Gottardid17a6772018-02-27 17:41:55 +0000129 /** Set the data layout of the tensor.
130 *
131 * @param[in] data_layout DataLayout containing the layout data information.
132 *
133 * @return Reference to this ITensorInfo object
134 */
135 virtual ITensorInfo &set_data_layout(const DataLayout &data_layout) = 0;
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000136 /** Resets the padding settings of the tensor.
137 *
138 * @return Reference to this ITensorInfo object
139 */
140 virtual ITensorInfo &reset_padding() = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100141 /** Update the offset to the first element and the strides to automatically computed values.
142 *
143 * @note The padding used by this method is really conservative so that the tensor can be used for most functions.
144 *
145 * @return True if the strides or the offset to the first element have changed.
146 */
147 virtual bool auto_padding() = 0;
Ramy Elgammald2d93612022-12-22 15:21:03 +0000148 /** Set the lock paddings flag of the tensor.
149 * It should be set to True, when the tensor could be mapped to camera or frame buffer.
150 *
151 * @return Reference to this ITensorInfo object
152 */
153 virtual ITensorInfo &set_lock_paddings(bool flag) = 0;
154 /** Get the lock paddings flag value
155 *
156 * @return lock paddings flag value
157 */
158 virtual bool lock_paddings() const = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159 /** Update the offset to the first element, the strides and the total size.
160 *
161 * @note This function can only increase the offset, strides and total size.
162 *
163 * @param[in] padding Padding around the XY plane in number of elements.
164 *
165 * @return True if the strides, offset and total size have changed.
166 */
167 virtual bool extend_padding(const PaddingSize &padding) = 0;
168 /** Return the size of the requested dimension
169 *
170 * @param[in] index Index of the dimension
171 *
172 * @return Dimension of the requested dimension
173 */
174 virtual size_t dimension(size_t index) const = 0;
Isabella Gottardid56e7702018-02-28 14:29:36 +0000175 /** Return the size of the requested data layout dimension
176 *
177 * @param[in] dimension DataLayoutDimension of the dimension
178 *
179 * @return Dimension of the requested dimension
180 */
181 virtual size_t dimension(DataLayoutDimension dimension) const = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100182 /** The strides in bytes for accessing each dimension of the tensor
183 *
184 * @return Strides in bytes for each tensor dimension
185 */
186 virtual const Strides &strides_in_bytes() const = 0;
187 /** The offset from the beginning of the memory allocation to the first element of the tensor.
188 * This can be used to access efficiently elements in a 2D tensor
189 *
190 * @return The offset in bytes to access the first element of the tensor.
191 */
192 virtual size_t offset_first_element_in_bytes() const = 0;
193 /** The offset in bytes from the beginning of the memory allocation to access the element at position (x, y, z ...)
194 *
195 * @param[in] pos Vector with the coordinates of the element to access.
196 * The size of this vector must be equal to the number of dimensions of the tensor
197 *
198 * @return Offset in bytes from the beginning of the memory allocation to access the element (x, y, z, ...)
199 */
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100200 virtual int32_t offset_element_in_bytes(const Coordinates &pos) const = 0;
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100201
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100202 /** Element size in bytes calculated as data_size() * num_channels()
203 *
204 * @return The size of one element in bytes
205 */
206 virtual size_t element_size() const = 0;
207 /** The number of dimensions of the tensor (rank)
208 *
209 * @return The number of dimensions of the tensor (rank)
210 */
211 virtual size_t num_dimensions() const = 0;
212 /** The number of channels for each tensor element
213 *
214 * @return The number of channels for each tensor element
215 */
216 virtual size_t num_channels() const = 0;
217 /** Size for each dimension of the tensor
218 *
219 * @return A vector with the size for each dimension of the tensor
220 */
221 virtual const TensorShape &tensor_shape() const = 0;
Georgios Pinitasb14a0f02021-01-08 03:14:31 +0000222 /** State of each dimension of the tensor shape
223 *
224 * @return A vector with the state for each dimension of the tensor, where -1 specifies dynamic dimension
225 */
226 virtual const TensorDimsState &tensor_dims_state() const = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100227 /** Data type used for each element of the tensor
228 *
229 * @return Tensor data type
230 */
231 virtual DataType data_type() const = 0;
232 /** Colour format of the image
233 *
234 * @return Colour format of the image
235 */
236 virtual Format format() const = 0;
237 /** Returns the total size of the tensor in bytes.
238 *
239 * @return Total size of the tensor in bytes.
240 */
241 virtual size_t total_size() const = 0;
242 /** Padding of tensor.
243 *
244 * @return Padding.
245 */
246 virtual PaddingSize padding() const = 0;
247 /** Checks if the tensor has been allocated with padding or not.
248 *
249 * @return True if padding is allocated in the tensor, otherwise false.
250 */
251 virtual bool has_padding() const = 0;
252 /** Flag indicating whether the size of the tensor can be changed.
253 *
254 * @return True if the tensor size can be changed.
255 */
256 virtual bool is_resizable() const = 0;
Georgios Pinitas49be2e32019-09-02 13:18:55 +0100257 /** Flag indicating whether the shape of the tensor is dynamic, meaning that it can change on kernel/function execution.
258 *
259 * @return True if its dynamic else false
260 */
261 virtual bool is_dynamic() const = 0;
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100262 /** Flag indicating whether the values of the tensor are constant, meaning that they can change on kernel/function execution.
263 *
264 * @return True if values are constant else false
265 */
266 virtual bool are_values_constant() const = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100267 /** Set the flag whether the tensor size can be changed.
268 *
269 * @param[in] is_resizable Flag that marks the tensor if it can be changed or not.
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000270 *
271 * @return Reference to this ITensorInfo object
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100272 */
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000273 virtual ITensorInfo &set_is_resizable(bool is_resizable) = 0;
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100274 /** Set the flag whether the tensor values can change during kernel/function execution.
275 *
276 * @param[in] are_values_constant Flag that marks the tensor values if they can be changed or not.
277 *
278 * @return Reference to this ITensorInfo object
279 */
280 virtual ITensorInfo &set_are_values_constant(bool are_values_constant) = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100281 /** Valid region of the tensor. All elements in the valid region have defined values, i.e. are not undefined.
282 *
283 * @return The valid region.
284 */
285 virtual ValidRegion valid_region() const = 0;
286 /** Set the valid region of the tensor.
287 *
288 * @param[in] valid_region Valid region to set.
289 */
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000290 virtual void set_valid_region(const ValidRegion &valid_region) = 0;
Michel Iwaniec00633802017-10-12 14:14:15 +0100291
292 /** Get the quantization settings (scale and offset) of the tensor.
293 *
294 * @return A QuantizationInfo containing the scale and offset.
295 */
296 virtual QuantizationInfo quantization_info() const = 0;
Isabella Gottardid17a6772018-02-27 17:41:55 +0000297 /** Get the data layout of the tensor.
298 *
299 * @return A DataLayout containing the layout data information.
300 */
301 virtual DataLayout data_layout() const = 0;
SiCong Lif44bbc52022-08-29 18:25:51 +0100302 /** Get the workload tensor id of the tensor.
303 *
304 * @return Workload tensor id of the tensor
305 */
306 virtual Id id() const = 0;
307 /** Set the tensor id
308 */
309 virtual ITensorInfo &set_id(ITensorInfo::Id id) = 0;
310 /** Check if the tensor id is valid
311 */
312 bool has_valid_id() const
313 {
314 return id() != invalid_tensor_id;
315 }
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000316 /** If infos are broadcast compatible tensor info's, return the broadcasted shape and the intersection of
317 * the broadcasted valid regions of the tensors.
318 *
319 * Two tensor info's are broadcast compatible if their shapes are broadcast compatible.
320 *
321 * Two tensor shapes are broadcast compatible if for each dimension, they're equal or one of them is 1.
322 *
323 * If two shapes are compatible, each dimension in the broadcasted shape is the max of the original dimensions.
324 *
325 * @param[in] infos Tensor info's.
326 *
327 * @return The broadcasted shape and valid region, or an empty shape and valid region if the info's are
328 * not broadcast compatible.
329 */
330 template <typename... Infos>
331 static std::pair<TensorShape, ValidRegion> broadcast_shape_and_valid_region(const Infos &... infos)
332 {
333 TensorShape bc_shape = TensorShape::broadcast_shape(infos.tensor_shape()...);
334 ValidRegion bc_valid_region{ Coordinates(), bc_shape };
335
336 auto broadcast_valid_region = [&bc_valid_region](const ITensorInfo & info)
337 {
338 if(info.num_dimensions() != 0)
339 {
340 for(size_t d = 0; d < bc_valid_region.shape.num_dimensions(); ++d)
341 {
342 const bool is_broadcast = (info.tensor_shape()[d] == 1);
343
344 const int anchor_max = std::max(bc_valid_region.anchor[d], info.valid_region().anchor[d]);
345 const size_t valid_min = std::min(bc_valid_region.shape[d], info.valid_region().shape[d]);
346
347 if(!is_broadcast || (valid_min == 0))
348 {
349 bc_valid_region.anchor.set(d, anchor_max);
350 bc_valid_region.shape.set(d, valid_min);
351 }
352 }
353 }
354 };
355
356 utility::for_each(broadcast_valid_region, infos...);
357
358 return std::pair<TensorShape, ValidRegion>(bc_shape, bc_valid_region);
359 }
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +0000360
361private:
362 static constexpr int32_t _dynamic_dimension = -1;
363 static constexpr int32_t _static_dimension = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100364};
Georgios Pinitas49be2e32019-09-02 13:18:55 +0100365} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000366#endif /*ARM_COMPUTE_TENSORINFO_H */