blob: bedfe147b0cd375fe58c48090626ff9b28c2df1f [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitasb14a0f02021-01-08 03:14:31 +00002 * Copyright (c) 2016-2021 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#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"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010031#include "src/core/helpers/Utils.h"
Georgios Pinitas40f51a62020-11-21 03:04:18 +000032
33#include <memory>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
35using namespace arm_compute;
36
37TensorInfo::TensorInfo()
Georgios Pinitasb14a0f02021-01-08 03:14:31 +000038 : _total_size(0), _offset_first_element_in_bytes(0), _strides_in_bytes(), _num_channels(0), _tensor_shape(), _dims_state(), _data_type(DataType::UNKNOWN), _format(Format::UNKNOWN), _is_resizable{ true },
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010039 _valid_region{ Coordinates(), _tensor_shape }, _padding{ 0 }, _quantization_info(), _data_layout(DataLayout::NCHW)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040{
41}
42
43TensorInfo::TensorInfo(const ITensorInfo &info)
44 : TensorInfo()
45{
46 _total_size = info.total_size();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047 _offset_first_element_in_bytes = info.offset_first_element_in_bytes();
48 _strides_in_bytes = info.strides_in_bytes();
49 _num_channels = info.num_channels();
50 _tensor_shape = info.tensor_shape();
Georgios Pinitasb14a0f02021-01-08 03:14:31 +000051 _dims_state = info.tensor_dims_state();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052 _data_type = info.data_type();
53 _format = info.format();
54 _is_resizable = info.is_resizable();
55 _valid_region = info.valid_region();
56 _padding = info.padding();
Georgios Pinitas30902ed2017-11-14 15:32:57 +000057 _quantization_info = info.quantization_info();
Isabella Gottardid17a6772018-02-27 17:41:55 +000058 _data_layout = info.data_layout();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059}
60
61TensorInfo::TensorInfo(Format format)
62 : TensorInfo(TensorShape(), format)
63{
64}
65
66TensorInfo::TensorInfo(unsigned int width, unsigned int height, Format format)
67 : TensorInfo(TensorShape(width, height), format)
68{
69}
70
71TensorInfo::TensorInfo(const TensorShape &tensor_shape, Format format)
72 : TensorInfo()
73{
74 init(tensor_shape, format);
75}
76
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010077TensorInfo::TensorInfo(size_t num_channels, DataType data_type)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078 : TensorInfo()
79{
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010080 init(TensorShape(), num_channels, data_type);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010081}
82
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010083TensorInfo::TensorInfo(const TensorShape &tensor_shape, size_t num_channels, DataType data_type)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084 : TensorInfo()
85{
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010086 init(tensor_shape, num_channels, data_type);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087}
88
Michel Iwaniec00633802017-10-12 14:14:15 +010089TensorInfo::TensorInfo(const TensorShape &tensor_shape, size_t num_channels, DataType data_type, QuantizationInfo quantization_info)
90 : TensorInfo()
91{
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010092 init(tensor_shape, num_channels, data_type);
Michalis Spyrou18574c12019-06-05 10:51:07 +010093 _quantization_info = std::move(quantization_info);
Michel Iwaniec00633802017-10-12 14:14:15 +010094}
95
Manuel Bottini581f1782019-11-13 17:24:43 +000096TensorInfo::TensorInfo(const TensorShape &tensor_shape, size_t num_channels, DataType data_type, DataLayout data_layout)
97 : TensorInfo()
98{
99 init(tensor_shape, num_channels, data_type);
100 _data_layout = data_layout;
101}
102
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103TensorInfo::TensorInfo(const HOGInfo &hog_info, unsigned int width, unsigned int height)
104 : TensorInfo()
105{
106 init(hog_info, width, height);
107}
108
109void TensorInfo::init(Format format)
110{
111 init(TensorShape(), format);
112}
113
114void TensorInfo::init(const TensorShape &tensor_shape, Format format)
115{
116 size_t num_channels = num_channels_from_format(format);
117 const DataType type = data_type_from_format(format);
118
119 init(tensor_shape, num_channels, type);
120
121 _format = format;
122}
123
124void TensorInfo::init(const TensorShape &tensor_shape, Format format,
125 const Strides &strides_in_bytes, size_t offset_first_element_in_bytes,
126 size_t total_size_in_bytes)
127{
128 size_t num_channels = num_channels_from_format(format);
129 const DataType type = data_type_from_format(format);
130
131 init(tensor_shape, num_channels, type, strides_in_bytes, offset_first_element_in_bytes, total_size_in_bytes);
132
133 _format = format;
134}
135
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100136void TensorInfo::init(size_t num_channels, DataType data_type)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137{
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100138 init(TensorShape(), num_channels, data_type);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139}
140
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100141void TensorInfo::init(const TensorShape &tensor_shape, size_t num_channels, DataType data_type)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100142{
143 ARM_COMPUTE_ERROR_ON(num_channels == 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100144
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100145 _data_type = data_type;
146 _num_channels = num_channels;
147 _format = Format::UNKNOWN;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148
149 set_tensor_shape(tensor_shape);
150}
151
152void TensorInfo::init(const TensorShape &tensor_shape, size_t num_channels, DataType data_type,
153 const Strides &strides_in_bytes, size_t offset_first_element_in_bytes,
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100154 size_t total_size_in_bytes)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155{
156 ARM_COMPUTE_ERROR_ON(num_channels == 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158 _data_type = data_type;
159 _num_channels = num_channels;
160 _format = Format::UNKNOWN;
161 _tensor_shape = tensor_shape;
162 _offset_first_element_in_bytes = offset_first_element_in_bytes;
163 _strides_in_bytes = strides_in_bytes;
164 _total_size = total_size_in_bytes;
165
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000166 _valid_region = ValidRegion{ Coordinates(), _tensor_shape };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167}
168
169void TensorInfo::init(const HOGInfo &hog_info, unsigned int width, unsigned int height)
170{
171 // Number of cells for each block
172 const Size2D num_cells_per_block = hog_info.num_cells_per_block();
173
John Richardson684cb0f2018-01-09 11:17:00 +0000174 // Tensor Size = (Number of horizontal block positions) * (Number of vertical block positions)
175 const Size2D num_block_positions_per_img = hog_info.num_block_positions_per_image(Size2D(width, height));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176
177 // Number of tensor channels = (Number of cells per block) * (Number of bins per cell)
178 const size_t num_channels = num_cells_per_block.area() * hog_info.num_bins();
179
John Richardson684cb0f2018-01-09 11:17:00 +0000180 init(TensorShape(num_block_positions_per_img.width, num_block_positions_per_img.height), num_channels, DataType::F32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181}
182
183size_t TensorInfo::init_auto_padding(const TensorShape &tensor_shape, Format format)
184{
185 const size_t num_channels = num_channels_from_format(format);
186 const DataType type = data_type_from_format(format);
187 size_t total_size = init_auto_padding(tensor_shape, num_channels, type);
188
189 _format = format;
190
191 return total_size;
192}
193
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100194size_t TensorInfo::init_auto_padding(const TensorShape &tensor_shape, size_t num_channels, DataType data_type)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100195{
196 ARM_COMPUTE_ERROR_ON(num_channels == 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100197
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100198 _data_type = data_type;
199 _num_channels = num_channels;
200 _format = Format::UNKNOWN;
201 _tensor_shape = tensor_shape;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100202
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000203 _valid_region = ValidRegion{ Coordinates(), _tensor_shape };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204
205 auto_padding();
206
207 return _total_size;
208}
209
210size_t TensorInfo::init_auto_padding(const HOGInfo &hog_info, unsigned int width, unsigned int height)
211{
212 // Number of cells for each block
213 const Size2D num_cells_per_block = hog_info.num_cells_per_block();
214
John Richardson684cb0f2018-01-09 11:17:00 +0000215 // Tensor Size = (Number of horizontal block positions) * (Number of vertical block positions)
216 const Size2D num_block_positions_per_img = hog_info.num_block_positions_per_image(Size2D(width, height));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100217
218 // Number of tensor channels = (Number of cells per block) * (Number of bins per cell)
219 const size_t num_channels = num_cells_per_block.area() * hog_info.num_bins();
220
John Richardson684cb0f2018-01-09 11:17:00 +0000221 return init_auto_padding(TensorShape(num_block_positions_per_img.width, num_block_positions_per_img.height), num_channels, DataType::F32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100222}
223
224bool TensorInfo::auto_padding()
225{
226 ARM_COMPUTE_ERROR_ON(!_is_resizable);
227
228 // Some kernels compute 32 elements at the time, worst case scenario they
229 // will read 32 values after the last element
230 const size_t extra_pad_x = _tensor_shape.num_dimensions() < 1 ? 0 : 32;
231 const size_t pad_x = _tensor_shape.num_dimensions() < 1 ? 0 : 4;
232 const size_t pad_y = _tensor_shape.num_dimensions() < 2 ? 0 : 4;
233
234 return extend_padding(PaddingSize(pad_y, pad_x + extra_pad_x, pad_y, pad_x));
235}
236
237std::tuple<Strides, size_t, size_t> TensorInfo::calculate_padding_requirements(const PaddingSize &padding)
238{
239 // Calculate resulting stride for the X, Y and Z dimension
240 const size_t stride_x = element_size();
241 const size_t stride_y = (padding.left + _tensor_shape[0] + padding.right) * stride_x;
242 const size_t stride_z = (padding.top + _tensor_shape[1] + padding.bottom) * stride_y;
243
244 Strides required_strides;
245 size_t required_total_size = 0;
246 const size_t required_offset_first_element = padding.left * stride_x + padding.top * stride_y;
247
248 switch(_tensor_shape.num_dimensions())
249 {
250 case 0:
251 {
252 if(_tensor_shape.total_size() > 0)
253 {
Gian Marco Iodiceee94f6a2017-09-11 17:38:02 +0100254 required_strides = Strides(stride_x, stride_x);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100255 required_total_size = stride_z;
256 }
257 break;
258 }
259 case 1:
Gian Marco Iodiceee94f6a2017-09-11 17:38:02 +0100260 required_strides = compute_strides(*this, stride_x, stride_y);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100261 required_total_size = stride_z;
262 break;
263 case 2:
264 required_strides = compute_strides(*this, stride_x, stride_y);
265 required_total_size = stride_z;
266 break;
267 default:
268 {
269 required_strides = compute_strides(*this, stride_x, stride_y, stride_z);
270
271 const unsigned int idx_last_dimension = _tensor_shape.num_dimensions() - 1;
272
Sheri Zhanga3e6b6d2020-08-18 10:07:35 +0100273 required_total_size = static_cast<size_t>(_tensor_shape[idx_last_dimension]) * required_strides[idx_last_dimension];
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100274 break;
275 }
276 }
277
278 return std::make_tuple(required_strides, required_offset_first_element, required_total_size);
279}
280
281bool TensorInfo::extend_padding(const PaddingSize &padding)
282{
283 ARM_COMPUTE_ERROR_ON(!_is_resizable);
284
285 bool updated = false;
286
287 if(padding.top > _padding.top)
288 {
289 _padding.top = padding.top;
290 updated = true;
291 }
292
293 if(padding.right > _padding.right)
294 {
295 _padding.right = padding.right;
296 updated = true;
297 }
298
299 if(padding.bottom > _padding.bottom)
300 {
301 _padding.bottom = padding.bottom;
302 updated = true;
303 }
304
305 if(padding.left > _padding.left)
306 {
307 _padding.left = padding.left;
308 updated = true;
309 }
310
311 std::tie(_strides_in_bytes, _offset_first_element_in_bytes, _total_size) = calculate_padding_requirements(_padding);
312
313 return updated;
314}
315
Georgios Pinitas283c1792017-11-10 18:14:06 +0000316std::unique_ptr<ITensorInfo> TensorInfo::clone() const
317{
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000318 return std::make_unique<TensorInfo>(*this);
Georgios Pinitas283c1792017-11-10 18:14:06 +0000319}
320
321ITensorInfo &TensorInfo::set_data_type(DataType data_type)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100322{
323 _data_type = data_type;
324 _format = Format::UNKNOWN;
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100325 return set_tensor_shape(tensor_shape()); // Force total size and strides to update
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100326}
327
Georgios Pinitas283c1792017-11-10 18:14:06 +0000328ITensorInfo &TensorInfo::set_num_channels(int num_channels)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100329{
330 _num_channels = num_channels;
331 _format = Format::UNKNOWN;
Georgios Pinitas283c1792017-11-10 18:14:06 +0000332 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100333}
334
Georgios Pinitas283c1792017-11-10 18:14:06 +0000335ITensorInfo &TensorInfo::set_format(Format format)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100336{
337 _format = format;
338
339 if(_data_type == DataType::UNKNOWN)
340 {
341 _num_channels = num_channels_from_format(format);
342 _data_type = data_type_from_format(format);
343 }
344 else
345 {
346 ARM_COMPUTE_ERROR_ON(num_channels_from_format(format) != _num_channels);
347 ARM_COMPUTE_ERROR_ON(data_type_from_format(format) != _data_type);
348 }
Georgios Pinitas283c1792017-11-10 18:14:06 +0000349 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100350}
351
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000352ITensorInfo &TensorInfo::set_tensor_shape(const TensorShape &shape)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100353{
354 _tensor_shape = shape;
355 _offset_first_element_in_bytes = 0;
356 _strides_in_bytes = compute_strides(*this);
357
358 if(_tensor_shape.num_dimensions() == 0)
359 {
360 _total_size = _strides_in_bytes[0];
361 }
362 else
363 {
364 const unsigned int idx_last_dimension = _tensor_shape.num_dimensions() - 1;
Sheri Zhanga3e6b6d2020-08-18 10:07:35 +0100365 _total_size = static_cast<size_t>(_tensor_shape[idx_last_dimension]) * _strides_in_bytes[idx_last_dimension];
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100366 }
367
Georgios Pinitas652bde52018-01-10 15:33:28 +0000368 std::tie(_strides_in_bytes, _offset_first_element_in_bytes, _total_size) = calculate_padding_requirements(_padding);
369
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000370 _valid_region = ValidRegion{ Coordinates(), _tensor_shape };
Georgios Pinitas283c1792017-11-10 18:14:06 +0000371 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100372}
373
Georgios Pinitasb14a0f02021-01-08 03:14:31 +0000374ITensorInfo &TensorInfo::set_tensor_dims_state(const TensorDimsState &state)
375{
376 _dims_state = state;
377 return *this;
378}
379
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000380ITensorInfo &TensorInfo::set_quantization_info(const QuantizationInfo &quantization_info)
Georgios Pinitas283c1792017-11-10 18:14:06 +0000381{
382 _quantization_info = quantization_info;
383 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100384}
385
Isabella Gottardid17a6772018-02-27 17:41:55 +0000386ITensorInfo &TensorInfo::set_data_layout(const DataLayout &data_layout)
387{
388 _data_layout = data_layout;
389 return *this;
390}
391
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000392ITensorInfo &TensorInfo::reset_padding()
393{
394 _padding = PaddingSize();
395 if(((_format != Format::UNKNOWN) || (_data_type != DataType::UNKNOWN)) && _total_size != 0)
396 {
397 std::tie(_strides_in_bytes, _offset_first_element_in_bytes, _total_size) = calculate_padding_requirements(_padding);
398 }
399 return *this;
400}
401
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100402int32_t TensorInfo::offset_element_in_bytes(const Coordinates &pos) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100403{
404 ARM_COMPUTE_ERROR_ON_COORDINATES_DIMENSIONS_GTE(pos, _tensor_shape.num_dimensions());
405
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100406 int32_t offset = _offset_first_element_in_bytes;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100407
408 for(size_t i = 0; i < _tensor_shape.num_dimensions(); ++i)
409 {
410 offset += pos[i] * _strides_in_bytes[i];
411 }
412
413 return offset;
414}