blob: 899805a701bc5924ed598cc80b03e0dc4295bc16 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Diego Lopez Recas0021d752017-12-18 14:42:56 +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#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{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032inline 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)
33{
34 ARM_COMPUTE_ERROR_ON(first_pixel_ptr == nullptr);
35
36 // Calculate sampling position
37 float in_x = (x + 0.5f) * wr - 0.5f;
38 float in_y = (y + 0.5f) * hr - 0.5f;
39
40 // Get bounding box offsets
41 int x_from = std::floor(x * wr - 0.5f - in_x);
42 int y_from = std::floor(y * hr - 0.5f - in_y);
43 int x_to = std::ceil((x + 1) * wr - 0.5f - in_x);
44 int y_to = std::ceil((y + 1) * hr - 0.5f - in_y);
45
46 // Clamp position to borders
47 in_x = std::max(-1.f, std::min(in_x, static_cast<float>(width)));
48 in_y = std::max(-1.f, std::min(in_y, static_cast<float>(height)));
49
50 // Clamp bounding box offsets to borders
51 x_from = ((in_x + x_from) < -1) ? -1 : x_from;
52 y_from = ((in_y + y_from) < -1) ? -1 : y_from;
53 x_to = ((in_x + x_to) > width) ? (width - in_x) : x_to;
54 y_to = ((in_y + y_to) > height) ? (height - in_y) : y_to;
55
56 // Get pixel index
57 const int xi = std::floor(in_x);
58 const int yi = std::floor(in_y);
59
60 // Bounding box elements in each dimension
61 const int x_elements = (x_to - x_from + 1);
62 const int y_elements = (y_to - y_from + 1);
63 ARM_COMPUTE_ERROR_ON(x_elements == 0 || y_elements == 0);
64
65 // Sum pixels in area
66 int sum = 0;
67 for(int j = yi + y_from, je = yi + y_to; j <= je; ++j)
68 {
69 const uint8_t *ptr = first_pixel_ptr + j * stride + xi + x_from;
70 sum = std::accumulate(ptr, ptr + x_elements, sum);
71 }
72
73 // Return average
74 return sum / (x_elements * y_elements);
75}
76
77template <size_t dimension>
78struct IncrementIterators
79{
80 template <typename T, typename... Ts>
81 static void unroll(T &&it, Ts &&... iterators)
82 {
Diego Lopez Recas490b3d82017-12-19 15:42:25 +000083 auto increment = [](T && it)
84 {
85 it.increment(dimension);
86 };
87 utility::for_each(increment, std::forward<T>(it), std::forward<Ts>(iterators)...);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089 static void unroll()
90 {
91 // End of recursion
92 }
93};
94
95template <size_t dim>
96struct ForEachDimension
97{
98 template <typename L, typename... Ts>
99 static void unroll(const Window &w, Coordinates &id, L &&lambda_function, Ts &&... iterators)
100 {
101 const auto &d = w[dim - 1];
102
103 for(auto v = d.start(); v < d.end(); v += d.step(), IncrementIterators < dim - 1 >::unroll(iterators...))
104 {
105 id.set(dim - 1, v);
106 ForEachDimension < dim - 1 >::unroll(w, id, lambda_function, iterators...);
107 }
108 }
109};
110
111template <>
112struct ForEachDimension<0>
113{
114 template <typename L, typename... Ts>
115 static void unroll(const Window &w, Coordinates &id, L &&lambda_function, Ts &&... iterators)
116 {
117 lambda_function(id);
118 }
119};
120
121template <typename L, typename... Ts>
122inline void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
123{
124 w.validate();
125
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000126 for(unsigned int i = 0; i < Coordinates::num_max_dimensions; ++i)
127 {
128 ARM_COMPUTE_ERROR_ON(w[i].step() == 0);
129 }
130
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131 Coordinates id;
132 ForEachDimension<Coordinates::num_max_dimensions>::unroll(w, id, std::forward<L>(lambda_function), std::forward<Ts>(iterators)...);
133}
134
135inline constexpr Iterator::Iterator()
136 : _ptr(nullptr), _dims()
137{
138}
139
140inline Iterator::Iterator(const ITensor *tensor, const Window &win)
141 : Iterator()
142{
143 ARM_COMPUTE_ERROR_ON(tensor == nullptr);
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000144 ARM_COMPUTE_ERROR_ON(tensor->info() == nullptr);
145
146 const ITensorInfo *info = tensor->info();
147 const Strides &strides = info->strides_in_bytes();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148
149 _ptr = tensor->buffer() + info->offset_first_element_in_bytes();
150
151 //Initialize the stride for each dimension and calculate the position of the first element of the iteration:
152 for(unsigned int n = 0; n < info->num_dimensions(); ++n)
153 {
154 _dims[n]._stride = win[n].step() * strides[n];
155 std::get<0>(_dims)._dim_start += strides[n] * win[n].start();
156 }
157
158 //Copy the starting point to all the dimensions:
159 for(unsigned int n = 1; n < Coordinates::num_max_dimensions; ++n)
160 {
161 _dims[n]._dim_start = std::get<0>(_dims)._dim_start;
162 }
163
164 ARM_COMPUTE_ERROR_ON_WINDOW_DIMENSIONS_GTE(win, info->num_dimensions());
165}
166
167inline void Iterator::increment(const size_t dimension)
168{
169 ARM_COMPUTE_ERROR_ON(dimension >= Coordinates::num_max_dimensions);
170
171 _dims[dimension]._dim_start += _dims[dimension]._stride;
172
173 for(unsigned int n = 0; n < dimension; ++n)
174 {
175 _dims[n]._dim_start = _dims[dimension]._dim_start;
176 }
177}
178
179inline constexpr int Iterator::offset() const
180{
181 return _dims.at(0)._dim_start;
182}
183
184inline constexpr uint8_t *Iterator::ptr() const
185{
186 return _ptr + _dims.at(0)._dim_start;
187}
188
189inline void Iterator::reset(const size_t dimension)
190{
191 ARM_COMPUTE_ERROR_ON(dimension >= Coordinates::num_max_dimensions - 1);
192
193 _dims[dimension]._dim_start = _dims[dimension + 1]._dim_start;
194
195 for(unsigned int n = 0; n < dimension; ++n)
196 {
197 _dims[n]._dim_start = _dims[dimension]._dim_start;
198 }
199}
200
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000201inline bool auto_init_if_empty(ITensorInfo &info,
202 const TensorShape &shape,
203 int num_channels,
204 DataType data_type,
205 int fixed_point_position,
206 QuantizationInfo quantization_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100207{
208 if(info.tensor_shape().total_size() == 0)
209 {
210 info.set_data_type(data_type);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100211 info.set_num_channels(num_channels);
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100212 info.set_tensor_shape(shape);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100213 info.set_fixed_point_position(fixed_point_position);
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000214 info.set_quantization_info(quantization_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100215 return true;
216 }
217
218 return false;
219}
220
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100221inline bool auto_init_if_empty(ITensorInfo &info_sink, const ITensorInfo &info_source)
Georgios Pinitas283c1792017-11-10 18:14:06 +0000222{
223 if(info_sink.tensor_shape().total_size() == 0)
224 {
225 info_sink.set_data_type(info_source.data_type());
226 info_sink.set_num_channels(info_source.num_channels());
227 info_sink.set_tensor_shape(info_source.tensor_shape());
228 info_sink.set_fixed_point_position(info_source.fixed_point_position());
229 info_sink.set_quantization_info(info_source.quantization_info());
230 return true;
231 }
232
233 return false;
234}
235
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100236inline bool set_shape_if_empty(ITensorInfo &info, const TensorShape &shape)
237{
238 if(info.tensor_shape().total_size() == 0)
239 {
240 info.set_tensor_shape(shape);
241 return true;
242 }
243
244 return false;
245}
246
247inline bool set_format_if_unknown(ITensorInfo &info, Format format)
248{
249 if(info.data_type() == DataType::UNKNOWN)
250 {
251 info.set_format(format);
252 return true;
253 }
254
255 return false;
256}
257
258inline bool set_data_type_if_unknown(ITensorInfo &info, DataType data_type)
259{
260 if(info.data_type() == DataType::UNKNOWN)
261 {
262 info.set_data_type(data_type);
263 return true;
264 }
265
266 return false;
267}
268
269inline bool set_fixed_point_position_if_zero(ITensorInfo &info, int fixed_point_position)
270{
271 if(info.fixed_point_position() == 0 && (info.data_type() == DataType::QS8 || info.data_type() == DataType::QS16))
272 {
273 info.set_fixed_point_position(fixed_point_position);
274 return true;
275 }
276
277 return false;
278}
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100279
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000280inline bool set_quantization_info_if_empty(ITensorInfo &info, QuantizationInfo quantization_info)
281{
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000282 if(info.quantization_info().empty() && (is_data_type_quantized_asymmetric(info.data_type())))
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000283 {
284 info.set_quantization_info(quantization_info);
285 return true;
286 }
287
288 return false;
289}
290
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100291inline ValidRegion calculate_valid_region_scale(const ITensorInfo &src_info, const TensorShape &dst_shape, InterpolationPolicy policy, BorderSize border_size, bool border_undefined)
292{
Diego Lopez Recasf03561b2018-02-22 13:08:01 +0000293 const float scale_x = static_cast<float>(dst_shape[0]) / src_info.tensor_shape()[0];
294 const float scale_y = static_cast<float>(dst_shape[1]) / src_info.tensor_shape()[1];
295
296 int valid_start_out_x;
297 int valid_start_out_y;
298 int valid_end_out_x;
299 int valid_end_out_y;
300
301 switch(policy)
302 {
303 case InterpolationPolicy::NEAREST_NEIGHBOR:
304 {
305 const float valid_start_in_x = src_info.valid_region().anchor[0];
306 const float valid_start_in_y = src_info.valid_region().anchor[1];
307
308 const float valid_end_in_x = src_info.valid_region().anchor[0] + src_info.valid_region().shape[0];
309 const float valid_end_in_y = src_info.valid_region().anchor[1] + src_info.valid_region().shape[1];
310
311 // (start_out + 0.5) / scale >= start_in
312 // --> start_out >= (start_in * scale) - 0.5
313 // --> start_out = ceil((start_in * scale) - 0.5)
314 valid_start_out_x = std::ceil((valid_start_in_x * scale_x) - 0.5f);
315 valid_start_out_y = std::ceil((valid_start_in_y * scale_y) - 0.5f);
316
317 // (end_out - 0.5) / scale < end_in
318 // --> end_out < (end_in * scale) + 0.5
319 // --> end_out = ceil((end_in * scale) + 0.5 - 1)
320 valid_end_out_x = std::ceil((valid_end_in_x * scale_x) - 0.5f);
321 valid_end_out_y = std::ceil((valid_end_in_y * scale_y) - 0.5f);
322
323 break;
324 }
325 case InterpolationPolicy::BILINEAR:
326 {
327 const float k = border_undefined ? 0.5f : 0.0f;
328
329 const float valid_start_in_x = src_info.valid_region().anchor[0] + k;
330 const float valid_start_in_y = src_info.valid_region().anchor[1] + k;
331
332 const float valid_end_in_x = src_info.valid_region().anchor[0] + src_info.valid_region().shape[0] - k;
333 const float valid_end_in_y = src_info.valid_region().anchor[1] + src_info.valid_region().shape[1] - k;
334
335 // (start_out + 0.5) / scale >= start_in
336 // --> start_out >= (start_in * scale) - 0.5
337 // --> start_out = ceil((start_in * scale) - 0.5)
338 valid_start_out_x = std::ceil((valid_start_in_x * scale_x) - 0.5f);
339 valid_start_out_y = std::ceil((valid_start_in_y * scale_y) - 0.5f);
340
341 // (end_out - 0.5) / scale <= end_in
342 // --> end_out <= (end_in * scale) + 0.5
343 // --> end_out = floor((end_in * scale) + 0.5)
344 valid_end_out_x = std::floor((valid_end_in_x * scale_x) + 0.5f);
345 valid_end_out_y = std::floor((valid_end_in_y * scale_y) + 0.5f);
346
347 break;
348 }
349 case InterpolationPolicy::AREA:
350 {
351 const float valid_start_in_x = src_info.valid_region().anchor[0];
352 const float valid_start_in_y = src_info.valid_region().anchor[1];
353
354 const float valid_end_in_x = src_info.valid_region().anchor[0] + src_info.valid_region().shape[0];
355 const float valid_end_in_y = src_info.valid_region().anchor[1] + src_info.valid_region().shape[1];
356
357 // start_out / scale >= start_in
358 // --> start_out >= start_in * scale
359 // --> start_out = ceil(start_in * scale)
360 valid_start_out_x = std::ceil(valid_start_in_x * scale_x);
361 valid_start_out_y = std::ceil(valid_start_in_y * scale_y);
362
363 // end_out / scale <= end_in
364 // --> end_out <= end_in * scale
365 // --> end_out = floor(end_in * scale)
366 valid_end_out_x = std::floor(valid_end_in_x * scale_x);
367 valid_end_out_y = std::floor(valid_end_in_y * scale_y);
368
369 break;
370 }
371 default:
372 {
373 ARM_COMPUTE_ERROR("Invalid InterpolationPolicy");
374 break;
375 }
376 }
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000377
378 ValidRegion valid_region{ Coordinates(), dst_shape, src_info.tensor_shape().num_dimensions() };
379
Diego Lopez Recasf03561b2018-02-22 13:08:01 +0000380 valid_region.anchor.set(0, valid_start_out_x);
381 valid_region.anchor.set(1, valid_start_out_y);
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000382
Diego Lopez Recasf03561b2018-02-22 13:08:01 +0000383 valid_region.shape.set(0, static_cast<size_t>(valid_end_out_x - valid_start_out_x));
384 valid_region.shape.set(1, static_cast<size_t>(valid_end_out_y - valid_start_out_y));
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100385
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000386 return valid_region;
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100387}
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100388
389inline Coordinates index2coords(const TensorShape &shape, int index)
390{
391 int num_elements = shape.total_size();
392
393 ARM_COMPUTE_ERROR_ON_MSG(index < 0 || index >= num_elements, "Index has to be in [0, num_elements]!");
394 ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create coordinate from empty shape!");
395
396 Coordinates coord{ 0 };
397
398 for(int d = shape.num_dimensions() - 1; d >= 0; --d)
399 {
400 num_elements /= shape[d];
401 coord.set(d, index / num_elements);
402 index %= num_elements;
403 }
404
405 return coord;
406}
407
408inline int coords2index(const TensorShape &shape, const Coordinates &coord)
409{
410 int num_elements = shape.total_size();
411 ARM_COMPUTE_UNUSED(num_elements);
412 ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create linear index from empty shape!");
413
414 int index = 0;
415 int stride = 1;
416
417 for(unsigned int d = 0; d < coord.num_dimensions(); ++d)
418 {
419 index += coord[d] * stride;
420 stride *= shape[d];
421 }
422
423 return index;
424}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100425} // namespace arm_compute