Compute Library
 19.08
TensorInfo.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2018 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 
45 class TensorInfo final : public ITensorInfo
46 {
47 public:
49  TensorInfo();
51  ~TensorInfo() = default;
53  TensorInfo(const ITensorInfo &info);
55  TensorInfo(const TensorInfo &) = default;
57  TensorInfo &operator=(const TensorInfo &) = default;
59  TensorInfo(TensorInfo &&) = default;
61  TensorInfo &operator=(TensorInfo &&) = default;
62 
70 
77  TensorInfo(unsigned int width, unsigned int height, Format format);
84 
93 
101 
110 
117  TensorInfo(const HOGInfo &hog_info, unsigned int width, unsigned int height);
118 
125  void init(Format format);
126 
132  void init(const TensorShape &tensor_shape, Format format);
141  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);
142 
150  void init(size_t num_channels, DataType data_type);
151 
159 
170  size_t total_size_in_bytes);
177  void init(const HOGInfo &hog_info, unsigned int width, unsigned int height);
210  size_t init_auto_padding(const HOGInfo &hog_info, unsigned int width, unsigned int height);
211 
212  // Inherited methods overridden:
213  std::unique_ptr<ITensorInfo> clone() const override;
216  ITensorInfo &set_format(Format format) override;
217  ITensorInfo &set_tensor_shape(const TensorShape &shape) override;
220  ITensorInfo &reset_padding() override;
221  bool auto_padding() override;
222  bool extend_padding(const PaddingSize &padding) override;
223  size_t dimension(size_t index) const override
224  {
225  return _tensor_shape[index];
226  }
227  size_t dimension(DataLayoutDimension dimension) const override
228  {
229  return get_data_layout_dimension_index(_data_layout, dimension);
230  }
231  const Strides &strides_in_bytes() const override
232  {
233  return _strides_in_bytes;
234  }
235  size_t offset_first_element_in_bytes() const override
236  {
237  return _offset_first_element_in_bytes;
238  }
239  size_t offset_element_in_bytes(const Coordinates &pos) const override;
240  size_t element_size() const override
241  {
242  return data_size_from_type(_data_type) * _num_channels;
243  }
244  size_t num_dimensions() const override
245  {
246  return _tensor_shape.num_dimensions();
247  }
248  size_t num_channels() const override
249  {
250  return _num_channels;
251  }
252  const TensorShape &tensor_shape() const override
253  {
254  return _tensor_shape;
255  }
256  DataType data_type() const override
257  {
258  return _data_type;
259  }
260  Format format() const override
261  {
262  return _format;
263  }
264  size_t total_size() const override
265  {
266  return _total_size;
267  }
268  PaddingSize padding() const override
269  {
270  return _padding;
271  }
272  bool has_padding() const override
273  {
274  return !_padding.empty();
275  }
276  bool is_resizable() const override
277  {
278  return _is_resizable;
279  }
281  {
282  _is_resizable = is_resizable;
283  return *this;
284  }
285  ValidRegion valid_region() const override
286  {
287  return _valid_region;
288  }
290  {
291  _valid_region = valid_region;
292  }
294  {
295  return _quantization_info;
296  }
297  DataLayout data_layout() const override
298  {
299  return _data_layout;
300  }
301 
302 private:
307  std::tuple<Strides, size_t, size_t> calculate_padding_requirements(const PaddingSize &padding);
308 
309  size_t _total_size;
310  size_t _offset_first_element_in_bytes;
311  Strides _strides_in_bytes;
312  size_t _num_channels;
313  TensorShape _tensor_shape;
314  DataType _data_type;
315  Format _format;
316  bool _is_resizable;
317  ValidRegion _valid_region;
318  PaddingSize _padding;
319  QuantizationInfo _quantization_info;
320  DataLayout _data_layout;
321 };
322 }
323 #endif /*__ARM_COMPUTE_TENSORINFO_H__ */
ITensorInfo & set_format(Format format) override
Set the format of an already initialized tensor.
Definition: TensorInfo.cpp:325
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,...
Definition: TensorInfo.cpp:173
bool has_padding() const override
Checks if the tensor has been allocated with padding or not.
Definition: TensorInfo.h:272
void set_valid_region(const ValidRegion &valid_region) override
Set the valid region of the tensor.
Definition: TensorInfo.h:289
std::unique_ptr< ITensorInfo > clone() const override
Provide a clone of the current object of class T.
Definition: TensorInfo.cpp:306
bool is_resizable() const override
Flag indicating whether the size of the tensor can be changed.
Definition: TensorInfo.h:276
Container for 2D border size.
Definition: Types.h:259
ValidRegion valid_region() const override
Valid region of the tensor.
Definition: TensorInfo.h:285
size_t num_channels() const override
The number of channels for each tensor element.
Definition: TensorInfo.h:248
ITensorInfo & reset_padding() override
Resets the padding settings of the tensor.
Definition: TensorInfo.cpp:376
DataLayout data_layout() const override
Get the data layout of the tensor.
Definition: TensorInfo.h:297
DataLayoutDimension
[DataLayout enum definition]
Definition: Types.h:123
size_t dimension(size_t index) const override
Return the size of the requested dimension.
Definition: TensorInfo.h:223
Store the HOG's metadata.
Definition: HOGInfo.h:35
QuantizationInfo quantization_info() const override
Get the quantization settings (scale and offset) of the tensor.
Definition: TensorInfo.h:293
bool extend_padding(const PaddingSize &padding) override
Update the offset to the first element, the strides and the total size.
Definition: TensorInfo.cpp:271
ITensorInfo & set_data_type(DataType data_type) override
Set the data type to the specified value.
Definition: TensorInfo.cpp:311
Store the tensor'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:227
PaddingSize padding() const override
Padding of tensor.
Definition: TensorInfo.h:268
const Strides & strides_in_bytes() const override
The strides in bytes for accessing each dimension of the tensor.
Definition: TensorInfo.h:231
bool auto_padding() override
Update the offset to the first element and the strides to automatically computed values.
Definition: TensorInfo.cpp:214
Copyright (c) 2017-2018 ARM Limited.
Format format() const override
Colour format of the image.
Definition: TensorInfo.h:260
ITensorInfo & set_quantization_info(const QuantizationInfo &quantization_info) override
Set the quantization settings (scale and offset) of the tensor.
Definition: TensorInfo.cpp:364
DataType data_type() const override
Data type used for each element of the tensor.
Definition: TensorInfo.h:256
size_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:386
Quantization information.
size_t num_dimensions() const override
The number of dimensions of the tensor (rank)
Definition: TensorInfo.h:244
ITensorInfo & set_data_layout(const DataLayout &data_layout) override
Set the data layout of the tensor.
Definition: TensorInfo.cpp:370
Format
Image colour formats.
Definition: Types.h:52
size_t total_size() const override
Returns the total size of the tensor in bytes.
Definition: TensorInfo.h:264
Coordinates of an item.
Definition: Coordinates.h:37
size_t data_size_from_type(DataType data_type)
The size in bytes of the data type.
Definition: Utils.h:109
ITensorInfo & set_is_resizable(bool is_resizable) override
Set the flag whether the tensor size can be changed.
Definition: TensorInfo.h:280
ITensorInfo & set_num_channels(int num_channels) override
Set the number of channels to the specified value.
Definition: TensorInfo.cpp:318
void init(Format format)
Initialize the tensor info with just a format.
Definition: TensorInfo.cpp:99
Strides of an item in bytes.
Definition: Strides.h:37
constexpr bool empty() const
Check if the entire border is zero.
Definition: Types.h:286
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:122
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:235
Store the tensor'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:342
TensorInfo()
Default constructor.
Definition: TensorInfo.cpp:35
Container for valid region of a window.
Definition: Types.h:174
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:326
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:252
DataType
Available data types.
Definition: Types.h:74
DataLayout
[DataLayout enum definition]
Definition: Types.h:114
size_t element_size() const override
Element size in bytes calculated as data_size() * num_channels()
Definition: TensorInfo.h:240
~TensorInfo()=default
Default destructor.