blob: f8cfb35357d21d05f867d5d3f5896d37181b08df [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +00002 * Copyright (c) 2016-2018 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_TENSORINFO_H__
25#define __ARM_COMPUTE_TENSORINFO_H__
26
27#include "arm_compute/core/ITensorInfo.h"
28
Michel Iwaniec00633802017-10-12 14:14:15 +010029#include "ITensorInfo.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/Coordinates.h"
Isabella Gottardid56e7702018-02-28 14:29:36 +000031#include "arm_compute/core/Helpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include "arm_compute/core/Strides.h"
33#include "arm_compute/core/TensorShape.h"
34#include "arm_compute/core/Types.h"
35#include "arm_compute/core/Utils.h"
36
37#include <cstddef>
Georgios Pinitas283c1792017-11-10 18:14:06 +000038#include <memory>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039
40namespace arm_compute
41{
42class HOGInfo;
43
44/** Store the tensor's metadata */
45class TensorInfo final : public ITensorInfo
46{
47public:
48 /** Default constructor */
49 TensorInfo();
50 /** Default destructor */
51 ~TensorInfo() = default;
52 /** Allow instances of this class to be copy constructed */
53 TensorInfo(const ITensorInfo &info);
54 /** Allow instances of this class to be copy constructed */
55 TensorInfo(const TensorInfo &) = default;
56 /** Allow instances of this class to be copied */
57 TensorInfo &operator=(const TensorInfo &) = default;
58 /** Allow instances of this class to be move constructed */
59 TensorInfo(TensorInfo &&) = default;
60 /** Allow instances of this class to be moved */
61 TensorInfo &operator=(TensorInfo &&) = default;
62
63 /** Construct a tensor info with a format.
64 *
65 * Can be used for automatic derivation of the shape by the function.
66 *
67 * @param[in] format Format of the tensor.
68 */
69 TensorInfo(Format format);
70
71 /** 2D tensor constructor
72 *
73 * @param[in] width Width of the 2D tensor
74 * @param[in] height Height of the 2D tensor
75 * @param[in] format Single plane format of the tensor.
76 */
77 TensorInfo(unsigned int width, unsigned int height, Format format);
78 /** Constructor
79 *
80 * @param[in] tensor_shape It specifies the size for each dimension of the tensor in number of elements.
81 * @param[in] format Single plane format of the tensor.
82 */
83 TensorInfo(const TensorShape &tensor_shape, Format format);
84
85 /** Construct a tensor info with a data type and number of channels.
86 *
87 * Can be used for automatic derivation of the shape by the function.
88 *
89 * @param[in] num_channels It indicates the number of channels for each tensor element
90 * @param[in] data_type Data type to use for each tensor element
91 * @param[in] fixed_point_position (Optional) It specifies the fixed point position when the tensor data type is QS8, QS16 or QS32.
92 */
93 TensorInfo(size_t num_channels, DataType data_type, size_t fixed_point_position = 0);
94
95 /** Constructor
96 *
97 * @param[in] tensor_shape It specifies the size for each dimension of the tensor in number of elements.
98 * @param[in] num_channels It indicates the number of channels for each tensor element
99 * @param[in] data_type Data type to use for each tensor element
100 * @param[in] fixed_point_position (Optional) Fixed point position that expresses the number of bits for the fractional part of the number when the tensor's data type is QS8 or QS16.
101 */
102 TensorInfo(const TensorShape &tensor_shape, size_t num_channels, DataType data_type, int fixed_point_position = 0);
Michel Iwaniec00633802017-10-12 14:14:15 +0100103
104 /** Constructor
105 *
106 * @param[in] tensor_shape It specifies the size for each dimension of the tensor in number of elements.
107 * @param[in] num_channels It indicates the number of channels for each tensor element
108 * @param[in] data_type Data type to use for each tensor element
109 * @param[in] quantization_info The quantization settings for the tensor data.
110 */
111 TensorInfo(const TensorShape &tensor_shape, size_t num_channels, DataType data_type, QuantizationInfo quantization_info);
112
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113 /** Constructor
114 *
115 * @param[in] hog_info HOG's metadata used to allocate normalized HOG space
116 * @param[in] width Width of the 2D tensor where the HOG descriptor will be computed on
117 * @param[in] height Height of the 2D tensor where the HOG descriptor will be computed on
118 */
119 TensorInfo(const HOGInfo &hog_info, unsigned int width, unsigned int height);
120
121 /** Initialize the tensor info with just a format.
122 *
123 * Can be used for automatic derivation of the shape by the function.
124 *
125 * @param[in] format Single plane format of the tensor.
126 */
127 void init(Format format);
128
129 /** Initialize the metadata structure with the given parameters
130 *
131 * @param[in] tensor_shape Size for each dimension of the tensor in number of elements.
132 * @param[in] format Single plane format of the tensor.
133 */
134 void init(const TensorShape &tensor_shape, Format format);
135 /** Initialize the metadata structure with the given parameters
136 *
137 * @param[in] tensor_shape Size for each dimension of the tensor in number of elements.
138 * @param[in] format Single plane format of the tensor.
139 * @param[in] strides_in_bytes Stride in bytes for accessing each dimension of the tensor.
140 * @param[in] offset_first_element_in_bytes Offset in bytes from the beginning of memory allocation to access the first element.
141 * @param[in] total_size_in_bytes Size in bytes of the memory allocation (including the offset to the first element).
142 */
143 void init(const TensorShape &tensor_shape, Format format, const Strides &strides_in_bytes, size_t offset_first_element_in_bytes, size_t total_size_in_bytes);
144
145 /** Initialize the tensor info with just a format.
146 *
147 * Can be used for automatic derivation of the shape by the function.
148 *
149 * @param[in] num_channels Desired number of channels for each tensor element.
150 * @param[in] data_type Data type to use for each tensor element.
151 * @param[in] fixed_point_position (Optional) Fixed point position when the tensor data type is QS8, QS16 or QS32.
152 */
153 void init(size_t num_channels, DataType data_type, size_t fixed_point_position = 0);
154
155 /** Initialize the metadata structure with the given parameters
156 *
157 * @param[in] tensor_shape Size for each dimension of the tensor in number of elements.
158 * @param[in] num_channels Desired number of channels for each tensor element.
159 * @param[in] data_type Data type to use for each tensor element.
160 * @param[in] fixed_point_position (Optional) Fixed point position that expresses the number of bits for the fractional part of the number when the tensor's data type is QS8 or QS16.
161 */
162 void init(const TensorShape &tensor_shape, size_t num_channels, DataType data_type, int fixed_point_position = 0);
Michel Iwaniec00633802017-10-12 14:14:15 +0100163
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100164 /** Initialize the metadata structure with the given parameters
165 *
166 * @param[in] tensor_shape Size for each dimension of the tensor in number of elements.
167 * @param[in] num_channels Desired number of channels for each tensor element.
168 * @param[in] data_type Data type to use for each tensor element.
169 * @param[in] strides_in_bytes Stride in bytes for accessing each dimension of the tensor.
170 * @param[in] offset_first_element_in_bytes Offset in bytes from the beginning of memory allocation to access the first element.
171 * @param[in] total_size_in_bytes Size in bytes of the memory allocation (including the offset to the first element).
172 * @param[in] fixed_point_position (Optional) Fixed point position that expresses the number of bits for the fractional part of the number when the tensor's data type is QS8 or QS16.
173 */
174 void init(const TensorShape &tensor_shape, size_t num_channels, DataType data_type, const Strides &strides_in_bytes, size_t offset_first_element_in_bytes,
175 size_t total_size_in_bytes, int fixed_point_position = 0);
176 /** Initialize the metadata structure for the given HOG's metadata
177 *
178 * @param[in] hog_info HOG's metadata used to allocate normalized HOG space
179 * @param[in] width Width of the 2D tensor where the HOG descriptor will be computed on
180 * @param[in] height Height of the 2D tensor where the HOG descriptor will be computed on
181 */
182 void init(const HOGInfo &hog_info, unsigned int width, unsigned int height);
183 /** Initialize the metadata structure for the given tensor shape and single-plane format, (Padding is automatically calculated)
184 *
185 * @note The padding used by this method is really conservative so that the tensor can be used for most functions.
186 *
187 * @param[in] tensor_shape It specifies the size for each dimension of the tensor in number of elements
188 * @param[in] format Single plane format of the image.
189 *
190 * @return Total allocation size including padding in bytes.
191 */
192 size_t init_auto_padding(const TensorShape &tensor_shape, Format format);
193 /** Initialize the metadata structure for the given tensor shape, number of channels,
194 * data type and fixed point position. (Padding is automatically calculated)
195 *
196 * @note The padding used by this method is really conservative so that the tensor can be used for most functions.
197 *
198 * @param[in] tensor_shape It specifies the size for each dimension of the tensor in number of elements
199 * @param[in] num_channels It indicates the number of channels for each tensor element
200 * @param[in] data_type Data type to use for each tensor element
201 * @param[in] fixed_point_position (Optional) Fixed point position that expresses the number of bits for the fractional part of the number when the tensor's data type is QS8 or QS16.
202 *
203 * @return Total allocation size including padding in bytes.
204 */
205 size_t init_auto_padding(const TensorShape &tensor_shape, size_t num_channels, DataType data_type, int fixed_point_position = 0);
206 /** Initialize the metadata structure for the given HOG's metadata
207 *
208 * @note init_auto_padding will be used for the tensor initialization.
209 *
210 * @param[in] hog_info HOG's metadata used to allocate normalized HOG space
211 * @param[in] width Width of the 2D tensor where the HOG descriptor will be computed on
212 * @param[in] height Height of the 2D tensor where the HOG descriptor will be computed on
Alex Gildayc357c472018-03-21 13:54:09 +0000213 *
214 * @return Total allocation size including padding in bytes.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100215 */
216 size_t init_auto_padding(const HOGInfo &hog_info, unsigned int width, unsigned int height);
217
218 // Inherited methods overridden:
Georgios Pinitas283c1792017-11-10 18:14:06 +0000219 std::unique_ptr<ITensorInfo> clone() const override;
220 ITensorInfo &set_data_type(DataType data_type) override;
221 ITensorInfo &set_num_channels(int num_channels) override;
222 ITensorInfo &set_format(Format format) override;
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000223 ITensorInfo &set_tensor_shape(const TensorShape &shape) override;
Georgios Pinitas283c1792017-11-10 18:14:06 +0000224 ITensorInfo &set_fixed_point_position(int fixed_point_position) override;
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000225 ITensorInfo &set_quantization_info(const QuantizationInfo &quantization_info) override;
Isabella Gottardid17a6772018-02-27 17:41:55 +0000226 ITensorInfo &set_data_layout(const DataLayout &data_layout) override;
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000227 ITensorInfo &reset_padding() override;
228 bool auto_padding() override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100229 bool extend_padding(const PaddingSize &padding) override;
230 size_t dimension(size_t index) const override
231 {
232 return _tensor_shape[index];
233 }
Isabella Gottardid56e7702018-02-28 14:29:36 +0000234 size_t dimension(DataLayoutDimension dimension) const override
235 {
236 return get_data_layout_dimension_index(_data_layout, dimension);
237 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100238 const Strides &strides_in_bytes() const override
239 {
240 return _strides_in_bytes;
241 }
242 size_t offset_first_element_in_bytes() const override
243 {
244 return _offset_first_element_in_bytes;
245 }
246 size_t offset_element_in_bytes(const Coordinates &pos) const override;
247 int fixed_point_position() const override
248 {
249 return _fixed_point_position;
250 }
251 size_t element_size() const override
252 {
253 return data_size_from_type(_data_type) * _num_channels;
254 }
255 size_t num_dimensions() const override
256 {
257 return _tensor_shape.num_dimensions();
258 }
259 size_t num_channels() const override
260 {
261 return _num_channels;
262 }
263 const TensorShape &tensor_shape() const override
264 {
265 return _tensor_shape;
266 }
267 DataType data_type() const override
268 {
269 return _data_type;
270 }
271 Format format() const override
272 {
273 return _format;
274 }
275 size_t total_size() const override
276 {
277 return _total_size;
278 }
279 PaddingSize padding() const override
280 {
281 return _padding;
282 }
283 bool has_padding() const override
284 {
285 return !_padding.empty();
286 }
287 bool is_resizable() const override
288 {
289 return _is_resizable;
290 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000291 ITensorInfo &set_is_resizable(bool is_resizable) override
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100292 {
293 _is_resizable = is_resizable;
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000294 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100295 }
296 ValidRegion valid_region() const override
297 {
298 return _valid_region;
299 }
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000300 void set_valid_region(const ValidRegion &valid_region) override
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100301 {
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000302 _valid_region = valid_region;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100303 }
Michel Iwaniec00633802017-10-12 14:14:15 +0100304 QuantizationInfo quantization_info() const override
305 {
306 return _quantization_info;
307 }
Isabella Gottardid17a6772018-02-27 17:41:55 +0000308 DataLayout data_layout() const override
309 {
310 return _data_layout;
311 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100312
313private:
314 /** Calculates strides, offset and total size resulting from the specified padding around the XY plane.
315 *
316 * @param[in] padding Padding around the XY plane in elements.
317 */
318 std::tuple<Strides, size_t, size_t> calculate_padding_requirements(const PaddingSize &padding);
319
Michel Iwaniec00633802017-10-12 14:14:15 +0100320 size_t _total_size;
321 int _fixed_point_position;
322 size_t _offset_first_element_in_bytes;
323 Strides _strides_in_bytes;
324 size_t _num_channels;
325 TensorShape _tensor_shape;
326 DataType _data_type;
327 Format _format;
328 bool _is_resizable;
329 ValidRegion _valid_region;
330 PaddingSize _padding;
331 QuantizationInfo _quantization_info;
Isabella Gottardid17a6772018-02-27 17:41:55 +0000332 DataLayout _data_layout;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100333};
334}
335#endif /*__ARM_COMPUTE_TENSORINFO_H__ */