blob: 46c0747cf5e989758585f4e66d2f7b21ad19633c [file] [log] [blame]
Michalis Spyrou04f089c2017-08-08 17:42:38 +01001/*
Michalis Spyrou702dc0c2021-03-19 15:06:07 +00002 * Copyright (c) 2017-2021 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"
32#include "arm_compute/core/Validate.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/CL/CLValidate.h"
34#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
Michalis Spyrou04f089c2017-08-08 17:42:38 +010036
Matthew Bentham758b5ba2020-03-05 23:37:48 +000037#include "support/StringSupport.h"
Michalis Spyrou04f089c2017-08-08 17:42:38 +010038
Manuel Bottini4b5c5882019-05-14 10:38:30 +010039namespace arm_compute
40{
John Richardson62385bc2018-04-20 13:11:36 +010041namespace
42{
Manuel Bottini4b5c5882019-05-14 10:38:30 +010043constexpr int max_input_tensor_dim = 3;
44
45Status validate_arguments(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, int axis, float epsilon)
John Richardson62385bc2018-04-20 13:11:36 +010046{
47 ARM_COMPUTE_UNUSED(epsilon);
48
Manuel Bottini4b5c5882019-05-14 10:38:30 +010049 const uint32_t actual_axis = wrap_around(axis, max_input_tensor_dim);
John Richardson62385bc2018-04-20 13:11:36 +010050 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, sum, output);
51 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum);
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000052 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +010053 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
Manuel Bottini4b5c5882019-05-14 10:38:30 +010054 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");
John Richardson62385bc2018-04-20 13:11:36 +010056
57 // Reduce shape on axis
58 TensorShape sum_shape = input->tensor_shape();
Manuel Bottini4b5c5882019-05-14 10:38:30 +010059 sum_shape.set(actual_axis, 1);
John Richardson62385bc2018-04-20 13:11:36 +010060 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);
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +010065 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
John Richardson62385bc2018-04-20 13:11:36 +010066 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
67 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output->tensor_shape());
John Richardson62385bc2018-04-20 13:11:36 +010068 }
69
70 return Status{};
71}
John Richardson62385bc2018-04-20 13:11:36 +010072} // namespace
73
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +010074CLL2NormalizeLayerKernel::CLL2NormalizeLayerKernel()
75 : _input(nullptr), _sum(nullptr), _output(nullptr), _actual_axis(0), _epsilon(1e-12)
76{
Giorgio Arena4a95bba2021-06-28 11:00:27 +010077 _type = CLKernelType::ELEMENTWISE;
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +010078}
79
Manuel Bottini4b5c5882019-05-14 10:38:30 +010080void CLL2NormalizeLayerKernel::configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, int axis, float epsilon)
Michalis Spyrou04f089c2017-08-08 17:42:38 +010081{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010082 configure(CLKernelLibrary::get().get_compile_context(), input, sum, output, axis, epsilon);
83}
84
Manuel Bottini679fc962020-04-21 16:08:53 +010085void CLL2NormalizeLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, int axis, float epsilon)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010086{
John Richardson62385bc2018-04-20 13:11:36 +010087 ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output);
88 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), sum->info(), output->info(), axis, epsilon));
Manuel Bottini7a452fe2021-03-31 18:22:59 +010089 auto padding_info = get_padding_info({ input, sum, output });
Michalis Spyrou04f089c2017-08-08 17:42:38 +010090
Georgios Pinitas51403b52019-06-18 14:10:48 +010091 _input = input;
92 _sum = sum;
93 _output = output;
94 _actual_axis = wrap_around(axis, max_input_tensor_dim);
95 _epsilon = epsilon;
Michalis Spyrou04f089c2017-08-08 17:42:38 +010096
Manuel Bottini7a452fe2021-03-31 18:22:59 +010097 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
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100100 // Set build options
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100101 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));
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100105
106 // Create kernel
Michalis Spyrou5538d342018-11-14 08:10:13 +0000107 std::string kernel_name;
108 unsigned int idx = 0;
Manuel Bottini4b5c5882019-05-14 10:38:30 +0100109 switch(_actual_axis)
Michalis Spyrou5538d342018-11-14 08:10:13 +0000110 {
111 case 0:
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100112 kernel_name = "l2_normalize_x";
Usama Arifae0001e2019-03-26 13:44:01 +0000113 idx = num_arguments_per_2D_tensor() * 3;
Michalis Spyrou5538d342018-11-14 08:10:13 +0000114 break;
115 case 1:
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100116 kernel_name = "l2_normalize_y";
Michalis Spyrou5538d342018-11-14 08:10:13 +0000117 idx = num_arguments_per_2D_tensor() * 3;
118 break;
119 case 2:
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100120 kernel_name = "l2_normalize_z";
Michalis Spyrou5538d342018-11-14 08:10:13 +0000121 idx = num_arguments_per_3D_tensor() * 3;
122 break;
123 default:
Manuel Bottini4b5c5882019-05-14 10:38:30 +0100124 ARM_COMPUTE_ERROR("Axis not supported");
Michalis Spyrou5538d342018-11-14 08:10:13 +0000125 }
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100126 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100127
128 // Set epsilon argument
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100129 if(input->info()->data_type() == DataType::F32)
130 {
Georgios Pinitas51403b52019-06-18 14:10:48 +0100131 _kernel.setArg<cl_float>(idx, _epsilon);
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100132 }
133 else
134 {
Georgios Pinitas51403b52019-06-18 14:10:48 +0100135 _kernel.setArg<cl_half>(idx, _epsilon);
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100136 }
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100137
138 // Configure kernel window
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100139 Window win = calculate_max_window(*input->info(), Steps(vec_size_x));
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100140
Manuel Bottini7a452fe2021-03-31 18:22:59 +0100141 // 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));
John Richardson62385bc2018-04-20 13:11:36 +0100146}
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100147
Manuel Bottini4b5c5882019-05-14 10:38:30 +0100148Status CLL2NormalizeLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, int axis, float epsilon)
John Richardson62385bc2018-04-20 13:11:36 +0100149{
150 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, sum, output, axis, epsilon));
John Richardson62385bc2018-04-20 13:11:36 +0100151 return Status{};
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100152}
153
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000154void CLL2NormalizeLayerKernel::run(const Window &window, cl::CommandQueue &queue)
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100155{
156 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
157 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
158
159 Window window_sum(window);
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100160
Manuel Bottini4b5c5882019-05-14 10:38:30 +0100161 switch(_actual_axis)
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100162 {
Michalis Spyrou5538d342018-11-14 08:10:13 +0000163 case 0:
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100164 {
165 window_sum.set(Window::DimX, Window::Dimension(0, 0, 0));
Usama Arifae0001e2019-03-26 13:44:01 +0000166 Window in_slice = window.first_slice_window_2D();
167 Window sum_slice = window_sum.first_slice_window_2D();
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100168 do
169 {
170 unsigned int idx = 0;
Usama Arifae0001e2019-03-26 13:44:01 +0000171 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);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100174 enqueue(queue, *this, in_slice, lws_hint());
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100175 }
Usama Arifae0001e2019-03-26 13:44:01 +0000176 while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(sum_slice));
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100177 }
178 break;
Michalis Spyrou5538d342018-11-14 08:10:13 +0000179 case 1:
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100180 {
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);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100190 enqueue(queue, *this, in_slice, lws_hint());
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100191 }
192 while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(sum_slice));
193 }
194 break;
Michalis Spyrou5538d342018-11-14 08:10:13 +0000195 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);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100206 enqueue(queue, *this, in_slice, lws_hint());
Michalis Spyrou5538d342018-11-14 08:10:13 +0000207 }
208 while(window.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(sum_slice));
209 }
210 break;
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100211 default:
212 ARM_COMPUTE_ERROR("Not supported");
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100213 }
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100214}
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +0100215} // namespace arm_compute