xref: /aosp_15_r20/external/ComputeLibrary/src/core/CL/kernels/CLL2NormalizeLayerKernel.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-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  */
24 #include "src/core/CL/kernels/CLL2NormalizeLayerKernel.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/Helpers.h"
30 #include "arm_compute/core/TensorInfo.h"
31 #include "arm_compute/core/Utils.h"
32 #include "arm_compute/core/Validate.h"
33 #include "src/core/CL/CLValidate.h"
34 #include "src/core/helpers/AutoConfiguration.h"
35 #include "src/core/helpers/WindowHelpers.h"
36 
37 #include "support/StringSupport.h"
38 
39 namespace arm_compute
40 {
41 namespace
42 {
43 constexpr int max_input_tensor_dim = 3;
44 
validate_arguments(const ITensorInfo * input,const ITensorInfo * sum,const ITensorInfo * output,int axis,float epsilon)45 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, int axis, float epsilon)
46 {
47     ARM_COMPUTE_UNUSED(epsilon);
48 
49     const uint32_t actual_axis = wrap_around(axis, max_input_tensor_dim);
50     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, sum, output);
51     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum);
52     ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
53     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
54     ARM_COMPUTE_RETURN_ERROR_ON_MSG(actual_axis > 2, "Actual axis greater than 2 is not supported");
55     ARM_COMPUTE_RETURN_ERROR_ON_MSG(actual_axis >= TensorShape::num_max_dimensions, "Actual normalization axis greater than max number of dimensions");
56 
57     // Reduce shape on axis
58     TensorShape sum_shape = input->tensor_shape();
59     sum_shape.set(actual_axis, 1);
60     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(sum->tensor_shape(), sum_shape);
61 
62     if(output->total_size() != 0)
63     {
64         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
65         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
66         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
67         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output->tensor_shape());
68     }
69 
70     return Status{};
71 }
72 } // namespace
73 
CLL2NormalizeLayerKernel()74 CLL2NormalizeLayerKernel::CLL2NormalizeLayerKernel()
75     : _input(nullptr), _sum(nullptr), _output(nullptr), _actual_axis(0), _epsilon(1e-12)
76 {
77     _type = CLKernelType::ELEMENTWISE;
78 }
79 
configure(const ICLTensor * input,const ICLTensor * sum,ICLTensor * output,int axis,float epsilon)80 void CLL2NormalizeLayerKernel::configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, int axis, float epsilon)
81 {
82     configure(CLKernelLibrary::get().get_compile_context(), input, sum, output, axis, epsilon);
83 }
84 
configure(const CLCompileContext & compile_context,const ICLTensor * input,const ICLTensor * sum,ICLTensor * output,int axis,float epsilon)85 void CLL2NormalizeLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, int axis, float epsilon)
86 {
87     ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output);
88     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), sum->info(), output->info(), axis, epsilon));
89     auto padding_info = get_padding_info({ input, sum, output });
90 
91     _input       = input;
92     _sum         = sum;
93     _output      = output;
94     _actual_axis = wrap_around(axis, max_input_tensor_dim);
95     _epsilon     = epsilon;
96 
97     const unsigned int vec_size_x           = adjust_vec_size(max_cl_vector_width / input->info()->element_size(), input->info()->dimension(0));
98     const int          vec_size_x_leftovers = input->info()->dimension(0) % vec_size_x;
99 
100     // Set build options
101     CLBuildOptions build_opts;
102     build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
103     build_opts.add_option("-DVEC_SIZE_X=" + support::cpp11::to_string(vec_size_x));
104     build_opts.add_option("-DVEC_SIZE_LEFTOVER_X=" + support::cpp11::to_string(vec_size_x_leftovers));
105 
106     // Create kernel
107     std::string  kernel_name;
108     unsigned int idx = 0;
109     switch(_actual_axis)
110     {
111         case 0:
112             kernel_name = "l2_normalize_x";
113             idx         = num_arguments_per_2D_tensor() * 3;
114             break;
115         case 1:
116             kernel_name = "l2_normalize_y";
117             idx         = num_arguments_per_2D_tensor() * 3;
118             break;
119         case 2:
120             kernel_name = "l2_normalize_z";
121             idx         = num_arguments_per_3D_tensor() * 3;
122             break;
123         default:
124             ARM_COMPUTE_ERROR("Axis not supported");
125     }
126     _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
127 
128     // Set epsilon argument
129     if(input->info()->data_type() == DataType::F32)
130     {
131         _kernel.setArg<cl_float>(idx, _epsilon);
132     }
133     else
134     {
135         _kernel.setArg<cl_half>(idx, _epsilon);
136     }
137 
138     // Configure kernel window
139     Window win = calculate_max_window(*input->info(), Steps(vec_size_x));
140 
141     // Output tensor auto initialization if not yet initialized
142     auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type());
143 
144     ICLKernel::configure_internal(win);
145     ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
146 }
147 
validate(const ITensorInfo * input,const ITensorInfo * sum,const ITensorInfo * output,int axis,float epsilon)148 Status CLL2NormalizeLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, int axis, float epsilon)
149 {
150     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, sum, output, axis, epsilon));
151     return Status{};
152 }
153 
run(const Window & window,cl::CommandQueue & queue)154 void CLL2NormalizeLayerKernel::run(const Window &window, cl::CommandQueue &queue)
155 {
156     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
157     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
158 
159     Window window_sum(window);
160 
161     switch(_actual_axis)
162     {
163         case 0:
164         {
165             window_sum.set(Window::DimX, Window::Dimension(0, 0, 0));
166             Window in_slice  = window.first_slice_window_2D();
167             Window sum_slice = window_sum.first_slice_window_2D();
168             do
169             {
170                 unsigned int idx = 0;
171                 add_2D_tensor_argument(idx, _input, in_slice);
172                 add_2D_tensor_argument(idx, _sum, sum_slice);
173                 add_2D_tensor_argument(idx, _output, in_slice);
174                 enqueue(queue, *this, in_slice, lws_hint());
175             }
176             while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(sum_slice));
177         }
178         break;
179         case 1:
180         {
181             window_sum.set(Window::DimY, Window::Dimension(0, 0, 0));
182             Window in_slice  = window.first_slice_window_2D();
183             Window sum_slice = window_sum.first_slice_window_2D();
184             do
185             {
186                 unsigned int idx = 0;
187                 add_2D_tensor_argument(idx, _input, in_slice);
188                 add_2D_tensor_argument(idx, _sum, sum_slice);
189                 add_2D_tensor_argument(idx, _output, in_slice);
190                 enqueue(queue, *this, in_slice, lws_hint());
191             }
192             while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(sum_slice));
193         }
194         break;
195         case 2:
196         {
197             window_sum.set(Window::DimZ, Window::Dimension(0, 0, 0));
198             Window in_slice  = window.first_slice_window_3D();
199             Window sum_slice = window_sum.first_slice_window_3D();
200             do
201             {
202                 unsigned int idx = 0;
203                 add_3D_tensor_argument(idx, _input, in_slice);
204                 add_3D_tensor_argument(idx, _sum, sum_slice);
205                 add_3D_tensor_argument(idx, _output, in_slice);
206                 enqueue(queue, *this, in_slice, lws_hint());
207             }
208             while(window.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(sum_slice));
209         }
210         break;
211         default:
212             ARM_COMPUTE_ERROR("Not supported");
213     }
214 }
215 } // namespace arm_compute
216