blob: 30461f3b8463bc8ecc817d4f48260ac2d355783a [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Matthew Bentham0082c362020-02-03 12:05:18 +00002 * Copyright (c) 2016-2020 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/Error.h"
25#include "arm_compute/core/Validate.h"
26
27#include <cmath>
28#include <numeric>
29
30namespace arm_compute
31{
SiCong Lie1536a82020-06-11 10:47:53 +010032inline size_t dim_index_2_num_dims(int32_t dim_axis, int32_t num_dims)
33{
34 return static_cast<size_t>(wrap_around(dim_axis, num_dims)) + 1;
35}
36
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037inline uint8_t pixel_area_c1u8_clamp(const uint8_t *first_pixel_ptr, size_t stride, size_t width, size_t height, float wr, float hr, int x, int y)
38{
39 ARM_COMPUTE_ERROR_ON(first_pixel_ptr == nullptr);
40
41 // Calculate sampling position
42 float in_x = (x + 0.5f) * wr - 0.5f;
43 float in_y = (y + 0.5f) * hr - 0.5f;
44
45 // Get bounding box offsets
46 int x_from = std::floor(x * wr - 0.5f - in_x);
47 int y_from = std::floor(y * hr - 0.5f - in_y);
48 int x_to = std::ceil((x + 1) * wr - 0.5f - in_x);
49 int y_to = std::ceil((y + 1) * hr - 0.5f - in_y);
50
51 // Clamp position to borders
52 in_x = std::max(-1.f, std::min(in_x, static_cast<float>(width)));
53 in_y = std::max(-1.f, std::min(in_y, static_cast<float>(height)));
54
55 // Clamp bounding box offsets to borders
56 x_from = ((in_x + x_from) < -1) ? -1 : x_from;
57 y_from = ((in_y + y_from) < -1) ? -1 : y_from;
58 x_to = ((in_x + x_to) > width) ? (width - in_x) : x_to;
59 y_to = ((in_y + y_to) > height) ? (height - in_y) : y_to;
60
61 // Get pixel index
62 const int xi = std::floor(in_x);
63 const int yi = std::floor(in_y);
64
65 // Bounding box elements in each dimension
66 const int x_elements = (x_to - x_from + 1);
67 const int y_elements = (y_to - y_from + 1);
68 ARM_COMPUTE_ERROR_ON(x_elements == 0 || y_elements == 0);
69
70 // Sum pixels in area
71 int sum = 0;
72 for(int j = yi + y_from, je = yi + y_to; j <= je; ++j)
73 {
74 const uint8_t *ptr = first_pixel_ptr + j * stride + xi + x_from;
75 sum = std::accumulate(ptr, ptr + x_elements, sum);
76 }
77
78 // Return average
79 return sum / (x_elements * y_elements);
80}
81
82template <size_t dimension>
83struct IncrementIterators
84{
85 template <typename T, typename... Ts>
86 static void unroll(T &&it, Ts &&... iterators)
87 {
Diego Lopez Recas490b3d82017-12-19 15:42:25 +000088 auto increment = [](T && it)
89 {
90 it.increment(dimension);
91 };
92 utility::for_each(increment, std::forward<T>(it), std::forward<Ts>(iterators)...);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094 static void unroll()
95 {
96 // End of recursion
97 }
98};
99
100template <size_t dim>
101struct ForEachDimension
102{
103 template <typename L, typename... Ts>
104 static void unroll(const Window &w, Coordinates &id, L &&lambda_function, Ts &&... iterators)
105 {
106 const auto &d = w[dim - 1];
107
108 for(auto v = d.start(); v < d.end(); v += d.step(), IncrementIterators < dim - 1 >::unroll(iterators...))
109 {
110 id.set(dim - 1, v);
111 ForEachDimension < dim - 1 >::unroll(w, id, lambda_function, iterators...);
112 }
113 }
114};
115
116template <>
117struct ForEachDimension<0>
118{
119 template <typename L, typename... Ts>
120 static void unroll(const Window &w, Coordinates &id, L &&lambda_function, Ts &&... iterators)
121 {
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100122 ARM_COMPUTE_UNUSED(w, iterators...);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123 lambda_function(id);
124 }
125};
126
127template <typename L, typename... Ts>
128inline void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
129{
130 w.validate();
131
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000132 for(unsigned int i = 0; i < Coordinates::num_max_dimensions; ++i)
133 {
134 ARM_COMPUTE_ERROR_ON(w[i].step() == 0);
135 }
136
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137 Coordinates id;
138 ForEachDimension<Coordinates::num_max_dimensions>::unroll(w, id, std::forward<L>(lambda_function), std::forward<Ts>(iterators)...);
139}
140
141inline constexpr Iterator::Iterator()
142 : _ptr(nullptr), _dims()
143{
144}
145
146inline Iterator::Iterator(const ITensor *tensor, const Window &win)
147 : Iterator()
148{
149 ARM_COMPUTE_ERROR_ON(tensor == nullptr);
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000150 ARM_COMPUTE_ERROR_ON(tensor->info() == nullptr);
151
152 const ITensorInfo *info = tensor->info();
153 const Strides &strides = info->strides_in_bytes();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154
155 _ptr = tensor->buffer() + info->offset_first_element_in_bytes();
156
157 //Initialize the stride for each dimension and calculate the position of the first element of the iteration:
158 for(unsigned int n = 0; n < info->num_dimensions(); ++n)
159 {
160 _dims[n]._stride = win[n].step() * strides[n];
161 std::get<0>(_dims)._dim_start += strides[n] * win[n].start();
162 }
163
164 //Copy the starting point to all the dimensions:
165 for(unsigned int n = 1; n < Coordinates::num_max_dimensions; ++n)
166 {
167 _dims[n]._dim_start = std::get<0>(_dims)._dim_start;
168 }
169
170 ARM_COMPUTE_ERROR_ON_WINDOW_DIMENSIONS_GTE(win, info->num_dimensions());
171}
172
173inline void Iterator::increment(const size_t dimension)
174{
175 ARM_COMPUTE_ERROR_ON(dimension >= Coordinates::num_max_dimensions);
176
177 _dims[dimension]._dim_start += _dims[dimension]._stride;
178
179 for(unsigned int n = 0; n < dimension; ++n)
180 {
181 _dims[n]._dim_start = _dims[dimension]._dim_start;
182 }
183}
184
185inline constexpr int Iterator::offset() const
186{
187 return _dims.at(0)._dim_start;
188}
189
190inline constexpr uint8_t *Iterator::ptr() const
191{
192 return _ptr + _dims.at(0)._dim_start;
193}
194
195inline void Iterator::reset(const size_t dimension)
196{
197 ARM_COMPUTE_ERROR_ON(dimension >= Coordinates::num_max_dimensions - 1);
198
199 _dims[dimension]._dim_start = _dims[dimension + 1]._dim_start;
200
201 for(unsigned int n = 0; n < dimension; ++n)
202 {
203 _dims[n]._dim_start = _dims[dimension]._dim_start;
204 }
205}
206
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000207inline bool auto_init_if_empty(ITensorInfo &info,
208 const TensorShape &shape,
209 int num_channels,
210 DataType data_type,
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000211 QuantizationInfo quantization_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100212{
213 if(info.tensor_shape().total_size() == 0)
214 {
215 info.set_data_type(data_type);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100216 info.set_num_channels(num_channels);
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100217 info.set_tensor_shape(shape);
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000218 info.set_quantization_info(quantization_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100219 return true;
220 }
221
222 return false;
223}
224
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100225inline bool auto_init_if_empty(ITensorInfo &info_sink, const ITensorInfo &info_source)
Georgios Pinitas283c1792017-11-10 18:14:06 +0000226{
227 if(info_sink.tensor_shape().total_size() == 0)
228 {
229 info_sink.set_data_type(info_source.data_type());
230 info_sink.set_num_channels(info_source.num_channels());
231 info_sink.set_tensor_shape(info_source.tensor_shape());
Georgios Pinitas283c1792017-11-10 18:14:06 +0000232 info_sink.set_quantization_info(info_source.quantization_info());
Isabella Gottardid17a6772018-02-27 17:41:55 +0000233 info_sink.set_data_layout(info_source.data_layout());
Georgios Pinitas283c1792017-11-10 18:14:06 +0000234 return true;
235 }
236
237 return false;
238}
239
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100240inline bool set_shape_if_empty(ITensorInfo &info, const TensorShape &shape)
241{
242 if(info.tensor_shape().total_size() == 0)
243 {
244 info.set_tensor_shape(shape);
245 return true;
246 }
247
248 return false;
249}
250
251inline bool set_format_if_unknown(ITensorInfo &info, Format format)
252{
253 if(info.data_type() == DataType::UNKNOWN)
254 {
255 info.set_format(format);
256 return true;
257 }
258
259 return false;
260}
261
262inline bool set_data_type_if_unknown(ITensorInfo &info, DataType data_type)
263{
264 if(info.data_type() == DataType::UNKNOWN)
265 {
266 info.set_data_type(data_type);
267 return true;
268 }
269
270 return false;
271}
272
Isabella Gottardid17a6772018-02-27 17:41:55 +0000273inline bool set_data_layout_if_unknown(ITensorInfo &info, DataLayout data_layout)
274{
275 if(info.data_layout() == DataLayout::UNKNOWN)
276 {
277 info.set_data_layout(data_layout);
278 return true;
279 }
280
281 return false;
282}
283
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000284inline bool set_quantization_info_if_empty(ITensorInfo &info, QuantizationInfo quantization_info)
285{
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000286 if(info.quantization_info().empty() && (is_data_type_quantized_asymmetric(info.data_type())))
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000287 {
288 info.set_quantization_info(quantization_info);
289 return true;
290 }
291
292 return false;
293}
294
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100295inline Coordinates index2coords(const TensorShape &shape, int index)
296{
297 int num_elements = shape.total_size();
298
299 ARM_COMPUTE_ERROR_ON_MSG(index < 0 || index >= num_elements, "Index has to be in [0, num_elements]!");
300 ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create coordinate from empty shape!");
301
302 Coordinates coord{ 0 };
303
304 for(int d = shape.num_dimensions() - 1; d >= 0; --d)
305 {
306 num_elements /= shape[d];
307 coord.set(d, index / num_elements);
308 index %= num_elements;
309 }
310
311 return coord;
312}
313
314inline int coords2index(const TensorShape &shape, const Coordinates &coord)
315{
316 int num_elements = shape.total_size();
317 ARM_COMPUTE_UNUSED(num_elements);
318 ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create linear index from empty shape!");
319
320 int index = 0;
321 int stride = 1;
322
323 for(unsigned int d = 0; d < coord.num_dimensions(); ++d)
324 {
325 index += coord[d] * stride;
326 stride *= shape[d];
327 }
328
329 return index;
330}
Isabella Gottardid17a6772018-02-27 17:41:55 +0000331
Isabella Gottardid56e7702018-02-28 14:29:36 +0000332inline size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Isabella Gottardid17a6772018-02-27 17:41:55 +0000333{
Isabella Gottardid56e7702018-02-28 14:29:36 +0000334 ARM_COMPUTE_ERROR_ON_MSG(data_layout == DataLayout::UNKNOWN, "Cannot retrieve the dimension index for an unknown layout!");
Isabella Gottardid17a6772018-02-27 17:41:55 +0000335
336 /* Return the index based on the data layout
337 * [N C H W]
338 * [3 2 1 0]
339 * [N H W C]
340 */
341 switch(data_layout_dimension)
342 {
343 case DataLayoutDimension::CHANNEL:
Isabella Gottardid56e7702018-02-28 14:29:36 +0000344 return (data_layout == DataLayout::NCHW) ? 2 : 0;
Isabella Gottardid17a6772018-02-27 17:41:55 +0000345 break;
346 case DataLayoutDimension::HEIGHT:
Isabella Gottardid56e7702018-02-28 14:29:36 +0000347 return (data_layout == DataLayout::NCHW) ? 1 : 2;
Isabella Gottardid17a6772018-02-27 17:41:55 +0000348 break;
349 case DataLayoutDimension::WIDTH:
Isabella Gottardid56e7702018-02-28 14:29:36 +0000350 return (data_layout == DataLayout::NCHW) ? 0 : 1;
Isabella Gottardid17a6772018-02-27 17:41:55 +0000351 break;
352 case DataLayoutDimension::BATCHES:
353 return 3;
354 break;
355 default:
Isabella Gottardid17a6772018-02-27 17:41:55 +0000356 break;
357 }
Matthew Bentham0082c362020-02-03 12:05:18 +0000358 ARM_COMPUTE_ERROR("Data layout index not supported!");
Isabella Gottardid17a6772018-02-27 17:41:55 +0000359}
Usama Arif8cf8c112019-03-14 15:36:54 +0000360
361inline DataLayoutDimension get_index_data_layout_dimension(const DataLayout data_layout, const size_t index)
362{
363 ARM_COMPUTE_ERROR_ON_MSG(data_layout == DataLayout::UNKNOWN, "Cannot retrieve the dimension index for an unknown layout!");
364
365 /* Return the index based on the data layout
366 * [N C H W]
367 * [3 2 1 0]
368 * [N H W C]
369 */
370 switch(index)
371 {
372 case 0:
373 return (data_layout == DataLayout::NCHW) ? DataLayoutDimension::WIDTH : DataLayoutDimension::CHANNEL;
374 break;
375 case 1:
376 return (data_layout == DataLayout::NCHW) ? DataLayoutDimension::HEIGHT : DataLayoutDimension::WIDTH;
377 break;
378 case 2:
379 return (data_layout == DataLayout::NCHW) ? DataLayoutDimension::CHANNEL : DataLayoutDimension::HEIGHT;
380 break;
381 case 3:
382 return DataLayoutDimension::BATCHES;
383 break;
384 default:
385 ARM_COMPUTE_ERROR("Index value not supported!");
386 break;
387 }
388}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100389} // namespace arm_compute