Compute Library
 20.05
SubTensorInfo.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 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 using 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  // Subtensor should not index in x, y dimensions.
45  ARM_COMPUTE_ERROR_ON((coords.x() != 0) || (coords.y() != 0));
46 
47  // Cannot extend on x, y ?
48  ARM_COMPUTE_ERROR_ON((parent_shape.total_size() != 0) && (parent_shape.x() != shape.x()) && (parent_shape.y() != shape.y()));
49 
50  // Extend shape
51  for(unsigned int i = 0; i < TensorShape::num_max_dimensions; ++i)
52  {
53  int dimension_extend = coords[i] + static_cast<int>(shape[i]);
54  if((dimension_extend > static_cast<int>(parent_shape[i])) && (dimension_extend > 0))
55  {
56  parent_shape.set(i, static_cast<size_t>(dimension_extend));
57  }
58  }
59 
60  return parent_shape;
61 }
62 } // namespace
63 
65  : _parent(nullptr), _tensor_shape(), _coords(), _valid_region{ Coordinates(), _tensor_shape }, _extend_parent(false)
66 {
67 }
68 
69 SubTensorInfo::SubTensorInfo(ITensorInfo *parent, TensorShape tensor_shape, Coordinates coords, bool extend_parent)
70  : _parent(parent), _tensor_shape(tensor_shape), _coords(coords), _valid_region{ Coordinates(), _tensor_shape }, _extend_parent(extend_parent)
71 {
72  ARM_COMPUTE_ERROR_ON(parent == nullptr);
73  // Check if subtensor is valid if parent is configured
74  if(parent->tensor_shape().total_size() != 0 && !_extend_parent)
75  {
76  ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR(parent->tensor_shape(), coords, tensor_shape);
77  }
78 
79  // Initialize valid region
80  _valid_region = ValidRegion{ Coordinates(), _tensor_shape };
81 }
82 
83 std::unique_ptr<ITensorInfo> SubTensorInfo::clone() const
84 {
85  // Clone creates a TensorInfo object from SubTensorInfo's parent which will conclude to a TensorInfo
86  // For now it does not make sense to copy a SubTensorInfo explicitly
87  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
88  auto clone_obj = _parent->clone();
89  clone_obj->set_tensor_shape(_tensor_shape);
90  clone_obj->set_valid_region(_valid_region);
91  return clone_obj;
92 }
93 
95 {
96  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
97 
98  // Check if subtensor is valid if parent is configured
99  if(_parent->tensor_shape().total_size() != 0 && !_extend_parent)
100  {
102  _valid_region = ValidRegion{ _coords, shape };
103  }
104  else if(_extend_parent) // Extend parent shape, configure if specified
105  {
106  ARM_COMPUTE_ERROR_ON((_parent->data_type() == DataType::UNKNOWN) && (_parent->format() == Format::UNKNOWN));
107  TensorShape parent_extended_shape = extend_parent_shape(_parent->tensor_shape(), shape, _coords);
108  _parent->set_tensor_shape(parent_extended_shape);
109  _parent->set_valid_region(ValidRegion{ Coordinates(), parent_extended_shape });
110  }
111  _tensor_shape = shape;
112  return *this;
113 }
114 
116 {
117  ARM_COMPUTE_ERROR_ON(_parent == nullptr);
118  ARM_COMPUTE_ERROR_ON(!_parent->is_resizable());
119  ARM_COMPUTE_ERROR_ON(_parent->total_size() == 0);
120 
121  // Extend parent padding if required
122  return _parent->extend_padding(padding);
123 }
124 
126 {
128 
130  const Strides &strides = strides_in_bytes();
131 
132  for(size_t i = 0; i < _tensor_shape.num_dimensions(); ++i)
133  {
134  offset += pos[i] * strides[i];
135  }
136 
137  return offset;
138 }
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:510
Shape of a tensor.
Definition: TensorShape.h:39
bool extend_padding(const PaddingSize &padding) override
Update the offset to the first element, the strides and the total size.
Container for 2D border size.
Definition: Types.h:272
virtual ITensorInfo & set_tensor_shape(const TensorShape &shape)=0
Set the shape of an already initialized tensor.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
#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
Store the tensor's metadata.
Definition: ITensorInfo.h:40
const Strides & strides_in_bytes() const override
The strides in bytes for accessing each dimension of the tensor.
Copyright (c) 2017-2020 ARM Limited.
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
T x() const
Alias to access the size of the first dimension.
Definition: Dimensions.h:81
virtual bool is_resizable() const =0
Flag indicating whether the size of the tensor can be changed.
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.
virtual Format format() const =0
Colour format of the image.
PaddingSize padding() const override
Padding of tensor.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
#define ARM_COMPUTE_ERROR_ON_COORDINATES_DIMENSIONS_GTE(p, md)
Definition: Validate.h:244
Coordinates of an item.
Definition: Coordinates.h:37
size_t total_size() const
Collapses all dimensions to a single linear total size.
Definition: TensorShape.h:171
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR(p, c, s)
Definition: Validate.h:959
Strides of an item in bytes.
Definition: Strides.h:37
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
SubTensorInfo()
Default constructor.
std::unique_ptr< ITensorInfo > clone() const override
Provide a clone of the current object of class T.
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:122
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,...
TensorShape & set(size_t dimension, size_t value, bool apply_dim_correction=true)
Accessor to set the value of one of the dimensions.
Definition: TensorShape.h:78
T y() const
Alias to access the size of the second dimension.
Definition: Dimensions.h:86
ITensorInfo & set_tensor_shape(const TensorShape &shape) override
Set the shape of an already initialized tensor.
Container for valid region of a window.
Definition: Types.h:187
static constexpr size_t num_max_dimensions
Number of dimensions the tensor has.
Definition: Dimensions.h:45
virtual bool extend_padding(const PaddingSize &padding)=0
Update the offset to the first element, the strides and the total size.