xref: /aosp_15_r20/external/ComputeLibrary/arm_compute/core/ITensorInfo.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2016-2023 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 #ifndef ARM_COMPUTE_ITENSORINFO_H
25 #define ARM_COMPUTE_ITENSORINFO_H
26 
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"
32 #include "arm_compute/core/utils/misc/Utility.h"
33 #include "support/ICloneable.h"
34 
35 #include <cstddef>
36 
37 namespace arm_compute
38 {
39 /** Store the tensor's metadata */
40 class ITensorInfo : public misc::ICloneable<ITensorInfo>
41 {
42 public:
43     using TensorDimsState = std::vector<int>;
44     /** An id that uniquely identifies an ITensorInfo within some domain (e.g. a workload)
45      */
46     using Id = int32_t;
47     /** An invalid tensor id within a domain */
48     static constexpr Id invalid_tensor_id = 0;
49     /** Get the value representing dynamic dimension state
50      *
51      * @return Value representing dynamic dimension state
52      *
53      */
get_dynamic_state_value()54     static constexpr int32_t get_dynamic_state_value()
55     {
56         return _dynamic_dimension;
57     }
58     /** Get the value representing static dimension state
59      *
60      * @return Value representing static dimension state
61      *
62      */
get_static_state_value()63     static constexpr int32_t get_static_state_value()
64     {
65         return _static_dimension;
66     }
67     /** Default virtual destructor */
68     virtual ~ITensorInfo() = default;
69     /** Set the data type to the specified value.
70      *
71      * @warning This resets the format to UNKNOWN.
72      *
73      * @param[in] data_type The new data type.
74      *
75      * @return Reference to this ITensorInfo object
76      */
77     virtual ITensorInfo &set_data_type(DataType data_type) = 0;
78     /** Set the number of channels to the specified value.
79      *
80      * @warning This resets the format to UNKNOWN.
81      *
82      * @param[in] num_channels New number of channels.
83      *
84      * @return Reference to this ITensorInfo object
85      */
86     virtual ITensorInfo &set_num_channels(int num_channels) = 0;
87     /** Set the format of an already initialized tensor.
88      *
89      * @note If the data type has already been configured (i.e. not UNKNOWN) it
90      * must match the new format. If data type hasn't been configured it will
91      * be based on the format.
92      *
93      * @param[in] format Single-plane format of the tensor.
94      *
95      * @return Reference to this ITensorInfo object
96      */
97     virtual ITensorInfo &set_format(Format format) = 0;
98     /** Set the shape of an already initialized tensor.
99      *
100      * @warning Changing the shape requires to recompute the strides and is
101      * therefore only possible if the tensor hasn't been allocated yet.
102      *
103      * @param[in] shape New tensor shape.
104      *
105      * @return Reference to this ITensorInfo object
106      */
107     virtual ITensorInfo &set_tensor_shape(const TensorShape &shape) = 0;
108     /** Set the state for each dimension of the tensor
109      *
110      * This sets the state of each dimension of the shape in terms of dynamic behavior using -1 where appropriate.
111      * The index in the state is a 1 to 1 mapping with the shape dimension index.
112      * For example if you want to express [?, 3, 3] as a dynamic input then [-1, 3, 3] has to be set as a state
113      *
114      * @param[in] state Tensor dimensions state
115      *
116      * @return Reference to this ITensorInfo object
117      */
118     virtual ITensorInfo &set_tensor_dims_state(const TensorDimsState &state) = 0;
119     /** Set the quantization settings (scale and offset) of the tensor.
120      *
121      * @param[in] quantization_info QuantizationInfo containing the scale and offset
122      *
123      * @return Reference to this ITensorInfo object
124      */
125     virtual ITensorInfo &set_quantization_info(const QuantizationInfo &quantization_info) = 0;
126     /** Set the data layout of the tensor.
127      *
128      * @param[in] data_layout DataLayout containing the layout data information.
129      *
130      * @return Reference to this ITensorInfo object
131      */
132     virtual ITensorInfo &set_data_layout(const DataLayout &data_layout) = 0;
133     /** Resets the padding settings of the tensor.
134     *
135     * @return Reference to this ITensorInfo object
136     */
137     virtual ITensorInfo &reset_padding() = 0;
138     /** Update the offset to the first element and the strides to automatically computed values.
139      *
140      * @note The padding used by this method is really conservative so that the tensor can be used for most functions.
141      *
142      * @return True if the strides or the offset to the first element have changed.
143      */
144     virtual bool auto_padding() = 0;
145     /** Set the lock paddings flag of the tensor.
146      * It should be set to True, when the tensor could be mapped to camera or frame buffer.
147      *
148      * @return Reference to this ITensorInfo object
149      */
150     virtual ITensorInfo &set_lock_paddings(bool flag) = 0;
151     /** Get the lock paddings flag value
152      *
153      * @return lock paddings flag value
154      */
155     virtual bool lock_paddings() const = 0;
156     /** Update the offset to the first element, the strides and the total size.
157      *
158      * @note This function can only increase the offset, strides and total size.
159      *
160      * @param[in] padding Padding around the XY plane in number of elements.
161      *
162      * @return True if the strides, offset and total size have changed.
163      */
164     virtual bool extend_padding(const PaddingSize &padding) = 0;
165     /** Return the size of the requested dimension
166      *
167      * @param[in] index Index of the dimension
168      *
169      * @return Dimension of the requested dimension
170      */
171     virtual size_t dimension(size_t index) const = 0;
172     /** Return the size of the requested data layout dimension
173      *
174      * @param[in] dimension DataLayoutDimension of the dimension
175      *
176      * @return Dimension of the requested dimension
177      */
178     virtual size_t dimension(DataLayoutDimension dimension) const = 0;
179     /** The strides in bytes for accessing each dimension of the tensor
180      *
181      * @return Strides in bytes for each tensor dimension
182      */
183     virtual const Strides &strides_in_bytes() const = 0;
184     /** The offset from the beginning of the memory allocation to the first element of the tensor.
185      *  This can be used to access efficiently elements in a 2D tensor
186      *
187      * @return The offset in bytes to access the first element of the tensor.
188      */
189     virtual size_t offset_first_element_in_bytes() const = 0;
190     /** The offset in bytes from the beginning of the memory allocation to access the element at position (x, y, z ...)
191      *
192      * @param[in] pos Vector with the coordinates of the element to access.
193      *                The size of this vector must be equal to the number of dimensions of the tensor
194      *
195      * @return Offset in bytes from the beginning of the memory allocation to access the element (x, y, z, ...)
196      */
197     virtual int32_t offset_element_in_bytes(const Coordinates &pos) const = 0;
198 
199     /** Element size in bytes calculated as data_size() * num_channels()
200      *
201      * @return The size of one element in bytes
202      */
203     virtual size_t element_size() const = 0;
204     /** The number of dimensions of the tensor (rank)
205      *
206      * @return The number of dimensions of the tensor (rank)
207      */
208     virtual size_t num_dimensions() const = 0;
209     /** The number of channels for each tensor element
210      *
211      * @return The number of channels for each tensor element
212      */
213     virtual size_t num_channels() const = 0;
214     /** Size for each dimension of the tensor
215      *
216      * @return A vector with the size for each dimension of the tensor
217      */
218     virtual const TensorShape &tensor_shape() const = 0;
219     /** State of each dimension of the tensor shape
220      *
221      * @return A vector with the state for each dimension of the tensor, where -1 specifies dynamic dimension
222      */
223     virtual const TensorDimsState &tensor_dims_state() const = 0;
224     /** Data type used for each element of the tensor
225      *
226      * @return Tensor data type
227      */
228     virtual DataType data_type() const = 0;
229     /** Colour format of the image
230      *
231      * @return Colour format of the image
232      */
233     virtual Format format() const = 0;
234     /** Returns the total size of the tensor in bytes.
235      *
236      * @return Total size of the tensor in bytes.
237      */
238     virtual size_t total_size() const = 0;
239     /** Padding of tensor.
240      *
241      * @return Padding.
242      */
243     virtual PaddingSize padding() const = 0;
244     /** Checks if the tensor has been allocated with padding or not.
245      *
246      * @return True if padding is allocated in the tensor, otherwise false.
247      */
248     virtual bool has_padding() const = 0;
249     /** Flag indicating whether the size of the tensor can be changed.
250      *
251      * @return True if the tensor size can be changed.
252      */
253     virtual bool is_resizable() const = 0;
254     /** Flag indicating whether the shape of the tensor is dynamic, meaning that it can change on kernel/function execution.
255      *
256      * @return True if its dynamic else false
257      */
258     virtual bool is_dynamic() const = 0;
259     /** Flag indicating whether the values of the tensor are constant, meaning that they can change on kernel/function execution.
260      *
261      * @return True if values are constant else false
262      */
263     virtual bool are_values_constant() const = 0;
264     /** Set the flag whether the tensor size can be changed.
265      *
266      * @param[in] is_resizable Flag that marks the tensor if it can be changed or not.
267      *
268      * @return Reference to this ITensorInfo object
269      */
270     virtual ITensorInfo &set_is_resizable(bool is_resizable) = 0;
271     /** Set the flag whether the tensor values can change during kernel/function execution.
272      *
273      * @param[in] are_values_constant Flag that marks the tensor values if they can be changed or not.
274      *
275      * @return Reference to this ITensorInfo object
276      */
277     virtual ITensorInfo &set_are_values_constant(bool are_values_constant) = 0;
278     /** Valid region of the tensor. All elements in the valid region have defined values, i.e. are not undefined.
279      *
280      * @return The valid region.
281      */
282     virtual ValidRegion valid_region() const = 0;
283     /** Set the valid region of the tensor.
284      *
285      * @param[in] valid_region Valid region to set.
286      */
287     virtual void set_valid_region(const ValidRegion &valid_region) = 0;
288 
289     /** Get the quantization settings (scale and offset) of the tensor.
290     *
291     * @return A QuantizationInfo containing the scale and offset.
292     */
293     virtual QuantizationInfo quantization_info() const = 0;
294     /** Get the data layout of the tensor.
295     *
296     * @return A DataLayout containing the layout data information.
297     */
298     virtual DataLayout data_layout() const = 0;
299     /** Get the workload tensor id of the tensor.
300     *
301     * @return Workload tensor id of the tensor
302     */
303     virtual Id id() const = 0;
304     /** Set the tensor id
305     */
306     virtual ITensorInfo &set_id(ITensorInfo::Id id) = 0;
307     /** Check if the tensor id is valid
308      */
has_valid_id()309     bool has_valid_id() const
310     {
311         return id() != invalid_tensor_id;
312     }
313     /** If infos are broadcast compatible tensor info's, return the broadcasted shape and the intersection of
314      * the broadcasted valid regions of the tensors.
315      *
316      * Two tensor info's are broadcast compatible if their shapes are broadcast compatible.
317      *
318      * Two tensor shapes are broadcast compatible if for each dimension, they're equal or one of them is 1.
319      *
320      * If two shapes are compatible, each dimension in the broadcasted shape is the max of the original dimensions.
321      *
322      * @param[in] infos Tensor info's.
323      *
324      * @return The broadcasted shape and valid region, or an empty shape and valid region if the info's are
325      * not broadcast compatible.
326      */
327     template <typename... Infos>
broadcast_shape_and_valid_region(const Infos &...infos)328     static std::pair<TensorShape, ValidRegion> broadcast_shape_and_valid_region(const Infos &... infos)
329     {
330         TensorShape bc_shape = TensorShape::broadcast_shape(infos.tensor_shape()...);
331         ValidRegion bc_valid_region{ Coordinates(), bc_shape };
332 
333         auto broadcast_valid_region = [&bc_valid_region](const ITensorInfo & info)
334         {
335             if(info.num_dimensions() != 0)
336             {
337                 for(size_t d = 0; d < bc_valid_region.shape.num_dimensions(); ++d)
338                 {
339                     const bool is_broadcast = (info.tensor_shape()[d] == 1);
340 
341                     const int    anchor_max = std::max(bc_valid_region.anchor[d], info.valid_region().anchor[d]);
342                     const size_t valid_min  = std::min(bc_valid_region.shape[d], info.valid_region().shape[d]);
343 
344                     if(!is_broadcast || (valid_min == 0))
345                     {
346                         bc_valid_region.anchor.set(d, anchor_max);
347                         bc_valid_region.shape.set(d, valid_min);
348                     }
349                 }
350             }
351         };
352 
353         utility::for_each(broadcast_valid_region, infos...);
354 
355         return std::pair<TensorShape, ValidRegion>(bc_shape, bc_valid_region);
356     }
357 
358 private:
359     static constexpr int32_t _dynamic_dimension = -1;
360     static constexpr int32_t _static_dimension  = 0;
361 };
362 } // namespace arm_compute
363 #endif /*ARM_COMPUTE_TENSORINFO_H */
364