blob: 5aee115dd336177dedbe2f1345a857219499dd54 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitas49be2e32019-09-02 13:18:55 +01002 * Copyright (c) 2017-2019 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#ifndef __ARM_COMPUTE_SUBTENSORINFO_H__
25#define __ARM_COMPUTE_SUBTENSORINFO_H__
26
27#include "arm_compute/core/ITensorInfo.h"
28
29#include "arm_compute/core/Coordinates.h"
Georgios Pinitas6f669f02017-09-26 12:32:57 +010030#include "arm_compute/core/Helpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/core/Strides.h"
32#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/TensorShape.h"
34#include "arm_compute/core/Validate.h"
35
36#include <cstddef>
Georgios Pinitas283c1792017-11-10 18:14:06 +000037#include <memory>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038
39namespace arm_compute
40{
41/** Store the sub tensor's metadata */
42class SubTensorInfo final : public ITensorInfo
43{
44public:
45 /** Default constructor */
46 SubTensorInfo();
47 /** Default constructor
48 *
Georgios Pinitas652bde52018-01-10 15:33:28 +000049 * @param[in] parent Metadata of parent tensor.
50 * @param[in] tensor_shape Tensor shape. Shape must fit inside parent's shape.
51 * X and Y dimensions must match the parent's ones.
52 * @param[in] coords Coordinates of starting element inside parent tensor.
53 * @param[in] extend_parent (Optional) Extend parent with subtensor shape if subtensor indexes out of bounds
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054 */
Georgios Pinitas652bde52018-01-10 15:33:28 +000055 SubTensorInfo(ITensorInfo *parent, TensorShape tensor_shape, Coordinates coords, bool extend_parent = false);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056 /** Default destructor */
57 ~SubTensorInfo() = default;
58 /** Allow instances of this class to be copy constructed */
59 SubTensorInfo(const SubTensorInfo &) = default;
60 /** Allow instances of this class to be copied */
61 SubTensorInfo &operator=(const SubTensorInfo &) = default;
62 /** Allow instances of this class to be move constructed */
63 SubTensorInfo(SubTensorInfo &&) = default;
64 /** Allow instances of this class to be moved */
65 SubTensorInfo &operator=(SubTensorInfo &&) = default;
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010066 /** Returns the coordinates of the sub-tensor inside the parent tensor
67 *
68 * @return Sub-tensor coordinates
69 */
70 Coordinates coords() const
71 {
72 return _coords;
73 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074
75 // Inherited methods overridden:
Georgios Pinitas283c1792017-11-10 18:14:06 +000076 std::unique_ptr<ITensorInfo> clone() const override;
77 ITensorInfo &set_data_type(DataType data_type) override
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078 {
79 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
80 _parent->set_data_type(data_type);
Georgios Pinitas283c1792017-11-10 18:14:06 +000081 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082 };
Isabella Gottardid17a6772018-02-27 17:41:55 +000083 ITensorInfo &set_data_layout(const DataLayout &data_layout) override
84 {
85 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
86 _parent->set_data_layout(data_layout);
87 return *this;
88 };
Georgios Pinitas283c1792017-11-10 18:14:06 +000089 ITensorInfo &set_num_channels(int num_channels) override
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090 {
91 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
92 _parent->set_num_channels(num_channels);
Georgios Pinitas283c1792017-11-10 18:14:06 +000093 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094 };
Georgios Pinitas283c1792017-11-10 18:14:06 +000095 ITensorInfo &set_format(Format format) override
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096 {
97 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
98 _parent->set_format(format);
Georgios Pinitas283c1792017-11-10 18:14:06 +000099 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100 };
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000101 ITensorInfo &set_tensor_shape(const TensorShape &shape) override;
102 ITensorInfo &set_quantization_info(const QuantizationInfo &quantization_info) override
Georgios Pinitas283c1792017-11-10 18:14:06 +0000103 {
104 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
105 _parent->set_quantization_info(quantization_info);
106 return *this;
107 }
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000108 ITensorInfo &reset_padding() override
109 {
110 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
111 _parent->reset_padding();
112 return *this;
113 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114 bool auto_padding() override
115 {
116 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
117 return _parent->auto_padding();
118 };
119 bool extend_padding(const PaddingSize &padding) override;
120 size_t dimension(size_t index) const override
121 {
122 return _tensor_shape[index];
123 }
Isabella Gottardid56e7702018-02-28 14:29:36 +0000124 size_t dimension(DataLayoutDimension dimension) const override
125 {
126 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
127 return get_data_layout_dimension_index(_parent->data_layout(), dimension);
128 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129 const Strides &strides_in_bytes() const override
130 {
131 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
132 return _parent->strides_in_bytes();
133 }
134 size_t offset_first_element_in_bytes() const override
135 {
136 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
137 return _parent->offset_element_in_bytes(_coords);
138 }
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100139 int32_t offset_element_in_bytes(const Coordinates &pos) const override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140 size_t element_size() const override
141 {
142 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
143 return _parent->element_size();
144 }
145 size_t num_dimensions() const override
146 {
147 return _tensor_shape.num_dimensions();
148 }
149 size_t num_channels() const override
150 {
151 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
152 return _parent->num_channels();
153 }
154 const TensorShape &tensor_shape() const override
155 {
156 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
157 return _tensor_shape;
158 }
159 DataType data_type() const override
160 {
161 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
162 return _parent->data_type();
163 }
164 Format format() const override
165 {
166 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
167 return _parent->format();
168 }
169 size_t total_size() const override
170 {
171 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
172 return _parent->total_size();
173 }
174 PaddingSize padding() const override
175 {
176 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
177 return _parent->padding();
178 }
179 bool has_padding() const override
180 {
181 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
182 return _parent->has_padding();
183 }
184 bool is_resizable() const override
185 {
186 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
187 return _parent->is_resizable();
188 }
Georgios Pinitas49be2e32019-09-02 13:18:55 +0100189 bool is_dynamic() const override
190 {
191 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
192 return _parent->is_dynamic();
193 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000194 ITensorInfo &set_is_resizable(bool is_resizable) override
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100195 {
196 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
197 _parent->set_is_resizable(is_resizable);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000198 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100199 }
Georgios Pinitas49be2e32019-09-02 13:18:55 +0100200 ITensorInfo &set_is_dynamic(bool is_dynamic) override
201 {
202 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
203 _parent->set_is_dynamic(is_dynamic);
204 return *this;
205 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100206 ValidRegion valid_region() const override
207 {
208 return _valid_region;
209 }
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000210 void set_valid_region(const ValidRegion &valid_region) override
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100211 {
212 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100213 // Check if subtensor is valid if parent is configured
214 if(_parent->tensor_shape().total_size() != 0)
215 {
216 ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR_VALID_REGION(_parent->valid_region(), valid_region);
217 }
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000218 _valid_region = valid_region;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100219 }
Michel Iwaniec00633802017-10-12 14:14:15 +0100220 QuantizationInfo quantization_info() const override
221 {
222 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
223 return _parent->quantization_info();
224 }
Isabella Gottardid17a6772018-02-27 17:41:55 +0000225 DataLayout data_layout() const override
226 {
227 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
228 return _parent->data_layout();
229 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100230
231private:
232 ITensorInfo *_parent;
233 TensorShape _tensor_shape;
234 Coordinates _coords;
235 ValidRegion _valid_region;
Georgios Pinitas652bde52018-01-10 15:33:28 +0000236 bool _extend_parent;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100237};
Georgios Pinitas49be2e32019-09-02 13:18:55 +0100238} // namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100239#endif /*__ARM_COMPUTE_SUBTENSORINFO_H__ */