blob: 6f3aba7077c3b651454455d07f54ee21c4e54035 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +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#ifndef __ARM_COMPUTE_HELPERS_H__
25#define __ARM_COMPUTE_HELPERS_H__
26
27#include "arm_compute/core/CL/CLTypes.h"
28#include "arm_compute/core/Coordinates.h"
Georgios Pinitas583137c2017-08-31 18:12:42 +010029#include "arm_compute/core/Error.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/IAccessWindow.h"
31#include "arm_compute/core/Steps.h"
32#include "arm_compute/core/Strides.h"
33#include "arm_compute/core/TensorShape.h"
34#include "arm_compute/core/Types.h"
35#include "arm_compute/core/Window.h"
Georgios Pinitas583137c2017-08-31 18:12:42 +010036
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037#include <array>
38#include <cstddef>
39#include <cstdint>
40#include <memory>
41#include <tuple>
42#include <type_traits>
43#include <utility>
44
45namespace arm_compute
46{
47class IKernel;
48class ITensor;
49class ITensorInfo;
50
Alex Gildayc357c472018-03-21 13:54:09 +000051/** Disable bitwise operations by default */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052template <typename T>
53struct enable_bitwise_ops
54{
Alex Gildayc357c472018-03-21 13:54:09 +000055 static constexpr bool value = false; /**< Disabled */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056};
57
Alex Gildayc357c472018-03-21 13:54:09 +000058#ifndef DOXYGEN_SKIP_THIS
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059template <typename T>
60typename std::enable_if<enable_bitwise_ops<T>::value, T>::type operator&(T lhs, T rhs)
61{
62 using underlying_type = typename std::underlying_type<T>::type;
63 return static_cast<T>(static_cast<underlying_type>(lhs) & static_cast<underlying_type>(rhs));
64}
Alex Gildayc357c472018-03-21 13:54:09 +000065#endif /* DOXYGEN_SKIP_THIS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066
67namespace traits
68{
69/** Check if a type T is contained in a tuple Tuple of types */
70template <typename T, typename Tuple>
71struct is_contained;
72
73template <typename T>
74struct is_contained<T, std::tuple<>> : std::false_type
75{
76};
77
78template <typename T, typename... Ts>
79struct is_contained<T, std::tuple<T, Ts...>> : std::true_type
80{
81};
82
83template <typename T, typename U, typename... Ts>
84struct is_contained<T, std::tuple<U, Ts...>> : is_contained<T, std::tuple<Ts...>>
85{
86};
87}
88
89/** Computes bilinear interpolation using the pointer to the top-left pixel and the pixel's distance between
Georgios Pinitas583137c2017-08-31 18:12:42 +010090 * the real coordinates and the smallest following integer coordinates. Input must be in single channel format.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091 *
Georgios Pinitas583137c2017-08-31 18:12:42 +010092 * @param[in] pixel_ptr Pointer to the top-left pixel value of a single channel input.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093 * @param[in] stride Stride to access the bottom-left and bottom-right pixel values
94 * @param[in] dx Pixel's distance between the X real coordinate and the smallest X following integer
95 * @param[in] dy Pixel's distance between the Y real coordinate and the smallest Y following integer
96 *
97 * @note dx and dy must be in the range [0, 1.0]
98 *
99 * @return The bilinear interpolated pixel value
100 */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100101template <typename T>
102inline T delta_bilinear_c1(const T *pixel_ptr, size_t stride, float dx, float dy)
103{
104 ARM_COMPUTE_ERROR_ON(pixel_ptr == nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105
Georgios Pinitas583137c2017-08-31 18:12:42 +0100106 const float dx1 = 1.0f - dx;
107 const float dy1 = 1.0f - dy;
108
109 const T a00 = *pixel_ptr;
110 const T a01 = *(pixel_ptr + 1);
111 const T a10 = *(pixel_ptr + stride);
112 const T a11 = *(pixel_ptr + stride + 1);
113
114 const float w1 = dx1 * dy1;
115 const float w2 = dx * dy1;
116 const float w3 = dx1 * dy;
117 const float w4 = dx * dy;
118
119 return static_cast<T>(a00 * w1 + a01 * w2 + a10 * w3 + a11 * w4);
120}
121
Anthony Barbier9a33b542017-12-12 22:08:59 +0000122/** Computes linear interpolation using the pointer to the top pixel and the pixel's distance between
123 * the real coordinates and the smallest following integer coordinates. Input must be in single channel format.
124 *
125 * @param[in] pixel_ptr Pointer to the top pixel value of a single channel input.
126 * @param[in] stride Stride to access the bottom pixel value
127 * @param[in] dy Pixel's distance between the Y real coordinate and the smallest Y following integer
128 *
129 * @note dy must be in the range [0, 1.0]
130 *
131 * @return The linear interpolated pixel value
132 */
133template <typename T>
134inline T delta_linear_c1_y(const T *pixel_ptr, size_t stride, float dy)
135{
136 ARM_COMPUTE_ERROR_ON(pixel_ptr == nullptr);
137
138 const float dy1 = 1.0f - dy;
139
140 const T a00 = *pixel_ptr;
141 const T a10 = *(pixel_ptr + stride);
142
143 const float w1 = dy1;
144 const float w3 = dy;
145
146 return static_cast<T>(a00 * w1 + a10 * w3);
147}
148/** Computes linear interpolation using the pointer to the left pixel and the pixel's distance between
149 * the real coordinates and the smallest following integer coordinates. Input must be in single channel format.
150 *
151 * @param[in] pixel_ptr Pointer to the left pixel value of a single channel input.
152 * @param[in] dx Pixel's distance between the X real coordinate and the smallest X following integer
153 *
154 * @note dx must be in the range [0, 1.0]
155 *
156 * @return The linear interpolated pixel value
157 */
158template <typename T>
159inline T delta_linear_c1_x(const T *pixel_ptr, float dx)
160{
161 ARM_COMPUTE_ERROR_ON(pixel_ptr == nullptr);
162
163 const T a00 = *pixel_ptr;
164 const T a01 = *(pixel_ptr + 1);
165
166 const float dx1 = 1.0f - dx;
167
168 const float w1 = dx1;
169 const float w2 = dx;
170
171 return static_cast<T>(a00 * w1 + a01 * w2);
172}
Georgios Pinitas583137c2017-08-31 18:12:42 +0100173/** Return the pixel at (x,y) using bilinear interpolation.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174 *
175 * @warning Only works if the iterator was created with an IImage
176 *
Georgios Pinitas583137c2017-08-31 18:12:42 +0100177 * @param[in] first_pixel_ptr Pointer to the first pixel of a single channel input.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100178 * @param[in] stride Stride in bytes of the image;
179 * @param[in] x X position of the wanted pixel
180 * @param[in] y Y position of the wanted pixel
181 *
182 * @return The pixel at (x, y) using bilinear interpolation.
183 */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100184template <typename T>
185inline T pixel_bilinear_c1(const T *first_pixel_ptr, size_t stride, float x, float y)
186{
187 ARM_COMPUTE_ERROR_ON(first_pixel_ptr == nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100188
Georgios Pinitas583137c2017-08-31 18:12:42 +0100189 const int32_t xi = std::floor(x);
190 const int32_t yi = std::floor(y);
191
192 const float dx = x - xi;
193 const float dy = y - yi;
194
195 return delta_bilinear_c1(first_pixel_ptr + xi + yi * stride, stride, dx, dy);
196}
197
198/** 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 +0100199 *
200 * @warning Only works if the iterator was created with an IImage
201 *
Georgios Pinitas583137c2017-08-31 18:12:42 +0100202 * @param[in] first_pixel_ptr Pointer to the first pixel of a single channel image.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100203 * @param[in] stride Stride in bytes of the image
204 * @param[in] width Width of the image
205 * @param[in] height Height of the image
206 * @param[in] x X position of the wanted pixel
207 * @param[in] y Y position of the wanted pixel
208 *
209 * @return The pixel at (x, y) using bilinear interpolation.
210 */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100211template <typename T>
212inline uint8_t pixel_bilinear_c1_clamp(const T *first_pixel_ptr, size_t stride, size_t width, size_t height, float x, float y)
213{
214 ARM_COMPUTE_ERROR_ON(first_pixel_ptr == nullptr);
215
216 x = std::max(-1.f, std::min(x, static_cast<float>(width)));
217 y = std::max(-1.f, std::min(y, static_cast<float>(height)));
218
219 const float xi = std::floor(x);
220 const float yi = std::floor(y);
221
222 const float dx = x - xi;
223 const float dy = y - yi;
224
Anthony Barbier9a33b542017-12-12 22:08:59 +0000225 if(dx == 0.0f)
226 {
227 if(dy == 0.0f)
228 {
229 return static_cast<T>(first_pixel_ptr[static_cast<int32_t>(xi) + static_cast<int32_t>(yi) * stride]);
230 }
231 return delta_linear_c1_y(first_pixel_ptr + static_cast<int32_t>(xi) + static_cast<int32_t>(yi) * stride, stride, dy);
232 }
233 if(dy == 0.0f)
234 {
235 return delta_linear_c1_x(first_pixel_ptr + static_cast<int32_t>(xi) + static_cast<int32_t>(yi) * stride, dx);
236 }
Georgios Pinitas583137c2017-08-31 18:12:42 +0100237 return delta_bilinear_c1(first_pixel_ptr + static_cast<int32_t>(xi) + static_cast<int32_t>(yi) * stride, stride, dx, dy);
238}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100239
240/** Return the pixel at (x,y) using area interpolation by clamping when out of borders. The image must be single channel U8
241 *
242 * @note The interpolation area depends on the width and height ration of the input and output images
243 * @note Currently average of the contributing pixels is calculated
244 *
245 * @param[in] first_pixel_ptr Pointer to the first pixel of a single channel U8 image.
246 * @param[in] stride Stride in bytes of the image
247 * @param[in] width Width of the image
248 * @param[in] height Height of the image
249 * @param[in] wr Width ratio among the input image width and output image width.
250 * @param[in] hr Height ratio among the input image height and output image height.
251 * @param[in] x X position of the wanted pixel
252 * @param[in] y Y position of the wanted pixel
253 *
254 * @return The pixel at (x, y) using area interpolation.
255 */
256inline 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);
257
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100258/** Iterator updated by @ref execute_window_loop for each window element */
259class Iterator
260{
261public:
262 /** Default constructor to create an empty iterator */
263 constexpr Iterator();
264 /** Create a container iterator for the metadata and allocation contained in the ITensor
265 *
266 * @param[in] tensor The tensor to associate to the iterator.
267 * @param[in] window The window which will be used to iterate over the tensor.
268 */
269 Iterator(const ITensor *tensor, const Window &window);
270
271 /** Increment the iterator along the specified dimension of the step value associated to the dimension.
272 *
273 * @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.
274 *
275 * @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.
276 *
277 * @param[in] dimension Dimension to increment
278 */
279 void increment(size_t dimension);
280
281 /** Return the offset in bytes from the first element to the current position of the iterator
282 *
283 * @return The current position of the iterator in bytes relative to the first element.
284 */
285 constexpr int offset() const;
286
287 /** Return a pointer to the current pixel.
288 *
289 * @warning Only works if the iterator was created with an ITensor.
290 *
291 * @return equivalent to buffer() + offset()
292 */
293 constexpr uint8_t *ptr() const;
294
295 /** Move the iterator back to the beginning of the specified dimension.
296 *
297 * @param[in] dimension Dimension to reset
298 */
299 void reset(size_t dimension);
300
301private:
302 uint8_t *_ptr;
303
304 class Dimension
305 {
306 public:
307 constexpr Dimension()
308 : _dim_start(0), _stride(0)
309 {
310 }
311
312 int _dim_start;
313 int _stride;
314 };
315
316 std::array<Dimension, Coordinates::num_max_dimensions> _dims;
317};
318
319/** Iterate through the passed window, automatically adjusting the iterators and calling the lambda_functino for each element.
320 * It passes the x and y positions to the lambda_function for each iteration
321 *
322 * @param[in] w Window to iterate through.
323 * @param[in] lambda_function The function of type void(function)( const Coordinates & id ) to call at each iteration.
324 * Where id represents the absolute coordinates of the item to process.
325 * @param[in,out] iterators Tensor iterators which will be updated by this function before calling lambda_function.
326 */
327template <typename L, typename... Ts>
328inline void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators);
329
330/** Update window and padding size for each of the access patterns.
331 *
332 * First the window size is reduced based on all access patterns that are not
333 * allowed to modify the padding of the underlying tensor. Then the padding of
334 * the remaining tensors is increased to match the window.
335 *
336 * @param[in] win Window that is used by the kernel.
337 * @param[in] patterns Access patterns used to calculate the final window and padding.
338 *
339 * @return True if the window has been changed. Changes to the padding do not
340 * influence the returned value.
341 */
342template <typename... Ts>
343bool update_window_and_padding(Window &win, Ts &&... patterns)
344{
345 bool window_changed = false;
346
Diego Lopez Recas490b3d82017-12-19 15:42:25 +0000347 utility::for_each([&](const IAccessWindow & w)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100348 {
349 window_changed |= w.update_window_if_needed(win);
350 },
351 patterns...);
352
353 bool padding_changed = false;
354
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000355 utility::for_each([&](IAccessWindow & w)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100356 {
357 padding_changed |= w.update_padding_if_needed(win);
358 },
359 patterns...);
360
361 return window_changed;
362}
363
364/** Calculate the maximum window for a given tensor shape and border setting
365 *
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000366 * @param[in] valid_region Valid region object defining the shape of the tensor space for which the window is created.
367 * @param[in] steps (Optional) Number of elements processed for each step.
368 * @param[in] skip_border (Optional) If true exclude the border region from the window.
369 * @param[in] border_size (Optional) Border size.
370 *
371 * @return The maximum window the kernel can be executed on.
372 */
373Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps = Steps(), bool skip_border = false, BorderSize border_size = BorderSize());
374
375/** Calculate the maximum window for a given tensor shape and border setting
376 *
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100377 * @param[in] info Tensor info object defining the shape of the object for which the window is created.
378 * @param[in] steps (Optional) Number of elements processed for each step.
379 * @param[in] skip_border (Optional) If true exclude the border region from the window.
380 * @param[in] border_size (Optional) Border size.
381 *
382 * @return The maximum window the kernel can be executed on.
383 */
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000384inline Window calculate_max_window(const ITensorInfo &info, const Steps &steps = Steps(), bool skip_border = false, BorderSize border_size = BorderSize())
385{
386 return calculate_max_window(info.valid_region(), steps, skip_border, border_size);
387}
388
389/** Calculate the maximum window used by a horizontal kernel for a given tensor shape and border setting
390 *
391 * @param[in] valid_region Valid region object defining the shape of the tensor space for which the window is created.
392 * @param[in] steps (Optional) Number of elements processed for each step.
393 * @param[in] skip_border (Optional) If true exclude the border region from the window.
394 * @param[in] border_size (Optional) Border size. The border region will be excluded from the window.
395 *
396 * @return The maximum window the kernel can be executed on.
397 */
398Window 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 +0100399
400/** Calculate the maximum window used by a horizontal kernel for a given tensor shape and border setting
401 *
402 * @param[in] info Tensor info object defining the shape of the object for which the window is created.
403 * @param[in] steps (Optional) Number of elements processed for each step.
404 * @param[in] skip_border (Optional) If true exclude the border region from the window.
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000405 * @param[in] border_size (Optional) Border size.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100406 *
407 * @return The maximum window the kernel can be executed on.
408 */
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000409inline Window calculate_max_window_horizontal(const ITensorInfo &info, const Steps &steps = Steps(), bool skip_border = false, BorderSize border_size = BorderSize())
410{
411 return calculate_max_window_horizontal(info.valid_region(), steps, skip_border, border_size);
412}
413
414/** Calculate the maximum window for a given tensor shape and border setting. The window will also includes the border.
415 *
416 * @param[in] valid_region Valid region object defining the shape of the tensor space for which the window is created.
417 * @param[in] steps (Optional) Number of elements processed for each step.
418 * @param[in] border_size (Optional) Border size. The border region will be included in the window.
419 *
420 * @return The maximum window the kernel can be executed on.
421 */
422Window calculate_max_enlarged_window(const ValidRegion &valid_region, const Steps &steps = Steps(), BorderSize border_size = BorderSize());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100423
424/** Calculate the maximum window for a given tensor shape and border setting. The window will also includes the border.
425 *
426 * @param[in] info Tensor info object defining the shape of the object for which the window is created.
427 * @param[in] steps (Optional) Number of elements processed for each step.
428 * @param[in] border_size (Optional) Border size. The border region will be included in the window.
429 *
430 * @return The maximum window the kernel can be executed on.
431 */
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000432inline Window calculate_max_enlarged_window(const ITensorInfo &info, const Steps &steps = Steps(), BorderSize border_size = BorderSize())
433{
434 return calculate_max_enlarged_window(info.valid_region(), steps, border_size);
435}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100436
437/** Intersect multiple valid regions.
438 *
439 * @param[in] regions Valid regions.
440 *
441 * @return Intersection of all regions.
442 */
443template <typename... Ts>
Diego Lopez Recas490b3d82017-12-19 15:42:25 +0000444ValidRegion intersect_valid_regions(const Ts &... regions)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100445{
446 auto intersect = [](const ValidRegion & r1, const ValidRegion & r2) -> ValidRegion
447 {
448 ValidRegion region;
449
450 for(size_t d = 0; d < std::min(r1.anchor.num_dimensions(), r2.anchor.num_dimensions()); ++d)
451 {
452 region.anchor.set(d, std::max(r1.anchor[d], r2.anchor[d]));
453 }
454
455 for(size_t d = 0; d < std::min(r1.shape.num_dimensions(), r2.shape.num_dimensions()); ++d)
456 {
457 region.shape.set(d, std::min(r1.shape[d], r2.shape[d]));
458 }
459
460 return region;
461 };
462
Diego Lopez Recas490b3d82017-12-19 15:42:25 +0000463 return utility::foldl(intersect, regions...);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100464}
465
466/** Create a strides object based on the provided strides and the tensor dimensions.
467 *
468 * @param[in] info Tensor info object providing the shape of the tensor for unspecified strides.
469 * @param[in] stride_x Stride to be used in X dimension (in bytes).
470 * @param[in] fixed_strides Strides to be used in higher dimensions starting at Y (in bytes).
471 *
472 * @return Strides object based on the specified strides. Missing strides are
473 * calculated based on the tensor shape and the strides of lower dimensions.
474 */
475template <typename T, typename... Ts>
476inline Strides compute_strides(const ITensorInfo &info, T stride_x, Ts &&... fixed_strides)
477{
478 const TensorShape &shape = info.tensor_shape();
479
480 // Create strides object
481 Strides strides(stride_x, fixed_strides...);
482
483 for(size_t i = 1 + sizeof...(Ts); i < info.num_dimensions(); ++i)
484 {
485 strides.set(i, shape[i - 1] * strides[i - 1]);
486 }
487
488 return strides;
489}
490
491/** Create a strides object based on the tensor dimensions.
492 *
493 * @param[in] info Tensor info object used to compute the strides.
494 *
495 * @return Strides object based on element size and tensor shape.
496 */
497template <typename... Ts>
498inline Strides compute_strides(const ITensorInfo &info)
499{
500 return compute_strides(info, info.element_size());
501}
502
Georgios Pinitas8795ffb2017-12-01 16:13:40 +0000503/** Permutes given Dimensions according to a permutation vector
504 *
505 * @warning Validity of permutation is not checked
506 *
507 * @param[in, out] dimensions Dimensions to permute
508 * @param[in] perm Permutation vector
509 */
510template <typename T>
511inline void permute(Dimensions<T> &dimensions, const PermutationVector &perm)
512{
Georgios Pinitas69af6cf2018-02-14 19:23:44 +0000513 auto dimensions_copy = utility::make_array<Dimensions<T>::num_max_dimensions>(dimensions.begin(), dimensions.end());
Georgios Pinitas8795ffb2017-12-01 16:13:40 +0000514 for(unsigned int i = 0; i < perm.num_dimensions(); ++i)
515 {
Georgios Pinitas69af6cf2018-02-14 19:23:44 +0000516 T dimension_val = (perm[i] < dimensions.num_dimensions()) ? dimensions_copy[perm[i]] : 0;
517 dimensions.set(i, dimension_val);
518 }
519}
520
521/** Permutes given TensorShape according to a permutation vector
522 *
523 * @warning Validity of permutation is not checked
524 *
525 * @param[in, out] shape Shape to permute
526 * @param[in] perm Permutation vector
527 */
528inline void permute(TensorShape &shape, const PermutationVector &perm)
529{
530 auto shape_copy = utility::make_array<TensorShape::num_max_dimensions>(shape.begin(), shape.end());
531 for(unsigned int i = 0; i < perm.num_dimensions(); ++i)
532 {
533 size_t dimension_val = (perm[i] < shape.num_dimensions()) ? shape_copy[perm[i]] : 1;
534 shape.set(i, dimension_val);
Georgios Pinitas8795ffb2017-12-01 16:13:40 +0000535 }
536}
537
Alex Gildayc357c472018-03-21 13:54:09 +0000538/** Auto initialize the tensor info (shape, number of channels, data type and fixed point position) if the current assignment is empty.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100539 *
540 * @param[in,out] info Tensor info used to check and assign.
541 * @param[in] shape New shape.
542 * @param[in] num_channels New number of channels.
543 * @param[in] data_type New data type
544 * @param[in] fixed_point_position New fixed point position
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000545 * @param[in] quantization_info (Optional) New quantization info
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100546 *
547 * @return True if the tensor info has been initialized
548 */
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000549bool auto_init_if_empty(ITensorInfo &info,
550 const TensorShape &shape,
551 int num_channels, DataType data_type,
552 int fixed_point_position,
553 QuantizationInfo quantization_info = QuantizationInfo());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100554
Georgios Pinitas283c1792017-11-10 18:14:06 +0000555/** Auto initialize the tensor info using another tensor info.
556 *
557 * @param info_sink Tensor info used to check and assign
558 * @param info_source Tensor info used to assign
559 *
560 * @return True if the tensor info has been initialized
561 */
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100562bool auto_init_if_empty(ITensorInfo &info_sink, const ITensorInfo &info_source);
Georgios Pinitas283c1792017-11-10 18:14:06 +0000563
Alex Gildayc357c472018-03-21 13:54:09 +0000564/** Set the shape to the specified value if the current assignment is empty.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100565 *
566 * @param[in,out] info Tensor info used to check and assign.
567 * @param[in] shape New shape.
568 *
569 * @return True if the shape has been changed.
570 */
571bool set_shape_if_empty(ITensorInfo &info, const TensorShape &shape);
572
Alex Gildayc357c472018-03-21 13:54:09 +0000573/** Set the format, data type and number of channels to the specified value if
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100574 * the current data type is unknown.
575 *
576 * @param[in,out] info Tensor info used to check and assign.
577 * @param[in] format New format.
578 *
579 * @return True if the format has been changed.
580 */
581bool set_format_if_unknown(ITensorInfo &info, Format format);
582
Alex Gildayc357c472018-03-21 13:54:09 +0000583/** Set the data type and number of channels to the specified value if
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100584 * the current data type is unknown.
585 *
586 * @param[in,out] info Tensor info used to check and assign.
587 * @param[in] data_type New data type.
588 *
589 * @return True if the data type has been changed.
590 */
591bool set_data_type_if_unknown(ITensorInfo &info, DataType data_type);
592
Alex Gildayc357c472018-03-21 13:54:09 +0000593/** Set the data layout to the specified value if
Isabella Gottardid17a6772018-02-27 17:41:55 +0000594 * the current data layout is unknown.
595 *
596 * @param[in,out] info Tensor info used to check and assign.
597 * @param[in] data_layout New data layout.
598 *
599 * @return True if the data type has been changed.
600 */
601bool set_data_layout_if_unknown(ITensorInfo &info, DataLayout data_layout);
602
Alex Gildayc357c472018-03-21 13:54:09 +0000603/** Set the fixed point position to the specified value if
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100604 * the current fixed point position is 0 and the data type is QS8 or QS16
605 *
606 * @param[in,out] info Tensor info used to check and assign.
607 * @param[in] fixed_point_position New fixed point position
608 *
609 * @return True if the fixed point position has been changed.
610 */
611bool set_fixed_point_position_if_zero(ITensorInfo &info, int fixed_point_position);
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000612
Alex Gildayc357c472018-03-21 13:54:09 +0000613/** Set the quantization info to the specified value if
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000614 * the current quantization info is empty and the data type of asymmetric quantized type
615 *
616 * @param[in,out] info Tensor info used to check and assign.
617 * @param[in] quantization_info Quantization info
618 *
619 * @return True if the quantization info has been changed.
620 */
621bool set_quantization_info_if_empty(ITensorInfo &info, QuantizationInfo quantization_info);
622
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100623/** Helper function to calculate the Valid Region for Scale.
624 *
Georgios Pinitas6727f122018-03-13 10:29:13 +0000625 * @param[in] src_info Input tensor info used to check.
626 * @param[in] dst_shape Shape of the output.
627 * @param[in] policy Interpolation policy.
628 * @param[in] border_size Size of the border.
629 * @param[in] border_undefined True if the border is undefined.
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100630 *
Georgios Pinitas6727f122018-03-13 10:29:13 +0000631 * @return The corrispondent valid region
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100632 */
Georgios Pinitas6727f122018-03-13 10:29:13 +0000633ValidRegion calculate_valid_region_scale(const ITensorInfo &src_info, const TensorShape &dst_shape, InterpolationPolicy policy, BorderSize border_size, bool border_undefined);
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000634
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100635/** Convert a linear index into n-dimensional coordinates.
636 *
637 * @param[in] shape Shape of the n-dimensional tensor.
638 * @param[in] index Linear index specifying the i-th element.
639 *
640 * @return n-dimensional coordinates.
641 */
642inline Coordinates index2coords(const TensorShape &shape, int index);
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000643
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100644/** Convert n-dimensional coordinates into a linear index.
645 *
646 * @param[in] shape Shape of the n-dimensional tensor.
647 * @param[in] coord N-dimensional coordinates.
648 *
649 * @return linead index
650 */
651inline int coords2index(const TensorShape &shape, const Coordinates &coord);
Isabella Gottardid17a6772018-02-27 17:41:55 +0000652
Alex Gildayc357c472018-03-21 13:54:09 +0000653/** Get the index of the given dimension.
Isabella Gottardid17a6772018-02-27 17:41:55 +0000654 *
Alex Gildayc357c472018-03-21 13:54:09 +0000655 * @param[in] data_layout The data layout.
656 * @param[in] data_layout_dimension The dimension which this index is requested for.
Isabella Gottardid17a6772018-02-27 17:41:55 +0000657 *
658 * @return The int conversion of the requested data layout index.
659 */
Isabella Gottardid56e7702018-02-28 14:29:36 +0000660inline size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100661} // namespace arm_compute
662
663#include "arm_compute/core/Helpers.inl"
664#endif /*__ARM_COMPUTE_HELPERS_H__ */