blob: 357231940b2c6253e30eaa45ab74808a69c58b64 [file] [log] [blame]
Georgios Pinitasc9369172018-09-26 11:25:40 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Georgios Pinitasc9369172018-09-26 11:25:40 +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/CLFuseBatchNormalizationKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
Georgios Pinitasc9369172018-09-26 11:25:40 +010028#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"
Georgios Pinitasc9369172018-09-26 11:25:40 +010035
Matthew Bentham758b5ba2020-03-05 23:37:48 +000036#include "support/StringSupport.h"
Georgios Pinitasc9369172018-09-26 11:25:40 +010037
38namespace arm_compute
39{
40namespace
41{
Manuel Bottini2732cca2019-05-28 11:44:41 +010042Status validate_arguments(const ITensorInfo *input_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var,
Georgios Pinitasc9369172018-09-26 11:25:40 +010043 const ITensorInfo *fused_weights, const ITensorInfo *fused_bias,
Manuel Bottini2732cca2019-05-28 11:44:41 +010044 const ITensorInfo *input_bias, const ITensorInfo *bn_beta, const ITensorInfo *bn_gamma,
45 float epsilon, FuseBatchNormalizationType fbn_type)
Georgios Pinitasc9369172018-09-26 11:25:40 +010046{
47 ARM_COMPUTE_UNUSED(epsilon);
Manuel Bottini2732cca2019-05-28 11:44:41 +010048 ARM_COMPUTE_ERROR_ON_NULLPTR(input_weights, bn_mean, bn_var);
49 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input_weights);
50 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input_weights, 1, DataType::F16, DataType::F32);
Georgios Pinitasc9369172018-09-26 11:25:40 +010051 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_var);
Manuel Bottini2732cca2019-05-28 11:44:41 +010052 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, bn_mean, bn_var);
53 ARM_COMPUTE_RETURN_ERROR_ON(input_bias == nullptr && fused_bias == nullptr);
Gian Marco Iodice761c8d02019-06-10 14:46:49 +010054 ARM_COMPUTE_RETURN_ERROR_ON(bn_mean->num_dimensions() > 1);
Georgios Pinitasc9369172018-09-26 11:25:40 +010055
Manuel Bottini2732cca2019-05-28 11:44:41 +010056 if(fbn_type == FuseBatchNormalizationType::CONVOLUTION)
Georgios Pinitasc9369172018-09-26 11:25:40 +010057 {
Manuel Bottini2732cca2019-05-28 11:44:41 +010058 ARM_COMPUTE_RETURN_ERROR_ON(input_weights->dimension(3) != bn_mean->dimension(0));
59 }
60 else
61 {
62 const size_t channel_idx = get_data_layout_dimension_index(input_weights->data_layout(), DataLayoutDimension::CHANNEL);
63 ARM_COMPUTE_RETURN_ERROR_ON(input_weights->dimension(channel_idx) != bn_mean->dimension(0));
64 }
65
66 // Validate bias
67 if(input_bias != nullptr)
68 {
69 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, input_bias);
70 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, input_bias);
Georgios Pinitasc9369172018-09-26 11:25:40 +010071 }
72 // Validate beta
73 if(bn_beta != nullptr)
74 {
75 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_beta);
Manuel Bottini2732cca2019-05-28 11:44:41 +010076 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, bn_beta);
Georgios Pinitasc9369172018-09-26 11:25:40 +010077 }
78 // Validate gamma
79 if(bn_gamma != nullptr)
80 {
81 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_gamma);
Manuel Bottini2732cca2019-05-28 11:44:41 +010082 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, bn_gamma);
Georgios Pinitasc9369172018-09-26 11:25:40 +010083 }
Georgios Pinitasc9369172018-09-26 11:25:40 +010084 // Validate output weights
85 if(fused_weights != nullptr && fused_weights->total_size() != 0)
86 {
Manuel Bottini2732cca2019-05-28 11:44:41 +010087 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input_weights, fused_weights);
88 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input_weights, fused_weights);
89 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, fused_weights);
Georgios Pinitasc9369172018-09-26 11:25:40 +010090 }
91 // Validate output bias
92 if(fused_bias != nullptr && fused_bias->total_size() != 0)
93 {
94 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, fused_bias);
Manuel Bottini2732cca2019-05-28 11:44:41 +010095 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, fused_bias);
Georgios Pinitasc9369172018-09-26 11:25:40 +010096 }
97
98 return Status{};
99}
100} // namespace
101
102CLFuseBatchNormalizationKernel::CLFuseBatchNormalizationKernel()
Manuel Bottini2732cca2019-05-28 11:44:41 +0100103 : _input_weights(nullptr), _input_bias(nullptr), _bn_mean(nullptr), _bn_var(nullptr), _bn_gamma(nullptr), _bn_beta(nullptr), _fused_weights(nullptr), _fused_bias(nullptr), _epsilon(),
Georgios Pinitasc9369172018-09-26 11:25:40 +0100104 _run_in_place_weights(false), _run_in_place_bias(false)
105{
106}
107
Manuel Bottini2732cca2019-05-28 11:44:41 +0100108void CLFuseBatchNormalizationKernel::configure(const ICLTensor *input_weights, const ICLTensor *bn_mean, const ICLTensor *bn_var,
Georgios Pinitasc9369172018-09-26 11:25:40 +0100109 ICLTensor *fused_weights, ICLTensor *fused_bias,
Manuel Bottini2732cca2019-05-28 11:44:41 +0100110 const ICLTensor *input_bias, const ICLTensor *bn_beta, const ICLTensor *bn_gamma,
111 float epsilon, FuseBatchNormalizationType fbn_type)
Georgios Pinitasc9369172018-09-26 11:25:40 +0100112{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100113 configure(CLKernelLibrary::get().get_compile_context(), input_weights, bn_mean, bn_var, fused_weights, fused_bias, input_bias, bn_beta, bn_gamma, epsilon, fbn_type);
114}
115
Manuel Bottini256c0b92020-04-21 13:29:30 +0100116void CLFuseBatchNormalizationKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input_weights, const ICLTensor *bn_mean, const ICLTensor *bn_var,
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100117 ICLTensor *fused_weights, ICLTensor *fused_bias,
118 const ICLTensor *input_bias, const ICLTensor *bn_beta, const ICLTensor *bn_gamma,
119 float epsilon, FuseBatchNormalizationType fbn_type)
120{
Manuel Bottini2732cca2019-05-28 11:44:41 +0100121 ARM_COMPUTE_ERROR_ON_NULLPTR(input_weights, bn_mean, bn_var);
Georgios Pinitasc9369172018-09-26 11:25:40 +0100122
Giorgio Arena861c2ec2020-10-21 14:29:51 +0100123 auto padding_info = get_padding_info({ input_weights, bn_mean, bn_var, fused_weights, fused_bias, input_bias, bn_beta, bn_gamma });
124
Manuel Bottini2732cca2019-05-28 11:44:41 +0100125 _input_weights = input_weights;
126 _input_bias = input_bias;
Georgios Pinitasc9369172018-09-26 11:25:40 +0100127 _bn_mean = bn_mean;
128 _bn_var = bn_var;
129 _bn_beta = bn_beta;
130 _bn_gamma = bn_gamma;
131 _fused_weights = fused_weights;
132 _fused_bias = fused_bias;
133 _epsilon = epsilon;
134
Manuel Bottini2732cca2019-05-28 11:44:41 +0100135 _run_in_place_weights = (fused_weights == nullptr) || (fused_weights == input_weights);
136 _run_in_place_bias = (input_bias != nullptr && fused_bias == nullptr) || (input_bias != nullptr && fused_bias == input_bias);
Georgios Pinitasc9369172018-09-26 11:25:40 +0100137
138 // Auto initialize outputs
139 if(_fused_weights != nullptr)
140 {
141 // Output tensor auto initialization if not yet initialized
Manuel Bottini2732cca2019-05-28 11:44:41 +0100142 auto_init_if_empty(*_fused_weights->info(), *_input_weights->info()->clone());
Georgios Pinitasc9369172018-09-26 11:25:40 +0100143 }
144 if(_fused_bias != nullptr)
145 {
146 // Output tensor auto initialization if not yet initialized
147 auto_init_if_empty(*_fused_bias->info(), *_bn_mean->info()->clone());
Georgios Pinitasc9369172018-09-26 11:25:40 +0100148 }
149
150 // Validate arguments
Manuel Bottini2732cca2019-05-28 11:44:41 +0100151 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input_weights->info(), bn_mean->info(), bn_var->info(),
Georgios Pinitasc9369172018-09-26 11:25:40 +0100152 (fused_weights != nullptr) ? fused_weights->info() : nullptr,
153 (fused_bias != nullptr) ? fused_bias->info() : nullptr,
Manuel Bottini2732cca2019-05-28 11:44:41 +0100154 (input_bias != nullptr) ? input_bias->info() : nullptr,
Georgios Pinitasc9369172018-09-26 11:25:40 +0100155 (bn_beta != nullptr) ? bn_beta->info() : nullptr,
156 (bn_gamma != nullptr) ? bn_gamma->info() : nullptr,
Manuel Bottini2732cca2019-05-28 11:44:41 +0100157 epsilon, fbn_type));
Georgios Pinitasc9369172018-09-26 11:25:40 +0100158
159 // Configure kernel window
Manuel Bottini2732cca2019-05-28 11:44:41 +0100160 Window win = calculate_max_window(*input_weights->info());
Georgios Pinitasc9369172018-09-26 11:25:40 +0100161 ICLKernel::configure_internal(win);
162
163 // Set build options
164 CLBuildOptions build_opts;
Manuel Bottini2732cca2019-05-28 11:44:41 +0100165 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input_weights->info()->data_type()));
166 build_opts.add_option_if(fbn_type == FuseBatchNormalizationType::CONVOLUTION, "-DDIM2=" + support::cpp11::to_string(input_weights->info()->dimension(2)));
Georgios Pinitasc9369172018-09-26 11:25:40 +0100167 build_opts.add_option("-DEPSILON=" + float_to_string_with_full_precision(epsilon));
Manuel Bottini2732cca2019-05-28 11:44:41 +0100168 build_opts.add_option_if(_input_weights->info()->data_layout() == DataLayout::NHWC, "-DNHWC");
Georgios Pinitasc9369172018-09-26 11:25:40 +0100169 build_opts.add_option_if(_run_in_place_weights, "-DIN_PLACE_W");
170 build_opts.add_option_if(_run_in_place_bias, "-DIN_PLACE_B");
Manuel Bottini2732cca2019-05-28 11:44:41 +0100171 build_opts.add_option_if(input_bias != nullptr, "-DBIAS");
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100172 build_opts.add_option_if(bn_beta != nullptr, "-DBETA");
173 build_opts.add_option_if(bn_gamma != nullptr, "-DGAMMA");
Georgios Pinitasc9369172018-09-26 11:25:40 +0100174
175 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100176 _kernel = create_kernel(compile_context, "fuse_batchnormalization_layer", build_opts.options());
Giorgio Arena861c2ec2020-10-21 14:29:51 +0100177
178 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Georgios Pinitasc9369172018-09-26 11:25:40 +0100179}
180
Manuel Bottini2732cca2019-05-28 11:44:41 +0100181Status CLFuseBatchNormalizationKernel::validate(const ITensorInfo *input_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var,
Georgios Pinitasc9369172018-09-26 11:25:40 +0100182 const ITensorInfo *fused_weights, const ITensorInfo *fused_bias,
Manuel Bottini2732cca2019-05-28 11:44:41 +0100183 const ITensorInfo *input_bias, const ITensorInfo *bn_beta, const ITensorInfo *bn_gamma,
184 float epsilon, FuseBatchNormalizationType fbn_type)
Georgios Pinitasc9369172018-09-26 11:25:40 +0100185{
Manuel Bottini2732cca2019-05-28 11:44:41 +0100186 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input_weights, bn_mean, bn_var, fused_weights, fused_bias, input_bias, bn_beta, bn_gamma, epsilon, fbn_type));
Georgios Pinitasc9369172018-09-26 11:25:40 +0100187 return Status{};
188}
189
190void CLFuseBatchNormalizationKernel::run(const arm_compute::Window &window, cl::CommandQueue &queue)
191{
192 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
193 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
194
195 // Create window slice
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100196 Window collapsed_window = window.collapse(window, Window::DimZ);
197 Window slice_1d = window.first_slice_window_1D();
198 Window slice_3d = collapsed_window.first_slice_window_3D();
Georgios Pinitasc9369172018-09-26 11:25:40 +0100199
200 // Add kernel arguments
201 unsigned int idx = 0;
Manuel Bottini2732cca2019-05-28 11:44:41 +0100202 add_3D_tensor_argument(idx, _input_weights, slice_3d);
203 if(_input_bias != nullptr)
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100204 {
Manuel Bottini2732cca2019-05-28 11:44:41 +0100205 add_1D_tensor_argument(idx, _input_bias, slice_1d);
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100206 }
207 add_1D_tensor_argument(idx, _bn_mean, slice_1d);
208 add_1D_tensor_argument(idx, _bn_var, slice_1d);
Georgios Pinitasc9369172018-09-26 11:25:40 +0100209 if(!_run_in_place_weights)
210 {
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100211 add_3D_tensor_argument(idx, _fused_weights, slice_3d);
Georgios Pinitasc9369172018-09-26 11:25:40 +0100212 }
213 if(!_run_in_place_bias)
214 {
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100215 add_1D_tensor_argument(idx, _fused_bias, slice_1d);
Georgios Pinitasc9369172018-09-26 11:25:40 +0100216 }
217 if(_bn_beta != nullptr)
218 {
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100219 add_1D_tensor_argument(idx, _bn_beta, slice_1d);
Georgios Pinitasc9369172018-09-26 11:25:40 +0100220 }
221 if(_bn_gamma != nullptr)
222 {
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100223 add_1D_tensor_argument(idx, _bn_gamma, slice_1d);
Georgios Pinitasc9369172018-09-26 11:25:40 +0100224 }
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100225 enqueue(queue, *this, slice_3d, lws_hint());
Georgios Pinitasc9369172018-09-26 11:25:40 +0100226}
227} // namespace arm_compute