Compute Library
 21.02
TensorInfo.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_TENSORINFO_H
25 #define ARM_COMPUTE_TENSORINFO_H
26 
28 
29 #include "ITensorInfo.h"
34 #include "arm_compute/core/Types.h"
35 #include "arm_compute/core/Utils.h"
36 
37 #include <cstddef>
38 #include <memory>
39 
40 namespace arm_compute
41 {
42 class HOGInfo;
43 
44 /** Store the tensor's metadata */
45 class TensorInfo final : public ITensorInfo
46 {
47 public:
48  /** Default constructor */
49  TensorInfo();
50  /** Default destructor */
51  ~TensorInfo() = default;
52  /** Allow instances of this class to be copy constructed */
53  TensorInfo(const ITensorInfo &info);
54  /** Allow instances of this class to be copy constructed */
55  TensorInfo(const TensorInfo &) = default;
56  /** Allow instances of this class to be copied */
57  TensorInfo &operator=(const TensorInfo &) = default;
58  /** Allow instances of this class to be move constructed */
59  TensorInfo(TensorInfo &&) = default;
60  /** Allow instances of this class to be moved */
61  TensorInfo &operator=(TensorInfo &&) = default;
62 
63  /** Construct a tensor info with a format.
64  *
65  * Can be used for automatic derivation of the shape by the function.
66  *
67  * @param[in] format Format of the tensor.
68  */
70 
71  /** 2D tensor constructor
72  *
73  * @param[in] width Width of the 2D tensor
74  * @param[in] height Height of the 2D tensor
75  * @param[in] format Single plane format of the tensor.
76  */
77  TensorInfo(unsigned int width, unsigned int height, Format format);
78  /** Constructor
79  *
80  * @param[in] tensor_shape It specifies the size for each dimension of the tensor in number of elements.
81  * @param[in] format Single plane format of the tensor.
82  */
83  TensorInfo(const TensorShape &tensor_shape, Format format);
84 
85  /** Construct a tensor info with a data type and number of channels.
86  *
87  * Can be used for automatic derivation of the shape by the function.
88  *
89  * @param[in] num_channels It indicates the number of channels for each tensor element
90  * @param[in] data_type Data type to use for each tensor element
91  */
93 
94  /** Constructor
95  *
96  * @param[in] tensor_shape It specifies the size for each dimension of the tensor in number of elements.
97  * @param[in] num_channels It indicates the number of channels for each tensor element
98  * @param[in] data_type Data type to use for each tensor element
99  */
100  TensorInfo(const TensorShape &tensor_shape, size_t num_channels, DataType data_type);
101 
102  /** Constructor
103  *
104  * @param[in] tensor_shape It specifies the size for each dimension of the tensor in number of elements.
105  * @param[in] num_channels It indicates the number of channels for each tensor element
106  * @param[in] data_type Data type to use for each tensor element
107  * @param[in] data_layout The data layout setting for the tensor data.
108  */
109  TensorInfo(const TensorShape &tensor_shape, size_t num_channels, DataType data_type, DataLayout data_layout);
110 
111  /** Constructor
112  *
113  * @param[in] tensor_shape It specifies the size for each dimension of the tensor in number of elements.
114  * @param[in] num_channels It indicates the number of channels for each tensor element
115  * @param[in] data_type Data type to use for each tensor element
116  * @param[in] quantization_info The quantization settings for the tensor data.
117  */
118  TensorInfo(const TensorShape &tensor_shape, size_t num_channels, DataType data_type, QuantizationInfo quantization_info);
119 
120  /** Constructor
121  *
122  * @param[in] hog_info HOG's metadata used to allocate normalized HOG space
123  * @param[in] width Width of the 2D tensor where the HOG descriptor will be computed on
124  * @param[in] height Height of the 2D tensor where the HOG descriptor will be computed on
125  */
126  TensorInfo(const HOGInfo &hog_info, unsigned int width, unsigned int height);
127 
128  /** Initialize the tensor info with just a format.
129  *
130  * Can be used for automatic derivation of the shape by the function.
131  *
132  * @param[in] format Single plane format of the tensor.
133  */
134  void init(Format format);
135 
136  /** Initialize the metadata structure with the given parameters
137  *
138  * @param[in] tensor_shape Size for each dimension of the tensor in number of elements.
139  * @param[in] format Single plane format of the tensor.
140  */
141  void init(const TensorShape &tensor_shape, Format format);
142  /** Initialize the metadata structure with the given parameters
143  *
144  * @param[in] tensor_shape Size for each dimension of the tensor in number of elements.
145  * @param[in] format Single plane format of the tensor.
146  * @param[in] strides_in_bytes Stride in bytes for accessing each dimension of the tensor.
147  * @param[in] offset_first_element_in_bytes Offset in bytes from the beginning of memory allocation to access the first element.
148  * @param[in] total_size_in_bytes Size in bytes of the memory allocation (including the offset to the first element).
149  */
150  void init(const TensorShape &tensor_shape, Format format, const Strides &strides_in_bytes, size_t offset_first_element_in_bytes, size_t total_size_in_bytes);
151 
152  /** Initialize the tensor info with just a format.
153  *
154  * Can be used for automatic derivation of the shape by the function.
155  *
156  * @param[in] num_channels Desired number of channels for each tensor element.
157  * @param[in] data_type Data type to use for each tensor element.
158  */
159  void init(size_t num_channels, DataType data_type);
160 
161  /** Initialize the metadata structure with the given parameters
162  *
163  * @param[in] tensor_shape Size for each dimension of the tensor in number of elements.
164  * @param[in] num_channels Desired number of channels for each tensor element.
165  * @param[in] data_type Data type to use for each tensor element.
166  */
167  void init(const TensorShape &tensor_shape, size_t num_channels, DataType data_type);
168 
169  /** Initialize the metadata structure with the given parameters
170  *
171  * @param[in] tensor_shape Size for each dimension of the tensor in number of elements.
172  * @param[in] num_channels Desired number of channels for each tensor element.
173  * @param[in] data_type Data type to use for each tensor element.
174  * @param[in] strides_in_bytes Stride in bytes for accessing each dimension of the tensor.
175  * @param[in] offset_first_element_in_bytes Offset in bytes from the beginning of memory allocation to access the first element.
176  * @param[in] total_size_in_bytes Size in bytes of the memory allocation (including the offset to the first element).
177  */
178  void init(const TensorShape &tensor_shape, size_t num_channels, DataType data_type, const Strides &strides_in_bytes, size_t offset_first_element_in_bytes,
179  size_t total_size_in_bytes);
180  /** Initialize the metadata structure for the given HOG's metadata
181  *
182  * @param[in] hog_info HOG's metadata used to allocate normalized HOG space
183  * @param[in] width Width of the 2D tensor where the HOG descriptor will be computed on
184  * @param[in] height Height of the 2D tensor where the HOG descriptor will be computed on
185  */
186  void init(const HOGInfo &hog_info, unsigned int width, unsigned int height);
187  /** Initialize the metadata structure for the given tensor shape and single-plane format, (Padding is automatically calculated)
188  *
189  * @note The padding used by this method is really conservative so that the tensor can be used for most functions.
190  *
191  * @param[in] tensor_shape It specifies the size for each dimension of the tensor in number of elements
192  * @param[in] format Single plane format of the image.
193  *
194  * @return Total allocation size including padding in bytes.
195  */
196  size_t init_auto_padding(const TensorShape &tensor_shape, Format format);
197  /** Initialize the metadata structure for the given tensor shape, number of channels and
198  * data type. (Padding is automatically calculated)
199  *
200  * @note The padding used by this method is really conservative so that the tensor can be used for most functions.
201  *
202  * @param[in] tensor_shape It specifies the size for each dimension of the tensor in number of elements
203  * @param[in] num_channels It indicates the number of channels for each tensor element
204  * @param[in] data_type Data type to use for each tensor element
205  *
206  * @return Total allocation size including padding in bytes.
207  */
208  size_t init_auto_padding(const TensorShape &tensor_shape, size_t num_channels, DataType data_type);
209  /** Initialize the metadata structure for the given HOG's metadata
210  *
211  * @note init_auto_padding will be used for the tensor initialization.
212  *
213  * @param[in] hog_info HOG's metadata used to allocate normalized HOG space
214  * @param[in] width Width of the 2D tensor where the HOG descriptor will be computed on
215  * @param[in] height Height of the 2D tensor where the HOG descriptor will be computed on
216  *
217  * @return Total allocation size including padding in bytes.
218  */
219  size_t init_auto_padding(const HOGInfo &hog_info, unsigned int width, unsigned int height);
220 
221  // Inherited methods overridden:
222  std::unique_ptr<ITensorInfo> clone() const override;
223  ITensorInfo &set_data_type(DataType data_type) override;
224  ITensorInfo &set_num_channels(int num_channels) override;
225  ITensorInfo &set_format(Format format) override;
226  ITensorInfo &set_tensor_shape(const TensorShape &shape) override;
227  ITensorInfo &set_tensor_dims_state(const TensorDimsState &state) override;
228  ITensorInfo &set_quantization_info(const QuantizationInfo &quantization_info) override;
229  ITensorInfo &set_data_layout(const DataLayout &data_layout) override;
230  ITensorInfo &reset_padding() override;
231  bool auto_padding() override;
232  bool extend_padding(const PaddingSize &padding) override;
233  size_t dimension(size_t index) const override
234  {
235  return _tensor_shape[index];
236  }
237  size_t dimension(DataLayoutDimension dimension) const override
238  {
239  return get_data_layout_dimension_index(_data_layout, dimension);
240  }
241  const Strides &strides_in_bytes() const override
242  {
243  return _strides_in_bytes;
244  }
245  size_t offset_first_element_in_bytes() const override
246  {
247  return _offset_first_element_in_bytes;
248  }
249  int32_t offset_element_in_bytes(const Coordinates &pos) const override;
250  size_t element_size() const override
251  {
252  return data_size_from_type(_data_type) * _num_channels;
253  }
254  size_t num_dimensions() const override
255  {
256  return _tensor_shape.num_dimensions();
257  }
258  size_t num_channels() const override
259  {
260  return _num_channels;
261  }
262  const TensorShape &tensor_shape() const override
263  {
264  return _tensor_shape;
265  }
266  const TensorDimsState &tensor_dims_state() const override
267  {
268  return _dims_state;
269  }
270  DataType data_type() const override
271  {
272  return _data_type;
273  }
274  Format format() const override
275  {
276  return _format;
277  }
278  size_t total_size() const override
279  {
280  return _total_size;
281  }
282  PaddingSize padding() const override
283  {
284  return _padding;
285  }
286  bool has_padding() const override
287  {
288  return !_padding.empty();
289  }
290  bool is_resizable() const override
291  {
292  return _is_resizable;
293  }
294  bool is_dynamic() const override
295  {
296  return std::find(std::cbegin(_dims_state), std::cend(_dims_state), -1) != std::cend(_dims_state);
297  }
299  {
300  _is_resizable = is_resizable;
301  return *this;
302  }
303  ValidRegion valid_region() const override
304  {
305  return _valid_region;
306  }
308  {
309  _valid_region = valid_region;
310  }
312  {
313  return _quantization_info;
314  }
315  DataLayout data_layout() const override
316  {
317  return _data_layout;
318  }
319 
320 private:
321  /** Calculates strides, offset and total size resulting from the specified padding around the XY plane.
322  *
323  * @param[in] padding Padding around the XY plane in elements.
324  */
325  std::tuple<Strides, size_t, size_t> calculate_padding_requirements(const PaddingSize &padding);
326 
327  size_t _total_size;
328  size_t _offset_first_element_in_bytes;
329  Strides _strides_in_bytes;
330  size_t _num_channels;
331  TensorShape _tensor_shape;
332  TensorDimsState _dims_state;
333  DataType _data_type;
334  Format _format;
335  bool _is_resizable;
336  ValidRegion _valid_region;
337  PaddingSize _padding;
338  QuantizationInfo _quantization_info;
339  DataLayout _data_layout;
340 };
341 } // namespace arm_compute
342 #endif /*ARM_COMPUTE_TENSORINFO_H */
ITensorInfo & set_format(Format format) override
Set the format of an already initialized tensor.
Definition: TensorInfo.cpp:335
Shape of a tensor.
Definition: TensorShape.h:39
size_t init_auto_padding(const TensorShape &tensor_shape, Format format)
Initialize the metadata structure for the given tensor shape and single-plane format, (Padding is automatically calculated)
Definition: TensorInfo.cpp:183
bool has_padding() const override
Checks if the tensor has been allocated with padding or not.
Definition: TensorInfo.h:286
void set_valid_region(const ValidRegion &valid_region) override
Set the valid region of the tensor.
Definition: TensorInfo.h:307
std::unique_ptr< ITensorInfo > clone() const override
Provide a clone of the current object of class T.
Definition: TensorInfo.cpp:316
bool is_resizable() const override
Flag indicating whether the size of the tensor can be changed.
Definition: TensorInfo.h:290
Container for 2D border size.
Definition: Types.h:273
ValidRegion valid_region() const override
Valid region of the tensor.
Definition: TensorInfo.h:303
size_t num_channels() const override
The number of channels for each tensor element.
Definition: TensorInfo.h:258
ITensorInfo & reset_padding() override
Resets the padding settings of the tensor.
Definition: TensorInfo.cpp:392
bool is_dynamic() const override
Flag indicating whether the shape of the tensor is dynamic, meaning that it can change on kernel/func...
Definition: TensorInfo.h:294
DataLayout data_layout() const override
Get the data layout of the tensor.
Definition: TensorInfo.h:315
DataLayoutDimension
[DataLayout enum definition]
Definition: Types.h:129
size_t dimension(size_t index) const override
Return the size of the requested dimension.
Definition: TensorInfo.h:233
Store the HOG&#39;s metadata.
Definition: HOGInfo.h:35
QuantizationInfo quantization_info() const override
Get the quantization settings (scale and offset) of the tensor.
Definition: TensorInfo.h:311
bool extend_padding(const PaddingSize &padding) override
Update the offset to the first element, the strides and the total size.
Definition: TensorInfo.cpp:281
ITensorInfo & set_data_type(DataType data_type) override
Set the data type to the specified value.
Definition: TensorInfo.cpp:321
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
size_t dimension(DataLayoutDimension dimension) const override
Return the size of the requested data layout dimension.
Definition: TensorInfo.h:237
PaddingSize padding() const override
Padding of tensor.
Definition: TensorInfo.h:282
const Strides & strides_in_bytes() const override
The strides in bytes for accessing each dimension of the tensor.
Definition: TensorInfo.h:241
bool auto_padding() override
Update the offset to the first element and the strides to automatically computed values.
Definition: TensorInfo.cpp:224
Copyright (c) 2017-2021 Arm Limited.
Format format() const override
Colour format of the image.
Definition: TensorInfo.h:274
ITensorInfo & set_quantization_info(const QuantizationInfo &quantization_info) override
Set the quantization settings (scale and offset) of the tensor.
Definition: TensorInfo.cpp:380
DataType data_type() const override
Data type used for each element of the tensor.
Definition: TensorInfo.h:270
Quantization information.
size_t num_dimensions() const override
The number of dimensions of the tensor (rank)
Definition: TensorInfo.h:254
ITensorInfo & set_data_layout(const DataLayout &data_layout) override
Set the data layout of the tensor.
Definition: TensorInfo.cpp:386
Format
Image colour formats.
Definition: Types.h:54
size_t total_size() const override
Returns the total size of the tensor in bytes.
Definition: TensorInfo.h:278
Coordinates of an item.
Definition: Coordinates.h:37
const TensorDimsState & tensor_dims_state() const override
State of each dimension of the tensor shape.
Definition: TensorInfo.h:266
size_t data_size_from_type(DataType data_type)
The size in bytes of the data type.
Definition: Utils.h:106
ITensorInfo & set_is_resizable(bool is_resizable) override
Set the flag whether the tensor size can be changed.
Definition: TensorInfo.h:298
ITensorInfo & set_num_channels(int num_channels) override
Set the number of channels to the specified value.
Definition: TensorInfo.cpp:328
void init(Format format)
Initialize the tensor info with just a format.
Definition: TensorInfo.cpp:109
Strides of an item in bytes.
Definition: Strides.h:37
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
ITensorInfo & set_tensor_dims_state(const TensorDimsState &state) override
Set the state for each dimension of the tensor.
Definition: TensorInfo.cpp:374
constexpr bool empty() const
Check if the entire border is zero.
Definition: Types.h:300
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:143
int32_t offset_element_in_bytes(const Coordinates &pos) const override
The offset in bytes from the beginning of the memory allocation to access the element at position (x...
Definition: TensorInfo.cpp:402
size_t offset_first_element_in_bytes() const override
The offset from the beginning of the memory allocation to the first element of the tensor...
Definition: TensorInfo.h:245
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:45
ITensorInfo & set_tensor_shape(const TensorShape &shape) override
Set the shape of an already initialized tensor.
Definition: TensorInfo.cpp:352
TensorInfo()
Default constructor.
Definition: TensorInfo.cpp:37
Container for valid region of a window.
Definition: Types.h:188
size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:193
TensorInfo & operator=(const TensorInfo &)=default
Allow instances of this class to be copied.
const TensorShape & tensor_shape() const override
Size for each dimension of the tensor.
Definition: TensorInfo.h:262
DataType
Available data types.
Definition: Types.h:77
def find(path, pattern)
DataLayout
[DataLayout enum definition]
Definition: Types.h:120
size_t element_size() const override
Element size in bytes calculated as data_size() * num_channels()
Definition: TensorInfo.h:250
~TensorInfo()=default
Default destructor.