blob: a49b7b7e02a8a8d0f6367ee36cf2b38eb239eb25 [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#include "arm_compute/core/TensorInfo.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/HOGInfo.h"
28#include "arm_compute/core/Helpers.h"
Michel Iwaniec00633802017-10-12 14:14:15 +010029#include "arm_compute/core/TensorInfo.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/Validate.h"
Georgios Pinitas283c1792017-11-10 18:14:06 +000031#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032
33using namespace arm_compute;
34
35TensorInfo::TensorInfo()
36 : _total_size(0), _fixed_point_position(0), _offset_first_element_in_bytes(0), _strides_in_bytes(), _num_channels(0), _tensor_shape(), _data_type(DataType::UNKNOWN), _format(Format::UNKNOWN),
Michel Iwaniec00633802017-10-12 14:14:15 +010037 _is_resizable{ true }, _valid_region{ Coordinates(), _tensor_shape }, _padding{ 0 }, _quantization_info()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038{
39}
40
41TensorInfo::TensorInfo(const ITensorInfo &info)
42 : TensorInfo()
43{
44 _total_size = info.total_size();
45 _fixed_point_position = info.fixed_point_position();
46 _offset_first_element_in_bytes = info.offset_first_element_in_bytes();
47 _strides_in_bytes = info.strides_in_bytes();
48 _num_channels = info.num_channels();
49 _tensor_shape = info.tensor_shape();
50 _data_type = info.data_type();
51 _format = info.format();
52 _is_resizable = info.is_resizable();
53 _valid_region = info.valid_region();
54 _padding = info.padding();
55}
56
57TensorInfo::TensorInfo(Format format)
58 : TensorInfo(TensorShape(), format)
59{
60}
61
62TensorInfo::TensorInfo(unsigned int width, unsigned int height, Format format)
63 : TensorInfo(TensorShape(width, height), format)
64{
65}
66
67TensorInfo::TensorInfo(const TensorShape &tensor_shape, Format format)
68 : TensorInfo()
69{
70 init(tensor_shape, format);
71}
72
73TensorInfo::TensorInfo(size_t num_channels, DataType data_type, size_t fixed_point_position)
74 : TensorInfo()
75{
76 init(TensorShape(), num_channels, data_type, fixed_point_position);
77}
78
79TensorInfo::TensorInfo(const TensorShape &tensor_shape, size_t num_channels, DataType data_type, int fixed_point_position)
80 : TensorInfo()
81{
82 init(tensor_shape, num_channels, data_type, fixed_point_position);
83}
84
Michel Iwaniec00633802017-10-12 14:14:15 +010085TensorInfo::TensorInfo(const TensorShape &tensor_shape, size_t num_channels, DataType data_type, QuantizationInfo quantization_info)
86 : TensorInfo()
87{
88 init(tensor_shape, num_channels, data_type, 0);
89 _quantization_info = quantization_info;
90}
91
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092TensorInfo::TensorInfo(const HOGInfo &hog_info, unsigned int width, unsigned int height)
93 : TensorInfo()
94{
95 init(hog_info, width, height);
96}
97
98void TensorInfo::init(Format format)
99{
100 init(TensorShape(), format);
101}
102
103void TensorInfo::init(const TensorShape &tensor_shape, Format format)
104{
105 size_t num_channels = num_channels_from_format(format);
106 const DataType type = data_type_from_format(format);
107
108 init(tensor_shape, num_channels, type);
109
110 _format = format;
111}
112
113void TensorInfo::init(const TensorShape &tensor_shape, Format format,
114 const Strides &strides_in_bytes, size_t offset_first_element_in_bytes,
115 size_t total_size_in_bytes)
116{
117 size_t num_channels = num_channels_from_format(format);
118 const DataType type = data_type_from_format(format);
119
120 init(tensor_shape, num_channels, type, strides_in_bytes, offset_first_element_in_bytes, total_size_in_bytes);
121
122 _format = format;
123}
124
125void TensorInfo::init(size_t num_channels, DataType data_type, size_t fixed_point_position)
126{
127 init(TensorShape(), num_channels, data_type, fixed_point_position);
128}
129
130void TensorInfo::init(const TensorShape &tensor_shape, size_t num_channels, DataType data_type, int fixed_point_position)
131{
132 ARM_COMPUTE_ERROR_ON(num_channels == 0);
133 ARM_COMPUTE_ERROR_ON(data_type == DataType::QS8 && (fixed_point_position < 1 || fixed_point_position > 6));
134 ARM_COMPUTE_ERROR_ON(data_type == DataType::QS16 && (fixed_point_position < 1 || fixed_point_position > 14));
135
136 _fixed_point_position = fixed_point_position;
137 _data_type = data_type;
138 _num_channels = num_channels;
139 _format = Format::UNKNOWN;
140
141 set_tensor_shape(tensor_shape);
142}
143
144void TensorInfo::init(const TensorShape &tensor_shape, size_t num_channels, DataType data_type,
145 const Strides &strides_in_bytes, size_t offset_first_element_in_bytes,
146 size_t total_size_in_bytes, int fixed_point_position)
147{
148 ARM_COMPUTE_ERROR_ON(num_channels == 0);
149 ARM_COMPUTE_ERROR_ON(data_type == DataType::QS8 && (fixed_point_position < 1 || fixed_point_position > 6));
150 ARM_COMPUTE_ERROR_ON(data_type == DataType::QS16 && (fixed_point_position < 1 || fixed_point_position > 14));
151
152 _fixed_point_position = fixed_point_position;
153 _data_type = data_type;
154 _num_channels = num_channels;
155 _format = Format::UNKNOWN;
156 _tensor_shape = tensor_shape;
157 _offset_first_element_in_bytes = offset_first_element_in_bytes;
158 _strides_in_bytes = strides_in_bytes;
159 _total_size = total_size_in_bytes;
160
161 Coordinates coordinates;
162 coordinates.set_num_dimensions(_tensor_shape.num_dimensions());
163 _valid_region = ValidRegion{ coordinates, _tensor_shape };
164}
165
166void TensorInfo::init(const HOGInfo &hog_info, unsigned int width, unsigned int height)
167{
168 // Number of cells for each block
169 const Size2D num_cells_per_block = hog_info.num_cells_per_block();
170
171 // Tensor Size = (Number of horizontal blocks) * (Number of vertical blocks )
172 const Size2D num_blocks_per_img = hog_info.num_blocks_per_image(Size2D(width, height));
173
174 // Number of tensor channels = (Number of cells per block) * (Number of bins per cell)
175 const size_t num_channels = num_cells_per_block.area() * hog_info.num_bins();
176
177 init(TensorShape(num_blocks_per_img.width, num_blocks_per_img.height), num_channels, DataType::F32);
178}
179
180size_t TensorInfo::init_auto_padding(const TensorShape &tensor_shape, Format format)
181{
182 const size_t num_channels = num_channels_from_format(format);
183 const DataType type = data_type_from_format(format);
184 size_t total_size = init_auto_padding(tensor_shape, num_channels, type);
185
186 _format = format;
187
188 return total_size;
189}
190
191size_t TensorInfo::init_auto_padding(const TensorShape &tensor_shape, size_t num_channels, DataType data_type, int fixed_point_position)
192{
193 ARM_COMPUTE_ERROR_ON(num_channels == 0);
194 ARM_COMPUTE_ERROR_ON(data_type == DataType::QS8 && (fixed_point_position < 1 || fixed_point_position > 6));
195 ARM_COMPUTE_ERROR_ON(data_type == DataType::QS16 && (fixed_point_position < 1 || fixed_point_position > 14));
196
197 _fixed_point_position = fixed_point_position;
198 _data_type = data_type;
199 _num_channels = num_channels;
200 _format = Format::UNKNOWN;
201 _tensor_shape = tensor_shape;
202
203 Coordinates coordinates;
204 coordinates.set_num_dimensions(_tensor_shape.num_dimensions());
205 _valid_region = ValidRegion{ coordinates, _tensor_shape };
206
207 auto_padding();
208
209 return _total_size;
210}
211
212size_t TensorInfo::init_auto_padding(const HOGInfo &hog_info, unsigned int width, unsigned int height)
213{
214 // Number of cells for each block
215 const Size2D num_cells_per_block = hog_info.num_cells_per_block();
216
217 // Tensor Size = (Number of horizontal blocks) * (Number of vertical blocks )
218 const Size2D num_blocks_per_img = hog_info.num_blocks_per_image(Size2D(width, height));
219
220 // Number of tensor channels = (Number of cells per block) * (Number of bins per cell)
221 const size_t num_channels = num_cells_per_block.area() * hog_info.num_bins();
222
223 return init_auto_padding(TensorShape(num_blocks_per_img.width, num_blocks_per_img.height), num_channels, DataType::F32);
224}
225
226bool TensorInfo::auto_padding()
227{
228 ARM_COMPUTE_ERROR_ON(!_is_resizable);
229
230 // Some kernels compute 32 elements at the time, worst case scenario they
231 // will read 32 values after the last element
232 const size_t extra_pad_x = _tensor_shape.num_dimensions() < 1 ? 0 : 32;
233 const size_t pad_x = _tensor_shape.num_dimensions() < 1 ? 0 : 4;
234 const size_t pad_y = _tensor_shape.num_dimensions() < 2 ? 0 : 4;
235
236 return extend_padding(PaddingSize(pad_y, pad_x + extra_pad_x, pad_y, pad_x));
237}
238
239std::tuple<Strides, size_t, size_t> TensorInfo::calculate_padding_requirements(const PaddingSize &padding)
240{
241 // Calculate resulting stride for the X, Y and Z dimension
242 const size_t stride_x = element_size();
243 const size_t stride_y = (padding.left + _tensor_shape[0] + padding.right) * stride_x;
244 const size_t stride_z = (padding.top + _tensor_shape[1] + padding.bottom) * stride_y;
245
246 Strides required_strides;
247 size_t required_total_size = 0;
248 const size_t required_offset_first_element = padding.left * stride_x + padding.top * stride_y;
249
250 switch(_tensor_shape.num_dimensions())
251 {
252 case 0:
253 {
254 if(_tensor_shape.total_size() > 0)
255 {
Gian Marco Iodiceee94f6a2017-09-11 17:38:02 +0100256 required_strides = Strides(stride_x, stride_x);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100257 required_total_size = stride_z;
258 }
259 break;
260 }
261 case 1:
Gian Marco Iodiceee94f6a2017-09-11 17:38:02 +0100262 required_strides = compute_strides(*this, stride_x, stride_y);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100263 required_total_size = stride_z;
264 break;
265 case 2:
266 required_strides = compute_strides(*this, stride_x, stride_y);
267 required_total_size = stride_z;
268 break;
269 default:
270 {
271 required_strides = compute_strides(*this, stride_x, stride_y, stride_z);
272
273 const unsigned int idx_last_dimension = _tensor_shape.num_dimensions() - 1;
274
275 required_total_size = _tensor_shape[idx_last_dimension] * required_strides[idx_last_dimension];
276 break;
277 }
278 }
279
280 return std::make_tuple(required_strides, required_offset_first_element, required_total_size);
281}
282
283bool TensorInfo::extend_padding(const PaddingSize &padding)
284{
285 ARM_COMPUTE_ERROR_ON(!_is_resizable);
286
287 bool updated = false;
288
289 if(padding.top > _padding.top)
290 {
291 _padding.top = padding.top;
292 updated = true;
293 }
294
295 if(padding.right > _padding.right)
296 {
297 _padding.right = padding.right;
298 updated = true;
299 }
300
301 if(padding.bottom > _padding.bottom)
302 {
303 _padding.bottom = padding.bottom;
304 updated = true;
305 }
306
307 if(padding.left > _padding.left)
308 {
309 _padding.left = padding.left;
310 updated = true;
311 }
312
313 std::tie(_strides_in_bytes, _offset_first_element_in_bytes, _total_size) = calculate_padding_requirements(_padding);
314
315 return updated;
316}
317
Georgios Pinitas283c1792017-11-10 18:14:06 +0000318std::unique_ptr<ITensorInfo> TensorInfo::clone() const
319{
320 return support::cpp14::make_unique<TensorInfo>(*this);
321}
322
323ITensorInfo &TensorInfo::set_data_type(DataType data_type)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100324{
325 _data_type = data_type;
326 _format = Format::UNKNOWN;
Georgios Pinitas283c1792017-11-10 18:14:06 +0000327 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100328}
329
Georgios Pinitas283c1792017-11-10 18:14:06 +0000330ITensorInfo &TensorInfo::set_num_channels(int num_channels)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100331{
332 _num_channels = num_channels;
333 _format = Format::UNKNOWN;
Georgios Pinitas283c1792017-11-10 18:14:06 +0000334 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100335}
336
Georgios Pinitas283c1792017-11-10 18:14:06 +0000337ITensorInfo &TensorInfo::set_format(Format format)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100338{
339 _format = format;
340
341 if(_data_type == DataType::UNKNOWN)
342 {
343 _num_channels = num_channels_from_format(format);
344 _data_type = data_type_from_format(format);
345 }
346 else
347 {
348 ARM_COMPUTE_ERROR_ON(num_channels_from_format(format) != _num_channels);
349 ARM_COMPUTE_ERROR_ON(data_type_from_format(format) != _data_type);
350 }
Georgios Pinitas283c1792017-11-10 18:14:06 +0000351 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100352}
353
Georgios Pinitas283c1792017-11-10 18:14:06 +0000354ITensorInfo &TensorInfo::set_tensor_shape(TensorShape shape)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100355{
356 _tensor_shape = shape;
357 _offset_first_element_in_bytes = 0;
358 _strides_in_bytes = compute_strides(*this);
359
360 if(_tensor_shape.num_dimensions() == 0)
361 {
362 _total_size = _strides_in_bytes[0];
363 }
364 else
365 {
366 const unsigned int idx_last_dimension = _tensor_shape.num_dimensions() - 1;
367 _total_size = _tensor_shape[idx_last_dimension] * _strides_in_bytes[idx_last_dimension];
368 }
369
370 Coordinates coordinates;
371 coordinates.set_num_dimensions(_tensor_shape.num_dimensions());
372 _valid_region = ValidRegion{ coordinates, _tensor_shape };
Georgios Pinitas283c1792017-11-10 18:14:06 +0000373 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100374}
375
Georgios Pinitas283c1792017-11-10 18:14:06 +0000376ITensorInfo &TensorInfo::set_fixed_point_position(int fixed_point_position)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100377{
378 ARM_COMPUTE_ERROR_ON(_data_type == DataType::QS8 && (fixed_point_position < 1 || fixed_point_position > 6));
379 ARM_COMPUTE_ERROR_ON(_data_type == DataType::QS16 && (fixed_point_position < 1 || fixed_point_position > 14));
380 _fixed_point_position = fixed_point_position;
Georgios Pinitas283c1792017-11-10 18:14:06 +0000381 return *this;
382}
383
384ITensorInfo &TensorInfo::set_quantization_info(QuantizationInfo quantization_info)
385{
386 _quantization_info = quantization_info;
387 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100388}
389
390size_t TensorInfo::offset_element_in_bytes(const Coordinates &pos) const
391{
392 ARM_COMPUTE_ERROR_ON_COORDINATES_DIMENSIONS_GTE(pos, _tensor_shape.num_dimensions());
393
394 size_t offset = _offset_first_element_in_bytes;
395
396 for(size_t i = 0; i < _tensor_shape.num_dimensions(); ++i)
397 {
398 offset += pos[i] * _strides_in_bytes[i];
399 }
400
401 return offset;
402}