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 | */ |
| 24 | #include "arm_compute/core/Error.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | |
| 26 | #include <cmath> |
| 27 | #include <numeric> |
| 28 | |
| 29 | namespace arm_compute |
| 30 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | template <size_t dimension> |
| 32 | struct IncrementIterators |
| 33 | { |
| 34 | template <typename T, typename... Ts> |
| 35 | static void unroll(T &&it, Ts &&... iterators) |
| 36 | { |
Diego Lopez Recas | 490b3d8 | 2017-12-19 15:42:25 +0000 | [diff] [blame] | 37 | auto increment = [](T && it) |
| 38 | { |
| 39 | it.increment(dimension); |
| 40 | }; |
| 41 | utility::for_each(increment, std::forward<T>(it), std::forward<Ts>(iterators)...); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 42 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 43 | static void unroll() |
| 44 | { |
| 45 | // End of recursion |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | template <size_t dim> |
| 50 | struct ForEachDimension |
| 51 | { |
| 52 | template <typename L, typename... Ts> |
| 53 | static void unroll(const Window &w, Coordinates &id, L &&lambda_function, Ts &&... iterators) |
| 54 | { |
| 55 | const auto &d = w[dim - 1]; |
| 56 | |
| 57 | for(auto v = d.start(); v < d.end(); v += d.step(), IncrementIterators < dim - 1 >::unroll(iterators...)) |
| 58 | { |
| 59 | id.set(dim - 1, v); |
| 60 | ForEachDimension < dim - 1 >::unroll(w, id, lambda_function, iterators...); |
| 61 | } |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | template <> |
| 66 | struct ForEachDimension<0> |
| 67 | { |
| 68 | template <typename L, typename... Ts> |
| 69 | static void unroll(const Window &w, Coordinates &id, L &&lambda_function, Ts &&... iterators) |
| 70 | { |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 71 | ARM_COMPUTE_UNUSED(w, iterators...); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 72 | lambda_function(id); |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | template <typename L, typename... Ts> |
| 77 | inline void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators) |
| 78 | { |
| 79 | w.validate(); |
| 80 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 81 | for(unsigned int i = 0; i < Coordinates::num_max_dimensions; ++i) |
| 82 | { |
| 83 | ARM_COMPUTE_ERROR_ON(w[i].step() == 0); |
| 84 | } |
| 85 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 86 | Coordinates id; |
| 87 | ForEachDimension<Coordinates::num_max_dimensions>::unroll(w, id, std::forward<L>(lambda_function), std::forward<Ts>(iterators)...); |
| 88 | } |
| 89 | |
| 90 | inline constexpr Iterator::Iterator() |
| 91 | : _ptr(nullptr), _dims() |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | inline Iterator::Iterator(const ITensor *tensor, const Window &win) |
| 96 | : Iterator() |
| 97 | { |
| 98 | ARM_COMPUTE_ERROR_ON(tensor == nullptr); |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 99 | ARM_COMPUTE_ERROR_ON(tensor->info() == nullptr); |
| 100 | |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 101 | initialize(tensor->info()->num_dimensions(), tensor->info()->strides_in_bytes(), tensor->buffer(), tensor->info()->offset_first_element_in_bytes(), win); |
| 102 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 103 | |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 104 | inline Iterator::Iterator(size_t num_dims, const Strides &strides, uint8_t *buffer, size_t offset, const Window &win) |
| 105 | : Iterator() |
| 106 | { |
| 107 | initialize(num_dims, strides, buffer, offset, win); |
| 108 | } |
| 109 | |
| 110 | inline void Iterator::initialize(size_t num_dims, const Strides &strides, uint8_t *buffer, size_t offset, const Window &win) |
| 111 | { |
| 112 | ARM_COMPUTE_ERROR_ON(buffer == nullptr); |
| 113 | |
| 114 | _ptr = buffer + offset; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 115 | |
| 116 | //Initialize the stride for each dimension and calculate the position of the first element of the iteration: |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 117 | for(unsigned int n = 0; n < num_dims; ++n) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 118 | { |
| 119 | _dims[n]._stride = win[n].step() * strides[n]; |
Sheri Zhang | a3e6b6d | 2020-08-18 10:07:35 +0100 | [diff] [blame] | 120 | std::get<0>(_dims)._dim_start += static_cast<size_t>(strides[n]) * win[n].start(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | //Copy the starting point to all the dimensions: |
| 124 | for(unsigned int n = 1; n < Coordinates::num_max_dimensions; ++n) |
| 125 | { |
| 126 | _dims[n]._dim_start = std::get<0>(_dims)._dim_start; |
| 127 | } |
| 128 | |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 129 | ARM_COMPUTE_ERROR_ON_WINDOW_DIMENSIONS_GTE(win, num_dims); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | inline void Iterator::increment(const size_t dimension) |
| 133 | { |
| 134 | ARM_COMPUTE_ERROR_ON(dimension >= Coordinates::num_max_dimensions); |
| 135 | |
| 136 | _dims[dimension]._dim_start += _dims[dimension]._stride; |
| 137 | |
| 138 | for(unsigned int n = 0; n < dimension; ++n) |
| 139 | { |
| 140 | _dims[n]._dim_start = _dims[dimension]._dim_start; |
| 141 | } |
| 142 | } |
| 143 | |
Sheri Zhang | a3e6b6d | 2020-08-18 10:07:35 +0100 | [diff] [blame] | 144 | inline constexpr size_t Iterator::offset() const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 145 | { |
| 146 | return _dims.at(0)._dim_start; |
| 147 | } |
| 148 | |
| 149 | inline constexpr uint8_t *Iterator::ptr() const |
| 150 | { |
| 151 | return _ptr + _dims.at(0)._dim_start; |
| 152 | } |
| 153 | |
| 154 | inline void Iterator::reset(const size_t dimension) |
| 155 | { |
| 156 | ARM_COMPUTE_ERROR_ON(dimension >= Coordinates::num_max_dimensions - 1); |
| 157 | |
| 158 | _dims[dimension]._dim_start = _dims[dimension + 1]._dim_start; |
| 159 | |
| 160 | for(unsigned int n = 0; n < dimension; ++n) |
| 161 | { |
| 162 | _dims[n]._dim_start = _dims[dimension]._dim_start; |
| 163 | } |
| 164 | } |
| 165 | |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 166 | inline Coordinates index2coords(const TensorShape &shape, int index) |
| 167 | { |
| 168 | int num_elements = shape.total_size(); |
| 169 | |
| 170 | ARM_COMPUTE_ERROR_ON_MSG(index < 0 || index >= num_elements, "Index has to be in [0, num_elements]!"); |
| 171 | ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create coordinate from empty shape!"); |
| 172 | |
| 173 | Coordinates coord{ 0 }; |
| 174 | |
| 175 | for(int d = shape.num_dimensions() - 1; d >= 0; --d) |
| 176 | { |
| 177 | num_elements /= shape[d]; |
| 178 | coord.set(d, index / num_elements); |
| 179 | index %= num_elements; |
| 180 | } |
| 181 | |
| 182 | return coord; |
| 183 | } |
| 184 | |
| 185 | inline int coords2index(const TensorShape &shape, const Coordinates &coord) |
| 186 | { |
| 187 | int num_elements = shape.total_size(); |
| 188 | ARM_COMPUTE_UNUSED(num_elements); |
| 189 | ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create linear index from empty shape!"); |
| 190 | |
| 191 | int index = 0; |
| 192 | int stride = 1; |
| 193 | |
| 194 | for(unsigned int d = 0; d < coord.num_dimensions(); ++d) |
| 195 | { |
| 196 | index += coord[d] * stride; |
| 197 | stride *= shape[d]; |
| 198 | } |
| 199 | |
| 200 | return index; |
| 201 | } |
Isabella Gottardi | d17a677 | 2018-02-27 17:41:55 +0000 | [diff] [blame] | 202 | |
Giorgio Arena | c9fe9fc | 2021-10-06 12:54:29 +0100 | [diff] [blame] | 203 | inline size_t get_data_layout_dimension_index(const DataLayout &data_layout, const DataLayoutDimension &data_layout_dimension) |
Isabella Gottardi | d17a677 | 2018-02-27 17:41:55 +0000 | [diff] [blame] | 204 | { |
Isabella Gottardi | d56e770 | 2018-02-28 14:29:36 +0000 | [diff] [blame] | 205 | ARM_COMPUTE_ERROR_ON_MSG(data_layout == DataLayout::UNKNOWN, "Cannot retrieve the dimension index for an unknown layout!"); |
Giorgio Arena | c9fe9fc | 2021-10-06 12:54:29 +0100 | [diff] [blame] | 206 | 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 Gottardi | d17a677 | 2018-02-27 17:41:55 +0000 | [diff] [blame] | 210 | } |
Usama Arif | 8cf8c11 | 2019-03-14 15:36:54 +0000 | [diff] [blame] | 211 | |
Giorgio Arena | c9fe9fc | 2021-10-06 12:54:29 +0100 | [diff] [blame] | 212 | 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] | 213 | { |
Giorgio Arena | c9fe9fc | 2021-10-06 12:54:29 +0100 | [diff] [blame] | 214 | ARM_COMPUTE_ERROR_ON_MSG(data_layout == DataLayout::UNKNOWN, "Cannot retrieve the layout dimension for an unknown layout!"); |
| 215 | const auto &dims = get_layout_map().at(data_layout); |
| 216 | ARM_COMPUTE_ERROR_ON_MSG(index >= dims.size(), "Invalid index for the given layout."); |
| 217 | return dims[index]; |
Usama Arif | 8cf8c11 | 2019-03-14 15:36:54 +0000 | [diff] [blame] | 218 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 219 | } // namespace arm_compute |