blob: 60a21e94181c09b3c763a454ff800aefdcae27f6 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Viet-Hoa Do37c989a2023-02-24 15:52:21 +00002 * Copyright (c) 2016-2021, 2023 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "arm_compute/core/Error.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
26#include <cmath>
27#include <numeric>
28
29namespace arm_compute
30{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031template <size_t dimension>
32struct IncrementIterators
33{
34 template <typename T, typename... Ts>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010035 static void unroll(T &&it, Ts &&...iterators)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010037 auto increment = [](T &&it) { it.increment(dimension); };
Diego Lopez Recas490b3d82017-12-19 15:42:25 +000038 utility::for_each(increment, std::forward<T>(it), std::forward<Ts>(iterators)...);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040 static void unroll()
41 {
42 // End of recursion
43 }
44};
45
46template <size_t dim>
47struct ForEachDimension
48{
49 template <typename L, typename... Ts>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010050 static void unroll(const Window &w, Coordinates &id, L &&lambda_function, Ts &&...iterators)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051 {
52 const auto &d = w[dim - 1];
53
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010054 for (auto v = d.start(); v < d.end(); v += d.step(), IncrementIterators<dim - 1>::unroll(iterators...))
Anthony Barbier6ff3b192017-09-04 18:44:23 +010055 {
56 id.set(dim - 1, v);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010057 ForEachDimension<dim - 1>::unroll(w, id, lambda_function, iterators...);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058 }
59 }
60};
61
62template <>
63struct ForEachDimension<0>
64{
65 template <typename L, typename... Ts>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010066 static void unroll(const Window &w, Coordinates &id, L &&lambda_function, Ts &&...iterators)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067 {
Michalis Spyrou6bff1952019-10-02 17:22:11 +010068 ARM_COMPUTE_UNUSED(w, iterators...);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069 lambda_function(id);
70 }
71};
72
73template <typename L, typename... Ts>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010074inline void execute_window_loop(const Window &w, L &&lambda_function, Ts &&...iterators)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075{
76 w.validate();
77
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010078 for (unsigned int i = 0; i < Coordinates::num_max_dimensions; ++i)
Diego Lopez Recas0021d752017-12-18 14:42:56 +000079 {
80 ARM_COMPUTE_ERROR_ON(w[i].step() == 0);
81 }
82
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083 Coordinates id;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010084 ForEachDimension<Coordinates::num_max_dimensions>::unroll(w, id, std::forward<L>(lambda_function),
85 std::forward<Ts>(iterators)...);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086}
87
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010088inline constexpr Iterator::Iterator() : _ptr(nullptr), _dims()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089{
90}
91
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010092inline Iterator::Iterator(const ITensor *tensor, const Window &win) : Iterator()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093{
94 ARM_COMPUTE_ERROR_ON(tensor == nullptr);
Diego Lopez Recas0021d752017-12-18 14:42:56 +000095 ARM_COMPUTE_ERROR_ON(tensor->info() == nullptr);
96
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010097 initialize(tensor->info()->num_dimensions(), tensor->info()->strides_in_bytes(), tensor->buffer(),
98 tensor->info()->offset_first_element_in_bytes(), win);
Viet-Hoa Do37c989a2023-02-24 15:52:21 +000099}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100
Viet-Hoa Do37c989a2023-02-24 15:52:21 +0000101inline Iterator::Iterator(size_t num_dims, const Strides &strides, uint8_t *buffer, size_t offset, const Window &win)
102 : Iterator()
103{
104 initialize(num_dims, strides, buffer, offset, win);
105}
106
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100107inline void
108Iterator::initialize(size_t num_dims, const Strides &strides, uint8_t *buffer, size_t offset, const Window &win)
Viet-Hoa Do37c989a2023-02-24 15:52:21 +0000109{
110 ARM_COMPUTE_ERROR_ON(buffer == nullptr);
111
112 _ptr = buffer + offset;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113
114 //Initialize the stride for each dimension and calculate the position of the first element of the iteration:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100115 for (unsigned int n = 0; n < num_dims; ++n)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100116 {
117 _dims[n]._stride = win[n].step() * strides[n];
Sheri Zhanga3e6b6d2020-08-18 10:07:35 +0100118 std::get<0>(_dims)._dim_start += static_cast<size_t>(strides[n]) * win[n].start();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119 }
120
121 //Copy the starting point to all the dimensions:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100122 for (unsigned int n = 1; n < Coordinates::num_max_dimensions; ++n)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123 {
124 _dims[n]._dim_start = std::get<0>(_dims)._dim_start;
125 }
126
Viet-Hoa Do37c989a2023-02-24 15:52:21 +0000127 ARM_COMPUTE_ERROR_ON_WINDOW_DIMENSIONS_GTE(win, num_dims);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128}
129
130inline void Iterator::increment(const size_t dimension)
131{
132 ARM_COMPUTE_ERROR_ON(dimension >= Coordinates::num_max_dimensions);
133
134 _dims[dimension]._dim_start += _dims[dimension]._stride;
135
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100136 for (unsigned int n = 0; n < dimension; ++n)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137 {
138 _dims[n]._dim_start = _dims[dimension]._dim_start;
139 }
140}
141
Sheri Zhanga3e6b6d2020-08-18 10:07:35 +0100142inline constexpr size_t Iterator::offset() const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143{
144 return _dims.at(0)._dim_start;
145}
146
147inline constexpr uint8_t *Iterator::ptr() const
148{
149 return _ptr + _dims.at(0)._dim_start;
150}
151
152inline void Iterator::reset(const size_t dimension)
153{
154 ARM_COMPUTE_ERROR_ON(dimension >= Coordinates::num_max_dimensions - 1);
155
156 _dims[dimension]._dim_start = _dims[dimension + 1]._dim_start;
157
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100158 for (unsigned int n = 0; n < dimension; ++n)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159 {
160 _dims[n]._dim_start = _dims[dimension]._dim_start;
161 }
162}
163
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100164inline Coordinates index2coords(const TensorShape &shape, int index)
165{
166 int num_elements = shape.total_size();
167
168 ARM_COMPUTE_ERROR_ON_MSG(index < 0 || index >= num_elements, "Index has to be in [0, num_elements]!");
169 ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create coordinate from empty shape!");
170
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100171 Coordinates coord{0};
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100172
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100173 for (int d = shape.num_dimensions() - 1; d >= 0; --d)
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100174 {
175 num_elements /= shape[d];
176 coord.set(d, index / num_elements);
177 index %= num_elements;
178 }
179
180 return coord;
181}
182
183inline int coords2index(const TensorShape &shape, const Coordinates &coord)
184{
185 int num_elements = shape.total_size();
186 ARM_COMPUTE_UNUSED(num_elements);
187 ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create linear index from empty shape!");
188
189 int index = 0;
190 int stride = 1;
191
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100192 for (unsigned int d = 0; d < coord.num_dimensions(); ++d)
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100193 {
194 index += coord[d] * stride;
195 stride *= shape[d];
196 }
197
198 return index;
199}
Isabella Gottardid17a6772018-02-27 17:41:55 +0000200
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100201inline size_t get_data_layout_dimension_index(const DataLayout &data_layout,
202 const DataLayoutDimension &data_layout_dimension)
Isabella Gottardid17a6772018-02-27 17:41:55 +0000203{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100204 ARM_COMPUTE_ERROR_ON_MSG(data_layout == DataLayout::UNKNOWN,
205 "Cannot retrieve the dimension index for an unknown layout!");
Giorgio Arenac9fe9fc2021-10-06 12:54:29 +0100206 const auto &dims = get_layout_map().at(data_layout);
207 const auto &it = std::find(dims.cbegin(), dims.cend(), data_layout_dimension);
208 ARM_COMPUTE_ERROR_ON_MSG(it == dims.cend(), "Invalid dimension for the given layout.");
209 return it - dims.cbegin();
Isabella Gottardid17a6772018-02-27 17:41:55 +0000210}
Usama Arif8cf8c112019-03-14 15:36:54 +0000211
Giorgio Arenac9fe9fc2021-10-06 12:54:29 +0100212inline DataLayoutDimension get_index_data_layout_dimension(const DataLayout &data_layout, const size_t index)
Usama Arif8cf8c112019-03-14 15:36:54 +0000213{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100214 ARM_COMPUTE_ERROR_ON_MSG(data_layout == DataLayout::UNKNOWN,
215 "Cannot retrieve the layout dimension for an unknown layout!");
Giorgio Arenac9fe9fc2021-10-06 12:54:29 +0100216 const auto &dims = get_layout_map().at(data_layout);
217 ARM_COMPUTE_ERROR_ON_MSG(index >= dims.size(), "Invalid index for the given layout.");
218 return dims[index];
Usama Arif8cf8c112019-03-14 15:36:54 +0000219}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100220} // namespace arm_compute