blob: 8012c3d7216969e416c2c2ec3d396eb67b60020d [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Ramy Elgammald2d93612022-12-22 15:21:03 +00002 * Copyright (c) 2017-2023 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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#include "arm_compute/core/SubTensorInfo.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/Validate.h"
29
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +000030namespace arm_compute
31{
Georgios Pinitas652bde52018-01-10 15:33:28 +000032namespace
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 */
42TensorShape extend_parent_shape(TensorShape parent_shape, TensorShape shape, Coordinates coords)
43{
Georgios Pinitas652bde52018-01-10 15:33:28 +000044 // Extend shape
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010045 for (unsigned int i = 0; i < TensorShape::num_max_dimensions; ++i)
Georgios Pinitas652bde52018-01-10 15:33:28 +000046 {
47 int dimension_extend = coords[i] + static_cast<int>(shape[i]);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010048 if ((dimension_extend > static_cast<int>(parent_shape[i])) && (dimension_extend > 0))
Georgios Pinitas652bde52018-01-10 15:33:28 +000049 {
50 parent_shape.set(i, static_cast<size_t>(dimension_extend));
51 }
52 }
53
54 return parent_shape;
55}
56} // namespace
57
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058SubTensorInfo::SubTensorInfo()
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010059 : _parent(nullptr),
60 _tensor_shape(),
61 _dims_state(),
62 _coords(),
63 _valid_region{Coordinates(), _tensor_shape},
64 _extend_parent(false),
65 _lock_paddings(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066{
67}
68
Georgios Pinitas652bde52018-01-10 15:33:28 +000069SubTensorInfo::SubTensorInfo(ITensorInfo *parent, TensorShape tensor_shape, Coordinates coords, bool extend_parent)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010070 : _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)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010077{
78 ARM_COMPUTE_ERROR_ON(parent == nullptr);
Georgios Pinitas0b5af9f2020-06-19 23:22:08 +010079
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010080 // Check if subtensor is valid if parent is configured
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010081 if (parent->tensor_shape().total_size() != 0 && !_extend_parent)
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010082 {
83 ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR(parent->tensor_shape(), coords, tensor_shape);
84 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010085
86 // Initialize valid region
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010087 _valid_region = ValidRegion{Coordinates(), _tensor_shape};
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088}
89
Georgios Pinitas283c1792017-11-10 18:14:06 +000090std::unique_ptr<ITensorInfo> SubTensorInfo::clone() const
91{
Georgios Pinitas9d364842017-12-12 12:20:55 +000092 // 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;
Georgios Pinitas283c1792017-11-10 18:14:06 +000099}
100
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000101ITensorInfo &SubTensorInfo::set_tensor_shape(const TensorShape &shape)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102{
103 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
Georgios Pinitas652bde52018-01-10 15:33:28 +0000104
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100105 // Check if subtensor is valid if parent is configured
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100106 if (_parent->tensor_shape().total_size() != 0 && !_extend_parent)
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100107 {
108 ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR(_parent->tensor_shape(), _coords, shape);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100109 _valid_region = ValidRegion{_coords, shape};
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100110 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100111 else if (_extend_parent) // Extend parent shape, configure if specified
Georgios Pinitas652bde52018-01-10 15:33:28 +0000112 {
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);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100116 _parent->set_valid_region(ValidRegion{Coordinates(), parent_extended_shape});
Georgios Pinitas652bde52018-01-10 15:33:28 +0000117 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100118 _tensor_shape = shape;
Georgios Pinitas283c1792017-11-10 18:14:06 +0000119 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120}
121
Georgios Pinitasb14a0f02021-01-08 03:14:31 +0000122ITensorInfo &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
Ramy Elgammald2d93612022-12-22 15:21:03 +0000129ITensorInfo &SubTensorInfo::set_lock_paddings(bool flag)
130{
131 _lock_paddings = flag;
132 return *this;
133}
134
135bool SubTensorInfo::lock_paddings() const
136{
137 return _lock_paddings;
138}
139
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140bool SubTensorInfo::extend_padding(const PaddingSize &padding)
141{
Ramy Elgammald2d93612022-12-22 15:21:03 +0000142 ARM_COMPUTE_ERROR_ON(_lock_paddings);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
144 ARM_COMPUTE_ERROR_ON(!_parent->is_resizable());
Georgios Pinitas652bde52018-01-10 15:33:28 +0000145 ARM_COMPUTE_ERROR_ON(_parent->total_size() == 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146
Georgios Pinitas0b5af9f2020-06-19 23:22:08 +0100147 // Check that you do not extend padding on sub-tensors unless XY shape matches parent tensor
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100148 if (!_extend_parent && (padding.left || padding.right))
Georgios Pinitas0b5af9f2020-06-19 23:22:08 +0100149 {
150 ARM_COMPUTE_ERROR_ON(_parent->tensor_shape().x() != tensor_shape().x());
151 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100152 if (!_extend_parent && (padding.top || padding.bottom))
Georgios Pinitas0b5af9f2020-06-19 23:22:08 +0100153 {
154 ARM_COMPUTE_ERROR_ON(_parent->tensor_shape().y() != tensor_shape().y());
155 }
156
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157 // Extend parent padding if required
158 return _parent->extend_padding(padding);
159}
160
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100161int32_t SubTensorInfo::offset_element_in_bytes(const Coordinates &pos) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100162{
163 ARM_COMPUTE_ERROR_ON_COORDINATES_DIMENSIONS_GTE(pos, _tensor_shape.num_dimensions());
164
Matthew Bentham92046462020-03-07 22:15:55 +0000165 int32_t offset = offset_first_element_in_bytes();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100166 const Strides &strides = strides_in_bytes();
167
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100168 for (size_t i = 0; i < _tensor_shape.num_dimensions(); ++i)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169 {
170 offset += pos[i] * strides[i];
171 }
172
173 return offset;
174}
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +0000175} // namespace arm_compute