Compute Library
 21.11
CLFFTRadixStageKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2021 Arm Limited.
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  */
25 
30 #include "arm_compute/core/Utils.h"
31 #include "src/core/CL/CLValidate.h"
34 #include "support/StringSupport.h"
36 
37 #include <cmath>
38 
39 namespace arm_compute
40 {
41 namespace
42 {
43 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const FFTRadixStageKernelInfo &config)
44 {
48  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);
50 
51  // Checks performed when output is configured
52  if((output != nullptr) && (output->total_size() != 0))
53  {
56  }
57 
58  return Status{};
59 }
60 
61 std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const FFTRadixStageKernelInfo &config)
62 {
63  if(output != nullptr)
64  {
65  auto_init_if_empty(*output, *input);
66  }
67 
68  // Setup window steps
69  Steps steps;
70  steps.set(config.axis, config.radix);
71 
72  Window win = calculate_max_window(*input, steps);
73 
74  return std::make_pair(Status{}, win);
75 }
76 } // namespace
77 
79  : _input(nullptr), _output(nullptr), _run_in_place(false)
80 {
82 }
83 
85 {
86  configure(CLKernelLibrary::get().get_compile_context(), input, output, config);
87 }
88 
89 void CLFFTRadixStageKernel::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, const FFTRadixStageKernelInfo &config)
90 {
92  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr, config));
93  auto padding_info = get_padding_info({ input, output });
94 
95  _input = input;
96  _output = output;
97  _run_in_place = (output == nullptr) || (output == input);
98 
99  // Create build options
100  CLBuildOptions build_opts;
101  build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
102  build_opts.add_option_if(_run_in_place, "-DIN_PLACE");
103 
104  // Create kernel
105  std::string kernel_name = "fft";
106  kernel_name += "_radix_" + support::cpp11::to_string(config.radix);
107  kernel_name += (config.is_first_stage) ? "_first_stage" : "";
108  kernel_name += "_axis_" + support::cpp11::to_string(config.axis);
109  _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
110 
111  // Set static arguments if not the first stage
112  if(!config.is_first_stage)
113  {
114  const unsigned int Ni = config.Nx * config.radix;
115  const float exp_const = (-2.0 * M_PI) / static_cast<float>(Ni);
116  unsigned int idx = (1 + (_run_in_place ? 0 : 1)) * num_arguments_per_3D_tensor(); // Skip the input and output parameters
117  _kernel.setArg<cl_uint>(idx++, config.Nx);
118  _kernel.setArg<cl_uint>(idx++, Ni);
119  _kernel.setArg<cl_float>(idx, exp_const);
120  }
121 
122  // Configure kernel window
123  auto win_config = validate_and_configure_window(input->info(), (_run_in_place) ? nullptr : output->info(), config);
124  ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
125  ICLKernel::configure_internal(win_config.second);
126 
127  // Set config_id for enabling LWS tuning
128  _config_id = kernel_name;
129  _config_id += "_";
130  _config_id += lower_string(string_from_data_type(input->info()->data_type()));
131  _config_id += "_";
132  _config_id += support::cpp11::to_string(input->info()->dimension(0));
133  _config_id += "_";
134  _config_id += support::cpp11::to_string(input->info()->dimension(1));
136 }
137 
139 {
140  const bool run_in_place = (output == nullptr) || (output == input);
141  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, config));
142  ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(),
143  (run_in_place) ? nullptr : output->clone().get(),
144  config)
145  .first);
146 
147  return Status{};
148 }
149 
151 {
152  return std::set<unsigned int> { 2, 3, 4, 5, 7, 8 };
153 }
154 
155 void CLFFTRadixStageKernel::run(const Window &window, cl::CommandQueue &queue)
156 {
159 
161  Window slice = collapsed.first_slice_window_3D();
162 
163  do
164  {
165  unsigned int idx = 0;
166  add_3D_tensor_argument(idx, _input, slice);
167  if(!_run_in_place)
168  {
169  add_3D_tensor_argument(idx, _output, slice);
170  }
171  enqueue(queue, *this, slice, lws_hint());
172  }
173  while(collapsed.slide_window_slice_3D(slice));
174 }
175 } // namespace arm_compute
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
#define ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(tensor)
Definition: CLValidate.h:35
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
void enqueue(cl::CommandQueue &queue, ICLKernel &kernel, const Window &window, const cl::NDRange &lws_hint=CLKernelLibrary::get().default_ndrange(), bool use_dummy_work_items=false)
Add the kernel to the command queue with the given window.
Definition: ICLKernel.cpp:32
const StringSet & options() const
Gets the current options list set.
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:318
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
std::string to_string(T &&value)
Convert integer and float values to string.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
1 channel, 1 F32 per channel
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
void configure(ICLTensor *input, ICLTensor *output, const FFTRadixStageKernelInfo &config)
Set the input and output tensors.
#define M_PI
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context...
unsigned int axis
Axis to run the kernel on.
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...
Status class.
Definition: Error.h:52
std::string lower_string(const std::string &val)
Lower a given string.
Definition: Utils.cpp:326
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 3D tensor&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: ICLKernel.h:214
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
void add_option(std::string option)
Adds option to the existing build option list.
cl::Kernel create_kernel(const CLCompileContext &ctx, const std::string &kernel_name, const std::set< std::string > &build_opts=std::set< std::string >())
Creates an opencl kernel using a compile context.
Definition: CLHelpers.cpp:391
const std::string & string_from_data_type(DataType dt)
Convert a data type identity into a string.
Definition: Utils.cpp:135
static constexpr unsigned int num_arguments_per_3D_tensor()
Returns the number of arguments enqueued per 3D tensor object.
Definition: ICLKernel.h:256
Window collapse_if_possible(const Window &full_window, size_t first, size_t last, bool *has_collapsed=nullptr) const
Collapse the dimensions between first and last if possible.
Definition: Window.inl:68
std::string get_cl_type_from_data_type(const DataType &dt)
Translates a tensor data type to the appropriate OpenCL type.
Definition: CLHelpers.cpp:39
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
Descriptor used by the FFT core kernels.
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
void add_option_if(bool cond, std::string option)
Adds option if a given condition is true;.
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const FFTRadixStageKernelInfo &config)
Static function to check if given info will lead to a valid configuration of CLFFTRadixStageKernel.
Elementeise CL kernel type.
Definition: CLTypes.h:84
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:335
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
bool has_padding_changed(const std::unordered_map< const ITensorInfo *, PaddingSize > &padding_map)
Check if the previously stored padding info has changed after configuring a kernel.
Definition: Utils.cpp:533
CLCompileContext class.
static std::set< unsigned int > supported_radix()
Returns the radix that are support by the FFT kernel.
unsigned int radix
Radix to use.
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:439
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
bool is_first_stage
Flags if the FFT kernels is the first stage of a decomposed FFT.
unsigned int Nx
Nx coefficient.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:541
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:788
std::unordered_map< const ITensorInfo *, PaddingSize > get_padding_info(std::initializer_list< const ITensorInfo *> infos)
Stores padding information before configuring a kernel.
Definition: Utils.cpp:518
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:291
std::string kernel_name
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)