blob: 7d8e5db2b41896f15d700e9a69a13797a1cd1a1e [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 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 */
24#include "arm_compute/core/CL/kernels/CLNormalizationLayerKernel.h"
25
Georgios Pinitas7f32d012018-10-11 18:41:19 +010026#include "arm_compute/core/AccessWindowStatic.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/CL/CLKernelLibrary.h"
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010029#include "arm_compute/core/CL/CLValidate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/CL/ICLTensor.h"
31#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Utils.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/core/Window.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000035#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
37using namespace arm_compute;
38
Giorgio Arenac23b6332017-11-28 15:23:51 +000039namespace
40{
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +000041constexpr unsigned int num_elems_processed_per_iteration = 4;
Georgios Pinitas631c41a2017-12-06 11:53:03 +000042Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, NormalizationLayerInfo norm_info)
Giorgio Arenac23b6332017-11-28 15:23:51 +000043{
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010044 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010045 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +000046 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(input, DataLayout::NCHW, DataLayout::NHWC);
Giorgio Arenac23b6332017-11-28 15:23:51 +000047 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
48
49 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!(norm_info.norm_size() % 2), "Normalization size should be odd");
Giorgio Arenac23b6332017-11-28 15:23:51 +000050
Giorgio Arenac23b6332017-11-28 15:23:51 +000051 // Checks performed when output is configured
52 if(output->total_size() != 0)
53 {
54 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +000055 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
Giorgio Arenac23b6332017-11-28 15:23:51 +000056 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
Giorgio Arenac23b6332017-11-28 15:23:51 +000057 }
58
Georgios Pinitas631c41a2017-12-06 11:53:03 +000059 return Status{};
Giorgio Arenac23b6332017-11-28 15:23:51 +000060}
61
Georgios Pinitas631c41a2017-12-06 11:53:03 +000062std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, NormalizationLayerInfo norm_info)
Giorgio Arenac23b6332017-11-28 15:23:51 +000063{
Giorgio Arenaf6a43c52017-12-01 12:16:25 +000064 // Output tensor auto initialization if not yet initialized
65 auto_init_if_empty(*output, *input->clone());
66
Georgios Pinitas7f32d012018-10-11 18:41:19 +010067 const unsigned int norm_idx = get_normalization_dimension_index(input->data_layout(), norm_info);
68 const bool is_norm_accross_width = norm_idx == 0;
69
70 const unsigned int border_width = is_norm_accross_width ? num_elems_processed_per_iteration - 1 : 0;
Giorgio Arenac23b6332017-11-28 15:23:51 +000071 const BorderSize border_size = BorderSize(0, border_width);
72
Georgios Pinitas7f32d012018-10-11 18:41:19 +010073 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
74 bool window_changed = false;
Giorgio Arenac23b6332017-11-28 15:23:51 +000075
Georgios Pinitas01624362017-11-30 10:53:31 +000076 // We do not use a Rectangle window for IN_MAP_2D as we clamp the top and bottom accesses inside the kernel, avoiding padding
Georgios Pinitas7f32d012018-10-11 18:41:19 +010077 // Reads can occur within the valid region of the input
78 if(is_norm_accross_width)
79 {
80 AccessWindowStatic input_access(input, -border_size.left, 0, input->dimension(0) + border_size.right, 0);
81 window_changed = window_changed || update_window_and_padding(win, input_access);
82 }
83 else
84 {
85 AccessWindowHorizontal input_access(input, -border_size.left, num_elems_processed_per_iteration);
86 window_changed = window_changed || update_window_and_padding(win, input_access);
87 }
88
Giorgio Arenac23b6332017-11-28 15:23:51 +000089 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
Georgios Pinitas7f32d012018-10-11 18:41:19 +010090 window_changed = window_changed || update_window_and_padding(win, output_access);
Giorgio Arenac23b6332017-11-28 15:23:51 +000091 output_access.set_valid_region(win, input->valid_region());
92
Georgios Pinitas631c41a2017-12-06 11:53:03 +000093 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arenac23b6332017-11-28 15:23:51 +000094 return std::make_pair(err, win);
95}
96} // namespace
97
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098CLNormalizationLayerKernel::CLNormalizationLayerKernel()
Georgios Pinitase2220552018-07-20 13:23:44 +010099 : _input(nullptr), _output(nullptr), _border_size(0), _is_norm_across_width(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100{
101}
102
103BorderSize CLNormalizationLayerKernel::border_size() const
104{
105 return _border_size;
106}
107
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +0100108void CLNormalizationLayerKernel::configure(const ICLTensor *input, ICLTensor *output, NormalizationLayerInfo norm_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100110 configure(CLKernelLibrary::get().get_compile_context(), input, output, norm_info);
111}
112
Manuel Bottini679fc962020-04-21 16:08:53 +0100113void CLNormalizationLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, NormalizationLayerInfo norm_info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100114{
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000115 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas09004ca2017-07-03 17:30:14 +0100116
117 // Output tensor auto initialization if not yet initialized
Giorgio Arenaf6a43c52017-12-01 12:16:25 +0000118 auto_init_if_empty(*output->info(), *input->info()->clone());
Georgios Pinitas09004ca2017-07-03 17:30:14 +0100119
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000120 // Perform validation step
Giorgio Arenaf6a43c52017-12-01 12:16:25 +0000121 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), norm_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +0100123 _input = input;
124 _output = output;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125
Georgios Pinitas7f32d012018-10-11 18:41:19 +0100126 const DataLayout data_layout = input->info()->data_layout();
127 const unsigned int norm_idx = get_normalization_dimension_index(data_layout, norm_info);
128 _is_norm_across_width = norm_idx == 0;
129 const unsigned int border_width = _is_norm_across_width ? num_elems_processed_per_iteration - 1 : 0;
130 _border_size = BorderSize(0, border_width);
131
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +0000132 const bool is_in_map_2D = (norm_info.type() == NormType::IN_MAP_2D);
133
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100134 // Set build options
Georgios Pinitas01624362017-11-30 10:53:31 +0000135 CLBuildOptions build_opts;
136 build_opts.add_option(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
Georgios Pinitas01624362017-11-30 10:53:31 +0000137 build_opts.add_option(("-DCOEFF=" + float_to_string_with_full_precision(norm_info.scale_coeff())));
138 build_opts.add_option(("-DBETA=" + float_to_string_with_full_precision(norm_info.beta())));
139 build_opts.add_option(("-DKAPPA=" + float_to_string_with_full_precision(norm_info.kappa())));
140 build_opts.add_option(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)));
141 build_opts.add_option(("-DRADIUS=" + support::cpp11::to_string(norm_info.norm_size() / 2)));
142 build_opts.add_option(("-DNUM_SLICES=" + support::cpp11::to_string(input->info()->dimension(2))));
143 build_opts.add_option_if(is_in_map_2D, "-DIN_MAP_2D");
Georgios Pinitas7f32d012018-10-11 18:41:19 +0100144 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)));
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100145
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146 // Create kernel
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +0000147 std::string kernel_name;
148 if(norm_info.is_in_map())
149 {
150 kernel_name = "normalization_layer_in_map_" + lower_string(string_from_data_layout(data_layout));
151 }
152 else
153 {
154 if(data_layout == DataLayout::NCHW)
155 {
156 kernel_name = "normalization_layer_cross_map";
157 }
158 else
159 {
160 // 1D Cross-Map normalization in NHWC is the same as 1D In-Map normalization in NCHW
161 kernel_name = "normalization_layer_in_map_nchw";
162 }
163 }
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100164 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100166 // Configure kernel window
Georgios Pinitas01624362017-11-30 10:53:31 +0000167 auto win_config = validate_and_configure_window(input->info(), output->info(), norm_info);
Giorgio Arenac23b6332017-11-28 15:23:51 +0000168 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100169 ICLKernel::configure_internal(win_config.second);
Giorgio Arena63485ce2017-11-15 16:04:20 +0000170
171 // Set config_id for enabling LWS tuning
172 _config_id = "normalization_layer_";
173 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
174 _config_id += "_";
175 _config_id += support::cpp11::to_string(static_cast<std::underlying_type<NormType>::type>(norm_info.type()));
176 _config_id += "_";
177 _config_id += support::cpp11::to_string(norm_info.norm_size());
178 _config_id += "_";
179 _config_id += support::cpp11::to_string(input->info()->dimension(0));
180 _config_id += "_";
181 _config_id += support::cpp11::to_string(input->info()->dimension(1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100182}
183
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000184Status CLNormalizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, NormalizationLayerInfo norm_info)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000185{
Giorgio Arenac23b6332017-11-28 15:23:51 +0000186 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, norm_info));
Georgios Pinitas01624362017-11-30 10:53:31 +0000187 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 +0000188
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000189 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000190}
191
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100192void CLNormalizationLayerKernel::run(const Window &window, cl::CommandQueue &queue)
193{
194 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
195 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
196
Georgios Pinitase2220552018-07-20 13:23:44 +0100197 const int collapsed_dimension = _is_norm_across_width ? Window::DimZ : 4;
steniu01f70256b2017-07-13 14:03:35 +0100198 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), collapsed_dimension);
199 Window slice = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100200
201 do
202 {
203 unsigned int idx = 0;
204 add_3D_tensor_argument(idx, _input, slice);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100205 add_3D_tensor_argument(idx, _output, slice);
Anthony Barbier5a65cfd2018-08-10 14:10:08 +0100206 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100207 }
steniu01f70256b2017-07-13 14:03:35 +0100208 while(window_collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209}