blob: c6c4229c0062123711b329624ad99b298c6fc4e2 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2017-2021, 2023 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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/CLNormalizationLayerKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +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"
29#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"
33#include "arm_compute/core/utils/StringUtils.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/core/Window.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035#include "src/core/AccessWindowStatic.h"
36#include "src/core/CL/CLValidate.h"
37#include "src/core/helpers/AutoConfiguration.h"
38#include "src/core/helpers/NormalizationHelpers.h"
39#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000040#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041
Manuel Bottini21c28952021-04-08 12:50:12 +010042namespace arm_compute
43{
Giorgio Arenac23b6332017-11-28 15:23:51 +000044namespace
45{
Georgios Pinitas631c41a2017-12-06 11:53:03 +000046Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, NormalizationLayerInfo norm_info)
Giorgio Arenac23b6332017-11-28 15:23:51 +000047{
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010048 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010049 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +000050 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(input, DataLayout::NCHW, DataLayout::NHWC);
Giorgio Arenac23b6332017-11-28 15:23:51 +000051 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
52
53 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!(norm_info.norm_size() % 2), "Normalization size should be odd");
Giorgio Arenac23b6332017-11-28 15:23:51 +000054
Giorgio Arenac23b6332017-11-28 15:23:51 +000055 // Checks performed when output is configured
56 if(output->total_size() != 0)
57 {
58 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +000059 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
Giorgio Arenac23b6332017-11-28 15:23:51 +000060 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
Giorgio Arenac23b6332017-11-28 15:23:51 +000061 }
62
Georgios Pinitas631c41a2017-12-06 11:53:03 +000063 return Status{};
Giorgio Arenac23b6332017-11-28 15:23:51 +000064}
65
Georgios Pinitas631c41a2017-12-06 11:53:03 +000066std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, NormalizationLayerInfo norm_info)
Giorgio Arenac23b6332017-11-28 15:23:51 +000067{
Giorgio Arenaf6a43c52017-12-01 12:16:25 +000068 // Output tensor auto initialization if not yet initialized
69 auto_init_if_empty(*output, *input->clone());
70
Manuel Bottini21c28952021-04-08 12:50:12 +010071 bool window_changed = false;
72 Window win;
73 const DataLayout data_layout = input->data_layout();
74 if(data_layout == DataLayout::NCHW)
Georgios Pinitas7f32d012018-10-11 18:41:19 +010075 {
SiCongLiaed61f22021-08-26 17:44:08 +010076 const unsigned int vec_size_x = adjust_vec_size(max_cl_vector_width / input->element_size(), input->dimension(0));
77 const unsigned int norm_idx = get_normalization_dimension_index(input->data_layout(), norm_info);
78 const bool is_norm_across_width = norm_idx == 0;
Manuel Bottini21c28952021-04-08 12:50:12 +010079
SiCongLiaed61f22021-08-26 17:44:08 +010080 const unsigned int norm_radius = norm_info.norm_size() / 2;
81 // Border / padding calculation:
82 // For NCHW no border handling is impelmeneted in the kernel in the x axis.
83 // This means the x axis is fully-padded depending on vec_size_x and norm_size
84 // E.G. for input x dimension = 3, norm_size = 3 (radius = 1), vec_size_x = 2 ('#' is element 'p' is padding):
85 // In : |p|#|#|#|p|p|
86 // Out: |#|#|#|p|
87 // The output has 1 right padding because of the vec_size_x.
88 // The input has 1 left padding because radius = 1.
89 // The input has 2 right padding because of radius = 1 AND because of the extra output padding
90 const unsigned int border_width_left = is_norm_across_width ? norm_radius : 0;
91 const unsigned int border_width_right = is_norm_across_width ? norm_radius + (vec_size_x - input->dimension(0) % vec_size_x) : 0;
92 const BorderSize border_size = BorderSize(0, border_width_right, 0, border_width_left);
Manuel Bottini21c28952021-04-08 12:50:12 +010093
94 win = calculate_max_window(*input, Steps(vec_size_x));
95
96 // We do not use a Rectangle window for IN_MAP_2D as we clamp the top and bottom accesses inside the kernel, avoiding padding
97 // Reads can occur within the valid region of the input
SiCongLiaed61f22021-08-26 17:44:08 +010098 if(is_norm_across_width)
Manuel Bottini21c28952021-04-08 12:50:12 +010099 {
100 AccessWindowStatic input_access(input, -border_size.left, 0, input->dimension(0) + border_size.right, 0);
101 window_changed = window_changed || update_window_and_padding(win, input_access);
102 }
103 else
104 {
105 AccessWindowHorizontal input_access(input, -border_size.left, vec_size_x);
106 window_changed = window_changed || update_window_and_padding(win, input_access);
107 }
108
109 AccessWindowHorizontal output_access(output, 0, vec_size_x);
110 window_changed = window_changed || update_window_and_padding(win, output_access);
Georgios Pinitas7f32d012018-10-11 18:41:19 +0100111 }
112 else
113 {
Manuel Bottini21c28952021-04-08 12:50:12 +0100114 unsigned int vec_size_x = adjust_vec_size(max_cl_vector_width / input->element_size(), input->dimension(0));
115 if(norm_info.is_cross_map())
116 {
117 vec_size_x = 1;
118 }
119 win = calculate_max_window(*input, Steps(vec_size_x));
Georgios Pinitas7f32d012018-10-11 18:41:19 +0100120 }
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000121 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arenac23b6332017-11-28 15:23:51 +0000122 return std::make_pair(err, win);
123}
124} // namespace
125
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126CLNormalizationLayerKernel::CLNormalizationLayerKernel()
Georgios Pinitase2220552018-07-20 13:23:44 +0100127 : _input(nullptr), _output(nullptr), _border_size(0), _is_norm_across_width(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128{
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100129 _type = CLKernelType::ELEMENTWISE;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130}
131
132BorderSize CLNormalizationLayerKernel::border_size() const
133{
134 return _border_size;
135}
136
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +0100137void CLNormalizationLayerKernel::configure(const ICLTensor *input, ICLTensor *output, NormalizationLayerInfo norm_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100138{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100139 configure(CLKernelLibrary::get().get_compile_context(), input, output, norm_info);
140}
141
Manuel Bottini679fc962020-04-21 16:08:53 +0100142void CLNormalizationLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, NormalizationLayerInfo norm_info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100143{
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000144 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Manuel Bottini21c28952021-04-08 12:50:12 +0100145 auto padding_info = get_padding_info({ input, output });
Georgios Pinitas09004ca2017-07-03 17:30:14 +0100146
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000147 // Perform validation step
Giorgio Arenaf6a43c52017-12-01 12:16:25 +0000148 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), norm_info));
Manuel Bottini21c28952021-04-08 12:50:12 +0100149 auto win_config = validate_and_configure_window(input->info(), output->info(), norm_info);
150 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +0100152 _input = input;
153 _output = output;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154
Manuel Bottini21c28952021-04-08 12:50:12 +0100155 const DataLayout data_layout = input->info()->data_layout();
156 unsigned int vec_size_x = adjust_vec_size(max_cl_vector_width / input->info()->element_size(), input->info()->dimension(0));
157 int vec_size_x_leftovers = input->info()->dimension(0) % vec_size_x;
158 if(norm_info.is_cross_map() && data_layout == DataLayout::NHWC)
159 {
160 vec_size_x = 1;
161 vec_size_x_leftovers = 0;
162 }
163
164 if(data_layout == DataLayout::NCHW)
165 {
SiCongLiaed61f22021-08-26 17:44:08 +0100166 const unsigned int norm_idx = get_normalization_dimension_index(data_layout, norm_info);
167 _is_norm_across_width = norm_idx == 0;
168 const unsigned int norm_radius = norm_info.norm_size() / 2;
169 // Border / padding calculation:
170 // For NCHW no border handling is impelmeneted in the kernel in the x axis.
171 // This means the x axis is fully-padded depending on vec_size_x and norm_size
172 // E.G. for input x dimension = 3, norm_size = 3 (radius = 1), vec_size_x = 2 ('#' is element 'p' is padding):
173 // In : |p|#|#|#|p|p|
174 // Out: |#|#|#|p|
175 // The output has 1 right padding because of the vec_size_x.
176 // The input has 1 left padding because radius = 1.
177 // The input has 2 right padding because of radius = 1 AND the extra output padding
178 const unsigned int border_width_left = _is_norm_across_width ? norm_radius : 0;
179 const unsigned int border_width_right = _is_norm_across_width ? norm_radius + (vec_size_x - input->info()->dimension(0) % vec_size_x) : 0;
180 _border_size = BorderSize(0, border_width_right, 0, border_width_left);
Manuel Bottini21c28952021-04-08 12:50:12 +0100181 }
Georgios Pinitas7f32d012018-10-11 18:41:19 +0100182
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +0000183 const bool is_in_map_2D = (norm_info.type() == NormType::IN_MAP_2D);
184
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100185 // Set build options
Georgios Pinitas01624362017-11-30 10:53:31 +0000186 CLBuildOptions build_opts;
187 build_opts.add_option(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
Georgios Pinitas01624362017-11-30 10:53:31 +0000188 build_opts.add_option(("-DCOEFF=" + float_to_string_with_full_precision(norm_info.scale_coeff())));
189 build_opts.add_option(("-DBETA=" + float_to_string_with_full_precision(norm_info.beta())));
190 build_opts.add_option(("-DKAPPA=" + float_to_string_with_full_precision(norm_info.kappa())));
Manuel Bottini21c28952021-04-08 12:50:12 +0100191 build_opts.add_option(("-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x)));
192 build_opts.add_option(("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(vec_size_x_leftovers)));
Georgios Pinitas01624362017-11-30 10:53:31 +0000193 build_opts.add_option(("-DRADIUS=" + support::cpp11::to_string(norm_info.norm_size() / 2)));
194 build_opts.add_option(("-DNUM_SLICES=" + support::cpp11::to_string(input->info()->dimension(2))));
195 build_opts.add_option_if(is_in_map_2D, "-DIN_MAP_2D");
Georgios Pinitas7f32d012018-10-11 18:41:19 +0100196 build_opts.add_option_if(norm_info.is_in_map() || (data_layout == DataLayout::NHWC && norm_info.is_cross_map()), "-DWIDTH_SIZE=" + support::cpp11::to_string(input->info()->dimension(0)));
Manuel Bottini21c28952021-04-08 12:50:12 +0100197 build_opts.add_option_if(norm_info.is_in_map() && data_layout == DataLayout::NHWC, "-DDIM1_SIZE=" + support::cpp11::to_string(input->info()->dimension(1)));
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100198
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100199 // Create kernel
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +0000200 std::string kernel_name;
201 if(norm_info.is_in_map())
202 {
203 kernel_name = "normalization_layer_in_map_" + lower_string(string_from_data_layout(data_layout));
204 }
205 else
206 {
Manuel Bottini21c28952021-04-08 12:50:12 +0100207 kernel_name = "normalization_layer_cross_map_" + lower_string(string_from_data_layout(data_layout));
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +0000208 }
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100209 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100210
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100211 // Configure kernel window
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100212 ICLKernel::configure_internal(win_config.second);
Giorgio Arena63485ce2017-11-15 16:04:20 +0000213
214 // Set config_id for enabling LWS tuning
215 _config_id = "normalization_layer_";
216 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
217 _config_id += "_";
218 _config_id += support::cpp11::to_string(static_cast<std::underlying_type<NormType>::type>(norm_info.type()));
219 _config_id += "_";
220 _config_id += support::cpp11::to_string(norm_info.norm_size());
221 _config_id += "_";
222 _config_id += support::cpp11::to_string(input->info()->dimension(0));
223 _config_id += "_";
224 _config_id += support::cpp11::to_string(input->info()->dimension(1));
Manuel Bottini21c28952021-04-08 12:50:12 +0100225 if(data_layout == DataLayout::NHWC)
226 {
227 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
228 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100229}
230
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000231Status CLNormalizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, NormalizationLayerInfo norm_info)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000232{
Giorgio Arenac23b6332017-11-28 15:23:51 +0000233 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, norm_info));
Georgios Pinitas01624362017-11-30 10:53:31 +0000234 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), norm_info).first);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000235
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000236 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000237}
238
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100239void CLNormalizationLayerKernel::run(const Window &window, cl::CommandQueue &queue)
240{
241 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
242 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
243
Georgios Pinitase2220552018-07-20 13:23:44 +0100244 const int collapsed_dimension = _is_norm_across_width ? Window::DimZ : 4;
steniu01f70256b2017-07-13 14:03:35 +0100245 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), collapsed_dimension);
246 Window slice = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100247
248 do
249 {
250 unsigned int idx = 0;
251 add_3D_tensor_argument(idx, _input, slice);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100252 add_3D_tensor_argument(idx, _output, slice);
Anthony Barbier5a65cfd2018-08-10 14:10:08 +0100253 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100254 }
steniu01f70256b2017-07-13 14:03:35 +0100255 while(window_collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100256}
Manuel Bottini21c28952021-04-08 12:50:12 +0100257} // namespace arm_compute