blob: 44bdc6f5872fd63d3b52b5a079a5e2988b7b3e33 [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 */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLBatchNormalizationLayerKernel.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"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/core/CL/CLValidate.h"
33#include "src/core/helpers/AutoConfiguration.h"
34#include "src/core/helpers/WindowHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
Matthew Bentham758b5ba2020-03-05 23:37:48 +000036#include "support/StringSupport.h"
Michalis Spyrou172e5702017-06-26 14:18:47 +010037
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038using namespace arm_compute;
39
Giorgio Arena70623822017-11-27 15:50:10 +000040namespace
41{
Georgios Pinitas631c41a2017-12-06 11:53:03 +000042Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output,
43 const ITensorInfo *mean, const ITensorInfo *var,
44 const ITensorInfo *beta, const ITensorInfo *gamma,
Giorgio Arena11674872018-02-07 15:38:12 +000045 float epsilon, ActivationLayerInfo act_info)
Giorgio Arena70623822017-11-27 15:50:10 +000046{
47 ARM_COMPUTE_UNUSED(epsilon);
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 Giorgio4d336302018-03-02 09:43:54 +000050 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, var);
51 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, mean, var);
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +000052 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL)) != mean->dimension(0));
Michele Di Giorgio4d336302018-03-02 09:43:54 +000053 if(beta != nullptr)
54 {
55 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, beta);
56 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, beta);
Michele Di Giorgio4d336302018-03-02 09:43:54 +000057 }
58 if(gamma != nullptr)
59 {
60 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, gamma);
61 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, gamma);
Michele Di Giorgio4d336302018-03-02 09:43:54 +000062 }
63
Giorgio Arena11674872018-02-07 15:38:12 +000064 if(act_info.enabled())
65 {
66 ActivationLayerInfo::ActivationFunction act = act_info.activation();
67 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() != DataType::F32 && input->data_type() != DataType::F16);
Georgios Pinitas6f109bd2018-07-16 12:57:42 +010068 ARM_COMPUTE_RETURN_ERROR_ON(act != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::RELU
69 && act != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU
Giorgio Arena11674872018-02-07 15:38:12 +000070 && act != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU);
71 ARM_COMPUTE_RETURN_ERROR_ON(act_info.b() > act_info.a());
72 }
Giorgio Arena70623822017-11-27 15:50:10 +000073
74 if(output != nullptr && output->total_size() != 0)
75 {
76 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +000077 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
Giorgio Arena70623822017-11-27 15:50:10 +000078 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Giorgio Arena70623822017-11-27 15:50:10 +000079 }
80
Georgios Pinitas631c41a2017-12-06 11:53:03 +000081 return Status{};
Giorgio Arena70623822017-11-27 15:50:10 +000082}
83
Sheri Zhang141c31a2020-10-08 12:35:28 +010084std::pair<Status, Window> validate_and_configure_window_nchw(ITensorInfo *input, ITensorInfo *output)
Giorgio Arena70623822017-11-27 15:50:10 +000085{
Sheri Zhang141c31a2020-10-08 12:35:28 +010086 const unsigned int num_elems_processed_per_iteration = adjust_vec_size(16 / input->element_size(), input->dimension(0));
Giorgio Arena70623822017-11-27 15:50:10 +000087
88 // Configure kernel window
89 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
90 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
91
Giorgio Arena2995f5b2017-11-29 17:33:59 +000092 bool window_changed = false;
Giorgio Arena70623822017-11-27 15:50:10 +000093 if(output != nullptr)
94 {
95 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
96 window_changed = update_window_and_padding(win, input_access, output_access);
97 output_access.set_valid_region(win, input->valid_region());
98 }
99 else
100 {
101 window_changed = update_window_and_padding(win, input_access);
102 }
103
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000104 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arena70623822017-11-27 15:50:10 +0000105 return std::make_pair(err, win);
106}
107} // namespace
108
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109CLBatchNormalizationLayerKernel::CLBatchNormalizationLayerKernel()
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000110 : _input(nullptr), _output(nullptr), _mean(nullptr), _var(nullptr), _beta(nullptr), _gamma(nullptr), _epsilon(0), _run_in_place(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111{
112}
113
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100114void CLBatchNormalizationLayerKernel::configure(ICLTensor *input, ICLTensor *output, const ICLTensor *mean, const ICLTensor *var, const ICLTensor *beta, const ICLTensor *gamma,
Giorgio Arena11674872018-02-07 15:38:12 +0000115 float epsilon, ActivationLayerInfo act_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100116{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100117 configure(CLKernelLibrary::get().get_compile_context(), input, output, mean, var, beta, gamma, epsilon, act_info);
118}
119
Manuel Bottini256c0b92020-04-21 13:29:30 +0100120void CLBatchNormalizationLayerKernel::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, const ICLTensor *mean, const ICLTensor *var, const ICLTensor *beta,
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100121 const ICLTensor *gamma,
122 float epsilon, ActivationLayerInfo act_info)
123{
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000124 ARM_COMPUTE_ERROR_ON_NULLPTR(input, mean, var);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125
SiCong Li04a07062020-11-17 14:09:01 +0000126 auto padding_info = get_padding_info({ input, output, mean, var, beta, gamma });
127 _input = input;
128 _output = output;
129 _mean = mean;
130 _var = var;
131 _beta = beta;
132 _gamma = gamma;
133 _epsilon = epsilon;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000135 _run_in_place = (output == nullptr) || (output == input);
136
Giorgio Arenaf6a43c52017-12-01 12:16:25 +0000137 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr,
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000138 mean->info(), var->info(), (beta != nullptr) ? beta->info() : nullptr,
139 (gamma != nullptr) ? gamma->info() : nullptr, epsilon, act_info));
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100140
Sheri Zhang141c31a2020-10-08 12:35:28 +0100141 unsigned int num_elems_processed_per_iteration = adjust_vec_size(16 / input->info()->element_size(), input->info()->dimension(0));
Michalis Spyrou172e5702017-06-26 14:18:47 +0100142
143 // Set build options
Giorgio Arena11674872018-02-07 15:38:12 +0000144 CLBuildOptions build_opts;
145 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
146 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
Sheri Zhang141c31a2020-10-08 12:35:28 +0100147 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(input->info()->dimension(0) % num_elems_processed_per_iteration));
Usama Arif6a98a6e2019-05-10 17:07:27 +0100148 build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation())));
Giorgio Arena11674872018-02-07 15:38:12 +0000149 build_opts.add_option_if(act_info.enabled(), "-DA_VAL=" + float_to_string_with_full_precision(act_info.a()));
150 build_opts.add_option_if(act_info.enabled(), "-DB_VAL=" + float_to_string_with_full_precision(act_info.b()));
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000151 build_opts.add_option_if(_run_in_place, "-DIN_PLACE");
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000152 build_opts.add_option_if(beta == nullptr, "-DUSE_DEFAULT_BETA");
153 build_opts.add_option_if(gamma == nullptr, "-DUSE_DEFAULT_GAMMA");
Michalis Spyrou172e5702017-06-26 14:18:47 +0100154
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100156 _kernel = create_kernel(compile_context, "batchnormalization_layer_" + lower_string(string_from_data_layout(input->info()->data_layout())), build_opts.options());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157
158 // Set kernel static arguments
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000159 unsigned int include_output = (!_run_in_place) ? 1 : 0;
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000160 unsigned int idx = (1 + include_output) * num_arguments_per_3D_tensor() + 2 * num_arguments_per_1D_tensor(); // Skip the input and output parameters
161 if(_beta != nullptr)
162 {
163 idx += num_arguments_per_1D_tensor(); // Skip beta parameter
164 }
165 if(_gamma != nullptr)
166 {
167 idx += num_arguments_per_1D_tensor(); // Skip gamma parameter
168 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169 _kernel.setArg<cl_float>(idx++, _epsilon);
170
Sheri Zhang141c31a2020-10-08 12:35:28 +0100171 if(output != nullptr)
172 {
173 // Output tensor auto initialization if not yet initialized
174 auto_init_if_empty(*output->info(), *input->info()->clone());
175 }
176
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177 // Configure kernel window
Sheri Zhang141c31a2020-10-08 12:35:28 +0100178 if(input->info()->data_layout() == DataLayout::NHWC)
179 {
180 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
181 ICLKernel::configure_internal(win);
182 }
183 else
184 {
185 auto win_config = validate_and_configure_window_nchw(input->info(), (_run_in_place) ? nullptr : output->info());
186 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
187 ICLKernel::configure_internal(win_config.second);
188 }
Giorgio Arena11674872018-02-07 15:38:12 +0000189
SiCong Li04a07062020-11-17 14:09:01 +0000190 ARM_COMPUTE_ERROR_ON(input->info()->data_layout() == DataLayout::NHWC && has_padding_changed(padding_info));
191
Giorgio Arena11674872018-02-07 15:38:12 +0000192 _config_id = "batch_normalization_layer_";
193 _config_id += string_from_data_type(input->info()->data_type());
194 _config_id += "_";
195 _config_id += support::cpp11::to_string(input->info()->dimension(0));
196 _config_id += "_";
197 _config_id += support::cpp11::to_string(input->info()->dimension(1));
198 _config_id += "_";
199 _config_id += support::cpp11::to_string(input->info()->dimension(2));
Giorgio Arena00b93f52018-06-28 17:18:50 +0100200 _config_id += "_";
201 _config_id += lower_string(string_from_data_layout(input->info()->data_layout()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100202}
203
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000204Status CLBatchNormalizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output,
205 const ITensorInfo *mean, const ITensorInfo *var,
206 const ITensorInfo *beta, const ITensorInfo *gamma,
Giorgio Arena11674872018-02-07 15:38:12 +0000207 float epsilon, ActivationLayerInfo act_info)
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000208{
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000209 const bool run_in_place = (output == nullptr) || (output == input);
Giorgio Arena11674872018-02-07 15:38:12 +0000210 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, mean, var, beta, gamma, epsilon, act_info));
Sheri Zhang141c31a2020-10-08 12:35:28 +0100211
212 if(input->data_layout() != DataLayout::NHWC)
213 {
214 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_nchw(input->clone().get(), (run_in_place) ? nullptr : output->clone().get())
215 .first);
216 }
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000217
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000218 return Status{};
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000219}
220
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100221void CLBatchNormalizationLayerKernel::run(const Window &window, cl::CommandQueue &queue)
222{
223 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
224 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
225
226 Window slice = window.first_slice_window_3D();
227
228 Window vector_slice = window.first_slice_window_1D();
229 vector_slice.set(Window::DimX, Window::Dimension(0, 0, 0));
230
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000231 unsigned int include_output = (!_run_in_place) ? 1 : 0;
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000232 unsigned int idx = (1 + include_output) * num_arguments_per_3D_tensor();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100233 add_1D_tensor_argument(idx, _mean, vector_slice);
234 add_1D_tensor_argument(idx, _var, vector_slice);
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000235 if(_beta != nullptr)
236 {
237 add_1D_tensor_argument(idx, _beta, vector_slice);
238 }
239 if(_gamma != nullptr)
240 {
241 add_1D_tensor_argument(idx, _gamma, vector_slice);
242 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100243
244 do
245 {
246 idx = 0;
247 add_3D_tensor_argument(idx, _input, slice);
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000248 if(!_run_in_place)
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100249 {
250 add_3D_tensor_argument(idx, _output, slice);
251 }
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100252 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100253 }
254 while(window.slide_window_slice_3D(slice));
255}