blob: 1c1df6c4ebdf2de84ba5183e97254366cb1b4879 [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"
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010028#include "arm_compute/core/CL/CLValidate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Utils.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033#include "arm_compute/core/Window.h"
34
Matthew Bentham758b5ba2020-03-05 23:37:48 +000035#include "support/StringSupport.h"
Michalis Spyrou172e5702017-06-26 14:18:47 +010036
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037using namespace arm_compute;
38
Giorgio Arena70623822017-11-27 15:50:10 +000039namespace
40{
Georgios Pinitas631c41a2017-12-06 11:53:03 +000041Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output,
42 const ITensorInfo *mean, const ITensorInfo *var,
43 const ITensorInfo *beta, const ITensorInfo *gamma,
Giorgio Arena11674872018-02-07 15:38:12 +000044 float epsilon, ActivationLayerInfo act_info)
Giorgio Arena70623822017-11-27 15:50:10 +000045{
46 ARM_COMPUTE_UNUSED(epsilon);
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010047 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010048 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
Michele Di Giorgio4d336302018-03-02 09:43:54 +000049 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, var);
50 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, mean, var);
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +000051 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 +000052 if(beta != nullptr)
53 {
54 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, beta);
55 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, beta);
Michele Di Giorgio4d336302018-03-02 09:43:54 +000056 }
57 if(gamma != nullptr)
58 {
59 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, gamma);
60 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, gamma);
Michele Di Giorgio4d336302018-03-02 09:43:54 +000061 }
62
Giorgio Arena11674872018-02-07 15:38:12 +000063 if(act_info.enabled())
64 {
65 ActivationLayerInfo::ActivationFunction act = act_info.activation();
66 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() != DataType::F32 && input->data_type() != DataType::F16);
Georgios Pinitas6f109bd2018-07-16 12:57:42 +010067 ARM_COMPUTE_RETURN_ERROR_ON(act != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::RELU
68 && act != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU
Giorgio Arena11674872018-02-07 15:38:12 +000069 && act != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU);
70 ARM_COMPUTE_RETURN_ERROR_ON(act_info.b() > act_info.a());
71 }
Giorgio Arena70623822017-11-27 15:50:10 +000072
73 if(output != nullptr && output->total_size() != 0)
74 {
75 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +000076 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
Giorgio Arena70623822017-11-27 15:50:10 +000077 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Giorgio Arena70623822017-11-27 15:50:10 +000078 }
79
Georgios Pinitas631c41a2017-12-06 11:53:03 +000080 return Status{};
Giorgio Arena70623822017-11-27 15:50:10 +000081}
82
Sheri Zhang141c31a2020-10-08 12:35:28 +010083std::pair<Status, Window> validate_and_configure_window_nchw(ITensorInfo *input, ITensorInfo *output)
Giorgio Arena70623822017-11-27 15:50:10 +000084{
Sheri Zhang141c31a2020-10-08 12:35:28 +010085 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 +000086
87 // Configure kernel window
88 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
89 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
90
Giorgio Arena2995f5b2017-11-29 17:33:59 +000091 bool window_changed = false;
Giorgio Arena70623822017-11-27 15:50:10 +000092 if(output != nullptr)
93 {
94 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
95 window_changed = update_window_and_padding(win, input_access, output_access);
96 output_access.set_valid_region(win, input->valid_region());
97 }
98 else
99 {
100 window_changed = update_window_and_padding(win, input_access);
101 }
102
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000103 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arena70623822017-11-27 15:50:10 +0000104 return std::make_pair(err, win);
105}
106} // namespace
107
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108CLBatchNormalizationLayerKernel::CLBatchNormalizationLayerKernel()
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000109 : _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 +0100110{
111}
112
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100113void 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 +0000114 float epsilon, ActivationLayerInfo act_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100116 configure(CLKernelLibrary::get().get_compile_context(), input, output, mean, var, beta, gamma, epsilon, act_info);
117}
118
Manuel Bottini256c0b92020-04-21 13:29:30 +0100119void 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 +0100120 const ICLTensor *gamma,
121 float epsilon, ActivationLayerInfo act_info)
122{
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000123 ARM_COMPUTE_ERROR_ON_NULLPTR(input, mean, var);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125 _input = input;
126 _output = output;
127 _mean = mean;
128 _var = var;
129 _beta = beta;
130 _gamma = gamma;
131 _epsilon = epsilon;
132
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000133 _run_in_place = (output == nullptr) || (output == input);
134
Giorgio Arenaf6a43c52017-12-01 12:16:25 +0000135 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr,
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000136 mean->info(), var->info(), (beta != nullptr) ? beta->info() : nullptr,
137 (gamma != nullptr) ? gamma->info() : nullptr, epsilon, act_info));
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100138
Sheri Zhang141c31a2020-10-08 12:35:28 +0100139 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 +0100140
141 // Set build options
Giorgio Arena11674872018-02-07 15:38:12 +0000142 CLBuildOptions build_opts;
143 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
144 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
Sheri Zhang141c31a2020-10-08 12:35:28 +0100145 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 +0100146 build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation())));
Giorgio Arena11674872018-02-07 15:38:12 +0000147 build_opts.add_option_if(act_info.enabled(), "-DA_VAL=" + float_to_string_with_full_precision(act_info.a()));
148 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 +0000149 build_opts.add_option_if(_run_in_place, "-DIN_PLACE");
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000150 build_opts.add_option_if(beta == nullptr, "-DUSE_DEFAULT_BETA");
151 build_opts.add_option_if(gamma == nullptr, "-DUSE_DEFAULT_GAMMA");
Michalis Spyrou172e5702017-06-26 14:18:47 +0100152
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100154 _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 +0100155
156 // Set kernel static arguments
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000157 unsigned int include_output = (!_run_in_place) ? 1 : 0;
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000158 unsigned int idx = (1 + include_output) * num_arguments_per_3D_tensor() + 2 * num_arguments_per_1D_tensor(); // Skip the input and output parameters
159 if(_beta != nullptr)
160 {
161 idx += num_arguments_per_1D_tensor(); // Skip beta parameter
162 }
163 if(_gamma != nullptr)
164 {
165 idx += num_arguments_per_1D_tensor(); // Skip gamma parameter
166 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167 _kernel.setArg<cl_float>(idx++, _epsilon);
168
Sheri Zhang141c31a2020-10-08 12:35:28 +0100169 if(output != nullptr)
170 {
171 // Output tensor auto initialization if not yet initialized
172 auto_init_if_empty(*output->info(), *input->info()->clone());
173 }
174
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100175 // Configure kernel window
Sheri Zhang141c31a2020-10-08 12:35:28 +0100176 if(input->info()->data_layout() == DataLayout::NHWC)
177 {
178 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
179 ICLKernel::configure_internal(win);
180 }
181 else
182 {
183 auto win_config = validate_and_configure_window_nchw(input->info(), (_run_in_place) ? nullptr : output->info());
184 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
185 ICLKernel::configure_internal(win_config.second);
186 }
Giorgio Arena11674872018-02-07 15:38:12 +0000187
188 _config_id = "batch_normalization_layer_";
189 _config_id += string_from_data_type(input->info()->data_type());
190 _config_id += "_";
191 _config_id += support::cpp11::to_string(input->info()->dimension(0));
192 _config_id += "_";
193 _config_id += support::cpp11::to_string(input->info()->dimension(1));
194 _config_id += "_";
195 _config_id += support::cpp11::to_string(input->info()->dimension(2));
Giorgio Arena00b93f52018-06-28 17:18:50 +0100196 _config_id += "_";
197 _config_id += lower_string(string_from_data_layout(input->info()->data_layout()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198}
199
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000200Status CLBatchNormalizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output,
201 const ITensorInfo *mean, const ITensorInfo *var,
202 const ITensorInfo *beta, const ITensorInfo *gamma,
Giorgio Arena11674872018-02-07 15:38:12 +0000203 float epsilon, ActivationLayerInfo act_info)
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000204{
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000205 const bool run_in_place = (output == nullptr) || (output == input);
Giorgio Arena11674872018-02-07 15:38:12 +0000206 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, mean, var, beta, gamma, epsilon, act_info));
Sheri Zhang141c31a2020-10-08 12:35:28 +0100207
208 if(input->data_layout() != DataLayout::NHWC)
209 {
210 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_nchw(input->clone().get(), (run_in_place) ? nullptr : output->clone().get())
211 .first);
212 }
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000213
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000214 return Status{};
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000215}
216
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100217void CLBatchNormalizationLayerKernel::run(const Window &window, cl::CommandQueue &queue)
218{
219 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
220 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
221
222 Window slice = window.first_slice_window_3D();
223
224 Window vector_slice = window.first_slice_window_1D();
225 vector_slice.set(Window::DimX, Window::Dimension(0, 0, 0));
226
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000227 unsigned int include_output = (!_run_in_place) ? 1 : 0;
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000228 unsigned int idx = (1 + include_output) * num_arguments_per_3D_tensor();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100229 add_1D_tensor_argument(idx, _mean, vector_slice);
230 add_1D_tensor_argument(idx, _var, vector_slice);
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000231 if(_beta != nullptr)
232 {
233 add_1D_tensor_argument(idx, _beta, vector_slice);
234 }
235 if(_gamma != nullptr)
236 {
237 add_1D_tensor_argument(idx, _gamma, vector_slice);
238 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100239
240 do
241 {
242 idx = 0;
243 add_3D_tensor_argument(idx, _input, slice);
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000244 if(!_run_in_place)
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100245 {
246 add_3D_tensor_argument(idx, _output, slice);
247 }
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100248 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100249 }
250 while(window.slide_window_slice_3D(slice));
251}