blob: 5df8ca6025ad0c89a5fd19d98ad51f34b981dc57 [file] [log] [blame]
Georgios Pinitas0bc78492019-03-18 20:07:37 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 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"
Georgios Pinitas0bc78492019-03-18 20:07:37 +000035
36#include <cmath>
37
38namespace arm_compute
39{
40namespace
41{
Georgios Pinitas8be91482019-03-26 17:23:28 +000042Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const FFTRadixStageKernelInfo &config)
Georgios Pinitas0bc78492019-03-18 20:07:37 +000043{
44 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Giorgio Arenaea7de7b2020-12-10 16:49:39 +000045 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 2, DataType::F16, DataType::F32);
Georgios Pinitas0bc78492019-03-18 20:07:37 +000046 ARM_COMPUTE_RETURN_ERROR_ON(CLFFTRadixStageKernel::supported_radix().count(config.radix) == 0);
Georgios Pinitas8be91482019-03-26 17:23:28 +000047 ARM_COMPUTE_RETURN_ERROR_ON(std::set<unsigned int>({ 0, 1 }).count(config.axis) == 0);
48 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[config.axis] % config.radix);
Georgios Pinitas0bc78492019-03-18 20:07:37 +000049
50 // Checks performed when output is configured
51 if((output != nullptr) && (output->total_size() != 0))
52 {
53 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
54 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
55 }
56
57 return Status{};
58}
59
Georgios Pinitas8be91482019-03-26 17:23:28 +000060std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const FFTRadixStageKernelInfo &config)
Georgios Pinitas0bc78492019-03-18 20:07:37 +000061{
62 if(output != nullptr)
63 {
64 auto_init_if_empty(*output, *input);
65 }
66
Georgios Pinitas8be91482019-03-26 17:23:28 +000067 // Setup window steps
68 Steps steps;
69 steps.set(config.axis, config.radix);
70
71 Window win = calculate_max_window(*input, steps);
Georgios Pinitas0bc78492019-03-18 20:07:37 +000072 if(output != nullptr)
73 {
74 output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
75 }
76
77 return std::make_pair(Status{}, win);
78}
79} // namespace
80
81CLFFTRadixStageKernel::CLFFTRadixStageKernel()
82 : _input(nullptr), _output(nullptr), _run_in_place(false)
83{
84}
85
Georgios Pinitas8be91482019-03-26 17:23:28 +000086void CLFFTRadixStageKernel::configure(ICLTensor *input, ICLTensor *output, const FFTRadixStageKernelInfo &config)
Georgios Pinitas0bc78492019-03-18 20:07:37 +000087{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010088 configure(CLKernelLibrary::get().get_compile_context(), input, output, config);
89}
90
Manuel Bottini256c0b92020-04-21 13:29:30 +010091void CLFFTRadixStageKernel::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, const FFTRadixStageKernelInfo &config)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010092{
Georgios Pinitas0bc78492019-03-18 20:07:37 +000093 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
94 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr, config));
Manuel Bottinib6869dd2020-12-16 15:34:25 +000095 auto padding_info = get_padding_info({ input, output });
Georgios Pinitas0bc78492019-03-18 20:07:37 +000096
97 _input = input;
98 _output = output;
99 _run_in_place = (output == nullptr) || (output == input);
100
101 // Create build options
102 CLBuildOptions build_opts;
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000103 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
Georgios Pinitas0bc78492019-03-18 20:07:37 +0000104 build_opts.add_option_if(_run_in_place, "-DIN_PLACE");
105
106 // Create kernel
107 std::string kernel_name = "fft";
108 kernel_name += "_radix_" + support::cpp11::to_string(config.radix);
109 kernel_name += (config.is_first_stage) ? "_first_stage" : "";
110 kernel_name += "_axis_" + support::cpp11::to_string(config.axis);
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100111 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Georgios Pinitas0bc78492019-03-18 20:07:37 +0000112
113 // Set static arguments if not the first stage
114 if(!config.is_first_stage)
115 {
116 const unsigned int Ni = config.Nx * config.radix;
117 const float exp_const = (-2.0 * M_PI) / static_cast<float>(Ni);
118 unsigned int idx = (1 + (_run_in_place ? 0 : 1)) * num_arguments_per_3D_tensor(); // Skip the input and output parameters
119 _kernel.setArg<cl_uint>(idx++, config.Nx);
120 _kernel.setArg<cl_uint>(idx++, Ni);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000121 _kernel.setArg<cl_float>(idx, exp_const);
Georgios Pinitas0bc78492019-03-18 20:07:37 +0000122 }
123
124 // Configure kernel window
125 auto win_config = validate_and_configure_window(input->info(), (_run_in_place) ? nullptr : output->info(), config);
126 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
127 ICLKernel::configure_internal(win_config.second);
128
129 // Set config_id for enabling LWS tuning
130 _config_id = kernel_name;
131 _config_id += "_";
132 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
133 _config_id += "_";
134 _config_id += support::cpp11::to_string(input->info()->dimension(0));
135 _config_id += "_";
136 _config_id += support::cpp11::to_string(input->info()->dimension(1));
Manuel Bottinib6869dd2020-12-16 15:34:25 +0000137 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Georgios Pinitas0bc78492019-03-18 20:07:37 +0000138}
139
Georgios Pinitas8be91482019-03-26 17:23:28 +0000140Status CLFFTRadixStageKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const FFTRadixStageKernelInfo &config)
Georgios Pinitas0bc78492019-03-18 20:07:37 +0000141{
142 const bool run_in_place = (output == nullptr) || (output == input);
143 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, config));
144 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(),
145 (run_in_place) ? nullptr : output->clone().get(),
146 config)
147 .first);
148
149 return Status{};
150}
151
152std::set<unsigned int> CLFFTRadixStageKernel::supported_radix()
153{
154 return std::set<unsigned int> { 2, 3, 4, 5, 7, 8 };
155}
156
157void CLFFTRadixStageKernel::run(const Window &window, cl::CommandQueue &queue)
158{
159 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
160 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
161
162 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
163 Window slice = collapsed.first_slice_window_3D();
164
165 do
166 {
167 unsigned int idx = 0;
168 add_3D_tensor_argument(idx, _input, slice);
169 if(!_run_in_place)
170 {
171 add_3D_tensor_argument(idx, _output, slice);
172 }
173 enqueue(queue, *this, slice, lws_hint());
174 }
175 while(collapsed.slide_window_slice_3D(slice));
176}
177} // namespace arm_compute