Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 36 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | #include <array> |
| 38 | #include <cstddef> |
| 39 | #include <cstdint> |
| 40 | #include <memory> |
| 41 | #include <tuple> |
| 42 | #include <type_traits> |
| 43 | #include <utility> |
| 44 | |
| 45 | namespace arm_compute |
| 46 | { |
| 47 | class IKernel; |
| 48 | class ITensor; |
| 49 | class ITensorInfo; |
| 50 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 51 | /** Disable bitwise operations by default */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 52 | template <typename T> |
| 53 | struct enable_bitwise_ops |
| 54 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 55 | static constexpr bool value = false; /**< Disabled */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 56 | }; |
| 57 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 58 | #ifndef DOXYGEN_SKIP_THIS |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 59 | template <typename T> |
| 60 | typename 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 Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 65 | #endif /* DOXYGEN_SKIP_THIS */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 66 | |
| 67 | namespace traits |
| 68 | { |
| 69 | /** Check if a type T is contained in a tuple Tuple of types */ |
| 70 | template <typename T, typename Tuple> |
| 71 | struct is_contained; |
| 72 | |
| 73 | template <typename T> |
| 74 | struct is_contained<T, std::tuple<>> : std::false_type |
| 75 | { |
| 76 | }; |
| 77 | |
| 78 | template <typename T, typename... Ts> |
| 79 | struct is_contained<T, std::tuple<T, Ts...>> : std::true_type |
| 80 | { |
| 81 | }; |
| 82 | |
| 83 | template <typename T, typename U, typename... Ts> |
| 84 | struct 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 Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 90 | * 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] | 91 | * |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 92 | * @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] | 93 | * @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 Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 101 | template <typename T> |
| 102 | inline 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 105 | |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 106 | 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 Barbier | 9a33b54 | 2017-12-12 22:08:59 +0000 | [diff] [blame] | 122 | /** 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 | */ |
| 133 | template <typename T> |
| 134 | inline 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 | */ |
| 158 | template <typename T> |
| 159 | inline 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 Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 173 | /** Return the pixel at (x,y) using bilinear interpolation. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 174 | * |
| 175 | * @warning Only works if the iterator was created with an IImage |
| 176 | * |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 177 | * @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] | 178 | * @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 Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 184 | template <typename T> |
| 185 | inline 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 188 | |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 189 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 199 | * |
| 200 | * @warning Only works if the iterator was created with an IImage |
| 201 | * |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 202 | * @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] | 203 | * @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 Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 211 | template <typename T> |
| 212 | 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) |
| 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 Barbier | 9a33b54 | 2017-12-12 22:08:59 +0000 | [diff] [blame] | 225 | 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 Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 237 | return delta_bilinear_c1(first_pixel_ptr + static_cast<int32_t>(xi) + static_cast<int32_t>(yi) * stride, stride, dx, dy); |
| 238 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 239 | |
| 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 | */ |
| 256 | 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); |
| 257 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 258 | /** Iterator updated by @ref execute_window_loop for each window element */ |
| 259 | class Iterator |
| 260 | { |
| 261 | public: |
| 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 | |
| 301 | private: |
| 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 | */ |
| 327 | template <typename L, typename... Ts> |
| 328 | inline 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 | */ |
| 342 | template <typename... Ts> |
| 343 | bool update_window_and_padding(Window &win, Ts &&... patterns) |
| 344 | { |
| 345 | bool window_changed = false; |
| 346 | |
Diego Lopez Recas | 490b3d8 | 2017-12-19 15:42:25 +0000 | [diff] [blame] | 347 | utility::for_each([&](const IAccessWindow & w) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 348 | { |
| 349 | window_changed |= w.update_window_if_needed(win); |
| 350 | }, |
| 351 | patterns...); |
| 352 | |
| 353 | bool padding_changed = false; |
| 354 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 355 | utility::for_each([&](IAccessWindow & w) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 356 | { |
| 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 Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 366 | * @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 | */ |
| 373 | Window 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 377 | * @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 Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 384 | inline 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 | */ |
| 398 | Window calculate_max_window_horizontal(const ValidRegion &valid_region, const Steps &steps = Steps(), bool skip_border = false, BorderSize border_size = BorderSize()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 399 | |
| 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 Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 405 | * @param[in] border_size (Optional) Border size. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 406 | * |
| 407 | * @return The maximum window the kernel can be executed on. |
| 408 | */ |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 409 | inline 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 | */ |
| 422 | Window calculate_max_enlarged_window(const ValidRegion &valid_region, const Steps &steps = Steps(), BorderSize border_size = BorderSize()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 423 | |
| 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 Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 432 | inline 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 436 | |
| 437 | /** Intersect multiple valid regions. |
| 438 | * |
| 439 | * @param[in] regions Valid regions. |
| 440 | * |
| 441 | * @return Intersection of all regions. |
| 442 | */ |
| 443 | template <typename... Ts> |
Diego Lopez Recas | 490b3d8 | 2017-12-19 15:42:25 +0000 | [diff] [blame] | 444 | ValidRegion intersect_valid_regions(const Ts &... regions) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 445 | { |
| 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 Recas | 490b3d8 | 2017-12-19 15:42:25 +0000 | [diff] [blame] | 463 | return utility::foldl(intersect, regions...); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 464 | } |
| 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 | */ |
| 475 | template <typename T, typename... Ts> |
| 476 | inline 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 | */ |
| 497 | template <typename... Ts> |
| 498 | inline Strides compute_strides(const ITensorInfo &info) |
| 499 | { |
| 500 | return compute_strides(info, info.element_size()); |
| 501 | } |
| 502 | |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 503 | /** 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 | */ |
| 510 | template <typename T> |
| 511 | inline void permute(Dimensions<T> &dimensions, const PermutationVector &perm) |
| 512 | { |
Georgios Pinitas | 69af6cf | 2018-02-14 19:23:44 +0000 | [diff] [blame] | 513 | auto dimensions_copy = utility::make_array<Dimensions<T>::num_max_dimensions>(dimensions.begin(), dimensions.end()); |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 514 | for(unsigned int i = 0; i < perm.num_dimensions(); ++i) |
| 515 | { |
Georgios Pinitas | 69af6cf | 2018-02-14 19:23:44 +0000 | [diff] [blame] | 516 | 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 | */ |
| 528 | inline 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 Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 538 | /** Auto initialize the tensor info (shape, number of channels, data type and fixed point position) if the current assignment is empty. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 539 | * |
| 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 Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 545 | * @param[in] quantization_info (Optional) New quantization info |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 546 | * |
| 547 | * @return True if the tensor info has been initialized |
| 548 | */ |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 549 | bool 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 554 | |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 555 | /** 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 Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 562 | bool auto_init_if_empty(ITensorInfo &info_sink, const ITensorInfo &info_source); |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 563 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 564 | /** Set the shape to the specified value if the current assignment is empty. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 565 | * |
| 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 | */ |
| 571 | bool set_shape_if_empty(ITensorInfo &info, const TensorShape &shape); |
| 572 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 573 | /** Set the format, data type and number of channels to the specified value if |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 574 | * 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 | */ |
| 581 | bool set_format_if_unknown(ITensorInfo &info, Format format); |
| 582 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 583 | /** Set the data type and number of channels to the specified value if |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 584 | * 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 | */ |
| 591 | bool set_data_type_if_unknown(ITensorInfo &info, DataType data_type); |
| 592 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 593 | /** Set the data layout to the specified value if |
Isabella Gottardi | d17a677 | 2018-02-27 17:41:55 +0000 | [diff] [blame] | 594 | * 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 | */ |
| 601 | bool set_data_layout_if_unknown(ITensorInfo &info, DataLayout data_layout); |
| 602 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 603 | /** Set the fixed point position to the specified value if |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 604 | * 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 | */ |
| 611 | 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] | 612 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 613 | /** Set the quantization info to the specified value if |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 614 | * 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 | */ |
| 621 | bool set_quantization_info_if_empty(ITensorInfo &info, QuantizationInfo quantization_info); |
| 622 | |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 623 | /** Helper function to calculate the Valid Region for Scale. |
| 624 | * |
Diego Lopez Recas | 0085429 | 2018-02-22 13:08:01 +0000 | [diff] [blame] | 625 | * @param[in] src_info Input tensor info used to check. |
| 626 | * @param[in] dst_shape Shape of the output. |
| 627 | * @param[in] interpolate_policy Interpolation policy. |
| 628 | * @param[in] sampling_policy Sampling policy. |
| 629 | * @param[in] border_undefined True if the border is undefined. |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 630 | * |
Diego Lopez Recas | 0085429 | 2018-02-22 13:08:01 +0000 | [diff] [blame] | 631 | * @return The corresponding valid region |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 632 | */ |
Diego Lopez Recas | 0085429 | 2018-02-22 13:08:01 +0000 | [diff] [blame] | 633 | ValidRegion calculate_valid_region_scale(const ITensorInfo &src_info, const TensorShape &dst_shape, |
| 634 | InterpolationPolicy interpolate_policy, SamplingPolicy sampling_policy, bool border_undefined); |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 635 | |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 636 | /** Convert a linear index into n-dimensional coordinates. |
| 637 | * |
| 638 | * @param[in] shape Shape of the n-dimensional tensor. |
| 639 | * @param[in] index Linear index specifying the i-th element. |
| 640 | * |
| 641 | * @return n-dimensional coordinates. |
| 642 | */ |
| 643 | inline Coordinates index2coords(const TensorShape &shape, int index); |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 644 | |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 645 | /** Convert n-dimensional coordinates into a linear index. |
| 646 | * |
| 647 | * @param[in] shape Shape of the n-dimensional tensor. |
| 648 | * @param[in] coord N-dimensional coordinates. |
| 649 | * |
| 650 | * @return linead index |
| 651 | */ |
| 652 | inline int coords2index(const TensorShape &shape, const Coordinates &coord); |
Isabella Gottardi | d17a677 | 2018-02-27 17:41:55 +0000 | [diff] [blame] | 653 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 654 | /** Get the index of the given dimension. |
Isabella Gottardi | d17a677 | 2018-02-27 17:41:55 +0000 | [diff] [blame] | 655 | * |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 656 | * @param[in] data_layout The data layout. |
| 657 | * @param[in] data_layout_dimension The dimension which this index is requested for. |
Isabella Gottardi | d17a677 | 2018-02-27 17:41:55 +0000 | [diff] [blame] | 658 | * |
| 659 | * @return The int conversion of the requested data layout index. |
| 660 | */ |
Isabella Gottardi | d56e770 | 2018-02-28 14:29:36 +0000 | [diff] [blame] | 661 | inline size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 662 | } // namespace arm_compute |
| 663 | |
| 664 | #include "arm_compute/core/Helpers.inl" |
| 665 | #endif /*__ARM_COMPUTE_HELPERS_H__ */ |