blob: 7a3ee2cfd0f2c6588955589d935da03925c05d65 [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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_SUBTENSORINFO_H
25#define ARM_COMPUTE_SUBTENSORINFO_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Coordinates.h"
Georgios Pinitas6f669f02017-09-26 12:32:57 +010028#include "arm_compute/core/Helpers.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010029#include "arm_compute/core/ITensorInfo.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/Strides.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/TensorShape.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033
34#include <cstddef>
Georgios Pinitas283c1792017-11-10 18:14:06 +000035#include <memory>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
37namespace arm_compute
38{
39/** Store the sub tensor's metadata */
40class SubTensorInfo final : public ITensorInfo
41{
42public:
43 /** Default constructor */
44 SubTensorInfo();
45 /** Default constructor
46 *
Georgios Pinitas652bde52018-01-10 15:33:28 +000047 * @param[in] parent Metadata of parent tensor.
48 * @param[in] tensor_shape Tensor shape. Shape must fit inside parent's shape.
49 * X and Y dimensions must match the parent's ones.
50 * @param[in] coords Coordinates of starting element inside parent tensor.
51 * @param[in] extend_parent (Optional) Extend parent with subtensor shape if subtensor indexes out of bounds
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052 */
Georgios Pinitas652bde52018-01-10 15:33:28 +000053 SubTensorInfo(ITensorInfo *parent, TensorShape tensor_shape, Coordinates coords, bool extend_parent = false);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054 /** Default destructor */
55 ~SubTensorInfo() = default;
56 /** Allow instances of this class to be copy constructed */
57 SubTensorInfo(const SubTensorInfo &) = default;
58 /** Allow instances of this class to be copied */
59 SubTensorInfo &operator=(const SubTensorInfo &) = default;
60 /** Allow instances of this class to be move constructed */
61 SubTensorInfo(SubTensorInfo &&) = default;
62 /** Allow instances of this class to be moved */
63 SubTensorInfo &operator=(SubTensorInfo &&) = default;
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010064 /** Returns the coordinates of the sub-tensor inside the parent tensor
65 *
66 * @return Sub-tensor coordinates
67 */
68 Coordinates coords() const
69 {
70 return _coords;
71 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072
73 // Inherited methods overridden:
Georgios Pinitas283c1792017-11-10 18:14:06 +000074 std::unique_ptr<ITensorInfo> clone() const override;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010075 ITensorInfo &set_data_type(DataType data_type) override
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076 {
77 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
78 _parent->set_data_type(data_type);
Georgios Pinitas283c1792017-11-10 18:14:06 +000079 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080 };
Isabella Gottardid17a6772018-02-27 17:41:55 +000081 ITensorInfo &set_data_layout(const DataLayout &data_layout) override
82 {
83 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
84 _parent->set_data_layout(data_layout);
85 return *this;
86 };
Georgios Pinitas283c1792017-11-10 18:14:06 +000087 ITensorInfo &set_num_channels(int num_channels) override
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088 {
89 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
90 _parent->set_num_channels(num_channels);
Georgios Pinitas283c1792017-11-10 18:14:06 +000091 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092 };
Georgios Pinitas283c1792017-11-10 18:14:06 +000093 ITensorInfo &set_format(Format format) override
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094 {
95 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
96 _parent->set_format(format);
Georgios Pinitas283c1792017-11-10 18:14:06 +000097 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098 };
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000099 ITensorInfo &set_tensor_shape(const TensorShape &shape) override;
Georgios Pinitasb14a0f02021-01-08 03:14:31 +0000100 ITensorInfo &set_tensor_dims_state(const TensorDimsState &state) override;
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000101 ITensorInfo &set_quantization_info(const QuantizationInfo &quantization_info) override
Georgios Pinitas283c1792017-11-10 18:14:06 +0000102 {
103 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
104 _parent->set_quantization_info(quantization_info);
105 return *this;
106 }
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000107 ITensorInfo &reset_padding() override
108 {
109 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
110 _parent->reset_padding();
111 return *this;
112 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113 bool auto_padding() override
114 {
115 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
116 return _parent->auto_padding();
117 };
Ramy Elgammald2d93612022-12-22 15:21:03 +0000118
119 ITensorInfo &set_lock_paddings(bool flag) override;
120
121 bool lock_paddings() const override;
122
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123 bool extend_padding(const PaddingSize &padding) override;
Ramy Elgammald2d93612022-12-22 15:21:03 +0000124
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125 size_t dimension(size_t index) const override
126 {
127 return _tensor_shape[index];
128 }
Isabella Gottardid56e7702018-02-28 14:29:36 +0000129 size_t dimension(DataLayoutDimension dimension) const override
130 {
131 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
132 return get_data_layout_dimension_index(_parent->data_layout(), dimension);
133 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134 const Strides &strides_in_bytes() const override
135 {
136 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
137 return _parent->strides_in_bytes();
138 }
139 size_t offset_first_element_in_bytes() const override
140 {
141 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
142 return _parent->offset_element_in_bytes(_coords);
143 }
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100144 int32_t offset_element_in_bytes(const Coordinates &pos) const override;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100145 size_t element_size() const override
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146 {
147 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
148 return _parent->element_size();
149 }
150 size_t num_dimensions() const override
151 {
152 return _tensor_shape.num_dimensions();
153 }
154 size_t num_channels() const override
155 {
156 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
157 return _parent->num_channels();
158 }
159 const TensorShape &tensor_shape() const override
160 {
161 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
162 return _tensor_shape;
163 }
Georgios Pinitasb14a0f02021-01-08 03:14:31 +0000164 const TensorDimsState &tensor_dims_state() const override
165 {
166 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
167 return _dims_state;
168 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169 DataType data_type() const override
170 {
171 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
172 return _parent->data_type();
173 }
174 Format format() const override
175 {
176 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
177 return _parent->format();
178 }
179 size_t total_size() const override
180 {
181 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
182 return _parent->total_size();
183 }
184 PaddingSize padding() const override
185 {
186 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
187 return _parent->padding();
188 }
189 bool has_padding() const override
190 {
191 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
192 return _parent->has_padding();
193 }
194 bool is_resizable() const override
195 {
196 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
197 return _parent->is_resizable();
198 }
Georgios Pinitas49be2e32019-09-02 13:18:55 +0100199 bool is_dynamic() const override
200 {
201 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
202 return _parent->is_dynamic();
203 }
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100204 bool are_values_constant() const override
205 {
206 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
207 return _parent->are_values_constant();
208 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000209 ITensorInfo &set_is_resizable(bool is_resizable) override
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100210 {
211 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
212 _parent->set_is_resizable(is_resizable);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000213 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100214 }
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100215 ITensorInfo &set_are_values_constant(bool are_values_constant) override
216 {
217 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
218 _parent->set_are_values_constant(are_values_constant);
219 return *this;
220 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100221 ValidRegion valid_region() const override
222 {
223 return _valid_region;
224 }
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000225 void set_valid_region(const ValidRegion &valid_region) override
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100226 {
227 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100228 // Check if subtensor is valid if parent is configured
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100229 if (_parent->tensor_shape().total_size() != 0)
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100230 {
231 ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR_VALID_REGION(_parent->valid_region(), valid_region);
232 }
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000233 _valid_region = valid_region;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100234 }
Michel Iwaniec00633802017-10-12 14:14:15 +0100235 QuantizationInfo quantization_info() const override
236 {
237 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
238 return _parent->quantization_info();
239 }
Isabella Gottardid17a6772018-02-27 17:41:55 +0000240 DataLayout data_layout() const override
241 {
242 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
243 return _parent->data_layout();
244 }
SiCong Lif44bbc52022-08-29 18:25:51 +0100245 ITensorInfo::Id id() const override
246 {
247 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
248 return _parent->id();
249 }
250 ITensorInfo &set_id(ITensorInfo::Id id) override
251 {
252 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
253 _parent->set_id(id);
254 return *this;
255 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100256
257private:
Georgios Pinitasb14a0f02021-01-08 03:14:31 +0000258 ITensorInfo *_parent;
259 TensorShape _tensor_shape;
260 TensorDimsState _dims_state;
261 Coordinates _coords;
262 ValidRegion _valid_region;
263 bool _extend_parent;
Ramy Elgammald2d93612022-12-22 15:21:03 +0000264 bool _lock_paddings;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100265};
Georgios Pinitas49be2e32019-09-02 13:18:55 +0100266} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000267#endif /*ARM_COMPUTE_SUBTENSORINFO_H */