xref: /aosp_15_r20/external/ComputeLibrary/src/core/CL/kernels/CLMeanStdDevNormalizationKernel.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2019-2022 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  */
24 #include "src/core/CL/kernels/CLMeanStdDevNormalizationKernel.h"
25 
26 #include "arm_compute/core/CL/CLHelpers.h"
27 #include "arm_compute/core/CL/CLKernelLibrary.h"
28 #include "arm_compute/core/CL/ICLTensor.h"
29 #include "arm_compute/core/CL/OpenCL.h"
30 #include "arm_compute/core/Helpers.h"
31 #include "arm_compute/core/TensorInfo.h"
32 #include "src/core/CL/CLValidate.h"
33 #include "src/core/helpers/AutoConfiguration.h"
34 #include "src/core/helpers/WindowHelpers.h"
35 #include "support/StringSupport.h"
36 
37 namespace arm_compute
38 {
39 namespace
40 {
validate_arguments(const ITensorInfo * input,const ITensorInfo * output,float epsilon)41 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, float epsilon)
42 {
43     ARM_COMPUTE_UNUSED(epsilon);
44     ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
45     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
46     ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() > 2, "Input tensor cannot have more than 2 dimensions");
47     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
48 
49     // Checks performed when output is configured
50     if((output != nullptr) && (output->total_size() != 0))
51     {
52         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
53         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
54     }
55     return Status{};
56 }
57 } // namespace
58 
CLMeanStdDevNormalizationKernel()59 CLMeanStdDevNormalizationKernel::CLMeanStdDevNormalizationKernel()
60     : _input(nullptr), _output(nullptr), _run_in_place(false)
61 {
62     _type = CLKernelType::ELEMENTWISE;
63 }
64 
configure(ICLTensor * input,ICLTensor * output,float epsilon)65 void CLMeanStdDevNormalizationKernel::configure(ICLTensor *input, ICLTensor *output, float epsilon)
66 {
67     configure(CLKernelLibrary::get().get_compile_context(), input, output, epsilon);
68 }
69 
configure(const CLCompileContext & compile_context,ICLTensor * input,ICLTensor * output,float epsilon)70 void CLMeanStdDevNormalizationKernel::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, float epsilon)
71 {
72     ARM_COMPUTE_ERROR_ON_NULLPTR(input);
73 
74     _run_in_place = (output == nullptr) || (output == input);
75 
76     ARM_COMPUTE_ERROR_THROW_ON(CLMeanStdDevNormalizationKernel::validate(input->info(), (output != nullptr) ? output->info() : nullptr, epsilon));
77 
78     if(output != nullptr)
79     {
80         auto_init_if_empty(*output->info(), *input->info());
81     }
82 
83     _input  = input;
84     _output = output;
85 
86     const unsigned int num_elems_processed_per_iteration = adjust_vec_size(16 / input->info()->element_size(), input->info()->dimension(0));
87 
88     // Set build options
89     CLBuildOptions build_opts;
90     build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
91     build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
92     build_opts.add_option("-DEPSILON=" + float_to_string_with_full_precision(epsilon));
93     build_opts.add_option("-DWIDTH=" + support::cpp11::to_string(input->info()->dimension(0)));
94     build_opts.add_option_if(input->info()->data_type() == DataType::F16, "-DMEANSTDNORM_HALF");
95     build_opts.add_option_if(_run_in_place, "-DIN_PLACE");
96 
97     // Create kernel
98     _kernel = create_kernel(compile_context, "mean_stddev_normalization", build_opts.options());
99 
100     // Configure kernel window
101     Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
102     ICLKernel::configure_internal(win);
103 
104     // Set config_id for enabling LWS tuning
105     _config_id = "mean_stddev_normalization_layer_";
106     _config_id += lower_string(string_from_data_type(input->info()->data_type()));
107     _config_id += "_";
108     _config_id += support::cpp11::to_string(input->info()->dimension(0));
109     _config_id += "_";
110     _config_id += support::cpp11::to_string(input->info()->dimension(1));
111 }
112 
validate(const ITensorInfo * input,const ITensorInfo * output,float epsilon)113 Status CLMeanStdDevNormalizationKernel::validate(const ITensorInfo *input, const ITensorInfo *output, float epsilon)
114 {
115     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, epsilon));
116     return Status{};
117 }
118 
run(const Window & window,cl::CommandQueue & queue)119 void CLMeanStdDevNormalizationKernel::run(const Window &window, cl::CommandQueue &queue)
120 {
121     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
122     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
123 
124     Window slice = window.first_slice_window_2D();
125     // Set slice step equal to width to force gws[0] to 1, as each thread normalizes across all rows
126     slice.set_dimension_step(Window::DimX, _input->info()->dimension(0));
127 
128     do
129     {
130         unsigned int idx = 0;
131         add_2D_tensor_argument(idx, _input, slice);
132         add_2D_tensor_argument_if((!_run_in_place), idx, _output, slice);
133 
134         enqueue(queue, *this, slice, lws_hint());
135     }
136     while(window.slide_window_slice_2D(slice));
137 }
138 } // namespace arm_compute
139