blob: 5d1ee7c57880e07b3b1ed6d9e2bf9a3d61647c3c [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 2017 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 */
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"
31#include "arm_compute/core/Strides.h"
32#include "arm_compute/core/TensorShape.h"
33#include "arm_compute/core/Types.h"
34#include "arm_compute/core/Utils.h"
35
36#include <cstddef>
37
38namespace arm_compute
39{
40class HOGInfo;
41
42/** Store the tensor's metadata */
43class TensorInfo final : public ITensorInfo
44{
45public:
46 /** Default constructor */
47 TensorInfo();
48 /** Default destructor */
49 ~TensorInfo() = default;
50 /** Allow instances of this class to be copy constructed */
51 TensorInfo(const ITensorInfo &info);
52 /** Allow instances of this class to be copy constructed */
53 TensorInfo(const TensorInfo &) = default;
54 /** Allow instances of this class to be copied */
55 TensorInfo &operator=(const TensorInfo &) = default;
56 /** Allow instances of this class to be move constructed */
57 TensorInfo(TensorInfo &&) = default;
58 /** Allow instances of this class to be moved */
59 TensorInfo &operator=(TensorInfo &&) = default;
60
61 /** Construct a tensor info with a format.
62 *
63 * Can be used for automatic derivation of the shape by the function.
64 *
65 * @param[in] format Format of the tensor.
66 */
67 TensorInfo(Format format);
68
69 /** 2D tensor constructor
70 *
71 * @param[in] width Width of the 2D tensor
72 * @param[in] height Height of the 2D tensor
73 * @param[in] format Single plane format of the tensor.
74 */
75 TensorInfo(unsigned int width, unsigned int height, Format format);
76 /** Constructor
77 *
78 * @param[in] tensor_shape It specifies the size for each dimension of the tensor in number of elements.
79 * @param[in] format Single plane format of the tensor.
80 */
81 TensorInfo(const TensorShape &tensor_shape, Format format);
82
83 /** Construct a tensor info with a data type and number of channels.
84 *
85 * Can be used for automatic derivation of the shape by the function.
86 *
87 * @param[in] num_channels It indicates the number of channels for each tensor element
88 * @param[in] data_type Data type to use for each tensor element
89 * @param[in] fixed_point_position (Optional) It specifies the fixed point position when the tensor data type is QS8, QS16 or QS32.
90 */
91 TensorInfo(size_t num_channels, DataType data_type, size_t fixed_point_position = 0);
92
93 /** Constructor
94 *
95 * @param[in] tensor_shape It specifies the size for each dimension of the tensor in number of elements.
96 * @param[in] num_channels It indicates the number of channels for each tensor element
97 * @param[in] data_type Data type to use for each tensor element
98 * @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.
99 */
100 TensorInfo(const TensorShape &tensor_shape, size_t num_channels, DataType data_type, int fixed_point_position = 0);
Michel Iwaniec00633802017-10-12 14:14:15 +0100101
102 /** Constructor
103 *
104 * @param[in] tensor_shape It specifies the size for each dimension of the tensor in number of elements.
105 * @param[in] num_channels It indicates the number of channels for each tensor element
106 * @param[in] data_type Data type to use for each tensor element
107 * @param[in] quantization_info The quantization settings for the tensor data.
108 */
109 TensorInfo(const TensorShape &tensor_shape, size_t num_channels, DataType data_type, QuantizationInfo quantization_info);
110
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111 /** Constructor
112 *
113 * @param[in] hog_info HOG's metadata used to allocate normalized HOG space
114 * @param[in] width Width of the 2D tensor where the HOG descriptor will be computed on
115 * @param[in] height Height of the 2D tensor where the HOG descriptor will be computed on
116 */
117 TensorInfo(const HOGInfo &hog_info, unsigned int width, unsigned int height);
118
119 /** Initialize the tensor info with just a format.
120 *
121 * Can be used for automatic derivation of the shape by the function.
122 *
123 * @param[in] format Single plane format of the tensor.
124 */
125 void init(Format format);
126
127 /** Initialize the metadata structure with the given parameters
128 *
129 * @param[in] tensor_shape Size for each dimension of the tensor in number of elements.
130 * @param[in] format Single plane format of the tensor.
131 */
132 void init(const TensorShape &tensor_shape, Format format);
133 /** Initialize the metadata structure with the given parameters
134 *
135 * @param[in] tensor_shape Size for each dimension of the tensor in number of elements.
136 * @param[in] format Single plane format of the tensor.
137 * @param[in] strides_in_bytes Stride in bytes for accessing each dimension of the tensor.
138 * @param[in] offset_first_element_in_bytes Offset in bytes from the beginning of memory allocation to access the first element.
139 * @param[in] total_size_in_bytes Size in bytes of the memory allocation (including the offset to the first element).
140 */
141 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);
142
143 /** Initialize the tensor info with just a format.
144 *
145 * Can be used for automatic derivation of the shape by the function.
146 *
147 * @param[in] num_channels Desired number of channels for each tensor element.
148 * @param[in] data_type Data type to use for each tensor element.
149 * @param[in] fixed_point_position (Optional) Fixed point position when the tensor data type is QS8, QS16 or QS32.
150 */
151 void init(size_t num_channels, DataType data_type, size_t fixed_point_position = 0);
152
153 /** Initialize the metadata structure with the given parameters
154 *
155 * @param[in] tensor_shape Size for each dimension of the tensor in number of elements.
156 * @param[in] num_channels Desired number of channels for each tensor element.
157 * @param[in] data_type Data type to use for each tensor element.
158 * @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.
159 */
160 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 +0100161
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100162 /** Initialize the metadata structure with the given parameters
163 *
164 * @param[in] tensor_shape Size for each dimension of the tensor in number of elements.
165 * @param[in] num_channels Desired number of channels for each tensor element.
166 * @param[in] data_type Data type to use for each tensor element.
167 * @param[in] strides_in_bytes Stride in bytes for accessing each dimension of the tensor.
168 * @param[in] offset_first_element_in_bytes Offset in bytes from the beginning of memory allocation to access the first element.
169 * @param[in] total_size_in_bytes Size in bytes of the memory allocation (including the offset to the first element).
170 * @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.
171 */
172 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,
173 size_t total_size_in_bytes, int fixed_point_position = 0);
174 /** Initialize the metadata structure for the given HOG's metadata
175 *
176 * @param[in] hog_info HOG's metadata used to allocate normalized HOG space
177 * @param[in] width Width of the 2D tensor where the HOG descriptor will be computed on
178 * @param[in] height Height of the 2D tensor where the HOG descriptor will be computed on
179 */
180 void init(const HOGInfo &hog_info, unsigned int width, unsigned int height);
181 /** Initialize the metadata structure for the given tensor shape and single-plane format, (Padding is automatically calculated)
182 *
183 * @note The padding used by this method is really conservative so that the tensor can be used for most functions.
184 *
185 * @param[in] tensor_shape It specifies the size for each dimension of the tensor in number of elements
186 * @param[in] format Single plane format of the image.
187 *
188 * @return Total allocation size including padding in bytes.
189 */
190 size_t init_auto_padding(const TensorShape &tensor_shape, Format format);
191 /** Initialize the metadata structure for the given tensor shape, number of channels,
192 * data type and fixed point position. (Padding is automatically calculated)
193 *
194 * @note The padding used by this method is really conservative so that the tensor can be used for most functions.
195 *
196 * @param[in] tensor_shape It specifies the size for each dimension of the tensor in number of elements
197 * @param[in] num_channels It indicates the number of channels for each tensor element
198 * @param[in] data_type Data type to use for each tensor element
199 * @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.
200 *
201 * @return Total allocation size including padding in bytes.
202 */
203 size_t init_auto_padding(const TensorShape &tensor_shape, size_t num_channels, DataType data_type, int fixed_point_position = 0);
204 /** Initialize the metadata structure for the given HOG's metadata
205 *
206 * @note init_auto_padding will be used for the tensor initialization.
207 *
208 * @param[in] hog_info HOG's metadata used to allocate normalized HOG space
209 * @param[in] width Width of the 2D tensor where the HOG descriptor will be computed on
210 * @param[in] height Height of the 2D tensor where the HOG descriptor will be computed on
211 */
212 size_t init_auto_padding(const HOGInfo &hog_info, unsigned int width, unsigned int height);
213
214 // Inherited methods overridden:
215 void set_data_type(DataType data_type) override;
216 void set_num_channels(int num_channels) override;
217 void set_format(Format format) override;
218 void set_tensor_shape(TensorShape shape) override;
219 void set_fixed_point_position(int fixed_point_position) override;
220 bool auto_padding() override;
221 bool extend_padding(const PaddingSize &padding) override;
222 size_t dimension(size_t index) const override
223 {
224 return _tensor_shape[index];
225 }
226 const Strides &strides_in_bytes() const override
227 {
228 return _strides_in_bytes;
229 }
230 size_t offset_first_element_in_bytes() const override
231 {
232 return _offset_first_element_in_bytes;
233 }
234 size_t offset_element_in_bytes(const Coordinates &pos) const override;
235 int fixed_point_position() const override
236 {
237 return _fixed_point_position;
238 }
239 size_t element_size() const override
240 {
241 return data_size_from_type(_data_type) * _num_channels;
242 }
243 size_t num_dimensions() const override
244 {
245 return _tensor_shape.num_dimensions();
246 }
247 size_t num_channels() const override
248 {
249 return _num_channels;
250 }
251 const TensorShape &tensor_shape() const override
252 {
253 return _tensor_shape;
254 }
255 DataType data_type() const override
256 {
257 return _data_type;
258 }
259 Format format() const override
260 {
261 return _format;
262 }
263 size_t total_size() const override
264 {
265 return _total_size;
266 }
267 PaddingSize padding() const override
268 {
269 return _padding;
270 }
271 bool has_padding() const override
272 {
273 return !_padding.empty();
274 }
275 bool is_resizable() const override
276 {
277 return _is_resizable;
278 }
279 void set_is_resizable(bool is_resizable) override
280 {
281 _is_resizable = is_resizable;
282 }
283 ValidRegion valid_region() const override
284 {
285 return _valid_region;
286 }
287 void set_valid_region(ValidRegion valid_region) override
288 {
289 _valid_region = std::move(valid_region);
290 }
Michel Iwaniec00633802017-10-12 14:14:15 +0100291 QuantizationInfo quantization_info() const override
292 {
293 return _quantization_info;
294 }
295 void set_quantization_info(QuantizationInfo quantization_info) override
296 {
297 _quantization_info = quantization_info;
298 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100299
300private:
301 /** Calculates strides, offset and total size resulting from the specified padding around the XY plane.
302 *
303 * @param[in] padding Padding around the XY plane in elements.
304 */
305 std::tuple<Strides, size_t, size_t> calculate_padding_requirements(const PaddingSize &padding);
306
Michel Iwaniec00633802017-10-12 14:14:15 +0100307 size_t _total_size;
308 int _fixed_point_position;
309 size_t _offset_first_element_in_bytes;
310 Strides _strides_in_bytes;
311 size_t _num_channels;
312 TensorShape _tensor_shape;
313 DataType _data_type;
314 Format _format;
315 bool _is_resizable;
316 ValidRegion _valid_region;
317 PaddingSize _padding;
318 QuantizationInfo _quantization_info;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100319};
320}
321#endif /*__ARM_COMPUTE_TENSORINFO_H__ */