blob: fd6e94c0793cd423315c80358ecf2d319e3acb68 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Giorgio Arenac9fe9fc2021-10-06 12:54:29 +01002 * Copyright (c) 2016-2021 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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_HELPERS_H
25#define ARM_COMPUTE_HELPERS_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
Georgios Pinitas583137c2017-08-31 18:12:42 +010027#include "arm_compute/core/Error.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/IAccessWindow.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010029#include "arm_compute/core/ITensor.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/Types.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010031#include "arm_compute/core/Validate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include "arm_compute/core/Window.h"
Georgios Pinitas583137c2017-08-31 18:12:42 +010033
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include <array>
35#include <cstddef>
36#include <cstdint>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037#include <tuple>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038
39namespace arm_compute
40{
41class IKernel;
42class ITensor;
43class ITensorInfo;
44
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045/** Iterator updated by @ref execute_window_loop for each window element */
46class Iterator
47{
48public:
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
58 /** Increment the iterator along the specified dimension of the step value associated to the dimension.
59 *
60 * @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.
61 *
62 * @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.
63 *
64 * @param[in] dimension Dimension to increment
65 */
66 void increment(size_t dimension);
67
68 /** Return the offset in bytes from the first element to the current position of the iterator
69 *
70 * @return The current position of the iterator in bytes relative to the first element.
71 */
Sheri Zhanga3e6b6d2020-08-18 10:07:35 +010072 constexpr size_t offset() const;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073
74 /** Return a pointer to the current pixel.
75 *
76 * @warning Only works if the iterator was created with an ITensor.
77 *
78 * @return equivalent to buffer() + offset()
79 */
80 constexpr uint8_t *ptr() const;
81
82 /** Move the iterator back to the beginning of the specified dimension.
83 *
84 * @param[in] dimension Dimension to reset
85 */
86 void reset(size_t dimension);
87
88private:
89 uint8_t *_ptr;
90
91 class Dimension
92 {
93 public:
94 constexpr Dimension()
95 : _dim_start(0), _stride(0)
96 {
97 }
98
Sheri Zhanga3e6b6d2020-08-18 10:07:35 +010099 size_t _dim_start;
100 size_t _stride;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101 };
102
103 std::array<Dimension, Coordinates::num_max_dimensions> _dims;
104};
105
106/** Iterate through the passed window, automatically adjusting the iterators and calling the lambda_functino for each element.
107 * It passes the x and y positions to the lambda_function for each iteration
108 *
109 * @param[in] w Window to iterate through.
110 * @param[in] lambda_function The function of type void(function)( const Coordinates & id ) to call at each iteration.
111 * Where id represents the absolute coordinates of the item to process.
112 * @param[in,out] iterators Tensor iterators which will be updated by this function before calling lambda_function.
113 */
114template <typename L, typename... Ts>
115inline void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators);
116
Georgios Pinitas8795ffb2017-12-01 16:13:40 +0000117/** Permutes given Dimensions according to a permutation vector
118 *
119 * @warning Validity of permutation is not checked
120 *
121 * @param[in, out] dimensions Dimensions to permute
122 * @param[in] perm Permutation vector
123 */
124template <typename T>
125inline void permute(Dimensions<T> &dimensions, const PermutationVector &perm)
126{
Georgios Pinitas69af6cf2018-02-14 19:23:44 +0000127 auto dimensions_copy = utility::make_array<Dimensions<T>::num_max_dimensions>(dimensions.begin(), dimensions.end());
Georgios Pinitas8795ffb2017-12-01 16:13:40 +0000128 for(unsigned int i = 0; i < perm.num_dimensions(); ++i)
129 {
Georgios Pinitas69af6cf2018-02-14 19:23:44 +0000130 T dimension_val = (perm[i] < dimensions.num_dimensions()) ? dimensions_copy[perm[i]] : 0;
131 dimensions.set(i, dimension_val);
132 }
133}
134
135/** Permutes given TensorShape according to a permutation vector
136 *
137 * @warning Validity of permutation is not checked
138 *
139 * @param[in, out] shape Shape to permute
140 * @param[in] perm Permutation vector
141 */
142inline void permute(TensorShape &shape, const PermutationVector &perm)
143{
Giorgio Arena563494c2018-04-30 17:29:41 +0100144 TensorShape shape_copy = shape;
Georgios Pinitas69af6cf2018-02-14 19:23:44 +0000145 for(unsigned int i = 0; i < perm.num_dimensions(); ++i)
146 {
147 size_t dimension_val = (perm[i] < shape.num_dimensions()) ? shape_copy[perm[i]] : 1;
Giorgio Arenaec241b42020-12-11 13:39:02 +0000148 shape.set(i, dimension_val, false, false); // Avoid changes in _num_dimension
Georgios Pinitas8795ffb2017-12-01 16:13:40 +0000149 }
150}
151
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100152/** Helper function to calculate the Valid Region for Scale.
153 *
Diego Lopez Recas00854292018-02-22 13:08:01 +0000154 * @param[in] src_info Input tensor info used to check.
155 * @param[in] dst_shape Shape of the output.
156 * @param[in] interpolate_policy Interpolation policy.
157 * @param[in] sampling_policy Sampling policy.
158 * @param[in] border_undefined True if the border is undefined.
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100159 *
Diego Lopez Recas00854292018-02-22 13:08:01 +0000160 * @return The corresponding valid region
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100161 */
Diego Lopez Recas00854292018-02-22 13:08:01 +0000162ValidRegion calculate_valid_region_scale(const ITensorInfo &src_info, const TensorShape &dst_shape,
163 InterpolationPolicy interpolate_policy, SamplingPolicy sampling_policy, bool border_undefined);
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000164
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100165/** Convert a linear index into n-dimensional coordinates.
166 *
167 * @param[in] shape Shape of the n-dimensional tensor.
168 * @param[in] index Linear index specifying the i-th element.
169 *
170 * @return n-dimensional coordinates.
171 */
172inline Coordinates index2coords(const TensorShape &shape, int index);
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000173
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100174/** Convert n-dimensional coordinates into a linear index.
175 *
176 * @param[in] shape Shape of the n-dimensional tensor.
177 * @param[in] coord N-dimensional coordinates.
178 *
179 * @return linead index
180 */
181inline int coords2index(const TensorShape &shape, const Coordinates &coord);
Isabella Gottardid17a6772018-02-27 17:41:55 +0000182
Giorgio Arenac9fe9fc2021-10-06 12:54:29 +0100183/** Returns a static map used to find an index or dimension based on a data layout
184 *
185 * *** Layouts ***
186 *
187 * *** 4D ***
188 * [N C H W]
189 * [3 2 1 0]
190 * [N H W C]
191 *
192 * * *** 5D ***
193 * [N C D H W]
194 * [4 3 2 1 0]
195 * [N D H W C]
196 */
197const std::map<DataLayout, std::vector<DataLayoutDimension>> &get_layout_map();
198
Alex Gildayc357c472018-03-21 13:54:09 +0000199/** Get the index of the given dimension.
Isabella Gottardid17a6772018-02-27 17:41:55 +0000200 *
Alex Gildayc357c472018-03-21 13:54:09 +0000201 * @param[in] data_layout The data layout.
202 * @param[in] data_layout_dimension The dimension which this index is requested for.
Isabella Gottardid17a6772018-02-27 17:41:55 +0000203 *
204 * @return The int conversion of the requested data layout index.
205 */
Giorgio Arenac9fe9fc2021-10-06 12:54:29 +0100206inline size_t get_data_layout_dimension_index(const DataLayout &data_layout, const DataLayoutDimension &data_layout_dimension);
Georgios Pinitase2220552018-07-20 13:23:44 +0100207
Usama Arif8cf8c112019-03-14 15:36:54 +0000208/** Get the DataLayoutDimension of a given index and layout.
209 *
210 * @param[in] data_layout The data layout.
211 * @param[in] index The data layout index.
212 *
213 * @return The dimension which this index is requested for.
214 */
Giorgio Arenac9fe9fc2021-10-06 12:54:29 +0100215inline DataLayoutDimension get_index_data_layout_dimension(const DataLayout &data_layout, const size_t index);
Usama Arif8cf8c112019-03-14 15:36:54 +0000216
Georgios Pinitase2220552018-07-20 13:23:44 +0100217/** Calculate the number of output tiles required by Winograd Convolution layer. This utility function can be used by the Winograd input transform
218 * to know the number of tiles on the x and y direction
219 *
220 * @param[in] in_dims Spatial dimensions of the input tensor of convolution layer
221 * @param[in] kernel_size Kernel size
222 * @param[in] output_tile_size Size of a single output tile
223 * @param[in] conv_info Convolution info (i.e. pad, stride,...)
224 *
225 * @return the number of output tiles along the x and y directions of size "output_tile_size"
226 */
227inline Size2D compute_winograd_convolution_tiles(const Size2D &in_dims, const Size2D &kernel_size, const Size2D &output_tile_size, const PadStrideInfo &conv_info)
228{
229 int num_tiles_x = std::ceil((in_dims.width - (kernel_size.width - 1) + conv_info.pad_left() + conv_info.pad_right()) / static_cast<float>(output_tile_size.width));
230 int num_tiles_y = std::ceil((in_dims.height - (kernel_size.height - 1) + conv_info.pad_top() + conv_info.pad_bottom()) / static_cast<float>(output_tile_size.height));
231
232 // Clamp in case we provide paddings but we have 1D convolution
233 num_tiles_x = std::min(num_tiles_x, static_cast<int>(in_dims.width));
234 num_tiles_y = std::min(num_tiles_y, static_cast<int>(in_dims.height));
235
236 return Size2D(num_tiles_x, num_tiles_y);
237}
238
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +0000239/** Wrap-around a number within the range 0 <= x < m
240 *
241 * @param[in] x Input value
242 * @param[in] m Range
243 *
244 * @return the wrapped-around number
245 */
246template <typename T>
247inline T wrap_around(T x, T m)
248{
249 return x >= 0 ? x % m : (x % m + m) % m;
250}
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000251
Pablo Tello93975152019-11-08 13:47:53 +0000252/** Convert negative coordinates to positive in the range [0, num_dims_input]
253 *
254 * @param[out] coords Array of coordinates to be converted.
255 * @param[in] max_value Maximum value to be used when wrapping the negative values in coords
256 */
257inline Coordinates &convert_negative_axis(Coordinates &coords, int max_value)
258{
259 for(unsigned int i = 0; i < coords.num_dimensions(); ++i)
260 {
261 coords[i] = wrap_around(coords[i], max_value);
262 }
263 return coords;
264}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100265} // namespace arm_compute
266
267#include "arm_compute/core/Helpers.inl"
Michalis Spyrouf4643372019-11-29 16:17:13 +0000268#endif /*ARM_COMPUTE_HELPERS_H */