Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, 2017 ARM Limited. |
| 3 | * |
| 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 Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Error.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #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 Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 36 | #include "arm_compute/core/utils/misc/utility.h" |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 37 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | #include <array> |
| 39 | #include <cstddef> |
| 40 | #include <cstdint> |
| 41 | #include <memory> |
| 42 | #include <tuple> |
| 43 | #include <type_traits> |
| 44 | #include <utility> |
| 45 | |
| 46 | namespace arm_compute |
| 47 | { |
| 48 | class IKernel; |
| 49 | class ITensor; |
| 50 | class ITensorInfo; |
| 51 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 52 | template <typename T> |
| 53 | struct enable_bitwise_ops |
| 54 | { |
| 55 | static constexpr bool value = false; |
| 56 | }; |
| 57 | |
| 58 | template <typename T> |
| 59 | typename 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 | } |
| 64 | |
| 65 | namespace traits |
| 66 | { |
| 67 | /** Check if a type T is contained in a tuple Tuple of types */ |
| 68 | template <typename T, typename Tuple> |
| 69 | struct is_contained; |
| 70 | |
| 71 | template <typename T> |
| 72 | struct is_contained<T, std::tuple<>> : std::false_type |
| 73 | { |
| 74 | }; |
| 75 | |
| 76 | template <typename T, typename... Ts> |
| 77 | struct is_contained<T, std::tuple<T, Ts...>> : std::true_type |
| 78 | { |
| 79 | }; |
| 80 | |
| 81 | template <typename T, typename U, typename... Ts> |
| 82 | struct is_contained<T, std::tuple<U, Ts...>> : is_contained<T, std::tuple<Ts...>> |
| 83 | { |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | /** Computes bilinear interpolation using the pointer to the top-left pixel and the pixel's distance between |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 88 | * the real coordinates and the smallest following integer coordinates. Input must be in single channel format. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 89 | * |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 90 | * @param[in] pixel_ptr Pointer to the top-left pixel value of a single channel input. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 91 | * @param[in] stride Stride to access the bottom-left and bottom-right pixel values |
| 92 | * @param[in] dx Pixel's distance between the X real coordinate and the smallest X following integer |
| 93 | * @param[in] dy Pixel's distance between the Y real coordinate and the smallest Y following integer |
| 94 | * |
| 95 | * @note dx and dy must be in the range [0, 1.0] |
| 96 | * |
| 97 | * @return The bilinear interpolated pixel value |
| 98 | */ |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 99 | template <typename T> |
| 100 | inline T delta_bilinear_c1(const T *pixel_ptr, size_t stride, float dx, float dy) |
| 101 | { |
| 102 | ARM_COMPUTE_ERROR_ON(pixel_ptr == nullptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 103 | |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 104 | const float dx1 = 1.0f - dx; |
| 105 | const float dy1 = 1.0f - dy; |
| 106 | |
| 107 | const T a00 = *pixel_ptr; |
| 108 | const T a01 = *(pixel_ptr + 1); |
| 109 | const T a10 = *(pixel_ptr + stride); |
| 110 | const T a11 = *(pixel_ptr + stride + 1); |
| 111 | |
| 112 | const float w1 = dx1 * dy1; |
| 113 | const float w2 = dx * dy1; |
| 114 | const float w3 = dx1 * dy; |
| 115 | const float w4 = dx * dy; |
| 116 | |
| 117 | return static_cast<T>(a00 * w1 + a01 * w2 + a10 * w3 + a11 * w4); |
| 118 | } |
| 119 | |
Anthony Barbier | 9a33b54 | 2017-12-12 22:08:59 +0000 | [diff] [blame] | 120 | /** Computes linear interpolation using the pointer to the top pixel and the pixel's distance between |
| 121 | * the real coordinates and the smallest following integer coordinates. Input must be in single channel format. |
| 122 | * |
| 123 | * @param[in] pixel_ptr Pointer to the top pixel value of a single channel input. |
| 124 | * @param[in] stride Stride to access the bottom pixel value |
| 125 | * @param[in] dy Pixel's distance between the Y real coordinate and the smallest Y following integer |
| 126 | * |
| 127 | * @note dy must be in the range [0, 1.0] |
| 128 | * |
| 129 | * @return The linear interpolated pixel value |
| 130 | */ |
| 131 | template <typename T> |
| 132 | inline T delta_linear_c1_y(const T *pixel_ptr, size_t stride, float dy) |
| 133 | { |
| 134 | ARM_COMPUTE_ERROR_ON(pixel_ptr == nullptr); |
| 135 | |
| 136 | const float dy1 = 1.0f - dy; |
| 137 | |
| 138 | const T a00 = *pixel_ptr; |
| 139 | const T a10 = *(pixel_ptr + stride); |
| 140 | |
| 141 | const float w1 = dy1; |
| 142 | const float w3 = dy; |
| 143 | |
| 144 | return static_cast<T>(a00 * w1 + a10 * w3); |
| 145 | } |
| 146 | /** Computes linear interpolation using the pointer to the left pixel and the pixel's distance between |
| 147 | * the real coordinates and the smallest following integer coordinates. Input must be in single channel format. |
| 148 | * |
| 149 | * @param[in] pixel_ptr Pointer to the left pixel value of a single channel input. |
| 150 | * @param[in] dx Pixel's distance between the X real coordinate and the smallest X following integer |
| 151 | * |
| 152 | * @note dx must be in the range [0, 1.0] |
| 153 | * |
| 154 | * @return The linear interpolated pixel value |
| 155 | */ |
| 156 | template <typename T> |
| 157 | inline T delta_linear_c1_x(const T *pixel_ptr, float dx) |
| 158 | { |
| 159 | ARM_COMPUTE_ERROR_ON(pixel_ptr == nullptr); |
| 160 | |
| 161 | const T a00 = *pixel_ptr; |
| 162 | const T a01 = *(pixel_ptr + 1); |
| 163 | |
| 164 | const float dx1 = 1.0f - dx; |
| 165 | |
| 166 | const float w1 = dx1; |
| 167 | const float w2 = dx; |
| 168 | |
| 169 | return static_cast<T>(a00 * w1 + a01 * w2); |
| 170 | } |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 171 | /** Return the pixel at (x,y) using bilinear interpolation. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 172 | * |
| 173 | * @warning Only works if the iterator was created with an IImage |
| 174 | * |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 175 | * @param[in] first_pixel_ptr Pointer to the first pixel of a single channel input. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 176 | * @param[in] stride Stride in bytes of the image; |
| 177 | * @param[in] x X position of the wanted pixel |
| 178 | * @param[in] y Y position of the wanted pixel |
| 179 | * |
| 180 | * @return The pixel at (x, y) using bilinear interpolation. |
| 181 | */ |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 182 | template <typename T> |
| 183 | inline T pixel_bilinear_c1(const T *first_pixel_ptr, size_t stride, float x, float y) |
| 184 | { |
| 185 | ARM_COMPUTE_ERROR_ON(first_pixel_ptr == nullptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 186 | |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 187 | const int32_t xi = std::floor(x); |
| 188 | const int32_t yi = std::floor(y); |
| 189 | |
| 190 | const float dx = x - xi; |
| 191 | const float dy = y - yi; |
| 192 | |
| 193 | return delta_bilinear_c1(first_pixel_ptr + xi + yi * stride, stride, dx, dy); |
| 194 | } |
| 195 | |
| 196 | /** Return the pixel at (x,y) using bilinear interpolation by clamping when out of borders. The image must be single channel input |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 197 | * |
| 198 | * @warning Only works if the iterator was created with an IImage |
| 199 | * |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 200 | * @param[in] first_pixel_ptr Pointer to the first pixel of a single channel image. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 201 | * @param[in] stride Stride in bytes of the image |
| 202 | * @param[in] width Width of the image |
| 203 | * @param[in] height Height of the image |
| 204 | * @param[in] x X position of the wanted pixel |
| 205 | * @param[in] y Y position of the wanted pixel |
| 206 | * |
| 207 | * @return The pixel at (x, y) using bilinear interpolation. |
| 208 | */ |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 209 | template <typename T> |
| 210 | inline uint8_t pixel_bilinear_c1_clamp(const T *first_pixel_ptr, size_t stride, size_t width, size_t height, float x, float y) |
| 211 | { |
| 212 | ARM_COMPUTE_ERROR_ON(first_pixel_ptr == nullptr); |
| 213 | |
| 214 | x = std::max(-1.f, std::min(x, static_cast<float>(width))); |
| 215 | y = std::max(-1.f, std::min(y, static_cast<float>(height))); |
| 216 | |
| 217 | const float xi = std::floor(x); |
| 218 | const float yi = std::floor(y); |
| 219 | |
| 220 | const float dx = x - xi; |
| 221 | const float dy = y - yi; |
| 222 | |
Anthony Barbier | 9a33b54 | 2017-12-12 22:08:59 +0000 | [diff] [blame] | 223 | if(dx == 0.0f) |
| 224 | { |
| 225 | if(dy == 0.0f) |
| 226 | { |
| 227 | return static_cast<T>(first_pixel_ptr[static_cast<int32_t>(xi) + static_cast<int32_t>(yi) * stride]); |
| 228 | } |
| 229 | return delta_linear_c1_y(first_pixel_ptr + static_cast<int32_t>(xi) + static_cast<int32_t>(yi) * stride, stride, dy); |
| 230 | } |
| 231 | if(dy == 0.0f) |
| 232 | { |
| 233 | return delta_linear_c1_x(first_pixel_ptr + static_cast<int32_t>(xi) + static_cast<int32_t>(yi) * stride, dx); |
| 234 | } |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 235 | return delta_bilinear_c1(first_pixel_ptr + static_cast<int32_t>(xi) + static_cast<int32_t>(yi) * stride, stride, dx, dy); |
| 236 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 237 | |
| 238 | /** Return the pixel at (x,y) using area interpolation by clamping when out of borders. The image must be single channel U8 |
| 239 | * |
| 240 | * @note The interpolation area depends on the width and height ration of the input and output images |
| 241 | * @note Currently average of the contributing pixels is calculated |
| 242 | * |
| 243 | * @param[in] first_pixel_ptr Pointer to the first pixel of a single channel U8 image. |
| 244 | * @param[in] stride Stride in bytes of the image |
| 245 | * @param[in] width Width of the image |
| 246 | * @param[in] height Height of the image |
| 247 | * @param[in] wr Width ratio among the input image width and output image width. |
| 248 | * @param[in] hr Height ratio among the input image height and output image height. |
| 249 | * @param[in] x X position of the wanted pixel |
| 250 | * @param[in] y Y position of the wanted pixel |
| 251 | * |
| 252 | * @return The pixel at (x, y) using area interpolation. |
| 253 | */ |
| 254 | inline 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); |
| 255 | |
| 256 | /** Performs clamping among a lower and upper value. |
| 257 | * |
| 258 | * @param[in] n Value to clamp. |
| 259 | * @param[in] lower Lower threshold. |
| 260 | * @param[in] upper Upper threshold. |
| 261 | * |
| 262 | * @return Clamped value. |
| 263 | */ |
| 264 | template <typename T> |
| 265 | inline T clamp(const T &n, const T &lower, const T &upper) |
| 266 | { |
| 267 | return std::max(lower, std::min(n, upper)); |
| 268 | } |
| 269 | |
| 270 | /** Base case of for_each. Does nothing. */ |
| 271 | template <typename F> |
| 272 | inline void for_each(F &&) |
| 273 | { |
| 274 | } |
| 275 | |
| 276 | /** Call the function for each of the arguments |
| 277 | * |
| 278 | * @param[in] func Function to be called |
| 279 | * @param[in] arg Argument passed to the function |
| 280 | * @param[in] args Remaining arguments |
| 281 | */ |
| 282 | template <typename F, typename T, typename... Ts> |
| 283 | inline void for_each(F &&func, T &&arg, Ts &&... args) |
| 284 | { |
| 285 | func(arg); |
| 286 | for_each(func, args...); |
| 287 | } |
| 288 | |
| 289 | /** Base case of foldl. |
| 290 | * |
| 291 | * @return value. |
| 292 | */ |
| 293 | template <typename F, typename T> |
| 294 | inline T foldl(F &&, const T &value) |
| 295 | { |
| 296 | return value; |
| 297 | } |
| 298 | |
| 299 | /** Base case of foldl. |
| 300 | * |
| 301 | * @return Function evaluation for value1 and value2 |
| 302 | */ |
| 303 | template <typename F, typename T, typename U> |
| 304 | inline auto foldl(F &&func, T &&value1, U &&value2) -> decltype(func(value1, value2)) |
| 305 | { |
| 306 | return func(value1, value2); |
| 307 | } |
| 308 | |
| 309 | /** Fold left. |
| 310 | * |
| 311 | * @param[in] func Function to be called |
| 312 | * @param[in] initial Initial value |
| 313 | * @param[in] value Argument passed to the function |
| 314 | * @param[in] values Remaining arguments |
| 315 | */ |
| 316 | template <typename F, typename I, typename T, typename... Vs> |
| 317 | inline I foldl(F &&func, I &&initial, T &&value, Vs &&... values) |
| 318 | { |
| 319 | return foldl(std::forward<F>(func), func(std::forward<I>(initial), std::forward<T>(value)), std::forward<Vs>(values)...); |
| 320 | } |
| 321 | |
| 322 | /** Iterator updated by @ref execute_window_loop for each window element */ |
| 323 | class Iterator |
| 324 | { |
| 325 | public: |
| 326 | /** Default constructor to create an empty iterator */ |
| 327 | constexpr Iterator(); |
| 328 | /** Create a container iterator for the metadata and allocation contained in the ITensor |
| 329 | * |
| 330 | * @param[in] tensor The tensor to associate to the iterator. |
| 331 | * @param[in] window The window which will be used to iterate over the tensor. |
| 332 | */ |
| 333 | Iterator(const ITensor *tensor, const Window &window); |
| 334 | |
| 335 | /** Increment the iterator along the specified dimension of the step value associated to the dimension. |
| 336 | * |
| 337 | * @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. |
| 338 | * |
| 339 | * @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. |
| 340 | * |
| 341 | * @param[in] dimension Dimension to increment |
| 342 | */ |
| 343 | void increment(size_t dimension); |
| 344 | |
| 345 | /** Return the offset in bytes from the first element to the current position of the iterator |
| 346 | * |
| 347 | * @return The current position of the iterator in bytes relative to the first element. |
| 348 | */ |
| 349 | constexpr int offset() const; |
| 350 | |
| 351 | /** Return a pointer to the current pixel. |
| 352 | * |
| 353 | * @warning Only works if the iterator was created with an ITensor. |
| 354 | * |
| 355 | * @return equivalent to buffer() + offset() |
| 356 | */ |
| 357 | constexpr uint8_t *ptr() const; |
| 358 | |
| 359 | /** Move the iterator back to the beginning of the specified dimension. |
| 360 | * |
| 361 | * @param[in] dimension Dimension to reset |
| 362 | */ |
| 363 | void reset(size_t dimension); |
| 364 | |
| 365 | private: |
| 366 | uint8_t *_ptr; |
| 367 | |
| 368 | class Dimension |
| 369 | { |
| 370 | public: |
| 371 | constexpr Dimension() |
| 372 | : _dim_start(0), _stride(0) |
| 373 | { |
| 374 | } |
| 375 | |
| 376 | int _dim_start; |
| 377 | int _stride; |
| 378 | }; |
| 379 | |
| 380 | std::array<Dimension, Coordinates::num_max_dimensions> _dims; |
| 381 | }; |
| 382 | |
| 383 | /** Iterate through the passed window, automatically adjusting the iterators and calling the lambda_functino for each element. |
| 384 | * It passes the x and y positions to the lambda_function for each iteration |
| 385 | * |
| 386 | * @param[in] w Window to iterate through. |
| 387 | * @param[in] lambda_function The function of type void(function)( const Coordinates & id ) to call at each iteration. |
| 388 | * Where id represents the absolute coordinates of the item to process. |
| 389 | * @param[in,out] iterators Tensor iterators which will be updated by this function before calling lambda_function. |
| 390 | */ |
| 391 | template <typename L, typename... Ts> |
| 392 | inline void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators); |
| 393 | |
| 394 | /** Update window and padding size for each of the access patterns. |
| 395 | * |
| 396 | * First the window size is reduced based on all access patterns that are not |
| 397 | * allowed to modify the padding of the underlying tensor. Then the padding of |
| 398 | * the remaining tensors is increased to match the window. |
| 399 | * |
| 400 | * @param[in] win Window that is used by the kernel. |
| 401 | * @param[in] patterns Access patterns used to calculate the final window and padding. |
| 402 | * |
| 403 | * @return True if the window has been changed. Changes to the padding do not |
| 404 | * influence the returned value. |
| 405 | */ |
| 406 | template <typename... Ts> |
| 407 | bool update_window_and_padding(Window &win, Ts &&... patterns) |
| 408 | { |
| 409 | bool window_changed = false; |
| 410 | |
| 411 | for_each([&](const IAccessWindow & w) |
| 412 | { |
| 413 | window_changed |= w.update_window_if_needed(win); |
| 414 | }, |
| 415 | patterns...); |
| 416 | |
| 417 | bool padding_changed = false; |
| 418 | |
| 419 | for_each([&](const IAccessWindow & w) |
| 420 | { |
| 421 | padding_changed |= w.update_padding_if_needed(win); |
| 422 | }, |
| 423 | patterns...); |
| 424 | |
| 425 | return window_changed; |
| 426 | } |
| 427 | |
| 428 | /** Calculate the maximum window for a given tensor shape and border setting |
| 429 | * |
| 430 | * @param[in] info Tensor info object defining the shape of the object for which the window is created. |
| 431 | * @param[in] steps (Optional) Number of elements processed for each step. |
| 432 | * @param[in] skip_border (Optional) If true exclude the border region from the window. |
| 433 | * @param[in] border_size (Optional) Border size. |
| 434 | * |
| 435 | * @return The maximum window the kernel can be executed on. |
| 436 | */ |
| 437 | Window calculate_max_window(const ITensorInfo &info, const Steps &steps = Steps(), bool skip_border = false, BorderSize border_size = BorderSize()); |
| 438 | |
| 439 | /** Calculate the maximum window used by a horizontal kernel for a given tensor shape and border setting |
| 440 | * |
| 441 | * @param[in] info Tensor info object defining the shape of the object for which the window is created. |
| 442 | * @param[in] steps (Optional) Number of elements processed for each step. |
| 443 | * @param[in] skip_border (Optional) If true exclude the border region from the window. |
| 444 | * @param[in] border_size (Optional) Border size. The border region will be excluded from the window. |
| 445 | * |
| 446 | * @return The maximum window the kernel can be executed on. |
| 447 | */ |
| 448 | Window calculate_max_window_horizontal(const ITensorInfo &info, const Steps &steps = Steps(), bool skip_border = false, BorderSize border_size = BorderSize()); |
| 449 | |
| 450 | /** Calculate the maximum window for a given tensor shape and border setting. The window will also includes the border. |
| 451 | * |
| 452 | * @param[in] info Tensor info object defining the shape of the object for which the window is created. |
| 453 | * @param[in] steps (Optional) Number of elements processed for each step. |
| 454 | * @param[in] border_size (Optional) Border size. The border region will be included in the window. |
| 455 | * |
| 456 | * @return The maximum window the kernel can be executed on. |
| 457 | */ |
| 458 | Window calculate_max_enlarged_window(const ITensorInfo &info, const Steps &steps = Steps(), BorderSize border_size = BorderSize()); |
| 459 | |
| 460 | /** Intersect multiple valid regions. |
| 461 | * |
| 462 | * @param[in] regions Valid regions. |
| 463 | * |
| 464 | * @return Intersection of all regions. |
| 465 | */ |
| 466 | template <typename... Ts> |
| 467 | ValidRegion intersect_valid_regions(Ts &&... regions) |
| 468 | { |
| 469 | auto intersect = [](const ValidRegion & r1, const ValidRegion & r2) -> ValidRegion |
| 470 | { |
| 471 | ValidRegion region; |
| 472 | |
| 473 | for(size_t d = 0; d < std::min(r1.anchor.num_dimensions(), r2.anchor.num_dimensions()); ++d) |
| 474 | { |
| 475 | region.anchor.set(d, std::max(r1.anchor[d], r2.anchor[d])); |
| 476 | } |
| 477 | |
| 478 | for(size_t d = 0; d < std::min(r1.shape.num_dimensions(), r2.shape.num_dimensions()); ++d) |
| 479 | { |
| 480 | region.shape.set(d, std::min(r1.shape[d], r2.shape[d])); |
| 481 | } |
| 482 | |
| 483 | return region; |
| 484 | }; |
| 485 | |
| 486 | return foldl(intersect, std::forward<Ts>(regions)...); |
| 487 | } |
| 488 | |
| 489 | /** Create a strides object based on the provided strides and the tensor dimensions. |
| 490 | * |
| 491 | * @param[in] info Tensor info object providing the shape of the tensor for unspecified strides. |
| 492 | * @param[in] stride_x Stride to be used in X dimension (in bytes). |
| 493 | * @param[in] fixed_strides Strides to be used in higher dimensions starting at Y (in bytes). |
| 494 | * |
| 495 | * @return Strides object based on the specified strides. Missing strides are |
| 496 | * calculated based on the tensor shape and the strides of lower dimensions. |
| 497 | */ |
| 498 | template <typename T, typename... Ts> |
| 499 | inline Strides compute_strides(const ITensorInfo &info, T stride_x, Ts &&... fixed_strides) |
| 500 | { |
| 501 | const TensorShape &shape = info.tensor_shape(); |
| 502 | |
| 503 | // Create strides object |
| 504 | Strides strides(stride_x, fixed_strides...); |
| 505 | |
| 506 | for(size_t i = 1 + sizeof...(Ts); i < info.num_dimensions(); ++i) |
| 507 | { |
| 508 | strides.set(i, shape[i - 1] * strides[i - 1]); |
| 509 | } |
| 510 | |
| 511 | return strides; |
| 512 | } |
| 513 | |
| 514 | /** Create a strides object based on the tensor dimensions. |
| 515 | * |
| 516 | * @param[in] info Tensor info object used to compute the strides. |
| 517 | * |
| 518 | * @return Strides object based on element size and tensor shape. |
| 519 | */ |
| 520 | template <typename... Ts> |
| 521 | inline Strides compute_strides(const ITensorInfo &info) |
| 522 | { |
| 523 | return compute_strides(info, info.element_size()); |
| 524 | } |
| 525 | |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 526 | /** Permutes given Dimensions according to a permutation vector |
| 527 | * |
| 528 | * @warning Validity of permutation is not checked |
| 529 | * |
| 530 | * @param[in, out] dimensions Dimensions to permute |
| 531 | * @param[in] perm Permutation vector |
| 532 | */ |
| 533 | template <typename T> |
| 534 | inline void permute(Dimensions<T> &dimensions, const PermutationVector &perm) |
| 535 | { |
| 536 | auto copy_dimensions = utility::make_array<Dimensions<T>::num_max_dimensions>(dimensions.begin(), dimensions.end()); |
| 537 | for(unsigned int i = 0; i < perm.num_dimensions(); ++i) |
| 538 | { |
| 539 | dimensions[i] = copy_dimensions[perm[i]]; |
| 540 | } |
| 541 | } |
| 542 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 543 | /* Auto initialize the tensor info (shape, number of channels, data type and fixed point position) if the current assignment is empty. |
| 544 | * |
| 545 | * @param[in,out] info Tensor info used to check and assign. |
| 546 | * @param[in] shape New shape. |
| 547 | * @param[in] num_channels New number of channels. |
| 548 | * @param[in] data_type New data type |
| 549 | * @param[in] fixed_point_position New fixed point position |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 550 | * @param[in] quantization_info (Optional) New quantization info |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 551 | * |
| 552 | * @return True if the tensor info has been initialized |
| 553 | */ |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 554 | bool auto_init_if_empty(ITensorInfo &info, |
| 555 | const TensorShape &shape, |
| 556 | int num_channels, DataType data_type, |
| 557 | int fixed_point_position, |
| 558 | QuantizationInfo quantization_info = QuantizationInfo()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 559 | |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 560 | /** Auto initialize the tensor info using another tensor info. |
| 561 | * |
| 562 | * @param info_sink Tensor info used to check and assign |
| 563 | * @param info_source Tensor info used to assign |
| 564 | * |
| 565 | * @return True if the tensor info has been initialized |
| 566 | */ |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 567 | bool auto_init_if_empty(ITensorInfo &info_sink, const ITensorInfo &info_source); |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 568 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 569 | /* Set the shape to the specified value if the current assignment is empty. |
| 570 | * |
| 571 | * @param[in,out] info Tensor info used to check and assign. |
| 572 | * @param[in] shape New shape. |
| 573 | * |
| 574 | * @return True if the shape has been changed. |
| 575 | */ |
| 576 | bool set_shape_if_empty(ITensorInfo &info, const TensorShape &shape); |
| 577 | |
| 578 | /* Set the format, data type and number of channels to the specified value if |
| 579 | * the current data type is unknown. |
| 580 | * |
| 581 | * @param[in,out] info Tensor info used to check and assign. |
| 582 | * @param[in] format New format. |
| 583 | * |
| 584 | * @return True if the format has been changed. |
| 585 | */ |
| 586 | bool set_format_if_unknown(ITensorInfo &info, Format format); |
| 587 | |
| 588 | /* Set the data type and number of channels to the specified value if |
| 589 | * the current data type is unknown. |
| 590 | * |
| 591 | * @param[in,out] info Tensor info used to check and assign. |
| 592 | * @param[in] data_type New data type. |
| 593 | * |
| 594 | * @return True if the data type has been changed. |
| 595 | */ |
| 596 | bool set_data_type_if_unknown(ITensorInfo &info, DataType data_type); |
| 597 | |
| 598 | /* Set the fixed point position to the specified value if |
| 599 | * the current fixed point position is 0 and the data type is QS8 or QS16 |
| 600 | * |
| 601 | * @param[in,out] info Tensor info used to check and assign. |
| 602 | * @param[in] fixed_point_position New fixed point position |
| 603 | * |
| 604 | * @return True if the fixed point position has been changed. |
| 605 | */ |
| 606 | bool set_fixed_point_position_if_zero(ITensorInfo &info, int fixed_point_position); |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 607 | |
| 608 | /* Set the quantization info to the specified value if |
| 609 | * the current quantization info is empty and the data type of asymmetric quantized type |
| 610 | * |
| 611 | * @param[in,out] info Tensor info used to check and assign. |
| 612 | * @param[in] quantization_info Quantization info |
| 613 | * |
| 614 | * @return True if the quantization info has been changed. |
| 615 | */ |
| 616 | bool set_quantization_info_if_empty(ITensorInfo &info, QuantizationInfo quantization_info); |
| 617 | |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 618 | /** Helper function to calculate the Valid Region for Scale. |
| 619 | * |
| 620 | * @param[in] src_info Input tensor info used to check. |
| 621 | * @param[in] dst_shape Shape of the output. |
| 622 | * @param[in] policy Interpolation policy. |
| 623 | * @param[in] border_size Size of the border. |
| 624 | * @param[in] border_undefined True if the border is undefined. |
| 625 | * |
| 626 | * @return The corrispondent valid region |
| 627 | */ |
| 628 | ValidRegion calculate_valid_region_scale(const ITensorInfo &src_info, const TensorShape &dst_shape, InterpolationPolicy policy, BorderSize border_size, bool border_undefined); |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 629 | |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 630 | /** Convert a linear index into n-dimensional coordinates. |
| 631 | * |
| 632 | * @param[in] shape Shape of the n-dimensional tensor. |
| 633 | * @param[in] index Linear index specifying the i-th element. |
| 634 | * |
| 635 | * @return n-dimensional coordinates. |
| 636 | */ |
| 637 | inline Coordinates index2coords(const TensorShape &shape, int index); |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 638 | |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 639 | /** Convert n-dimensional coordinates into a linear index. |
| 640 | * |
| 641 | * @param[in] shape Shape of the n-dimensional tensor. |
| 642 | * @param[in] coord N-dimensional coordinates. |
| 643 | * |
| 644 | * @return linead index |
| 645 | */ |
| 646 | inline int coords2index(const TensorShape &shape, const Coordinates &coord); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 647 | } // namespace arm_compute |
| 648 | |
| 649 | #include "arm_compute/core/Helpers.inl" |
| 650 | #endif /*__ARM_COMPUTE_HELPERS_H__ */ |