blob: 779bf43922163425b77412f02529534311f7a2bf [file] [log] [blame]
Georgios Pinitas0bc78492019-03-18 20:07:37 +00001/*
Michalis Spyrou702dc0c2021-03-19 15:06:07 +00002 * Copyright (c) 2019-2021 Arm Limited.
Georgios Pinitas0bc78492019-03-18 20:07:37 +00003 *
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 Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLFFTRadixStageKernel.h"
Georgios Pinitas0bc78492019-03-18 20:07:37 +000025
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
Georgios Pinitas0bc78492019-03-18 20:07:37 +000028#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/TensorInfo.h"
Georgios Pinitas0bc78492019-03-18 20:07:37 +000030#include "arm_compute/core/Utils.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010031#include "src/core/CL/CLValidate.h"
32#include "src/core/helpers/AutoConfiguration.h"
33#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000034#include "support/StringSupport.h"
SiCongLi410e21e2020-12-11 15:07:53 +000035#include "support/ToolchainSupport.h"
Georgios Pinitas0bc78492019-03-18 20:07:37 +000036
37#include <cmath>
38
39namespace arm_compute
40{
41namespace
42{
Georgios Pinitas8be91482019-03-26 17:23:28 +000043Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const FFTRadixStageKernelInfo &config)
Georgios Pinitas0bc78492019-03-18 20:07:37 +000044{
45 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Giorgio Arenaea7de7b2020-12-10 16:49:39 +000046 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 2, DataType::F16, DataType::F32);
Georgios Pinitas0bc78492019-03-18 20:07:37 +000047 ARM_COMPUTE_RETURN_ERROR_ON(CLFFTRadixStageKernel::supported_radix().count(config.radix) == 0);
Georgios Pinitas8be91482019-03-26 17:23:28 +000048 ARM_COMPUTE_RETURN_ERROR_ON(std::set<unsigned int>({ 0, 1 }).count(config.axis) == 0);
49 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[config.axis] % config.radix);
Georgios Pinitas0bc78492019-03-18 20:07:37 +000050
51 // Checks performed when output is configured
52 if((output != nullptr) && (output->total_size() != 0))
53 {
54 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
55 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
56 }
57
58 return Status{};
59}
60
Georgios Pinitas8be91482019-03-26 17:23:28 +000061std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const FFTRadixStageKernelInfo &config)
Georgios Pinitas0bc78492019-03-18 20:07:37 +000062{
63 if(output != nullptr)
64 {
65 auto_init_if_empty(*output, *input);
66 }
67
Georgios Pinitas8be91482019-03-26 17:23:28 +000068 // Setup window steps
69 Steps steps;
70 steps.set(config.axis, config.radix);
71
72 Window win = calculate_max_window(*input, steps);
Georgios Pinitas0bc78492019-03-18 20:07:37 +000073
74 return std::make_pair(Status{}, win);
75}
76} // namespace
77
78CLFFTRadixStageKernel::CLFFTRadixStageKernel()
79 : _input(nullptr), _output(nullptr), _run_in_place(false)
80{
81}
82
Georgios Pinitas8be91482019-03-26 17:23:28 +000083void CLFFTRadixStageKernel::configure(ICLTensor *input, ICLTensor *output, const FFTRadixStageKernelInfo &config)
Georgios Pinitas0bc78492019-03-18 20:07:37 +000084{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010085 configure(CLKernelLibrary::get().get_compile_context(), input, output, config);
86}
87
Manuel Bottini256c0b92020-04-21 13:29:30 +010088void CLFFTRadixStageKernel::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, const FFTRadixStageKernelInfo &config)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010089{
Georgios Pinitas0bc78492019-03-18 20:07:37 +000090 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
91 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr, config));
Manuel Bottinib6869dd2020-12-16 15:34:25 +000092 auto padding_info = get_padding_info({ input, output });
Georgios Pinitas0bc78492019-03-18 20:07:37 +000093
94 _input = input;
95 _output = output;
96 _run_in_place = (output == nullptr) || (output == input);
97
98 // Create build options
99 CLBuildOptions build_opts;
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000100 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
Georgios Pinitas0bc78492019-03-18 20:07:37 +0000101 build_opts.add_option_if(_run_in_place, "-DIN_PLACE");
102
103 // Create kernel
104 std::string kernel_name = "fft";
105 kernel_name += "_radix_" + support::cpp11::to_string(config.radix);
106 kernel_name += (config.is_first_stage) ? "_first_stage" : "";
107 kernel_name += "_axis_" + support::cpp11::to_string(config.axis);
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100108 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Georgios Pinitas0bc78492019-03-18 20:07:37 +0000109
110 // Set static arguments if not the first stage
111 if(!config.is_first_stage)
112 {
113 const unsigned int Ni = config.Nx * config.radix;
114 const float exp_const = (-2.0 * M_PI) / static_cast<float>(Ni);
115 unsigned int idx = (1 + (_run_in_place ? 0 : 1)) * num_arguments_per_3D_tensor(); // Skip the input and output parameters
116 _kernel.setArg<cl_uint>(idx++, config.Nx);
117 _kernel.setArg<cl_uint>(idx++, Ni);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000118 _kernel.setArg<cl_float>(idx, exp_const);
Georgios Pinitas0bc78492019-03-18 20:07:37 +0000119 }
120
121 // Configure kernel window
122 auto win_config = validate_and_configure_window(input->info(), (_run_in_place) ? nullptr : output->info(), config);
123 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
124 ICLKernel::configure_internal(win_config.second);
125
126 // Set config_id for enabling LWS tuning
127 _config_id = kernel_name;
128 _config_id += "_";
129 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
130 _config_id += "_";
131 _config_id += support::cpp11::to_string(input->info()->dimension(0));
132 _config_id += "_";
133 _config_id += support::cpp11::to_string(input->info()->dimension(1));
Manuel Bottinib6869dd2020-12-16 15:34:25 +0000134 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Georgios Pinitas0bc78492019-03-18 20:07:37 +0000135}
136
Georgios Pinitas8be91482019-03-26 17:23:28 +0000137Status CLFFTRadixStageKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const FFTRadixStageKernelInfo &config)
Georgios Pinitas0bc78492019-03-18 20:07:37 +0000138{
139 const bool run_in_place = (output == nullptr) || (output == input);
140 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, config));
141 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(),
142 (run_in_place) ? nullptr : output->clone().get(),
143 config)
144 .first);
145
146 return Status{};
147}
148
149std::set<unsigned int> CLFFTRadixStageKernel::supported_radix()
150{
151 return std::set<unsigned int> { 2, 3, 4, 5, 7, 8 };
152}
153
154void CLFFTRadixStageKernel::run(const Window &window, cl::CommandQueue &queue)
155{
156 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
157 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
158
159 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
160 Window slice = collapsed.first_slice_window_3D();
161
162 do
163 {
164 unsigned int idx = 0;
165 add_3D_tensor_argument(idx, _input, slice);
166 if(!_run_in_place)
167 {
168 add_3D_tensor_argument(idx, _output, slice);
169 }
170 enqueue(queue, *this, slice, lws_hint());
171 }
172 while(collapsed.slide_window_slice_3D(slice));
173}
174} // namespace arm_compute