Compute Library
 23.08
SubTensorInfo.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-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_SUBTENSORINFO_H
25 #define ARM_COMPUTE_SUBTENSORINFO_H
26 
28 
34 
35 #include <cstddef>
36 #include <memory>
37 
38 namespace arm_compute
39 {
40 /** Store the sub tensor's metadata */
41 class SubTensorInfo final : public ITensorInfo
42 {
43 public:
44  /** Default constructor */
45  SubTensorInfo();
46  /** Default constructor
47  *
48  * @param[in] parent Metadata of parent tensor.
49  * @param[in] tensor_shape Tensor shape. Shape must fit inside parent's shape.
50  * X and Y dimensions must match the parent's ones.
51  * @param[in] coords Coordinates of starting element inside parent tensor.
52  * @param[in] extend_parent (Optional) Extend parent with subtensor shape if subtensor indexes out of bounds
53  */
54  SubTensorInfo(ITensorInfo *parent, TensorShape tensor_shape, Coordinates coords, bool extend_parent = false);
55  /** Default destructor */
56  ~SubTensorInfo() = default;
57  /** Allow instances of this class to be copy constructed */
58  SubTensorInfo(const SubTensorInfo &) = default;
59  /** Allow instances of this class to be copied */
60  SubTensorInfo &operator=(const SubTensorInfo &) = default;
61  /** Allow instances of this class to be move constructed */
62  SubTensorInfo(SubTensorInfo &&) = default;
63  /** Allow instances of this class to be moved */
64  SubTensorInfo &operator=(SubTensorInfo &&) = default;
65  /** Returns the coordinates of the sub-tensor inside the parent tensor
66  *
67  * @return Sub-tensor coordinates
68  */
70  {
71  return _coords;
72  }
73 
74  // Inherited methods overridden:
75  std::unique_ptr<ITensorInfo> clone() const override;
77  {
78  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
79  _parent->set_data_type(data_type);
80  return *this;
81  };
83  {
84  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
85  _parent->set_data_layout(data_layout);
86  return *this;
87  };
89  {
90  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
92  return *this;
93  };
95  {
96  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
97  _parent->set_format(format);
98  return *this;
99  };
100  ITensorInfo &set_tensor_shape(const TensorShape &shape) override;
101  ITensorInfo &set_tensor_dims_state(const TensorDimsState &state) override;
103  {
104  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
106  return *this;
107  }
109  {
110  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
111  _parent->reset_padding();
112  return *this;
113  }
114  bool auto_padding() override
115  {
116  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
117  return _parent->auto_padding();
118  };
119 
120  ITensorInfo &set_lock_paddings(bool flag) override;
121 
122  bool lock_paddings() const override;
123 
124  bool extend_padding(const PaddingSize &padding) override;
125 
126  size_t dimension(size_t index) const override
127  {
128  return _tensor_shape[index];
129  }
130  size_t dimension(DataLayoutDimension dimension) const override
131  {
132  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
134  }
135  const Strides &strides_in_bytes() const override
136  {
137  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
138  return _parent->strides_in_bytes();
139  }
140  size_t offset_first_element_in_bytes() const override
141  {
142  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
143  return _parent->offset_element_in_bytes(_coords);
144  }
145  int32_t offset_element_in_bytes(const Coordinates &pos) const override;
146  size_t element_size() const override
147  {
148  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
149  return _parent->element_size();
150  }
151  size_t num_dimensions() const override
152  {
153  return _tensor_shape.num_dimensions();
154  }
155  size_t num_channels() const override
156  {
157  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
158  return _parent->num_channels();
159  }
160  const TensorShape &tensor_shape() const override
161  {
162  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
163  return _tensor_shape;
164  }
165  const TensorDimsState &tensor_dims_state() const override
166  {
167  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
168  return _dims_state;
169  }
170  DataType data_type() const override
171  {
172  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
173  return _parent->data_type();
174  }
175  Format format() const override
176  {
177  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
178  return _parent->format();
179  }
180  size_t total_size() const override
181  {
182  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
183  return _parent->total_size();
184  }
185  PaddingSize padding() const override
186  {
187  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
188  return _parent->padding();
189  }
190  bool has_padding() const override
191  {
192  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
193  return _parent->has_padding();
194  }
195  bool is_resizable() const override
196  {
197  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
198  return _parent->is_resizable();
199  }
200  bool is_dynamic() const override
201  {
202  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
203  return _parent->is_dynamic();
204  }
205  bool are_values_constant() const override
206  {
207  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
208  return _parent->are_values_constant();
209  }
211  {
212  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
213  _parent->set_is_resizable(is_resizable);
214  return *this;
215  }
217  {
218  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
220  return *this;
221  }
222  ValidRegion valid_region() const override
223  {
224  return _valid_region;
225  }
227  {
228  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
229  // Check if subtensor is valid if parent is configured
230  if(_parent->tensor_shape().total_size() != 0)
231  {
233  }
234  _valid_region = valid_region;
235  }
237  {
238  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
239  return _parent->quantization_info();
240  }
241  DataLayout data_layout() const override
242  {
243  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
244  return _parent->data_layout();
245  }
246  ITensorInfo::Id id() const override
247  {
248  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
249  return _parent->id();
250  }
252  {
253  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
254  _parent->set_id(id);
255  return *this;
256  }
257 
258 private:
259  ITensorInfo *_parent;
260  TensorShape _tensor_shape;
261  TensorDimsState _dims_state;
262  Coordinates _coords;
263  ValidRegion _valid_region;
264  bool _extend_parent;
265  bool _lock_paddings;
266 };
267 } // namespace arm_compute
268 #endif /*ARM_COMPUTE_SUBTENSORINFO_H */
arm_compute::ITensorInfo::set_format
virtual ITensorInfo & set_format(Format format)=0
Set the format of an already initialized tensor.
arm_compute::SubTensorInfo::~SubTensorInfo
~SubTensorInfo()=default
Default destructor.
arm_compute::ITensorInfo::valid_region
virtual ValidRegion valid_region() const =0
Valid region of the tensor.
arm_compute::SubTensorInfo::lock_paddings
bool lock_paddings() const override
Get the lock paddings flag value.
Definition: SubTensorInfo.cpp:123
arm_compute::ITensorInfo::data_layout
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.
arm_compute::SubTensorInfo::set_tensor_dims_state
ITensorInfo & set_tensor_dims_state(const TensorDimsState &state) override
Set the state for each dimension of the tensor.
Definition: SubTensorInfo.cpp:110
arm_compute::SubTensorInfo::operator=
SubTensorInfo & operator=(const SubTensorInfo &)=default
Allow instances of this class to be copied.
arm_compute::SubTensorInfo::set_lock_paddings
ITensorInfo & set_lock_paddings(bool flag) override
Set the lock paddings flag of the tensor.
Definition: SubTensorInfo.cpp:117
arm_compute::ITensorInfo::num_channels
virtual size_t num_channels() const =0
The number of channels for each tensor element.
arm_compute::SubTensorInfo::set_data_layout
ITensorInfo & set_data_layout(const DataLayout &data_layout) override
Set the data layout of the tensor.
Definition: SubTensorInfo.h:82
arm_compute::ITensorInfo::tensor_shape
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
arm_compute::QuantizationInfo
Quantization information.
Definition: QuantizationInfo.h:68
arm_compute::ITensorInfo::set_num_channels
virtual ITensorInfo & set_num_channels(int num_channels)=0
Set the number of channels to the specified value.
Helpers.h
arm_compute::DataLayout
DataLayout
[DataLayout enum definition]
Definition: CoreTypes.h:109
arm_compute::SubTensorInfo::id
ITensorInfo::Id id() const override
Get the workload tensor id of the tensor.
Definition: SubTensorInfo.h:246
arm_compute::BorderSize
Container for 2D border size.
Definition: Types.h:242
arm_compute::ITensorInfo::id
virtual Id id() const =0
Get the workload tensor id of the tensor.
arm_compute::DataLayoutDimension
DataLayoutDimension
[DataLayout enum definition]
Definition: CoreTypes.h:120
arm_compute::TensorShape
Shape of a tensor.
Definition: TensorShape.h:39
arm_compute::SubTensorInfo::offset_first_element_in_bytes
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: SubTensorInfo.h:140
arm_compute::SubTensorInfo::strides_in_bytes
const Strides & strides_in_bytes() const override
The strides in bytes for accessing each dimension of the tensor.
Definition: SubTensorInfo.h:135
arm_compute::ITensorInfo::element_size
virtual size_t element_size() const =0
Element size in bytes calculated as data_size() * num_channels()
arm_compute::ITensorInfo::offset_element_in_bytes
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,...
arm_compute::SubTensorInfo::set_quantization_info
ITensorInfo & set_quantization_info(const QuantizationInfo &quantization_info) override
Set the quantization settings (scale and offset) of the tensor.
Definition: SubTensorInfo.h:102
ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR_VALID_REGION
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR_VALID_REGION(pv, sv)
Definition: Validate.h:1039
arm_compute::SubTensorInfo::SubTensorInfo
SubTensorInfo()
Default constructor.
Definition: SubTensorInfo.cpp:58
TensorInfo.h
arm_compute::SubTensorInfo::set_data_type
ITensorInfo & set_data_type(DataType data_type) override
Set the data type to the specified value.
Definition: SubTensorInfo.h:76
arm_compute::ITensorInfo::set_data_type
virtual ITensorInfo & set_data_type(DataType data_type)=0
Set the data type to the specified value.
ITensorInfo.h
arm_compute::SubTensorInfo::reset_padding
ITensorInfo & reset_padding() override
Resets the padding settings of the tensor.
Definition: SubTensorInfo.h:108
arm_compute::ITensorInfo::has_padding
virtual bool has_padding() const =0
Checks if the tensor has been allocated with padding or not.
arm_compute::Strides
Strides of an item in bytes.
Definition: Strides.h:38
arm_compute::SubTensorInfo::total_size
size_t total_size() const override
Returns the total size of the tensor in bytes.
Definition: SubTensorInfo.h:180
arm_compute::SubTensorInfo
Store the sub tensor's metadata.
Definition: SubTensorInfo.h:41
arm_compute::ITensorInfo::is_dynamic
virtual bool is_dynamic() const =0
Flag indicating whether the shape of the tensor is dynamic, meaning that it can change on kernel/func...
arm_compute::test::validation::shape
shape
Definition: DFT.cpp:115
ARM_COMPUTE_ERROR_ON
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:467
arm_compute::SubTensorInfo::are_values_constant
bool are_values_constant() const override
Flag indicating whether the values of the tensor are constant, meaning that they can change on kernel...
Definition: SubTensorInfo.h:205
arm_compute::SubTensorInfo::tensor_dims_state
const TensorDimsState & tensor_dims_state() const override
State of each dimension of the tensor shape.
Definition: SubTensorInfo.h:165
arm_compute::SubTensorInfo::valid_region
ValidRegion valid_region() const override
Valid region of the tensor.
Definition: SubTensorInfo.h:222
arm_compute::ITensorInfo::reset_padding
virtual ITensorInfo & reset_padding()=0
Resets the padding settings of the tensor.
Coordinates.h
arm_compute::SubTensorInfo::is_resizable
bool is_resizable() const override
Flag indicating whether the size of the tensor can be changed.
Definition: SubTensorInfo.h:195
arm_compute::TensorShape::total_size
size_t total_size() const
Collapses all dimensions to a single linear total size.
Definition: TensorShape.h:176
arm_compute::SubTensorInfo::num_dimensions
size_t num_dimensions() const override
The number of dimensions of the tensor (rank)
Definition: SubTensorInfo.h:151
arm_compute::SubTensorInfo::data_type
DataType data_type() const override
Data type used for each element of the tensor.
Definition: SubTensorInfo.h:170
arm_compute::ValidRegion
Container for valid region of a window.
Definition: Types.h:144
arm_compute::ITensorInfo::Id
int32_t Id
An id that uniquely identifies an ITensorInfo within some domain (e.g.
Definition: ITensorInfo.h:49
arm_compute::SubTensorInfo::quantization_info
QuantizationInfo quantization_info() const override
Get the quantization settings (scale and offset) of the tensor.
Definition: SubTensorInfo.h:236
arm_compute::SubTensorInfo::is_dynamic
bool is_dynamic() const override
Flag indicating whether the shape of the tensor is dynamic, meaning that it can change on kernel/func...
Definition: SubTensorInfo.h:200
arm_compute::SubTensorInfo::element_size
size_t element_size() const override
Element size in bytes calculated as data_size() * num_channels()
Definition: SubTensorInfo.h:146
arm_compute::ITensorInfo::data_type
virtual DataType data_type() const =0
Data type used for each element of the tensor.
arm_compute::SubTensorInfo::set_tensor_shape
ITensorInfo & set_tensor_shape(const TensorShape &shape) override
Set the shape of an already initialized tensor.
Definition: SubTensorInfo.cpp:89
arm_compute::ITensorInfo::set_is_resizable
virtual ITensorInfo & set_is_resizable(bool is_resizable)=0
Set the flag whether the tensor size can be changed.
Strides.h
arm_compute::ITensorInfo::set_are_values_constant
virtual ITensorInfo & set_are_values_constant(bool are_values_constant)=0
Set the flag whether the tensor values can change during kernel/function execution.
arm_compute::SubTensorInfo::dimension
size_t dimension(DataLayoutDimension dimension) const override
Return the size of the requested data layout dimension.
Definition: SubTensorInfo.h:130
arm_compute::SubTensorInfo::set_valid_region
void set_valid_region(const ValidRegion &valid_region) override
Set the valid region of the tensor.
Definition: SubTensorInfo.h:226
arm_compute::Coordinates
Coordinates of an item.
Definition: Coordinates.h:37
arm_compute::Format
Format
Image colour formats.
Definition: CoreTypes.h:57
arm_compute::SubTensorInfo::extend_padding
bool extend_padding(const PaddingSize &padding) override
Update the offset to the first element, the strides and the total size.
Definition: SubTensorInfo.cpp:128
arm_compute::ITensorInfo::quantization_info
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
arm_compute::SubTensorInfo::num_channels
size_t num_channels() const override
The number of channels for each tensor element.
Definition: SubTensorInfo.h:155
arm_compute::get_data_layout_dimension_index
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:203
arm_compute::ITensorInfo::TensorDimsState
std::vector< int > TensorDimsState
Definition: ITensorInfo.h:46
arm_compute::SubTensorInfo::data_layout
DataLayout data_layout() const override
Get the data layout of the tensor.
Definition: SubTensorInfo.h:241
arm_compute::ITensorInfo::is_resizable
virtual bool is_resizable() const =0
Flag indicating whether the size of the tensor can be changed.
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::ITensorInfo::auto_padding
virtual bool auto_padding()=0
Update the offset to the first element and the strides to automatically computed values.
arm_compute::SubTensorInfo::format
Format format() const override
Colour format of the image.
Definition: SubTensorInfo.h:175
arm_compute::SubTensorInfo::tensor_shape
const TensorShape & tensor_shape() const override
Size for each dimension of the tensor.
Definition: SubTensorInfo.h:160
arm_compute::ITensorInfo::strides_in_bytes
virtual const Strides & strides_in_bytes() const =0
The strides in bytes for accessing each dimension of the tensor.
arm_compute::SubTensorInfo::set_is_resizable
ITensorInfo & set_is_resizable(bool is_resizable) override
Set the flag whether the tensor size can be changed.
Definition: SubTensorInfo.h:210
TensorShape.h
arm_compute::SubTensorInfo::set_format
ITensorInfo & set_format(Format format) override
Set the format of an already initialized tensor.
Definition: SubTensorInfo.h:94
arm_compute::SubTensorInfo::has_padding
bool has_padding() const override
Checks if the tensor has been allocated with padding or not.
Definition: SubTensorInfo.h:190
arm_compute::ITensorInfo
Store the tensor's metadata.
Definition: ITensorInfo.h:43
arm_compute::ITensorInfo::set_data_layout
virtual ITensorInfo & set_data_layout(const DataLayout &data_layout)=0
Set the data layout of the tensor.
arm_compute::SubTensorInfo::set_num_channels
ITensorInfo & set_num_channels(int num_channels) override
Set the number of channels to the specified value.
Definition: SubTensorInfo.h:88
arm_compute::SubTensorInfo::clone
std::unique_ptr< ITensorInfo > clone() const override
Definition: SubTensorInfo.cpp:78
arm_compute::ITensorInfo::are_values_constant
virtual bool are_values_constant() const =0
Flag indicating whether the values of the tensor are constant, meaning that they can change on kernel...
arm_compute::SubTensorInfo::offset_element_in_bytes
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: SubTensorInfo.cpp:149
arm_compute::SubTensorInfo::set_id
ITensorInfo & set_id(ITensorInfo::Id id) override
Set the tensor id.
Definition: SubTensorInfo.h:251
arm_compute::SubTensorInfo::dimension
size_t dimension(size_t index) const override
Return the size of the requested dimension.
Definition: SubTensorInfo.h:126
arm_compute::ITensorInfo::format
virtual Format format() const =0
Colour format of the image.
arm_compute::Dimensions::num_dimensions
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:143
arm_compute::SubTensorInfo::set_are_values_constant
ITensorInfo & set_are_values_constant(bool are_values_constant) override
Set the flag whether the tensor values can change during kernel/function execution.
Definition: SubTensorInfo.h:216
arm_compute::ITensorInfo::total_size
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
arm_compute::DataType
DataType
Available data types.
Definition: CoreTypes.h:82
arm_compute::SubTensorInfo::coords
Coordinates coords() const
Returns the coordinates of the sub-tensor inside the parent tensor.
Definition: SubTensorInfo.h:69
arm_compute::ITensorInfo::set_id
virtual ITensorInfo & set_id(ITensorInfo::Id id)=0
Set the tensor id.
arm_compute::ITensorInfo::set_quantization_info
virtual ITensorInfo & set_quantization_info(const QuantizationInfo &quantization_info)=0
Set the quantization settings (scale and offset) of the tensor.
arm_compute::SubTensorInfo::auto_padding
bool auto_padding() override
Update the offset to the first element and the strides to automatically computed values.
Definition: SubTensorInfo.h:114
arm_compute::SubTensorInfo::padding
PaddingSize padding() const override
Padding of tensor.
Definition: SubTensorInfo.h:185
arm_compute::ITensorInfo::padding
virtual PaddingSize padding() const =0
Padding of tensor.