blob: 9ed9d7c5b00fd28d42208ecce144ff8d17aba305 [file] [log] [blame]
Michalis Spyrou04f089c2017-08-08 17:42:38 +01001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2017-2021, 2023 Arm Limited.
Michalis Spyrou04f089c2017-08-08 17:42:38 +01003 *
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/CLL2NormalizeLayerKernel.h"
Michalis Spyrou04f089c2017-08-08 17:42:38 +010025
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
Michalis Spyrou04f089c2017-08-08 17:42:38 +010029#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Utils.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000032#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
Michalis Spyrou04f089c2017-08-08 17:42:38 +010033#include "arm_compute/core/Validate.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010034
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035#include "src/core/CL/CLValidate.h"
36#include "src/core/helpers/AutoConfiguration.h"
37#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000038#include "support/StringSupport.h"
Michalis Spyrou04f089c2017-08-08 17:42:38 +010039
Manuel Bottini4b5c5882019-05-14 10:38:30 +010040namespace arm_compute
41{
John Richardson62385bc2018-04-20 13:11:36 +010042namespace
43{
Manuel Bottini4b5c5882019-05-14 10:38:30 +010044constexpr int max_input_tensor_dim = 3;
45
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010046Status
47validate_arguments(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, int axis, float epsilon)
John Richardson62385bc2018-04-20 13:11:36 +010048{
49 ARM_COMPUTE_UNUSED(epsilon);
50
Manuel Bottini4b5c5882019-05-14 10:38:30 +010051 const uint32_t actual_axis = wrap_around(axis, max_input_tensor_dim);
John Richardson62385bc2018-04-20 13:11:36 +010052 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, sum, output);
53 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum);
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000054 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +010055 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
Manuel Bottini4b5c5882019-05-14 10:38:30 +010056 ARM_COMPUTE_RETURN_ERROR_ON_MSG(actual_axis > 2, "Actual axis greater than 2 is not supported");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010057 ARM_COMPUTE_RETURN_ERROR_ON_MSG(actual_axis >= TensorShape::num_max_dimensions,
58 "Actual normalization axis greater than max number of dimensions");
John Richardson62385bc2018-04-20 13:11:36 +010059
60 // Reduce shape on axis
61 TensorShape sum_shape = input->tensor_shape();
Manuel Bottini4b5c5882019-05-14 10:38:30 +010062 sum_shape.set(actual_axis, 1);
John Richardson62385bc2018-04-20 13:11:36 +010063 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(sum->tensor_shape(), sum_shape);
64
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010065 if (output->total_size() != 0)
John Richardson62385bc2018-04-20 13:11:36 +010066 {
67 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +010068 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
John Richardson62385bc2018-04-20 13:11:36 +010069 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
70 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output->tensor_shape());
John Richardson62385bc2018-04-20 13:11:36 +010071 }
72
73 return Status{};
74}
John Richardson62385bc2018-04-20 13:11:36 +010075} // namespace
76
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +010077CLL2NormalizeLayerKernel::CLL2NormalizeLayerKernel()
78 : _input(nullptr), _sum(nullptr), _output(nullptr), _actual_axis(0), _epsilon(1e-12)
79{
Giorgio Arena4a95bba2021-06-28 11:00:27 +010080 _type = CLKernelType::ELEMENTWISE;
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +010081}
82
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010083void CLL2NormalizeLayerKernel::configure(
84 const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, int axis, float epsilon)
Michalis Spyrou04f089c2017-08-08 17:42:38 +010085{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010086 configure(CLKernelLibrary::get().get_compile_context(), input, sum, output, axis, epsilon);
87}
88
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010089void CLL2NormalizeLayerKernel::configure(const CLCompileContext &compile_context,
90 const ICLTensor *input,
91 const ICLTensor *sum,
92 ICLTensor *output,
93 int axis,
94 float epsilon)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010095{
John Richardson62385bc2018-04-20 13:11:36 +010096 ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output);
97 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), sum->info(), output->info(), axis, epsilon));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010098 auto padding_info = get_padding_info({input, sum, output});
Michalis Spyrou04f089c2017-08-08 17:42:38 +010099
Georgios Pinitas51403b52019-06-18 14:10:48 +0100100 _input = input;
101 _sum = sum;
102 _output = output;
103 _actual_axis = wrap_around(axis, max_input_tensor_dim);
104 _epsilon = epsilon;
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100105
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100106 const unsigned int vec_size_x =
107 adjust_vec_size(max_cl_vector_width / input->info()->element_size(), input->info()->dimension(0));
108 const int vec_size_x_leftovers = input->info()->dimension(0) % vec_size_x;
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100109
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100110 // Set build options
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100111 CLBuildOptions build_opts;
112 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
113 build_opts.add_option("-DVEC_SIZE_X=" + support::cpp11::to_string(vec_size_x));
114 build_opts.add_option("-DVEC_SIZE_LEFTOVER_X=" + support::cpp11::to_string(vec_size_x_leftovers));
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100115
116 // Create kernel
Michalis Spyrou5538d342018-11-14 08:10:13 +0000117 std::string kernel_name;
118 unsigned int idx = 0;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100119 switch (_actual_axis)
Michalis Spyrou5538d342018-11-14 08:10:13 +0000120 {
121 case 0:
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100122 kernel_name = "l2_normalize_x";
Usama Arifae0001e2019-03-26 13:44:01 +0000123 idx = num_arguments_per_2D_tensor() * 3;
Michalis Spyrou5538d342018-11-14 08:10:13 +0000124 break;
125 case 1:
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100126 kernel_name = "l2_normalize_y";
Michalis Spyrou5538d342018-11-14 08:10:13 +0000127 idx = num_arguments_per_2D_tensor() * 3;
128 break;
129 case 2:
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100130 kernel_name = "l2_normalize_z";
Michalis Spyrou5538d342018-11-14 08:10:13 +0000131 idx = num_arguments_per_3D_tensor() * 3;
132 break;
133 default:
Manuel Bottini4b5c5882019-05-14 10:38:30 +0100134 ARM_COMPUTE_ERROR("Axis not supported");
Michalis Spyrou5538d342018-11-14 08:10:13 +0000135 }
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100136 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100137
138 // Set epsilon argument
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100139 if (input->info()->data_type() == DataType::F32)
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100140 {
Georgios Pinitas51403b52019-06-18 14:10:48 +0100141 _kernel.setArg<cl_float>(idx, _epsilon);
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100142 }
143 else
144 {
Georgios Pinitas51403b52019-06-18 14:10:48 +0100145 _kernel.setArg<cl_half>(idx, _epsilon);
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100146 }
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100147
148 // Configure kernel window
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100149 Window win = calculate_max_window(*input->info(), Steps(vec_size_x));
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100150
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100151 // Output tensor auto initialization if not yet initialized
152 auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type());
153
154 ICLKernel::configure_internal(win);
155 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
John Richardson62385bc2018-04-20 13:11:36 +0100156}
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100157
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100158Status CLL2NormalizeLayerKernel::validate(
159 const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, int axis, float epsilon)
John Richardson62385bc2018-04-20 13:11:36 +0100160{
161 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, sum, output, axis, epsilon));
John Richardson62385bc2018-04-20 13:11:36 +0100162 return Status{};
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100163}
164
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000165void CLL2NormalizeLayerKernel::run(const Window &window, cl::CommandQueue &queue)
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100166{
167 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
168 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
169
170 Window window_sum(window);
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100171
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100172 switch (_actual_axis)
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100173 {
Michalis Spyrou5538d342018-11-14 08:10:13 +0000174 case 0:
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100175 {
176 window_sum.set(Window::DimX, Window::Dimension(0, 0, 0));
Usama Arifae0001e2019-03-26 13:44:01 +0000177 Window in_slice = window.first_slice_window_2D();
178 Window sum_slice = window_sum.first_slice_window_2D();
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100179 do
180 {
181 unsigned int idx = 0;
Usama Arifae0001e2019-03-26 13:44:01 +0000182 add_2D_tensor_argument(idx, _input, in_slice);
183 add_2D_tensor_argument(idx, _sum, sum_slice);
184 add_2D_tensor_argument(idx, _output, in_slice);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100185 enqueue(queue, *this, in_slice, lws_hint());
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100186 } while (window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(sum_slice));
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100187 }
188 break;
Michalis Spyrou5538d342018-11-14 08:10:13 +0000189 case 1:
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100190 {
191 window_sum.set(Window::DimY, Window::Dimension(0, 0, 0));
192 Window in_slice = window.first_slice_window_2D();
193 Window sum_slice = window_sum.first_slice_window_2D();
194 do
195 {
196 unsigned int idx = 0;
197 add_2D_tensor_argument(idx, _input, in_slice);
198 add_2D_tensor_argument(idx, _sum, sum_slice);
199 add_2D_tensor_argument(idx, _output, in_slice);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100200 enqueue(queue, *this, in_slice, lws_hint());
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100201 } while (window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(sum_slice));
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100202 }
203 break;
Michalis Spyrou5538d342018-11-14 08:10:13 +0000204 case 2:
205 {
206 window_sum.set(Window::DimZ, Window::Dimension(0, 0, 0));
207 Window in_slice = window.first_slice_window_3D();
208 Window sum_slice = window_sum.first_slice_window_3D();
209 do
210 {
211 unsigned int idx = 0;
212 add_3D_tensor_argument(idx, _input, in_slice);
213 add_3D_tensor_argument(idx, _sum, sum_slice);
214 add_3D_tensor_argument(idx, _output, in_slice);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100215 enqueue(queue, *this, in_slice, lws_hint());
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100216 } while (window.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(sum_slice));
Michalis Spyrou5538d342018-11-14 08:10:13 +0000217 }
218 break;
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100219 default:
220 ARM_COMPUTE_ERROR("Not supported");
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100221 }
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100222}
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +0100223} // namespace arm_compute