blob: 9936e29c5f3a2a1f3f82997f6b2b6f2d5db841fa [file] [log] [blame]
Michalis Spyrou04f089c2017-08-08 17:42:38 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 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 */
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000024#include "arm_compute/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
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +010045constexpr unsigned int num_elems_processed_per_iteration = 16;
46
Manuel Bottini4b5c5882019-05-14 10:38:30 +010047Status validate_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");
57 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 +010058
59 // Reduce shape on axis
60 TensorShape sum_shape = input->tensor_shape();
Manuel Bottini4b5c5882019-05-14 10:38:30 +010061 sum_shape.set(actual_axis, 1);
John Richardson62385bc2018-04-20 13:11:36 +010062 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(sum->tensor_shape(), sum_shape);
63
64 if(output->total_size() != 0)
65 {
66 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +010067 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
John Richardson62385bc2018-04-20 13:11:36 +010068 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
69 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output->tensor_shape());
John Richardson62385bc2018-04-20 13:11:36 +010070 }
71
72 return Status{};
73}
74
75std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
76{
John Richardson62385bc2018-04-20 13:11:36 +010077 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
78
79 // Output tensor auto initialization if not yet initialized
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010080 auto_init_if_empty(*output, input->tensor_shape(), 1, input->data_type());
John Richardson62385bc2018-04-20 13:11:36 +010081
82 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
83 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
84
85 bool window_changed = update_window_and_padding(win, input_access, output_access);
86 output_access.set_valid_region(win, input->valid_region());
87
88 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
89
90 return std::make_tuple(err, win);
91}
92} // namespace
93
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +010094CLL2NormalizeLayerKernel::CLL2NormalizeLayerKernel()
95 : _input(nullptr), _sum(nullptr), _output(nullptr), _actual_axis(0), _epsilon(1e-12)
96{
97}
98
Manuel Bottini4b5c5882019-05-14 10:38:30 +010099void CLL2NormalizeLayerKernel::configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, int axis, float epsilon)
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100100{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100101 configure(CLKernelLibrary::get().get_compile_context(), input, sum, output, axis, epsilon);
102}
103
Manuel Bottini679fc962020-04-21 16:08:53 +0100104void 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 +0100105{
John Richardson62385bc2018-04-20 13:11:36 +0100106 ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output);
107 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), sum->info(), output->info(), axis, epsilon));
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100108
Georgios Pinitas51403b52019-06-18 14:10:48 +0100109 _input = input;
110 _sum = sum;
111 _output = output;
112 _actual_axis = wrap_around(axis, max_input_tensor_dim);
113 _epsilon = epsilon;
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100114
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100115 // Set build options
116 std::set<std::string> build_opts;
117 build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
118 build_opts.emplace(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)));
119
120 // Create kernel
Michalis Spyrou5538d342018-11-14 08:10:13 +0000121 std::string kernel_name;
122 unsigned int idx = 0;
Manuel Bottini4b5c5882019-05-14 10:38:30 +0100123 switch(_actual_axis)
Michalis Spyrou5538d342018-11-14 08:10:13 +0000124 {
125 case 0:
126 kernel_name = "x";
Usama Arifae0001e2019-03-26 13:44:01 +0000127 idx = num_arguments_per_2D_tensor() * 3;
Michalis Spyrou5538d342018-11-14 08:10:13 +0000128 break;
129 case 1:
130 kernel_name = "y";
131 idx = num_arguments_per_2D_tensor() * 3;
132 break;
133 case 2:
134 kernel_name = "z";
135 idx = num_arguments_per_3D_tensor() * 3;
136 break;
137 default:
Manuel Bottini4b5c5882019-05-14 10:38:30 +0100138 ARM_COMPUTE_ERROR("Axis not supported");
Michalis Spyrou5538d342018-11-14 08:10:13 +0000139 }
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100140 _kernel = create_kernel(compile_context, "l2_normalize_" + kernel_name, build_opts);
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100141
142 // Set epsilon argument
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100143 if(input->info()->data_type() == DataType::F32)
144 {
Georgios Pinitas51403b52019-06-18 14:10:48 +0100145 _kernel.setArg<cl_float>(idx, _epsilon);
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100146 }
147 else
148 {
Georgios Pinitas51403b52019-06-18 14:10:48 +0100149 _kernel.setArg<cl_half>(idx, _epsilon);
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100150 }
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100151
152 // Configure kernel window
John Richardson62385bc2018-04-20 13:11:36 +0100153 auto win_config = validate_and_configure_window(_input->info(), _output->info());
154 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100155
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100156 ICLKernel::configure_internal(std::get<1>(win_config));
John Richardson62385bc2018-04-20 13:11:36 +0100157}
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100158
Manuel Bottini4b5c5882019-05-14 10:38:30 +0100159Status CLL2NormalizeLayerKernel::validate(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));
162 ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get())));
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100163
John Richardson62385bc2018-04-20 13:11:36 +0100164 return Status{};
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100165}
166
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000167void CLL2NormalizeLayerKernel::run(const Window &window, cl::CommandQueue &queue)
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100168{
169 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
170 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
171
172 Window window_sum(window);
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100173
Manuel Bottini4b5c5882019-05-14 10:38:30 +0100174 switch(_actual_axis)
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100175 {
Michalis Spyrou5538d342018-11-14 08:10:13 +0000176 case 0:
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100177 {
178 window_sum.set(Window::DimX, Window::Dimension(0, 0, 0));
Usama Arifae0001e2019-03-26 13:44:01 +0000179 Window in_slice = window.first_slice_window_2D();
180 Window sum_slice = window_sum.first_slice_window_2D();
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100181 do
182 {
183 unsigned int idx = 0;
Usama Arifae0001e2019-03-26 13:44:01 +0000184 add_2D_tensor_argument(idx, _input, in_slice);
185 add_2D_tensor_argument(idx, _sum, sum_slice);
186 add_2D_tensor_argument(idx, _output, in_slice);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100187 enqueue(queue, *this, in_slice, lws_hint());
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100188 }
Usama Arifae0001e2019-03-26 13:44:01 +0000189 while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(sum_slice));
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100190 }
191 break;
Michalis Spyrou5538d342018-11-14 08:10:13 +0000192 case 1:
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100193 {
194 window_sum.set(Window::DimY, Window::Dimension(0, 0, 0));
195 Window in_slice = window.first_slice_window_2D();
196 Window sum_slice = window_sum.first_slice_window_2D();
197 do
198 {
199 unsigned int idx = 0;
200 add_2D_tensor_argument(idx, _input, in_slice);
201 add_2D_tensor_argument(idx, _sum, sum_slice);
202 add_2D_tensor_argument(idx, _output, in_slice);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100203 enqueue(queue, *this, in_slice, lws_hint());
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100204 }
205 while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(sum_slice));
206 }
207 break;
Michalis Spyrou5538d342018-11-14 08:10:13 +0000208 case 2:
209 {
210 window_sum.set(Window::DimZ, Window::Dimension(0, 0, 0));
211 Window in_slice = window.first_slice_window_3D();
212 Window sum_slice = window_sum.first_slice_window_3D();
213 do
214 {
215 unsigned int idx = 0;
216 add_3D_tensor_argument(idx, _input, in_slice);
217 add_3D_tensor_argument(idx, _sum, sum_slice);
218 add_3D_tensor_argument(idx, _output, in_slice);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100219 enqueue(queue, *this, in_slice, lws_hint());
Michalis Spyrou5538d342018-11-14 08:10:13 +0000220 }
221 while(window.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(sum_slice));
222 }
223 break;
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100224 default:
225 ARM_COMPUTE_ERROR("Not supported");
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100226 }
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100227}
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +0100228} // namespace arm_compute