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