blob: 09b668d6cdc74ea2f089ad2e350c24c741fd891a [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/CLBatchNormalizationLayerKernel.h"
25
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
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126 _input = input;
127 _output = output;
128 _mean = mean;
129 _var = var;
130 _beta = beta;
131 _gamma = gamma;
132 _epsilon = epsilon;
133
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000134 _run_in_place = (output == nullptr) || (output == input);
135
Giorgio Arenaf6a43c52017-12-01 12:16:25 +0000136 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr,
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000137 mean->info(), var->info(), (beta != nullptr) ? beta->info() : nullptr,
138 (gamma != nullptr) ? gamma->info() : nullptr, epsilon, act_info));
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100139
Sheri Zhang141c31a2020-10-08 12:35:28 +0100140 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 +0100141
142 // Set build options
Giorgio Arena11674872018-02-07 15:38:12 +0000143 CLBuildOptions build_opts;
144 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
145 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
Sheri Zhang141c31a2020-10-08 12:35:28 +0100146 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 +0100147 build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation())));
Giorgio Arena11674872018-02-07 15:38:12 +0000148 build_opts.add_option_if(act_info.enabled(), "-DA_VAL=" + float_to_string_with_full_precision(act_info.a()));
149 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 +0000150 build_opts.add_option_if(_run_in_place, "-DIN_PLACE");
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000151 build_opts.add_option_if(beta == nullptr, "-DUSE_DEFAULT_BETA");
152 build_opts.add_option_if(gamma == nullptr, "-DUSE_DEFAULT_GAMMA");
Michalis Spyrou172e5702017-06-26 14:18:47 +0100153
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100155 _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 +0100156
157 // Set kernel static arguments
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000158 unsigned int include_output = (!_run_in_place) ? 1 : 0;
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000159 unsigned int idx = (1 + include_output) * num_arguments_per_3D_tensor() + 2 * num_arguments_per_1D_tensor(); // Skip the input and output parameters
160 if(_beta != nullptr)
161 {
162 idx += num_arguments_per_1D_tensor(); // Skip beta parameter
163 }
164 if(_gamma != nullptr)
165 {
166 idx += num_arguments_per_1D_tensor(); // Skip gamma parameter
167 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100168 _kernel.setArg<cl_float>(idx++, _epsilon);
169
Sheri Zhang141c31a2020-10-08 12:35:28 +0100170 if(output != nullptr)
171 {
172 // Output tensor auto initialization if not yet initialized
173 auto_init_if_empty(*output->info(), *input->info()->clone());
174 }
175
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176 // Configure kernel window
Sheri Zhang141c31a2020-10-08 12:35:28 +0100177 if(input->info()->data_layout() == DataLayout::NHWC)
178 {
179 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
180 ICLKernel::configure_internal(win);
181 }
182 else
183 {
184 auto win_config = validate_and_configure_window_nchw(input->info(), (_run_in_place) ? nullptr : output->info());
185 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
186 ICLKernel::configure_internal(win_config.second);
187 }
Giorgio Arena11674872018-02-07 15:38:12 +0000188
189 _config_id = "batch_normalization_layer_";
190 _config_id += string_from_data_type(input->info()->data_type());
191 _config_id += "_";
192 _config_id += support::cpp11::to_string(input->info()->dimension(0));
193 _config_id += "_";
194 _config_id += support::cpp11::to_string(input->info()->dimension(1));
195 _config_id += "_";
196 _config_id += support::cpp11::to_string(input->info()->dimension(2));
Giorgio Arena00b93f52018-06-28 17:18:50 +0100197 _config_id += "_";
198 _config_id += lower_string(string_from_data_layout(input->info()->data_layout()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100199}
200
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000201Status CLBatchNormalizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output,
202 const ITensorInfo *mean, const ITensorInfo *var,
203 const ITensorInfo *beta, const ITensorInfo *gamma,
Giorgio Arena11674872018-02-07 15:38:12 +0000204 float epsilon, ActivationLayerInfo act_info)
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000205{
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000206 const bool run_in_place = (output == nullptr) || (output == input);
Giorgio Arena11674872018-02-07 15:38:12 +0000207 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, mean, var, beta, gamma, epsilon, act_info));
Sheri Zhang141c31a2020-10-08 12:35:28 +0100208
209 if(input->data_layout() != DataLayout::NHWC)
210 {
211 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_nchw(input->clone().get(), (run_in_place) ? nullptr : output->clone().get())
212 .first);
213 }
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000214
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000215 return Status{};
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000216}
217
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100218void CLBatchNormalizationLayerKernel::run(const Window &window, cl::CommandQueue &queue)
219{
220 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
221 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
222
223 Window slice = window.first_slice_window_3D();
224
225 Window vector_slice = window.first_slice_window_1D();
226 vector_slice.set(Window::DimX, Window::Dimension(0, 0, 0));
227
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000228 unsigned int include_output = (!_run_in_place) ? 1 : 0;
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000229 unsigned int idx = (1 + include_output) * num_arguments_per_3D_tensor();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100230 add_1D_tensor_argument(idx, _mean, vector_slice);
231 add_1D_tensor_argument(idx, _var, vector_slice);
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000232 if(_beta != nullptr)
233 {
234 add_1D_tensor_argument(idx, _beta, vector_slice);
235 }
236 if(_gamma != nullptr)
237 {
238 add_1D_tensor_argument(idx, _gamma, vector_slice);
239 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100240
241 do
242 {
243 idx = 0;
244 add_3D_tensor_argument(idx, _input, slice);
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000245 if(!_run_in_place)
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100246 {
247 add_3D_tensor_argument(idx, _output, slice);
248 }
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100249 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100250 }
251 while(window.slide_window_slice_3D(slice));
252}