blob: 235657a38a944461e75c7caae8004463eafdb065 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +00002 * Copyright (c) 2016-2019 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#ifndef __ARM_COMPUTE_HELPERS_H__
25#define __ARM_COMPUTE_HELPERS_H__
26
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Coordinates.h"
Georgios Pinitas583137c2017-08-31 18:12:42 +010028#include "arm_compute/core/Error.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/IAccessWindow.h"
30#include "arm_compute/core/Steps.h"
31#include "arm_compute/core/Strides.h"
32#include "arm_compute/core/TensorShape.h"
33#include "arm_compute/core/Types.h"
34#include "arm_compute/core/Window.h"
Georgios Pinitas583137c2017-08-31 18:12:42 +010035
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036#include <array>
37#include <cstddef>
38#include <cstdint>
39#include <memory>
40#include <tuple>
41#include <type_traits>
42#include <utility>
43
44namespace arm_compute
45{
46class IKernel;
47class ITensor;
48class ITensorInfo;
49
Alex Gildayc357c472018-03-21 13:54:09 +000050/** Disable bitwise operations by default */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051template <typename T>
52struct enable_bitwise_ops
53{
Alex Gildayc357c472018-03-21 13:54:09 +000054 static constexpr bool value = false; /**< Disabled */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010055};
56
Alex Gildayc357c472018-03-21 13:54:09 +000057#ifndef DOXYGEN_SKIP_THIS
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058template <typename T>
59typename std::enable_if<enable_bitwise_ops<T>::value, T>::type operator&(T lhs, T rhs)
60{
61 using underlying_type = typename std::underlying_type<T>::type;
62 return static_cast<T>(static_cast<underlying_type>(lhs) & static_cast<underlying_type>(rhs));
63}
Alex Gildayc357c472018-03-21 13:54:09 +000064#endif /* DOXYGEN_SKIP_THIS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065
Michele Di Giorgiob8fc60f2018-04-25 11:58:07 +010066/** Helper function to create and return a unique_ptr pointed to a CL/GLES kernel object
67 * It also calls the kernel's configuration.
68 *
69 * @param[in] args All the arguments that need pass to kernel's configuration.
70 *
71 * @return A unique pointer pointed to a CL/GLES kernel object
72 */
73template <typename Kernel, typename... T>
74std::unique_ptr<Kernel> create_configure_kernel(T &&... args)
75{
76 std::unique_ptr<Kernel> k = arm_compute::support::cpp14::make_unique<Kernel>();
77 k->configure(std::forward<T>(args)...);
78 return k;
79}
80
81/** Helper function to create and return a unique_ptr pointed to a CL/GLES kernel object
82 *
83 * @return A unique pointer pointed to a Kernel kernel object
84 */
85template <typename Kernel>
86std::unique_ptr<Kernel> create_kernel()
87{
88 std::unique_ptr<Kernel> k = arm_compute::support::cpp14::make_unique<Kernel>();
89 return k;
90}
91
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092namespace traits
93{
94/** Check if a type T is contained in a tuple Tuple of types */
95template <typename T, typename Tuple>
96struct is_contained;
97
98template <typename T>
99struct is_contained<T, std::tuple<>> : std::false_type
100{
101};
102
103template <typename T, typename... Ts>
104struct is_contained<T, std::tuple<T, Ts...>> : std::true_type
105{
106};
107
108template <typename T, typename U, typename... Ts>
109struct is_contained<T, std::tuple<U, Ts...>> : is_contained<T, std::tuple<Ts...>>
110{
111};
112}
113
114/** Computes bilinear interpolation using the pointer to the top-left pixel and the pixel's distance between
Georgios Pinitas583137c2017-08-31 18:12:42 +0100115 * the real coordinates and the smallest following integer coordinates. Input must be in single channel format.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100116 *
Georgios Pinitas583137c2017-08-31 18:12:42 +0100117 * @param[in] pixel_ptr Pointer to the top-left pixel value of a single channel input.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100118 * @param[in] stride Stride to access the bottom-left and bottom-right pixel values
119 * @param[in] dx Pixel's distance between the X real coordinate and the smallest X following integer
120 * @param[in] dy Pixel's distance between the Y real coordinate and the smallest Y following integer
121 *
122 * @note dx and dy must be in the range [0, 1.0]
123 *
124 * @return The bilinear interpolated pixel value
125 */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100126template <typename T>
127inline T delta_bilinear_c1(const T *pixel_ptr, size_t stride, float dx, float dy)
128{
129 ARM_COMPUTE_ERROR_ON(pixel_ptr == nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130
Georgios Pinitas583137c2017-08-31 18:12:42 +0100131 const float dx1 = 1.0f - dx;
132 const float dy1 = 1.0f - dy;
133
134 const T a00 = *pixel_ptr;
135 const T a01 = *(pixel_ptr + 1);
136 const T a10 = *(pixel_ptr + stride);
137 const T a11 = *(pixel_ptr + stride + 1);
138
139 const float w1 = dx1 * dy1;
140 const float w2 = dx * dy1;
141 const float w3 = dx1 * dy;
142 const float w4 = dx * dy;
143
144 return static_cast<T>(a00 * w1 + a01 * w2 + a10 * w3 + a11 * w4);
145}
146
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +0000147/** Computes bilinear interpolation for quantized input and output, using the pointer to the top-left pixel and the pixel's distance between
148 * the real coordinates and the smallest following integer coordinates. Input must be quantized and in single channel format.
149 *
150 * @param[in] pixel_ptr Pointer to the top-left pixel value of a single channel input.
151 * @param[in] stride Stride to access the bottom-left and bottom-right pixel values
152 * @param[in] dx Pixel's distance between the X real coordinate and the smallest X following integer
153 * @param[in] dy Pixel's distance between the Y real coordinate and the smallest Y following integer
154 * @param[in] iq_info Input QuantizationInfo
155 * @param[in] oq_info Output QuantizationInfo
156 *
157 * @note dx and dy must be in the range [0, 1.0]
158 *
159 * @return The bilinear interpolated pixel value
160 */
161inline uint8_t delta_bilinear_c1_quantized(const uint8_t *pixel_ptr, size_t stride, float dx, float dy, QuantizationInfo iq_info, QuantizationInfo oq_info)
162{
163 ARM_COMPUTE_ERROR_ON(pixel_ptr == nullptr);
164
165 const float dx1 = 1.0f - dx;
166 const float dy1 = 1.0f - dy;
167
168 const float a00 = iq_info.dequantize(*pixel_ptr);
169 const float a01 = iq_info.dequantize(*(pixel_ptr + 1));
170 const float a10 = iq_info.dequantize(*(pixel_ptr + stride));
171 const float a11 = iq_info.dequantize(*(pixel_ptr + stride + 1));
172
173 const float w1 = dx1 * dy1;
174 const float w2 = dx * dy1;
175 const float w3 = dx1 * dy;
176 const float w4 = dx * dy;
177 float res = a00 * w1 + a01 * w2 + a10 * w3 + a11 * w4;
178 return static_cast<uint8_t>(oq_info.quantize(res, RoundingPolicy::TO_NEAREST_UP));
179}
180
Anthony Barbier9a33b542017-12-12 22:08:59 +0000181/** Computes linear interpolation using the pointer to the top pixel and the pixel's distance between
182 * the real coordinates and the smallest following integer coordinates. Input must be in single channel format.
183 *
184 * @param[in] pixel_ptr Pointer to the top pixel value of a single channel input.
185 * @param[in] stride Stride to access the bottom pixel value
186 * @param[in] dy Pixel's distance between the Y real coordinate and the smallest Y following integer
187 *
188 * @note dy must be in the range [0, 1.0]
189 *
190 * @return The linear interpolated pixel value
191 */
192template <typename T>
193inline T delta_linear_c1_y(const T *pixel_ptr, size_t stride, float dy)
194{
195 ARM_COMPUTE_ERROR_ON(pixel_ptr == nullptr);
196
197 const float dy1 = 1.0f - dy;
198
199 const T a00 = *pixel_ptr;
200 const T a10 = *(pixel_ptr + stride);
201
202 const float w1 = dy1;
203 const float w3 = dy;
204
205 return static_cast<T>(a00 * w1 + a10 * w3);
206}
207/** Computes linear interpolation using the pointer to the left pixel and the pixel's distance between
208 * the real coordinates and the smallest following integer coordinates. Input must be in single channel format.
209 *
210 * @param[in] pixel_ptr Pointer to the left pixel value of a single channel input.
211 * @param[in] dx Pixel's distance between the X real coordinate and the smallest X following integer
212 *
213 * @note dx must be in the range [0, 1.0]
214 *
215 * @return The linear interpolated pixel value
216 */
217template <typename T>
218inline T delta_linear_c1_x(const T *pixel_ptr, float dx)
219{
220 ARM_COMPUTE_ERROR_ON(pixel_ptr == nullptr);
221
222 const T a00 = *pixel_ptr;
223 const T a01 = *(pixel_ptr + 1);
224
225 const float dx1 = 1.0f - dx;
226
227 const float w1 = dx1;
228 const float w2 = dx;
229
230 return static_cast<T>(a00 * w1 + a01 * w2);
231}
Georgios Pinitas583137c2017-08-31 18:12:42 +0100232/** Return the pixel at (x,y) using bilinear interpolation.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100233 *
234 * @warning Only works if the iterator was created with an IImage
235 *
Georgios Pinitas583137c2017-08-31 18:12:42 +0100236 * @param[in] first_pixel_ptr Pointer to the first pixel of a single channel input.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100237 * @param[in] stride Stride in bytes of the image;
238 * @param[in] x X position of the wanted pixel
239 * @param[in] y Y position of the wanted pixel
240 *
241 * @return The pixel at (x, y) using bilinear interpolation.
242 */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100243template <typename T>
244inline T pixel_bilinear_c1(const T *first_pixel_ptr, size_t stride, float x, float y)
245{
246 ARM_COMPUTE_ERROR_ON(first_pixel_ptr == nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100247
Georgios Pinitas583137c2017-08-31 18:12:42 +0100248 const int32_t xi = std::floor(x);
249 const int32_t yi = std::floor(y);
250
251 const float dx = x - xi;
252 const float dy = y - yi;
253
254 return delta_bilinear_c1(first_pixel_ptr + xi + yi * stride, stride, dx, dy);
255}
256
257/** Return the pixel at (x,y) using bilinear interpolation by clamping when out of borders. The image must be single channel input
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100258 *
259 * @warning Only works if the iterator was created with an IImage
260 *
Georgios Pinitas583137c2017-08-31 18:12:42 +0100261 * @param[in] first_pixel_ptr Pointer to the first pixel of a single channel image.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100262 * @param[in] stride Stride in bytes of the image
263 * @param[in] width Width of the image
264 * @param[in] height Height of the image
265 * @param[in] x X position of the wanted pixel
266 * @param[in] y Y position of the wanted pixel
267 *
268 * @return The pixel at (x, y) using bilinear interpolation.
269 */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100270template <typename T>
271inline uint8_t pixel_bilinear_c1_clamp(const T *first_pixel_ptr, size_t stride, size_t width, size_t height, float x, float y)
272{
273 ARM_COMPUTE_ERROR_ON(first_pixel_ptr == nullptr);
274
275 x = std::max(-1.f, std::min(x, static_cast<float>(width)));
276 y = std::max(-1.f, std::min(y, static_cast<float>(height)));
277
278 const float xi = std::floor(x);
279 const float yi = std::floor(y);
280
281 const float dx = x - xi;
282 const float dy = y - yi;
283
Anthony Barbier9a33b542017-12-12 22:08:59 +0000284 if(dx == 0.0f)
285 {
286 if(dy == 0.0f)
287 {
288 return static_cast<T>(first_pixel_ptr[static_cast<int32_t>(xi) + static_cast<int32_t>(yi) * stride]);
289 }
290 return delta_linear_c1_y(first_pixel_ptr + static_cast<int32_t>(xi) + static_cast<int32_t>(yi) * stride, stride, dy);
291 }
292 if(dy == 0.0f)
293 {
294 return delta_linear_c1_x(first_pixel_ptr + static_cast<int32_t>(xi) + static_cast<int32_t>(yi) * stride, dx);
295 }
Georgios Pinitas583137c2017-08-31 18:12:42 +0100296 return delta_bilinear_c1(first_pixel_ptr + static_cast<int32_t>(xi) + static_cast<int32_t>(yi) * stride, stride, dx, dy);
297}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100298
299/** Return the pixel at (x,y) using area interpolation by clamping when out of borders. The image must be single channel U8
300 *
301 * @note The interpolation area depends on the width and height ration of the input and output images
302 * @note Currently average of the contributing pixels is calculated
303 *
304 * @param[in] first_pixel_ptr Pointer to the first pixel of a single channel U8 image.
305 * @param[in] stride Stride in bytes of the image
306 * @param[in] width Width of the image
307 * @param[in] height Height of the image
308 * @param[in] wr Width ratio among the input image width and output image width.
309 * @param[in] hr Height ratio among the input image height and output image height.
310 * @param[in] x X position of the wanted pixel
311 * @param[in] y Y position of the wanted pixel
312 *
313 * @return The pixel at (x, y) using area interpolation.
314 */
315inline 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);
316
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100317/** Iterator updated by @ref execute_window_loop for each window element */
318class Iterator
319{
320public:
321 /** Default constructor to create an empty iterator */
322 constexpr Iterator();
323 /** Create a container iterator for the metadata and allocation contained in the ITensor
324 *
325 * @param[in] tensor The tensor to associate to the iterator.
326 * @param[in] window The window which will be used to iterate over the tensor.
327 */
328 Iterator(const ITensor *tensor, const Window &window);
329
330 /** Increment the iterator along the specified dimension of the step value associated to the dimension.
331 *
332 * @warning It is the caller's responsibility to call increment(dimension+1) when reaching the end of a dimension, the iterator will not check for overflow.
333 *
334 * @note When incrementing a dimension 'n' the coordinates of all the dimensions in the range (0,n-1) are reset. For example if you iterate over a 2D image, everytime you change row (dimension 1), the iterator for the width (dimension 0) is reset to its start.
335 *
336 * @param[in] dimension Dimension to increment
337 */
338 void increment(size_t dimension);
339
340 /** Return the offset in bytes from the first element to the current position of the iterator
341 *
342 * @return The current position of the iterator in bytes relative to the first element.
343 */
344 constexpr int offset() const;
345
346 /** Return a pointer to the current pixel.
347 *
348 * @warning Only works if the iterator was created with an ITensor.
349 *
350 * @return equivalent to buffer() + offset()
351 */
352 constexpr uint8_t *ptr() const;
353
354 /** Move the iterator back to the beginning of the specified dimension.
355 *
356 * @param[in] dimension Dimension to reset
357 */
358 void reset(size_t dimension);
359
360private:
361 uint8_t *_ptr;
362
363 class Dimension
364 {
365 public:
366 constexpr Dimension()
367 : _dim_start(0), _stride(0)
368 {
369 }
370
371 int _dim_start;
372 int _stride;
373 };
374
375 std::array<Dimension, Coordinates::num_max_dimensions> _dims;
376};
377
378/** Iterate through the passed window, automatically adjusting the iterators and calling the lambda_functino for each element.
379 * It passes the x and y positions to the lambda_function for each iteration
380 *
381 * @param[in] w Window to iterate through.
382 * @param[in] lambda_function The function of type void(function)( const Coordinates & id ) to call at each iteration.
383 * Where id represents the absolute coordinates of the item to process.
384 * @param[in,out] iterators Tensor iterators which will be updated by this function before calling lambda_function.
385 */
386template <typename L, typename... Ts>
387inline void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators);
388
389/** Update window and padding size for each of the access patterns.
390 *
391 * First the window size is reduced based on all access patterns that are not
392 * allowed to modify the padding of the underlying tensor. Then the padding of
393 * the remaining tensors is increased to match the window.
394 *
395 * @param[in] win Window that is used by the kernel.
396 * @param[in] patterns Access patterns used to calculate the final window and padding.
397 *
398 * @return True if the window has been changed. Changes to the padding do not
399 * influence the returned value.
400 */
401template <typename... Ts>
402bool update_window_and_padding(Window &win, Ts &&... patterns)
403{
404 bool window_changed = false;
405
Diego Lopez Recas490b3d82017-12-19 15:42:25 +0000406 utility::for_each([&](const IAccessWindow & w)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100407 {
408 window_changed |= w.update_window_if_needed(win);
409 },
410 patterns...);
411
412 bool padding_changed = false;
413
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000414 utility::for_each([&](IAccessWindow & w)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100415 {
416 padding_changed |= w.update_padding_if_needed(win);
417 },
418 patterns...);
419
420 return window_changed;
421}
422
423/** Calculate the maximum window for a given tensor shape and border setting
424 *
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000425 * @param[in] valid_region Valid region object defining the shape of the tensor space for which the window is created.
426 * @param[in] steps (Optional) Number of elements processed for each step.
427 * @param[in] skip_border (Optional) If true exclude the border region from the window.
428 * @param[in] border_size (Optional) Border size.
429 *
430 * @return The maximum window the kernel can be executed on.
431 */
432Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps = Steps(), bool skip_border = false, BorderSize border_size = BorderSize());
433
434/** Calculate the maximum window for a given tensor shape and border setting
435 *
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100436 * @param[in] info Tensor info object defining the shape of the object for which the window is created.
437 * @param[in] steps (Optional) Number of elements processed for each step.
438 * @param[in] skip_border (Optional) If true exclude the border region from the window.
439 * @param[in] border_size (Optional) Border size.
440 *
441 * @return The maximum window the kernel can be executed on.
442 */
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000443inline Window calculate_max_window(const ITensorInfo &info, const Steps &steps = Steps(), bool skip_border = false, BorderSize border_size = BorderSize())
444{
445 return calculate_max_window(info.valid_region(), steps, skip_border, border_size);
446}
447
448/** Calculate the maximum window used by a horizontal kernel for a given tensor shape and border setting
449 *
450 * @param[in] valid_region Valid region object defining the shape of the tensor space for which the window is created.
451 * @param[in] steps (Optional) Number of elements processed for each step.
452 * @param[in] skip_border (Optional) If true exclude the border region from the window.
453 * @param[in] border_size (Optional) Border size. The border region will be excluded from the window.
454 *
455 * @return The maximum window the kernel can be executed on.
456 */
457Window calculate_max_window_horizontal(const ValidRegion &valid_region, const Steps &steps = Steps(), bool skip_border = false, BorderSize border_size = BorderSize());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100458
459/** Calculate the maximum window used by a horizontal kernel for a given tensor shape and border setting
460 *
461 * @param[in] info Tensor info object defining the shape of the object for which the window is created.
462 * @param[in] steps (Optional) Number of elements processed for each step.
463 * @param[in] skip_border (Optional) If true exclude the border region from the window.
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000464 * @param[in] border_size (Optional) Border size.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100465 *
466 * @return The maximum window the kernel can be executed on.
467 */
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000468inline Window calculate_max_window_horizontal(const ITensorInfo &info, const Steps &steps = Steps(), bool skip_border = false, BorderSize border_size = BorderSize())
469{
470 return calculate_max_window_horizontal(info.valid_region(), steps, skip_border, border_size);
471}
472
473/** Calculate the maximum window for a given tensor shape and border setting. The window will also includes the border.
474 *
475 * @param[in] valid_region Valid region object defining the shape of the tensor space for which the window is created.
476 * @param[in] steps (Optional) Number of elements processed for each step.
477 * @param[in] border_size (Optional) Border size. The border region will be included in the window.
478 *
479 * @return The maximum window the kernel can be executed on.
480 */
481Window calculate_max_enlarged_window(const ValidRegion &valid_region, const Steps &steps = Steps(), BorderSize border_size = BorderSize());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100482
483/** Calculate the maximum window for a given tensor shape and border setting. The window will also includes the border.
484 *
485 * @param[in] info Tensor info object defining the shape of the object for which the window is created.
486 * @param[in] steps (Optional) Number of elements processed for each step.
487 * @param[in] border_size (Optional) Border size. The border region will be included in the window.
488 *
489 * @return The maximum window the kernel can be executed on.
490 */
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000491inline Window calculate_max_enlarged_window(const ITensorInfo &info, const Steps &steps = Steps(), BorderSize border_size = BorderSize())
492{
493 return calculate_max_enlarged_window(info.valid_region(), steps, border_size);
494}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100495
496/** Intersect multiple valid regions.
497 *
498 * @param[in] regions Valid regions.
499 *
500 * @return Intersection of all regions.
501 */
502template <typename... Ts>
Diego Lopez Recas490b3d82017-12-19 15:42:25 +0000503ValidRegion intersect_valid_regions(const Ts &... regions)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100504{
505 auto intersect = [](const ValidRegion & r1, const ValidRegion & r2) -> ValidRegion
506 {
507 ValidRegion region;
508
509 for(size_t d = 0; d < std::min(r1.anchor.num_dimensions(), r2.anchor.num_dimensions()); ++d)
510 {
511 region.anchor.set(d, std::max(r1.anchor[d], r2.anchor[d]));
512 }
513
514 for(size_t d = 0; d < std::min(r1.shape.num_dimensions(), r2.shape.num_dimensions()); ++d)
515 {
516 region.shape.set(d, std::min(r1.shape[d], r2.shape[d]));
517 }
518
519 return region;
520 };
521
Diego Lopez Recas490b3d82017-12-19 15:42:25 +0000522 return utility::foldl(intersect, regions...);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100523}
524
525/** Create a strides object based on the provided strides and the tensor dimensions.
526 *
527 * @param[in] info Tensor info object providing the shape of the tensor for unspecified strides.
528 * @param[in] stride_x Stride to be used in X dimension (in bytes).
529 * @param[in] fixed_strides Strides to be used in higher dimensions starting at Y (in bytes).
530 *
531 * @return Strides object based on the specified strides. Missing strides are
532 * calculated based on the tensor shape and the strides of lower dimensions.
533 */
534template <typename T, typename... Ts>
535inline Strides compute_strides(const ITensorInfo &info, T stride_x, Ts &&... fixed_strides)
536{
537 const TensorShape &shape = info.tensor_shape();
538
539 // Create strides object
540 Strides strides(stride_x, fixed_strides...);
541
542 for(size_t i = 1 + sizeof...(Ts); i < info.num_dimensions(); ++i)
543 {
544 strides.set(i, shape[i - 1] * strides[i - 1]);
545 }
546
547 return strides;
548}
549
550/** Create a strides object based on the tensor dimensions.
551 *
552 * @param[in] info Tensor info object used to compute the strides.
553 *
554 * @return Strides object based on element size and tensor shape.
555 */
556template <typename... Ts>
557inline Strides compute_strides(const ITensorInfo &info)
558{
559 return compute_strides(info, info.element_size());
560}
561
Georgios Pinitas8795ffb2017-12-01 16:13:40 +0000562/** Permutes given Dimensions according to a permutation vector
563 *
564 * @warning Validity of permutation is not checked
565 *
566 * @param[in, out] dimensions Dimensions to permute
567 * @param[in] perm Permutation vector
568 */
569template <typename T>
570inline void permute(Dimensions<T> &dimensions, const PermutationVector &perm)
571{
Georgios Pinitas69af6cf2018-02-14 19:23:44 +0000572 auto dimensions_copy = utility::make_array<Dimensions<T>::num_max_dimensions>(dimensions.begin(), dimensions.end());
Georgios Pinitas8795ffb2017-12-01 16:13:40 +0000573 for(unsigned int i = 0; i < perm.num_dimensions(); ++i)
574 {
Georgios Pinitas69af6cf2018-02-14 19:23:44 +0000575 T dimension_val = (perm[i] < dimensions.num_dimensions()) ? dimensions_copy[perm[i]] : 0;
576 dimensions.set(i, dimension_val);
577 }
578}
579
580/** Permutes given TensorShape according to a permutation vector
581 *
582 * @warning Validity of permutation is not checked
583 *
584 * @param[in, out] shape Shape to permute
585 * @param[in] perm Permutation vector
586 */
587inline void permute(TensorShape &shape, const PermutationVector &perm)
588{
Giorgio Arena563494c2018-04-30 17:29:41 +0100589 TensorShape shape_copy = shape;
Georgios Pinitas69af6cf2018-02-14 19:23:44 +0000590 for(unsigned int i = 0; i < perm.num_dimensions(); ++i)
591 {
592 size_t dimension_val = (perm[i] < shape.num_dimensions()) ? shape_copy[perm[i]] : 1;
Giorgio Arena563494c2018-04-30 17:29:41 +0100593 shape.set(i, dimension_val, false); // Avoid changes in _num_dimension
Georgios Pinitas8795ffb2017-12-01 16:13:40 +0000594 }
595}
596
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100597/** Auto initialize the tensor info (shape, number of channels and data type) if the current assignment is empty.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100598 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100599 * @param[in,out] info Tensor info used to check and assign.
600 * @param[in] shape New shape.
601 * @param[in] num_channels New number of channels.
602 * @param[in] data_type New data type
603 * @param[in] quantization_info (Optional) New quantization info
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100604 *
605 * @return True if the tensor info has been initialized
606 */
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000607bool auto_init_if_empty(ITensorInfo &info,
608 const TensorShape &shape,
609 int num_channels, DataType data_type,
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000610 QuantizationInfo quantization_info = QuantizationInfo());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100611
Georgios Pinitas283c1792017-11-10 18:14:06 +0000612/** Auto initialize the tensor info using another tensor info.
613 *
614 * @param info_sink Tensor info used to check and assign
615 * @param info_source Tensor info used to assign
616 *
617 * @return True if the tensor info has been initialized
618 */
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100619bool auto_init_if_empty(ITensorInfo &info_sink, const ITensorInfo &info_source);
Georgios Pinitas283c1792017-11-10 18:14:06 +0000620
Alex Gildayc357c472018-03-21 13:54:09 +0000621/** Set the shape to the specified value if the current assignment is empty.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100622 *
623 * @param[in,out] info Tensor info used to check and assign.
624 * @param[in] shape New shape.
625 *
626 * @return True if the shape has been changed.
627 */
628bool set_shape_if_empty(ITensorInfo &info, const TensorShape &shape);
629
Alex Gildayc357c472018-03-21 13:54:09 +0000630/** Set the format, data type and number of channels to the specified value if
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100631 * the current data type is unknown.
632 *
633 * @param[in,out] info Tensor info used to check and assign.
634 * @param[in] format New format.
635 *
636 * @return True if the format has been changed.
637 */
638bool set_format_if_unknown(ITensorInfo &info, Format format);
639
Alex Gildayc357c472018-03-21 13:54:09 +0000640/** Set the data type and number of channels to the specified value if
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100641 * the current data type is unknown.
642 *
643 * @param[in,out] info Tensor info used to check and assign.
644 * @param[in] data_type New data type.
645 *
646 * @return True if the data type has been changed.
647 */
648bool set_data_type_if_unknown(ITensorInfo &info, DataType data_type);
649
Alex Gildayc357c472018-03-21 13:54:09 +0000650/** Set the data layout to the specified value if
Isabella Gottardid17a6772018-02-27 17:41:55 +0000651 * the current data layout is unknown.
652 *
653 * @param[in,out] info Tensor info used to check and assign.
654 * @param[in] data_layout New data layout.
655 *
656 * @return True if the data type has been changed.
657 */
658bool set_data_layout_if_unknown(ITensorInfo &info, DataLayout data_layout);
659
Alex Gildayc357c472018-03-21 13:54:09 +0000660/** Set the quantization info to the specified value if
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000661 * the current quantization info is empty and the data type of asymmetric quantized type
662 *
663 * @param[in,out] info Tensor info used to check and assign.
664 * @param[in] quantization_info Quantization info
665 *
666 * @return True if the quantization info has been changed.
667 */
668bool set_quantization_info_if_empty(ITensorInfo &info, QuantizationInfo quantization_info);
669
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100670/** Helper function to calculate the Valid Region for Scale.
671 *
Diego Lopez Recas00854292018-02-22 13:08:01 +0000672 * @param[in] src_info Input tensor info used to check.
673 * @param[in] dst_shape Shape of the output.
674 * @param[in] interpolate_policy Interpolation policy.
675 * @param[in] sampling_policy Sampling policy.
676 * @param[in] border_undefined True if the border is undefined.
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100677 *
Diego Lopez Recas00854292018-02-22 13:08:01 +0000678 * @return The corresponding valid region
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100679 */
Diego Lopez Recas00854292018-02-22 13:08:01 +0000680ValidRegion calculate_valid_region_scale(const ITensorInfo &src_info, const TensorShape &dst_shape,
681 InterpolationPolicy interpolate_policy, SamplingPolicy sampling_policy, bool border_undefined);
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000682
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100683/** Convert a linear index into n-dimensional coordinates.
684 *
685 * @param[in] shape Shape of the n-dimensional tensor.
686 * @param[in] index Linear index specifying the i-th element.
687 *
688 * @return n-dimensional coordinates.
689 */
690inline Coordinates index2coords(const TensorShape &shape, int index);
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000691
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100692/** Convert n-dimensional coordinates into a linear index.
693 *
694 * @param[in] shape Shape of the n-dimensional tensor.
695 * @param[in] coord N-dimensional coordinates.
696 *
697 * @return linead index
698 */
699inline int coords2index(const TensorShape &shape, const Coordinates &coord);
Isabella Gottardid17a6772018-02-27 17:41:55 +0000700
Alex Gildayc357c472018-03-21 13:54:09 +0000701/** Get the index of the given dimension.
Isabella Gottardid17a6772018-02-27 17:41:55 +0000702 *
Alex Gildayc357c472018-03-21 13:54:09 +0000703 * @param[in] data_layout The data layout.
704 * @param[in] data_layout_dimension The dimension which this index is requested for.
Isabella Gottardid17a6772018-02-27 17:41:55 +0000705 *
706 * @return The int conversion of the requested data layout index.
707 */
Isabella Gottardid56e7702018-02-28 14:29:36 +0000708inline size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension);
Georgios Pinitase2220552018-07-20 13:23:44 +0100709
Usama Arif8cf8c112019-03-14 15:36:54 +0000710/** Get the DataLayoutDimension of a given index and layout.
711 *
712 * @param[in] data_layout The data layout.
713 * @param[in] index The data layout index.
714 *
715 * @return The dimension which this index is requested for.
716 */
717inline DataLayoutDimension get_index_data_layout_dimension(const DataLayout data_layout, const size_t index);
718
Georgios Pinitase2220552018-07-20 13:23:44 +0100719/** Calculate the normalization dimension index for a given normalization type
720 *
721 * @param[in] layout Data layout of the input and output tensor
722 * @param[in] info Normalization info
723 *
724 * @return Normalization dimension index
725 */
726inline unsigned int get_normalization_dimension_index(DataLayout layout, const NormalizationLayerInfo &info)
727{
728 const unsigned int width_idx = get_data_layout_dimension_index(layout, DataLayoutDimension::WIDTH);
729 const unsigned int channel_idx = get_data_layout_dimension_index(layout, DataLayoutDimension::CHANNEL);
730
731 return info.is_in_map() ? width_idx : channel_idx;
732}
733
734/** Calculate the number of output tiles required by Winograd Convolution layer. This utility function can be used by the Winograd input transform
735 * to know the number of tiles on the x and y direction
736 *
737 * @param[in] in_dims Spatial dimensions of the input tensor of convolution layer
738 * @param[in] kernel_size Kernel size
739 * @param[in] output_tile_size Size of a single output tile
740 * @param[in] conv_info Convolution info (i.e. pad, stride,...)
741 *
742 * @return the number of output tiles along the x and y directions of size "output_tile_size"
743 */
744inline Size2D compute_winograd_convolution_tiles(const Size2D &in_dims, const Size2D &kernel_size, const Size2D &output_tile_size, const PadStrideInfo &conv_info)
745{
746 int num_tiles_x = std::ceil((in_dims.width - (kernel_size.width - 1) + conv_info.pad_left() + conv_info.pad_right()) / static_cast<float>(output_tile_size.width));
747 int num_tiles_y = std::ceil((in_dims.height - (kernel_size.height - 1) + conv_info.pad_top() + conv_info.pad_bottom()) / static_cast<float>(output_tile_size.height));
748
749 // Clamp in case we provide paddings but we have 1D convolution
750 num_tiles_x = std::min(num_tiles_x, static_cast<int>(in_dims.width));
751 num_tiles_y = std::min(num_tiles_y, static_cast<int>(in_dims.height));
752
753 return Size2D(num_tiles_x, num_tiles_y);
754}
755
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +0000756/** Wrap-around a number within the range 0 <= x < m
757 *
758 * @param[in] x Input value
759 * @param[in] m Range
760 *
761 * @return the wrapped-around number
762 */
763template <typename T>
764inline T wrap_around(T x, T m)
765{
766 return x >= 0 ? x % m : (x % m + m) % m;
767}
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000768
769/** Given an integer value, this function returns the next power of two
770 *
771 * @param[in] x Input value
772 *
773 * @return the next power of two
774 */
775inline unsigned int get_next_power_two(unsigned int x)
776{
777 // Decrement by 1
778 x--;
779
780 // Shift right by 1
781 x |= x >> 1u;
782 // Shift right by 2
783 x |= x >> 2u;
784 // Shift right by 4
785 x |= x >> 4u;
786 // Shift right by 8
787 x |= x >> 8u;
788 // Shift right by 16
789 x |= x >> 16u;
790
791 // Increment by 1
792 x++;
793
794 return x;
795}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100796} // namespace arm_compute
797
798#include "arm_compute/core/Helpers.inl"
799#endif /*__ARM_COMPUTE_HELPERS_H__ */