Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2021, 2023 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_HELPERS_H |
| 25 | #define ARM_COMPUTE_HELPERS_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Error.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/IAccessWindow.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/ITensor.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/Types.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/Validate.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/Window.h" |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 33 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | #include <array> |
| 35 | #include <cstddef> |
| 36 | #include <cstdint> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | #include <tuple> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | class IKernel; |
| 42 | class ITensor; |
| 43 | class ITensorInfo; |
| 44 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 45 | /** Iterator updated by @ref execute_window_loop for each window element */ |
| 46 | class Iterator |
| 47 | { |
| 48 | public: |
| 49 | /** Default constructor to create an empty iterator */ |
| 50 | constexpr Iterator(); |
| 51 | /** Create a container iterator for the metadata and allocation contained in the ITensor |
| 52 | * |
| 53 | * @param[in] tensor The tensor to associate to the iterator. |
| 54 | * @param[in] window The window which will be used to iterate over the tensor. |
| 55 | */ |
| 56 | Iterator(const ITensor *tensor, const Window &window); |
| 57 | |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 58 | /** Create a container iterator for the tensor with the specified number of dimensions, stride, buffer pointer and window. |
| 59 | * |
| 60 | * @param[in] num_dims The number of dimensions. |
| 61 | * @param[in] strides The strides in bytes. |
| 62 | * @param[in] buffer The data buffer. |
| 63 | * @param[in] offset The offset in bytes from the beginning of the buffer to the first element of the tensor. |
| 64 | * @param[in] window The window which will be used to iterate over the tensor. |
| 65 | */ |
| 66 | Iterator(size_t num_dims, const Strides &strides, uint8_t *buffer, size_t offset, const Window &window); |
| 67 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 68 | /** Increment the iterator along the specified dimension of the step value associated to the dimension. |
| 69 | * |
| 70 | * @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. |
| 71 | * |
| 72 | * @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. |
| 73 | * |
| 74 | * @param[in] dimension Dimension to increment |
| 75 | */ |
| 76 | void increment(size_t dimension); |
| 77 | |
| 78 | /** Return the offset in bytes from the first element to the current position of the iterator |
| 79 | * |
| 80 | * @return The current position of the iterator in bytes relative to the first element. |
| 81 | */ |
Sheri Zhang | a3e6b6d | 2020-08-18 10:07:35 +0100 | [diff] [blame] | 82 | constexpr size_t offset() const; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 83 | |
| 84 | /** Return a pointer to the current pixel. |
| 85 | * |
| 86 | * @warning Only works if the iterator was created with an ITensor. |
| 87 | * |
| 88 | * @return equivalent to buffer() + offset() |
| 89 | */ |
| 90 | constexpr uint8_t *ptr() const; |
| 91 | |
| 92 | /** Move the iterator back to the beginning of the specified dimension. |
| 93 | * |
| 94 | * @param[in] dimension Dimension to reset |
| 95 | */ |
| 96 | void reset(size_t dimension); |
| 97 | |
| 98 | private: |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 99 | /** Initialize a container iterator for the tensor with the specified number of dimensions, stride, buffer pointer and window. |
| 100 | * |
| 101 | * @param[in] num_dims The number of dimensions. |
| 102 | * @param[in] strides The strides in bytes. |
| 103 | * @param[in] buffer The data buffer. |
| 104 | * @param[in] offset The offset in bytes from the beginning of the buffer to the first element of the tensor. |
| 105 | * @param[in] window The window which will be used to iterate over the tensor. |
| 106 | */ |
| 107 | void initialize(size_t num_dims, const Strides &strides, uint8_t *buffer, size_t offset, const Window &window); |
| 108 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 109 | uint8_t *_ptr; |
| 110 | |
| 111 | class Dimension |
| 112 | { |
| 113 | public: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 114 | constexpr Dimension() : _dim_start(0), _stride(0) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 115 | { |
| 116 | } |
| 117 | |
Sheri Zhang | a3e6b6d | 2020-08-18 10:07:35 +0100 | [diff] [blame] | 118 | size_t _dim_start; |
| 119 | size_t _stride; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | std::array<Dimension, Coordinates::num_max_dimensions> _dims; |
| 123 | }; |
| 124 | |
| 125 | /** Iterate through the passed window, automatically adjusting the iterators and calling the lambda_functino for each element. |
| 126 | * It passes the x and y positions to the lambda_function for each iteration |
| 127 | * |
| 128 | * @param[in] w Window to iterate through. |
| 129 | * @param[in] lambda_function The function of type void(function)( const Coordinates & id ) to call at each iteration. |
| 130 | * Where id represents the absolute coordinates of the item to process. |
| 131 | * @param[in,out] iterators Tensor iterators which will be updated by this function before calling lambda_function. |
| 132 | */ |
| 133 | template <typename L, typename... Ts> |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 134 | inline void execute_window_loop(const Window &w, L &&lambda_function, Ts &&...iterators); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 135 | |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 136 | /** Permutes given Dimensions according to a permutation vector |
| 137 | * |
| 138 | * @warning Validity of permutation is not checked |
| 139 | * |
| 140 | * @param[in, out] dimensions Dimensions to permute |
| 141 | * @param[in] perm Permutation vector |
| 142 | */ |
| 143 | template <typename T> |
| 144 | inline void permute(Dimensions<T> &dimensions, const PermutationVector &perm) |
| 145 | { |
Georgios Pinitas | 69af6cf | 2018-02-14 19:23:44 +0000 | [diff] [blame] | 146 | auto dimensions_copy = utility::make_array<Dimensions<T>::num_max_dimensions>(dimensions.begin(), dimensions.end()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 147 | for (unsigned int i = 0; i < perm.num_dimensions(); ++i) |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 148 | { |
Georgios Pinitas | 69af6cf | 2018-02-14 19:23:44 +0000 | [diff] [blame] | 149 | T dimension_val = (perm[i] < dimensions.num_dimensions()) ? dimensions_copy[perm[i]] : 0; |
| 150 | dimensions.set(i, dimension_val); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** Permutes given TensorShape according to a permutation vector |
| 155 | * |
| 156 | * @warning Validity of permutation is not checked |
| 157 | * |
| 158 | * @param[in, out] shape Shape to permute |
| 159 | * @param[in] perm Permutation vector |
| 160 | */ |
| 161 | inline void permute(TensorShape &shape, const PermutationVector &perm) |
| 162 | { |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 163 | TensorShape shape_copy = shape; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 164 | for (unsigned int i = 0; i < perm.num_dimensions(); ++i) |
Georgios Pinitas | 69af6cf | 2018-02-14 19:23:44 +0000 | [diff] [blame] | 165 | { |
| 166 | size_t dimension_val = (perm[i] < shape.num_dimensions()) ? shape_copy[perm[i]] : 1; |
Giorgio Arena | ec241b4 | 2020-12-11 13:39:02 +0000 | [diff] [blame] | 167 | shape.set(i, dimension_val, false, false); // Avoid changes in _num_dimension |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 171 | /** Helper function to calculate the Valid Region for Scale. |
| 172 | * |
Diego Lopez Recas | 0085429 | 2018-02-22 13:08:01 +0000 | [diff] [blame] | 173 | * @param[in] src_info Input tensor info used to check. |
| 174 | * @param[in] dst_shape Shape of the output. |
| 175 | * @param[in] interpolate_policy Interpolation policy. |
| 176 | * @param[in] sampling_policy Sampling policy. |
| 177 | * @param[in] border_undefined True if the border is undefined. |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 178 | * |
Diego Lopez Recas | 0085429 | 2018-02-22 13:08:01 +0000 | [diff] [blame] | 179 | * @return The corresponding valid region |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 180 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 181 | ValidRegion calculate_valid_region_scale(const ITensorInfo &src_info, |
| 182 | const TensorShape &dst_shape, |
| 183 | InterpolationPolicy interpolate_policy, |
| 184 | SamplingPolicy sampling_policy, |
| 185 | bool border_undefined); |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 186 | |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 187 | /** Convert a linear index into n-dimensional coordinates. |
| 188 | * |
| 189 | * @param[in] shape Shape of the n-dimensional tensor. |
| 190 | * @param[in] index Linear index specifying the i-th element. |
| 191 | * |
| 192 | * @return n-dimensional coordinates. |
| 193 | */ |
| 194 | inline Coordinates index2coords(const TensorShape &shape, int index); |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 195 | |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 196 | /** Convert n-dimensional coordinates into a linear index. |
| 197 | * |
| 198 | * @param[in] shape Shape of the n-dimensional tensor. |
| 199 | * @param[in] coord N-dimensional coordinates. |
| 200 | * |
| 201 | * @return linead index |
| 202 | */ |
| 203 | inline int coords2index(const TensorShape &shape, const Coordinates &coord); |
Isabella Gottardi | d17a677 | 2018-02-27 17:41:55 +0000 | [diff] [blame] | 204 | |
Giorgio Arena | c9fe9fc | 2021-10-06 12:54:29 +0100 | [diff] [blame] | 205 | /** Returns a static map used to find an index or dimension based on a data layout |
| 206 | * |
| 207 | * *** Layouts *** |
| 208 | * |
| 209 | * *** 4D *** |
| 210 | * [N C H W] |
| 211 | * [3 2 1 0] |
| 212 | * [N H W C] |
| 213 | * |
| 214 | * * *** 5D *** |
| 215 | * [N C D H W] |
| 216 | * [4 3 2 1 0] |
| 217 | * [N D H W C] |
| 218 | */ |
| 219 | const std::map<DataLayout, std::vector<DataLayoutDimension>> &get_layout_map(); |
| 220 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 221 | /** Get the index of the given dimension. |
Isabella Gottardi | d17a677 | 2018-02-27 17:41:55 +0000 | [diff] [blame] | 222 | * |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 223 | * @param[in] data_layout The data layout. |
| 224 | * @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] | 225 | * |
| 226 | * @return The int conversion of the requested data layout index. |
| 227 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 228 | inline size_t get_data_layout_dimension_index(const DataLayout &data_layout, |
| 229 | const DataLayoutDimension &data_layout_dimension); |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 230 | |
Usama Arif | 8cf8c11 | 2019-03-14 15:36:54 +0000 | [diff] [blame] | 231 | /** Get the DataLayoutDimension of a given index and layout. |
| 232 | * |
| 233 | * @param[in] data_layout The data layout. |
| 234 | * @param[in] index The data layout index. |
| 235 | * |
| 236 | * @return The dimension which this index is requested for. |
| 237 | */ |
Giorgio Arena | c9fe9fc | 2021-10-06 12:54:29 +0100 | [diff] [blame] | 238 | inline DataLayoutDimension get_index_data_layout_dimension(const DataLayout &data_layout, const size_t index); |
Usama Arif | 8cf8c11 | 2019-03-14 15:36:54 +0000 | [diff] [blame] | 239 | |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 240 | /** Calculate the number of output tiles required by Winograd Convolution layer. This utility function can be used by the Winograd input transform |
| 241 | * to know the number of tiles on the x and y direction |
| 242 | * |
| 243 | * @param[in] in_dims Spatial dimensions of the input tensor of convolution layer |
| 244 | * @param[in] kernel_size Kernel size |
| 245 | * @param[in] output_tile_size Size of a single output tile |
| 246 | * @param[in] conv_info Convolution info (i.e. pad, stride,...) |
| 247 | * |
| 248 | * @return the number of output tiles along the x and y directions of size "output_tile_size" |
| 249 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 250 | inline Size2D compute_winograd_convolution_tiles(const Size2D &in_dims, |
| 251 | const Size2D &kernel_size, |
| 252 | const Size2D &output_tile_size, |
| 253 | const PadStrideInfo &conv_info) |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 254 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 255 | int num_tiles_x = |
| 256 | std::ceil((in_dims.width - (kernel_size.width - 1) + conv_info.pad_left() + conv_info.pad_right()) / |
| 257 | static_cast<float>(output_tile_size.width)); |
| 258 | int num_tiles_y = |
| 259 | std::ceil((in_dims.height - (kernel_size.height - 1) + conv_info.pad_top() + conv_info.pad_bottom()) / |
| 260 | static_cast<float>(output_tile_size.height)); |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 261 | |
| 262 | // Clamp in case we provide paddings but we have 1D convolution |
| 263 | num_tiles_x = std::min(num_tiles_x, static_cast<int>(in_dims.width)); |
| 264 | num_tiles_y = std::min(num_tiles_y, static_cast<int>(in_dims.height)); |
| 265 | |
| 266 | return Size2D(num_tiles_x, num_tiles_y); |
| 267 | } |
| 268 | |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 269 | /** Wrap-around a number within the range 0 <= x < m |
| 270 | * |
| 271 | * @param[in] x Input value |
| 272 | * @param[in] m Range |
| 273 | * |
| 274 | * @return the wrapped-around number |
| 275 | */ |
| 276 | template <typename T> |
| 277 | inline T wrap_around(T x, T m) |
| 278 | { |
| 279 | return x >= 0 ? x % m : (x % m + m) % m; |
| 280 | } |
Gian Marco Iodice | b0c5037 | 2019-03-15 10:13:05 +0000 | [diff] [blame] | 281 | |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 282 | /** Convert negative coordinates to positive in the range [0, num_dims_input] |
| 283 | * |
| 284 | * @param[out] coords Array of coordinates to be converted. |
| 285 | * @param[in] max_value Maximum value to be used when wrapping the negative values in coords |
| 286 | */ |
| 287 | inline Coordinates &convert_negative_axis(Coordinates &coords, int max_value) |
| 288 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 289 | for (unsigned int i = 0; i < coords.num_dimensions(); ++i) |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 290 | { |
| 291 | coords[i] = wrap_around(coords[i], max_value); |
| 292 | } |
| 293 | return coords; |
| 294 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 295 | } // namespace arm_compute |
| 296 | |
| 297 | #include "arm_compute/core/Helpers.inl" |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 298 | #endif /*ARM_COMPUTE_HELPERS_H */ |