blob: 1eaf052d8efb5182f3f55d76226c2f4ecaf7f62d [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 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010089 * @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
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091 */
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010092 TensorInfo(size_t num_channels, DataType data_type);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093
94 /** Constructor
95 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010096 * @param[in] tensor_shape It specifies the size for each dimension of the tensor in number of elements.
97 * @param[in] num_channels It indicates the number of channels for each tensor element
98 * @param[in] data_type Data type to use for each tensor element
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099 */
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100100 TensorInfo(const TensorShape &tensor_shape, size_t num_channels, DataType data_type);
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 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100147 * @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.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149 */
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100150 void init(size_t num_channels, DataType data_type);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151
152 /** Initialize the metadata structure with the given parameters
153 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100154 * @param[in] tensor_shape Size for each dimension of the tensor in number of elements.
155 * @param[in] num_channels Desired number of channels for each tensor element.
156 * @param[in] data_type Data type to use for each tensor element.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157 */
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100158 void init(const TensorShape &tensor_shape, size_t num_channels, DataType data_type);
Michel Iwaniec00633802017-10-12 14:14:15 +0100159
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160 /** Initialize the metadata structure with the given parameters
161 *
162 * @param[in] tensor_shape Size for each dimension of the tensor in number of elements.
163 * @param[in] num_channels Desired number of channels for each tensor element.
164 * @param[in] data_type Data type to use for each tensor element.
165 * @param[in] strides_in_bytes Stride in bytes for accessing each dimension of the tensor.
166 * @param[in] offset_first_element_in_bytes Offset in bytes from the beginning of memory allocation to access the first element.
167 * @param[in] total_size_in_bytes Size in bytes of the memory allocation (including the offset to the first element).
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100168 */
169 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,
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100170 size_t total_size_in_bytes);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100171 /** Initialize the metadata structure for the given HOG's metadata
172 *
173 * @param[in] hog_info HOG's metadata used to allocate normalized HOG space
174 * @param[in] width Width of the 2D tensor where the HOG descriptor will be computed on
175 * @param[in] height Height of the 2D tensor where the HOG descriptor will be computed on
176 */
177 void init(const HOGInfo &hog_info, unsigned int width, unsigned int height);
178 /** Initialize the metadata structure for the given tensor shape and single-plane format, (Padding is automatically calculated)
179 *
180 * @note The padding used by this method is really conservative so that the tensor can be used for most functions.
181 *
182 * @param[in] tensor_shape It specifies the size for each dimension of the tensor in number of elements
183 * @param[in] format Single plane format of the image.
184 *
185 * @return Total allocation size including padding in bytes.
186 */
187 size_t init_auto_padding(const TensorShape &tensor_shape, Format format);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100188 /** Initialize the metadata structure for the given tensor shape, number of channels and
189 * data type. (Padding is automatically calculated)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100190 *
191 * @note The padding used by this method is really conservative so that the tensor can be used for most functions.
192 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100193 * @param[in] tensor_shape It specifies the size for each dimension of the tensor in number of elements
194 * @param[in] num_channels It indicates the number of channels for each tensor element
195 * @param[in] data_type Data type to use for each tensor element
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100196 *
197 * @return Total allocation size including padding in bytes.
198 */
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100199 size_t init_auto_padding(const TensorShape &tensor_shape, size_t num_channels, DataType data_type);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100200 /** Initialize the metadata structure for the given HOG's metadata
201 *
202 * @note init_auto_padding will be used for the tensor initialization.
203 *
204 * @param[in] hog_info HOG's metadata used to allocate normalized HOG space
205 * @param[in] width Width of the 2D tensor where the HOG descriptor will be computed on
206 * @param[in] height Height of the 2D tensor where the HOG descriptor will be computed on
Alex Gildayc357c472018-03-21 13:54:09 +0000207 *
208 * @return Total allocation size including padding in bytes.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209 */
210 size_t init_auto_padding(const HOGInfo &hog_info, unsigned int width, unsigned int height);
211
212 // Inherited methods overridden:
Georgios Pinitas283c1792017-11-10 18:14:06 +0000213 std::unique_ptr<ITensorInfo> clone() const override;
214 ITensorInfo &set_data_type(DataType data_type) override;
215 ITensorInfo &set_num_channels(int num_channels) override;
216 ITensorInfo &set_format(Format format) override;
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000217 ITensorInfo &set_tensor_shape(const TensorShape &shape) override;
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000218 ITensorInfo &set_quantization_info(const QuantizationInfo &quantization_info) override;
Isabella Gottardid17a6772018-02-27 17:41:55 +0000219 ITensorInfo &set_data_layout(const DataLayout &data_layout) override;
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000220 ITensorInfo &reset_padding() override;
221 bool auto_padding() override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100222 bool extend_padding(const PaddingSize &padding) override;
223 size_t dimension(size_t index) const override
224 {
225 return _tensor_shape[index];
226 }
Isabella Gottardid56e7702018-02-28 14:29:36 +0000227 size_t dimension(DataLayoutDimension dimension) const override
228 {
229 return get_data_layout_dimension_index(_data_layout, dimension);
230 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100231 const Strides &strides_in_bytes() const override
232 {
233 return _strides_in_bytes;
234 }
235 size_t offset_first_element_in_bytes() const override
236 {
237 return _offset_first_element_in_bytes;
238 }
239 size_t offset_element_in_bytes(const Coordinates &pos) const override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100240 size_t element_size() const override
241 {
242 return data_size_from_type(_data_type) * _num_channels;
243 }
244 size_t num_dimensions() const override
245 {
246 return _tensor_shape.num_dimensions();
247 }
248 size_t num_channels() const override
249 {
250 return _num_channels;
251 }
252 const TensorShape &tensor_shape() const override
253 {
254 return _tensor_shape;
255 }
256 DataType data_type() const override
257 {
258 return _data_type;
259 }
260 Format format() const override
261 {
262 return _format;
263 }
264 size_t total_size() const override
265 {
266 return _total_size;
267 }
268 PaddingSize padding() const override
269 {
270 return _padding;
271 }
272 bool has_padding() const override
273 {
274 return !_padding.empty();
275 }
276 bool is_resizable() const override
277 {
278 return _is_resizable;
279 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000280 ITensorInfo &set_is_resizable(bool is_resizable) override
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100281 {
282 _is_resizable = is_resizable;
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000283 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100284 }
285 ValidRegion valid_region() const override
286 {
287 return _valid_region;
288 }
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000289 void set_valid_region(const ValidRegion &valid_region) override
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100290 {
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000291 _valid_region = valid_region;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100292 }
Michel Iwaniec00633802017-10-12 14:14:15 +0100293 QuantizationInfo quantization_info() const override
294 {
295 return _quantization_info;
296 }
Isabella Gottardid17a6772018-02-27 17:41:55 +0000297 DataLayout data_layout() const override
298 {
299 return _data_layout;
300 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100301
302private:
303 /** Calculates strides, offset and total size resulting from the specified padding around the XY plane.
304 *
305 * @param[in] padding Padding around the XY plane in elements.
306 */
307 std::tuple<Strides, size_t, size_t> calculate_padding_requirements(const PaddingSize &padding);
308
Michel Iwaniec00633802017-10-12 14:14:15 +0100309 size_t _total_size;
Michel Iwaniec00633802017-10-12 14:14:15 +0100310 size_t _offset_first_element_in_bytes;
311 Strides _strides_in_bytes;
312 size_t _num_channels;
313 TensorShape _tensor_shape;
314 DataType _data_type;
315 Format _format;
316 bool _is_resizable;
317 ValidRegion _valid_region;
318 PaddingSize _padding;
319 QuantizationInfo _quantization_info;
Isabella Gottardid17a6772018-02-27 17:41:55 +0000320 DataLayout _data_layout;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100321};
322}
323#endif /*__ARM_COMPUTE_TENSORINFO_H__ */