blob: 95c8250ee730707ad23677d77e403210c7974868 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Giorgio Arena11674872018-02-07 15:38:12 +00002 * Copyright (c) 2017-2018 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"
Michalis Spyrou172e5702017-06-26 14:18:47 +010029#include "arm_compute/core/FixedPoint.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Utils.h"
33#include "arm_compute/core/Validate.h"
34#include "arm_compute/core/Window.h"
35
Michalis Spyrou172e5702017-06-26 14:18:47 +010036#include "support/ToolchainSupport.h"
37
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);
48 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
49 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, var, beta, gamma);
50 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, mean, var, beta, gamma);
51 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, mean, var, beta, gamma);
52 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) != mean->dimension(0));
Giorgio Arena11674872018-02-07 15:38:12 +000053 if(act_info.enabled())
54 {
55 ActivationLayerInfo::ActivationFunction act = act_info.activation();
56 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() != DataType::F32 && input->data_type() != DataType::F16);
57 ARM_COMPUTE_RETURN_ERROR_ON(act != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::RELU && act != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU
58 && act != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU);
59 ARM_COMPUTE_RETURN_ERROR_ON(act_info.b() > act_info.a());
60 }
Giorgio Arena70623822017-11-27 15:50:10 +000061
62 if(output != nullptr && output->total_size() != 0)
63 {
64 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
65 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
66 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
67 }
68
Georgios Pinitas631c41a2017-12-06 11:53:03 +000069 return Status{};
Giorgio Arena70623822017-11-27 15:50:10 +000070}
71
Georgios Pinitas631c41a2017-12-06 11:53:03 +000072std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
Giorgio Arena70623822017-11-27 15:50:10 +000073{
Giorgio Arenaf6a43c52017-12-01 12:16:25 +000074 if(output != nullptr)
75 {
76 // Output tensor auto initialization if not yet initialized
77 auto_init_if_empty(*output, *input->clone());
78 }
79
Giorgio Arena70623822017-11-27 15:50:10 +000080 const unsigned int num_elems_processed_per_iteration = 16 / input->element_size();
81
82 // Configure kernel window
83 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
84 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
85
Giorgio Arena2995f5b2017-11-29 17:33:59 +000086 bool window_changed = false;
Giorgio Arena70623822017-11-27 15:50:10 +000087 if(output != nullptr)
88 {
89 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
90 window_changed = update_window_and_padding(win, input_access, output_access);
91 output_access.set_valid_region(win, input->valid_region());
92 }
93 else
94 {
95 window_changed = update_window_and_padding(win, input_access);
96 }
97
Georgios Pinitas631c41a2017-12-06 11:53:03 +000098 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arena70623822017-11-27 15:50:10 +000099 return std::make_pair(err, win);
100}
101} // namespace
102
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103CLBatchNormalizationLayerKernel::CLBatchNormalizationLayerKernel()
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000104 : _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 +0100105{
106}
107
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100108void 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 +0000109 float epsilon, ActivationLayerInfo act_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110{
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000111 ARM_COMPUTE_ERROR_ON_NULLPTR(input, mean, var, beta, gamma);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113 _input = input;
114 _output = output;
115 _mean = mean;
116 _var = var;
117 _beta = beta;
118 _gamma = gamma;
119 _epsilon = epsilon;
120
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000121 _run_in_place = (output == nullptr) || (output == input);
122
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100123 if(output != nullptr)
124 {
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000125 ARM_COMPUTE_ERROR_ON_NULLPTR(input->info(), output->info());
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100126 // Output tensor auto initialization if not yet initialized
Giorgio Arenaf6a43c52017-12-01 12:16:25 +0000127 auto_init_if_empty(*output->info(), *input->info()->clone());
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100128 }
129
Giorgio Arenaf6a43c52017-12-01 12:16:25 +0000130 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr,
Giorgio Arena11674872018-02-07 15:38:12 +0000131 mean->info(), var->info(), beta->info(), gamma->info(), epsilon, act_info));
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100132
Michalis Spyrou172e5702017-06-26 14:18:47 +0100133 const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size();
134
135 // Set build options
Giorgio Arena11674872018-02-07 15:38:12 +0000136 CLBuildOptions build_opts;
137 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
138 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
Giorgio Arena99ac60b2018-02-16 15:17:23 +0000139 build_opts.add_option_if(act_info.enabled(), "-DFUSED_ACTIVATION=" + lower_string(string_from_activation_func(act_info.activation())));
Giorgio Arena11674872018-02-07 15:38:12 +0000140 build_opts.add_option_if(act_info.enabled(), "-DA_VAL=" + float_to_string_with_full_precision(act_info.a()));
141 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 +0000142 build_opts.add_option_if(_run_in_place, "-DIN_PLACE");
Giorgio Arena11674872018-02-07 15:38:12 +0000143 build_opts.add_option_if(is_data_type_fixed_point(input->info()->data_type()), "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position()));
Michalis Spyrou172e5702017-06-26 14:18:47 +0100144
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145 // Create kernel
Giorgio Arena11674872018-02-07 15:38:12 +0000146 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("batchnormalization_layer", build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147
148 // Set kernel static arguments
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000149 unsigned int include_output = (!_run_in_place) ? 1 : 0;
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000150 unsigned int idx = (1 + include_output) * num_arguments_per_3D_tensor() + 4 * num_arguments_per_1D_tensor(); // Skip the input and output parameters
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151 _kernel.setArg<cl_float>(idx++, _epsilon);
152
153 // Configure kernel window
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000154 auto win_config = validate_and_configure_window(input->info(), (_run_in_place) ? nullptr : output->info());
Giorgio Arena70623822017-11-27 15:50:10 +0000155 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
156 ICLKernel::configure(win_config.second);
Giorgio Arena11674872018-02-07 15:38:12 +0000157
158 _config_id = "batch_normalization_layer_";
159 _config_id += string_from_data_type(input->info()->data_type());
160 _config_id += "_";
161 _config_id += support::cpp11::to_string(input->info()->dimension(0));
162 _config_id += "_";
163 _config_id += support::cpp11::to_string(input->info()->dimension(1));
164 _config_id += "_";
165 _config_id += support::cpp11::to_string(input->info()->dimension(2));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100166}
167
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000168Status CLBatchNormalizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output,
169 const ITensorInfo *mean, const ITensorInfo *var,
170 const ITensorInfo *beta, const ITensorInfo *gamma,
Giorgio Arena11674872018-02-07 15:38:12 +0000171 float epsilon, ActivationLayerInfo act_info)
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000172{
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000173 const bool run_in_place = (output == nullptr) || (output == input);
Giorgio Arena11674872018-02-07 15:38:12 +0000174 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, mean, var, beta, gamma, epsilon, act_info));
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000175 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), (run_in_place) ? nullptr : output->clone().get()).first);
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000176
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000177 return Status{};
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000178}
179
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100180void CLBatchNormalizationLayerKernel::run(const Window &window, cl::CommandQueue &queue)
181{
182 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
183 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
184
185 Window slice = window.first_slice_window_3D();
186
187 Window vector_slice = window.first_slice_window_1D();
188 vector_slice.set(Window::DimX, Window::Dimension(0, 0, 0));
189
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000190 unsigned int include_output = (!_run_in_place) ? 1 : 0;
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000191 unsigned int idx = (1 + include_output) * num_arguments_per_3D_tensor();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100192 add_1D_tensor_argument(idx, _mean, vector_slice);
193 add_1D_tensor_argument(idx, _var, vector_slice);
194 add_1D_tensor_argument(idx, _beta, vector_slice);
195 add_1D_tensor_argument(idx, _gamma, vector_slice);
196
197 do
198 {
199 idx = 0;
200 add_3D_tensor_argument(idx, _input, slice);
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000201 if(!_run_in_place)
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100202 {
203 add_3D_tensor_argument(idx, _output, slice);
204 }
Giorgio Arena11674872018-02-07 15:38:12 +0000205 enqueue(queue, *this, slice, _lws_hint);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100206 }
207 while(window.slide_window_slice_3D(slice));
208}