blob: c745f3ff3c8cb2b7d7ee73f779315276dff8f470 [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Giorgio Arena11674872018-02-07 15:38:12 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier7068f992017-10-26 15:23:08 +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/GLES_COMPUTE/kernels/GCBatchNormalizationLayerKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h"
28#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h"
29#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h"
30#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/Utils.h"
32#include "arm_compute/core/Validate.h"
33#include "arm_compute/core/Window.h"
34
35#include "support/ToolchainSupport.h"
36
37using namespace arm_compute;
38
Michele Di Giorgio4d336302018-03-02 09:43:54 +000039namespace
40{
41Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output,
42 const ITensorInfo *mean, const ITensorInfo *var,
43 const ITensorInfo *beta, const ITensorInfo *gamma,
44 float epsilon, ActivationLayerInfo act_info)
45{
46 ARM_COMPUTE_UNUSED(epsilon);
47 ARM_COMPUTE_UNUSED(var);
48 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
49
50 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, mean, var);
Michele Di Giorgio4d336302018-03-02 09:43:54 +000051 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(mean, var);
52
53 if(output->total_size() != 0)
54 {
55 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
56 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Michele Di Giorgio4d336302018-03-02 09:43:54 +000057 }
58
59 if(beta != nullptr)
60 {
61 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, beta);
62 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, beta);
Michele Di Giorgio4d336302018-03-02 09:43:54 +000063 }
64 if(gamma != nullptr)
65 {
66 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, gamma);
67 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, gamma);
Michele Di Giorgio4d336302018-03-02 09:43:54 +000068 }
69 if(act_info.enabled())
70 {
71 ARM_COMPUTE_ERROR_ON(input->data_type() != DataType::F32 && input->data_type() != DataType::F16);
72 ARM_COMPUTE_ERROR_ON(act_info.activation() != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::RELU
73 && act_info.activation() != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU
74 && act_info.activation() != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU);
75 ARM_COMPUTE_ERROR_ON(act_info.b() > act_info.a());
76 }
77 return Status{};
78}
79
80std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output,
81 ITensorInfo *mean, ITensorInfo *var,
82 ITensorInfo *beta, ITensorInfo *gamma)
83{
84 // Output tensor auto initialization if not yet initialized
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010085 auto_init_if_empty(*output, input->tensor_shape(), 1, input->data_type());
Michele Di Giorgio4d336302018-03-02 09:43:54 +000086
87 unsigned int num_elems_processed_per_iteration = 1;
88 if(input->data_type() == DataType::F16)
89 {
90 num_elems_processed_per_iteration = 4;
91 }
92
93 // Configure kernel window
94 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
95
96 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
97 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
98 AccessWindowStatic mean_access(mean, 0, 0, mean->dimension(0) + 3, mean->dimension(1));
99 AccessWindowStatic var_access(var, 0, 0, var->dimension(0) + 3, var->dimension(1));
100
101 bool window_changed = false;
102 if(beta != nullptr)
103 {
104 AccessWindowStatic beta_access(beta, 0, 0, beta->dimension(0) + 3, beta->dimension(1));
105 if(gamma != nullptr)
106 {
107 AccessWindowStatic gamma_access(gamma, 0, 0, gamma->dimension(0) + 3, gamma->dimension(1));
108 window_changed = update_window_and_padding(win, input_access, output_access, mean_access, var_access, beta_access, gamma_access);
109 }
110 else
111 {
112 window_changed = update_window_and_padding(win, input_access, output_access, mean_access, var_access, beta_access);
113 }
114 }
115 else
116 {
117 if(gamma != nullptr)
118 {
119 AccessWindowStatic gamma_access(gamma, 0, 0, gamma->dimension(0) + 3, gamma->dimension(1));
120 window_changed = update_window_and_padding(win, input_access, output_access, mean_access, var_access, gamma_access);
121 }
122 else
123 {
124 window_changed = update_window_and_padding(win, input_access, output_access, mean_access, var_access);
125 }
126 }
127 output_access.set_valid_region(win, input->valid_region());
128
129 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
130 return std::make_pair(err, win);
131}
132} // namespace
133
Anthony Barbier7068f992017-10-26 15:23:08 +0100134GCBatchNormalizationLayerKernel::GCBatchNormalizationLayerKernel()
135 : _input(nullptr), _output(nullptr), _mean(nullptr), _var(nullptr), _beta(nullptr), _gamma(nullptr), _epsilon(0.0f)
136{
137}
138
139void GCBatchNormalizationLayerKernel::configure(const IGCTensor *input, IGCTensor *output, const IGCTensor *mean, const IGCTensor *var, const IGCTensor *beta, const IGCTensor *gamma,
Giorgio Arena11674872018-02-07 15:38:12 +0000140 float epsilon, ActivationLayerInfo act_info)
Anthony Barbier7068f992017-10-26 15:23:08 +0100141{
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000142 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, mean, var);
Anthony Barbier7068f992017-10-26 15:23:08 +0100143
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000144 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), mean->info(), var->info(),
145 (beta != nullptr) ? beta->info() : nullptr, (gamma != nullptr) ? gamma->info() : nullptr,
146 epsilon, act_info));
Anthony Barbier7068f992017-10-26 15:23:08 +0100147
148 _input = input;
149 _output = output;
150 _mean = mean;
151 _var = var;
152 _beta = beta;
153 _gamma = gamma;
154 _epsilon = epsilon;
155
Anthony Barbier7068f992017-10-26 15:23:08 +0100156 // Set build options
157 std::set<std::string> build_opts;
158 std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
159 build_opts.emplace(("#define " + dt_name));
160 build_opts.emplace(("#define ESPILON " + float_to_string_with_full_precision(_epsilon)));
161 build_opts.emplace(("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)));
162 build_opts.emplace(("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)));
163 build_opts.emplace(("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)));
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000164 if(beta == nullptr)
165 {
166 build_opts.emplace("#define USE_DEFAULT_BETA");
167 }
168 if(gamma == nullptr)
169 {
170 build_opts.emplace("#define USE_DEFAULT_GAMMA");
171 }
Anthony Barbier7068f992017-10-26 15:23:08 +0100172
Giorgio Arena11674872018-02-07 15:38:12 +0000173 if(act_info.enabled())
174 {
175 build_opts.emplace("#define " + string_from_activation_func(act_info.activation()));
176 build_opts.emplace("#define A_VAL " + float_to_string_with_full_precision(act_info.a()));
177 build_opts.emplace("#define B_VAL " + float_to_string_with_full_precision(act_info.b()));
178 }
179
Anthony Barbier7068f992017-10-26 15:23:08 +0100180 // Create kernel
181 _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("batchnormalization_layer", build_opts));
182
183 // Configure kernel window
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000184 auto win_config = validate_and_configure_window(input->info(), output->info(), mean->info(), var->info(),
185 (beta != nullptr) ? beta->info() : nullptr, (gamma != nullptr) ? gamma->info() : nullptr);
186 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbier7068f992017-10-26 15:23:08 +0100187
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000188 IGCKernel::configure(win_config.second);
189}
Anthony Barbier7068f992017-10-26 15:23:08 +0100190
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000191Status GCBatchNormalizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output,
192 const ITensorInfo *mean, const ITensorInfo *var,
193 const ITensorInfo *beta, const ITensorInfo *gamma,
194 float epsilon, ActivationLayerInfo act_info)
195{
196 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, mean, var, beta, gamma, epsilon, act_info));
197 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(),
198 mean->clone().get(), var->clone().get(),
199 beta->clone().get(), gamma->clone().get())
200 .first);
Anthony Barbier7068f992017-10-26 15:23:08 +0100201
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000202 return Status{};
Anthony Barbier7068f992017-10-26 15:23:08 +0100203}
204
205void GCBatchNormalizationLayerKernel::run(const Window &window)
206{
207 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
208 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
209
210 _kernel.use();
211
Frank Lei4406fd62018-02-01 14:47:14 +0800212 _output->set_needs_shifting(true);
213
214 Window slice = window.first_slice_window_3D();
215 Window slice_in = window.first_slice_window_3D();
Anthony Barbier7068f992017-10-26 15:23:08 +0100216
217 Window vector_slice = window.first_slice_window_1D();
218 vector_slice.set(Window::DimX, Window::Dimension(0, 0, 0));
219
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000220 unsigned int idx = 2 * num_arguments_per_3D_tensor();
221 unsigned int binding_point = 3;
222 add_1D_tensor_argument(idx, _mean, binding_point, vector_slice);
223 add_1D_tensor_argument(idx, _var, ++binding_point, vector_slice);
224 if(_beta != nullptr)
225 {
226 add_1D_tensor_argument(idx, _beta, ++binding_point, vector_slice);
227 }
228 if(_gamma != nullptr)
229 {
230 add_1D_tensor_argument(idx, _gamma, ++binding_point, vector_slice);
231 }
Anthony Barbier7068f992017-10-26 15:23:08 +0100232
Frank Lei4406fd62018-02-01 14:47:14 +0800233 slice.shift(Window::DimX, -(_output->info()->padding()).left);
234
Anthony Barbier7068f992017-10-26 15:23:08 +0100235 do
236 {
237 idx = 0;
Frank Lei4406fd62018-02-01 14:47:14 +0800238 add_3D_tensor_argument(idx, _input, 1, slice_in);
Anthony Barbier7068f992017-10-26 15:23:08 +0100239 add_3D_tensor_argument(idx, _output, 2, slice);
240
241 _kernel.update_shader_params();
242 enqueue(*this, slice);
243 }
Frank Lei4406fd62018-02-01 14:47:14 +0800244 while(window.slide_window_slice_3D(slice) && window.slide_window_slice_3D(slice_in));
Anthony Barbier7068f992017-10-26 15:23:08 +0100245}