Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 1 | /* |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 2 | * Copyright (c) 2019-2021, 2023 Arm Limited. |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +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 | */ |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 24 | #include "src/core/CL/kernels/CLFFTRadixStageKernel.h" |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/CL/ICLTensor.h" |
| 29 | #include "arm_compute/core/TensorInfo.h" |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/Utils.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/utils/StringUtils.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 32 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 33 | #include "src/core/CL/CLValidate.h" |
| 34 | #include "src/core/helpers/AutoConfiguration.h" |
| 35 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 36 | #include "support/StringSupport.h" |
SiCongLi | 410e21e | 2020-12-11 15:07:53 +0000 | [diff] [blame] | 37 | #include "support/ToolchainSupport.h" |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 38 | |
| 39 | #include <cmath> |
| 40 | |
| 41 | namespace arm_compute |
| 42 | { |
| 43 | namespace |
| 44 | { |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 45 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const FFTRadixStageKernelInfo &config) |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 46 | { |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Giorgio Arena | ea7de7b | 2020-12-10 16:49:39 +0000 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 2, DataType::F16, DataType::F32); |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 49 | ARM_COMPUTE_RETURN_ERROR_ON(CLFFTRadixStageKernel::supported_radix().count(config.radix) == 0); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON(std::set<unsigned int>({0, 1}).count(config.axis) == 0); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[config.axis] % config.radix); |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 52 | |
| 53 | // Checks performed when output is configured |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 54 | if ((output != nullptr) && (output->total_size() != 0)) |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 55 | { |
| 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> |
| 64 | validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const FFTRadixStageKernelInfo &config) |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 65 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 66 | if (output != nullptr) |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 67 | { |
| 68 | auto_init_if_empty(*output, *input); |
| 69 | } |
| 70 | |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 71 | // Setup window steps |
| 72 | Steps steps; |
| 73 | steps.set(config.axis, config.radix); |
| 74 | |
| 75 | Window win = calculate_max_window(*input, steps); |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 76 | |
| 77 | return std::make_pair(Status{}, win); |
| 78 | } |
| 79 | } // namespace |
| 80 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 81 | CLFFTRadixStageKernel::CLFFTRadixStageKernel() : _input(nullptr), _output(nullptr), _run_in_place(false) |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 82 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 83 | _type = CLKernelType::ELEMENTWISE; |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 86 | void CLFFTRadixStageKernel::configure(ICLTensor *input, ICLTensor *output, const FFTRadixStageKernelInfo &config) |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 87 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 88 | configure(CLKernelLibrary::get().get_compile_context(), input, output, config); |
| 89 | } |
| 90 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 91 | void CLFFTRadixStageKernel::configure(const CLCompileContext &compile_context, |
| 92 | ICLTensor *input, |
| 93 | ICLTensor *output, |
| 94 | const FFTRadixStageKernelInfo &config) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 95 | { |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 96 | ARM_COMPUTE_ERROR_ON_NULLPTR(input); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 97 | ARM_COMPUTE_ERROR_THROW_ON( |
| 98 | validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr, config)); |
| 99 | auto padding_info = get_padding_info({input, output}); |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 100 | |
| 101 | _input = input; |
| 102 | _output = output; |
| 103 | _run_in_place = (output == nullptr) || (output == input); |
| 104 | |
| 105 | // Create build options |
| 106 | CLBuildOptions build_opts; |
Giorgio Arena | ea7de7b | 2020-12-10 16:49:39 +0000 | [diff] [blame] | 107 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 108 | build_opts.add_option_if(_run_in_place, "-DIN_PLACE"); |
| 109 | |
| 110 | // Create kernel |
| 111 | std::string kernel_name = "fft"; |
| 112 | kernel_name += "_radix_" + support::cpp11::to_string(config.radix); |
| 113 | kernel_name += (config.is_first_stage) ? "_first_stage" : ""; |
| 114 | kernel_name += "_axis_" + support::cpp11::to_string(config.axis); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 115 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 116 | |
| 117 | // Set static arguments if not the first stage |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 118 | if (!config.is_first_stage) |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 119 | { |
| 120 | const unsigned int Ni = config.Nx * config.radix; |
| 121 | const float exp_const = (-2.0 * M_PI) / static_cast<float>(Ni); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 122 | unsigned int idx = |
| 123 | (1 + (_run_in_place ? 0 : 1)) * num_arguments_per_3D_tensor(); // Skip the input and output parameters |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 124 | _kernel.setArg<cl_uint>(idx++, config.Nx); |
| 125 | _kernel.setArg<cl_uint>(idx++, Ni); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 126 | _kernel.setArg<cl_float>(idx, exp_const); |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | // Configure kernel window |
| 130 | auto win_config = validate_and_configure_window(input->info(), (_run_in_place) ? nullptr : output->info(), config); |
| 131 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 132 | ICLKernel::configure_internal(win_config.second); |
| 133 | |
| 134 | // Set config_id for enabling LWS tuning |
| 135 | _config_id = kernel_name; |
| 136 | _config_id += "_"; |
| 137 | _config_id += lower_string(string_from_data_type(input->info()->data_type())); |
| 138 | _config_id += "_"; |
| 139 | _config_id += support::cpp11::to_string(input->info()->dimension(0)); |
| 140 | _config_id += "_"; |
| 141 | _config_id += support::cpp11::to_string(input->info()->dimension(1)); |
Manuel Bottini | b6869dd | 2020-12-16 15:34:25 +0000 | [diff] [blame] | 142 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 145 | Status CLFFTRadixStageKernel::validate(const ITensorInfo *input, |
| 146 | const ITensorInfo *output, |
| 147 | const FFTRadixStageKernelInfo &config) |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 148 | { |
| 149 | const bool run_in_place = (output == nullptr) || (output == input); |
| 150 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, 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(), (run_in_place) ? nullptr : output->clone().get(), config) |
| 153 | .first); |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 154 | |
| 155 | return Status{}; |
| 156 | } |
| 157 | |
| 158 | std::set<unsigned int> CLFFTRadixStageKernel::supported_radix() |
| 159 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 160 | return std::set<unsigned int>{2, 3, 4, 5, 7, 8}; |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void CLFFTRadixStageKernel::run(const Window &window, cl::CommandQueue &queue) |
| 164 | { |
| 165 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 166 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 167 | |
| 168 | Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 169 | Window slice = collapsed.first_slice_window_3D(); |
| 170 | |
| 171 | do |
| 172 | { |
| 173 | unsigned int idx = 0; |
| 174 | add_3D_tensor_argument(idx, _input, slice); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 175 | if (!_run_in_place) |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 176 | { |
| 177 | add_3D_tensor_argument(idx, _output, slice); |
| 178 | } |
| 179 | enqueue(queue, *this, slice, lws_hint()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 180 | } while (collapsed.slide_window_slice_3D(slice)); |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 181 | } |
| 182 | } // namespace arm_compute |