giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1 | /* |
SiCongLi | b88272e | 2021-02-24 15:40:57 +0000 | [diff] [blame] | 2 | * Copyright (c) 2019-2021 Arm Limited. |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [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 | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 24 | #include "src/core/NEON/kernels/NEFFTDigitReverseKernel.h" |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/ITensor.h" |
| 27 | #include "arm_compute/core/TensorInfo.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "arm_compute/core/Validate.h" |
| 30 | #include "arm_compute/core/Window.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 31 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 32 | #include "src/core/helpers/AutoConfiguration.h" |
| 33 | #include "src/core/helpers/WindowHelpers.h" |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 34 | |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 35 | #include <set> |
| 36 | |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace |
| 40 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 41 | Status validate_arguments(const ITensorInfo *input, |
| 42 | const ITensorInfo *output, |
| 43 | const ITensorInfo *idx, |
| 44 | const FFTDigitReverseKernelInfo &config) |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 45 | { |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 46 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() != DataType::F32); |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_channels() > 2); |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(idx, 1, DataType::U32); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 49 | ARM_COMPUTE_RETURN_ERROR_ON(std::set<unsigned int>({0, 1}).count(config.axis) == 0); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[config.axis] != idx->tensor_shape().x()); |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 51 | |
| 52 | // Checks performed when output is configured |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 53 | if ((output != nullptr) && (output->total_size() != 0)) |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 54 | { |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 55 | ARM_COMPUTE_RETURN_ERROR_ON(output->num_channels() != 2); |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 56 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 58 | } |
| 59 | |
| 60 | return Status{}; |
| 61 | } |
| 62 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 63 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, |
| 64 | ITensorInfo *output, |
| 65 | ITensorInfo *idx, |
| 66 | const FFTDigitReverseKernelInfo &config) |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 67 | { |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 68 | ARM_COMPUTE_UNUSED(idx, config); |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 69 | |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 70 | auto_init_if_empty(*output, input->clone()->set_num_channels(2)); |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 71 | |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 72 | Window win = calculate_max_window(*input, Steps()); |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 73 | |
| 74 | return std::make_pair(Status{}, win); |
| 75 | } |
| 76 | } // namespace |
| 77 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 78 | NEFFTDigitReverseKernel::NEFFTDigitReverseKernel() : _func(nullptr), _input(nullptr), _output(nullptr), _idx(nullptr) |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 79 | { |
| 80 | } |
| 81 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 82 | void NEFFTDigitReverseKernel::configure(const ITensor *input, |
| 83 | ITensor *output, |
| 84 | const ITensor *idx, |
| 85 | const FFTDigitReverseKernelInfo &config) |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 86 | { |
| 87 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, idx); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 88 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), idx->info(), config)); |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 89 | |
| 90 | _input = input; |
| 91 | _output = output; |
| 92 | _idx = idx; |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 93 | |
| 94 | const size_t axis = config.axis; |
| 95 | const bool is_conj = config.conjugate; |
| 96 | const bool is_input_complex = (input->info()->num_channels() == 2); |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 97 | |
| 98 | // Configure kernel window |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 99 | auto win_config = validate_and_configure_window(input->info(), output->info(), idx->info(), config); |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 100 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 101 | INEKernel::configure(win_config.second); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 102 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 103 | if (axis == 0) |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 104 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 105 | if (is_input_complex) |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 106 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 107 | if (is_conj) |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 108 | { |
| 109 | _func = &NEFFTDigitReverseKernel::digit_reverse_kernel_axis_0<true, true>; |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | _func = &NEFFTDigitReverseKernel::digit_reverse_kernel_axis_0<true, false>; |
| 114 | } |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | _func = &NEFFTDigitReverseKernel::digit_reverse_kernel_axis_0<false, false>; |
| 119 | } |
| 120 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 121 | else if (axis == 1) |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 122 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 123 | if (is_input_complex) |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 124 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 125 | if (is_conj) |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 126 | { |
| 127 | _func = &NEFFTDigitReverseKernel::digit_reverse_kernel_axis_1<true, true>; |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | _func = &NEFFTDigitReverseKernel::digit_reverse_kernel_axis_1<true, false>; |
| 132 | } |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | _func = &NEFFTDigitReverseKernel::digit_reverse_kernel_axis_1<false, false>; |
| 137 | } |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | ARM_COMPUTE_ERROR("Not supported"); |
| 142 | } |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 145 | Status NEFFTDigitReverseKernel::validate(const ITensorInfo *input, |
| 146 | const ITensorInfo *output, |
| 147 | const ITensorInfo *idx, |
| 148 | const FFTDigitReverseKernelInfo &config) |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 149 | { |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 150 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, idx, config)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 151 | ARM_COMPUTE_RETURN_ON_ERROR( |
| 152 | validate_and_configure_window(input->clone().get(), output->clone().get(), idx->clone().get(), config).first); |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 153 | return Status{}; |
| 154 | } |
| 155 | |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 156 | template <bool is_input_complex, bool is_conj> |
| 157 | void NEFFTDigitReverseKernel::digit_reverse_kernel_axis_0(const Window &window) |
| 158 | { |
| 159 | const size_t N = _input->info()->dimension(0); |
| 160 | |
| 161 | // Copy the look-up buffer to a local array |
| 162 | std::vector<unsigned int> buffer_idx(N); |
| 163 | std::copy_n(reinterpret_cast<unsigned int *>(_idx->buffer()), N, buffer_idx.data()); |
| 164 | |
| 165 | // Input/output iterators |
| 166 | Window slice = window; |
| 167 | slice.set(0, Window::DimX); |
| 168 | Iterator in(_input, slice); |
| 169 | Iterator out(_output, slice); |
| 170 | |
| 171 | // Row buffers |
| 172 | std::vector<float> buffer_row_out(2 * N); |
| 173 | std::vector<float> buffer_row_in(2 * N); |
| 174 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 175 | execute_window_loop( |
| 176 | slice, |
| 177 | [&](const Coordinates &) |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 178 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 179 | if (is_input_complex) |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 180 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 181 | // Load |
| 182 | memcpy(buffer_row_in.data(), reinterpret_cast<float *>(in.ptr()), 2 * N * sizeof(float)); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 183 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 184 | // Shuffle |
| 185 | for (size_t x = 0; x < 2 * N; x += 2) |
| 186 | { |
| 187 | size_t idx = buffer_idx[x / 2]; |
| 188 | buffer_row_out[x] = buffer_row_in[2 * idx]; |
| 189 | buffer_row_out[x + 1] = (is_conj ? -buffer_row_in[2 * idx + 1] : buffer_row_in[2 * idx + 1]); |
| 190 | } |
| 191 | } |
| 192 | else |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 193 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 194 | // Load |
| 195 | memcpy(buffer_row_in.data(), reinterpret_cast<float *>(in.ptr()), N * sizeof(float)); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 196 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 197 | // Shuffle |
| 198 | for (size_t x = 0; x < N; ++x) |
| 199 | { |
| 200 | size_t idx = buffer_idx[x]; |
| 201 | buffer_row_out[2 * x] = buffer_row_in[idx]; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // Copy back |
| 206 | memcpy(reinterpret_cast<float *>(out.ptr()), buffer_row_out.data(), 2 * N * sizeof(float)); |
| 207 | }, |
| 208 | in, out); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | template <bool is_input_complex, bool is_conj> |
| 212 | void NEFFTDigitReverseKernel::digit_reverse_kernel_axis_1(const Window &window) |
| 213 | { |
| 214 | const size_t Nx = _input->info()->dimension(0); |
| 215 | const size_t Ny = _input->info()->dimension(1); |
| 216 | |
| 217 | // Copy the look-up buffer to a local array |
| 218 | std::vector<unsigned int> buffer_idx(Ny); |
| 219 | std::copy_n(reinterpret_cast<unsigned int *>(_idx->buffer()), Ny, buffer_idx.data()); |
| 220 | |
| 221 | // Output iterator |
| 222 | Window slice = window; |
| 223 | slice.set(0, Window::DimX); |
| 224 | Iterator out(_output, slice); |
| 225 | |
| 226 | // Row buffer |
| 227 | std::vector<float> buffer_row(Nx); |
| 228 | |
| 229 | // Strides |
| 230 | const size_t stride_z = _input->info()->strides_in_bytes()[2]; |
| 231 | const size_t stride_w = _input->info()->strides_in_bytes()[3]; |
| 232 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 233 | execute_window_loop( |
| 234 | slice, |
| 235 | [&](const Coordinates &id) |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 236 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 237 | auto *out_ptr = reinterpret_cast<float *>(out.ptr()); |
| 238 | auto *in_ptr = reinterpret_cast<float *>(_input->buffer() + id.z() * stride_z + id[3] * stride_w); |
| 239 | const size_t y_shuffled = buffer_idx[id.y()]; |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 240 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 241 | if (is_input_complex) |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 242 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 243 | // Shuffle the entire row into the output |
| 244 | memcpy(out_ptr, in_ptr + 2 * Nx * y_shuffled, 2 * Nx * sizeof(float)); |
| 245 | |
| 246 | // Conjugate if necessary |
| 247 | if (is_conj) |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 248 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 249 | for (size_t x = 0; x < 2 * Nx; x += 2) |
| 250 | { |
| 251 | out_ptr[x + 1] = -out_ptr[x + 1]; |
| 252 | } |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 255 | else |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 256 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 257 | // Shuffle the entire row into the buffer |
| 258 | memcpy(buffer_row.data(), in_ptr + Nx * y_shuffled, Nx * sizeof(float)); |
| 259 | |
| 260 | // Copy the buffer to the output, with a zero imaginary part |
| 261 | for (size_t x = 0; x < 2 * Nx; x += 2) |
| 262 | { |
| 263 | out_ptr[x] = buffer_row[x / 2]; |
| 264 | } |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 265 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 266 | }, |
| 267 | out); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 268 | } |
| 269 | |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 270 | void NEFFTDigitReverseKernel::run(const Window &window, const ThreadInfo &info) |
| 271 | { |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 272 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 273 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 274 | ARM_COMPUTE_UNUSED(info); |
| 275 | (this->*_func)(window); |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 276 | } |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 277 | |
giuros01 | 14c4e0f | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 278 | } // namespace arm_compute |