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