Compute Library
 23.11
SubTensorInfo.cpp
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  */
25 
26 #include "arm_compute/core/Error.h"
29 
30 namespace arm_compute
31 {
32 namespace
33 {
34 /** Extends parent shape depending on subtensor's coordinates and shape
35  *
36  * @param parent_shape Parent shape
37  * @param shape Subtensor shape
38  * @param coords Subtensor coordinates inside parent tensor
39  *
40  * @return Extended parent shape
41  */
42 TensorShape extend_parent_shape(TensorShape parent_shape, TensorShape shape, Coordinates coords)
43 {
44  // Extend shape
45  for (unsigned int i = 0; i < TensorShape::num_max_dimensions; ++i)
46  {
47  int dimension_extend = coords[i] + static_cast<int>(shape[i]);
48  if ((dimension_extend > static_cast<int>(parent_shape[i])) && (dimension_extend > 0))
49  {
50  parent_shape.set(i, static_cast<size_t>(dimension_extend));
51  }
52  }
53 
54  return parent_shape;
55 }
56 } // namespace
57 
59  : _parent(nullptr),
60  _tensor_shape(),
61  _dims_state(),
62  _coords(),
63  _valid_region{Coordinates(), _tensor_shape},
64  _extend_parent(false),
65  _lock_paddings(false)
66 {
67 }
68 
69 SubTensorInfo::SubTensorInfo(ITensorInfo *parent, TensorShape tensor_shape, Coordinates coords, bool extend_parent)
70  : _parent(parent),
71  _tensor_shape(tensor_shape),
72  _dims_state(),
73  _coords(coords),
74  _valid_region{Coordinates(), _tensor_shape},
75  _extend_parent(extend_parent),
76  _lock_paddings(false)
77 {
78  ARM_COMPUTE_ERROR_ON(parent == nullptr);
79 
80  // Check if subtensor is valid if parent is configured
81  if (parent->tensor_shape().total_size() != 0 && !_extend_parent)
82  {
83  ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR(parent->tensor_shape(), coords, tensor_shape);
84  }
85 
86  // Initialize valid region
87  _valid_region = ValidRegion{Coordinates(), _tensor_shape};
88 }
89 
90 std::unique_ptr<ITensorInfo> SubTensorInfo::clone() const
91 {
92  // Clone creates a TensorInfo object from SubTensorInfo's parent which will conclude to a TensorInfo
93  // For now it does not make sense to copy a SubTensorInfo explicitly
94  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
95  auto clone_obj = _parent->clone();
96  clone_obj->set_tensor_shape(_tensor_shape);
97  clone_obj->set_valid_region(_valid_region);
98  return clone_obj;
99 }
100 
102 {
103  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
104 
105  // Check if subtensor is valid if parent is configured
106  if (_parent->tensor_shape().total_size() != 0 && !_extend_parent)
107  {
109  _valid_region = ValidRegion{_coords, shape};
110  }
111  else if (_extend_parent) // Extend parent shape, configure if specified
112  {
113  ARM_COMPUTE_ERROR_ON((_parent->data_type() == DataType::UNKNOWN) && (_parent->format() == Format::UNKNOWN));
114  TensorShape parent_extended_shape = extend_parent_shape(_parent->tensor_shape(), shape, _coords);
115  _parent->set_tensor_shape(parent_extended_shape);
116  _parent->set_valid_region(ValidRegion{Coordinates(), parent_extended_shape});
117  }
118  _tensor_shape = shape;
119  return *this;
120 }
121 
122 ITensorInfo &SubTensorInfo::set_tensor_dims_state(const TensorDimsState &state)
123 {
124  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
125  _dims_state = state;
126  return *this;
127 }
128 
130 {
131  _lock_paddings = flag;
132  return *this;
133 }
134 
136 {
137  return _lock_paddings;
138 }
139 
141 {
142  ARM_COMPUTE_ERROR_ON(_lock_paddings);
143  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
144  ARM_COMPUTE_ERROR_ON(!_parent->is_resizable());
145  ARM_COMPUTE_ERROR_ON(_parent->total_size() == 0);
146 
147  // Check that you do not extend padding on sub-tensors unless XY shape matches parent tensor
148  if (!_extend_parent && (padding.left || padding.right))
149  {
150  ARM_COMPUTE_ERROR_ON(_parent->tensor_shape().x() != tensor_shape().x());
151  }
152  if (!_extend_parent && (padding.top || padding.bottom))
153  {
154  ARM_COMPUTE_ERROR_ON(_parent->tensor_shape().y() != tensor_shape().y());
155  }
156 
157  // Extend parent padding if required
158  return _parent->extend_padding(padding);
159 }
160 
162 {
164 
166  const Strides &strides = strides_in_bytes();
167 
168  for (size_t i = 0; i < _tensor_shape.num_dimensions(); ++i)
169  {
170  offset += pos[i] * strides[i];
171  }
172 
173  return offset;
174 }
175 } // namespace arm_compute
arm_compute::BorderSize::right
unsigned int right
right of the border
Definition: Types.h:340
arm_compute::SubTensorInfo::lock_paddings
bool lock_paddings() const override
Get the lock paddings flag value.
Definition: SubTensorInfo.cpp:135
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:122
arm_compute::SubTensorInfo::set_lock_paddings
ITensorInfo & set_lock_paddings(bool flag) override
Set the lock paddings flag of the tensor.
Definition: SubTensorInfo.cpp:129
arm_compute::Format::UNKNOWN
@ UNKNOWN
Unknown image format.
arm_compute::ITensorInfo::tensor_shape
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Helpers.h
arm_compute::BorderSize
Container for 2D border size.
Definition: Types.h:239
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:139
arm_compute::ITensorInfo::extend_padding
virtual bool extend_padding(const PaddingSize &padding)=0
Update the offset to the first element, the strides and the total size.
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:134
arm_compute::ITensorInfo::set_tensor_shape
virtual ITensorInfo & set_tensor_shape(const TensorShape &shape)=0
Set the shape of an already initialized tensor.
SubTensorInfo.h
arm_compute::BorderSize::top
unsigned int top
top of the border
Definition: Types.h:339
arm_compute::SubTensorInfo::SubTensorInfo
SubTensorInfo()
Default constructor.
Definition: SubTensorInfo.cpp:58
Error.h
arm_compute::Strides
Strides of an item in bytes.
Definition: Strides.h:38
ARM_COMPUTE_ERROR_ON_COORDINATES_DIMENSIONS_GTE
#define ARM_COMPUTE_ERROR_ON_COORDINATES_DIMENSIONS_GTE(p, md)
Definition: Validate.h:244
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:466
arm_compute::BorderSize::bottom
unsigned int bottom
bottom of the border
Definition: Types.h:341
arm_compute::TensorShape::total_size
size_t total_size() const
Collapses all dimensions to a single linear total size.
Definition: TensorShape.h:175
ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR(p, c, s)
Definition: Validate.h:1101
arm_compute::ValidRegion
Container for valid region of a window.
Definition: Types.h:143
arm_compute::ITensorInfo::data_type
virtual DataType data_type() const =0
Data type used for each element of the tensor.
offset
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:1128
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:101
arm_compute::Dimensions::x
T x() const
Alias to access the size of the first dimension.
Definition: Dimensions.h:86
arm_compute::Coordinates
Coordinates of an item.
Definition: Coordinates.h:37
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:140
arm_compute::misc::ICloneable::clone
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
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::set_valid_region
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
arm_compute::BorderSize::left
unsigned int left
left of the border
Definition: Types.h:342
arm_compute::SubTensorInfo::tensor_shape
const TensorShape & tensor_shape() const override
Size for each dimension of the tensor.
Definition: SubTensorInfo.h:159
arm_compute::ITensorInfo
Store the tensor's metadata.
Definition: ITensorInfo.h:44
arm_compute::SubTensorInfo::clone
std::unique_ptr< ITensorInfo > clone() const override
Definition: SubTensorInfo.cpp:90
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:161
arm_compute::DataType::UNKNOWN
@ UNKNOWN
Unknown data type.
arm_compute::ITensorInfo::format
virtual Format format() const =0
Colour format of the image.
arm_compute::Dimensions::y
T y() const
Alias to access the size of the second dimension.
Definition: Dimensions.h:91
Validate.h
arm_compute::Dimensions::num_dimensions
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:142
arm_compute::ITensorInfo::total_size
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
arm_compute::Dimensions< size_t >::num_max_dimensions
static constexpr size_t num_max_dimensions
Number of dimensions the tensor has.
Definition: Dimensions.h:46
arm_compute::SubTensorInfo::padding
PaddingSize padding() const override
Padding of tensor.
Definition: SubTensorInfo.h:184