blob: 68fe324df6c3dfcdd4478619b4360054e21ac382 [file] [log] [blame]
Georgios Pinitasc9369172018-09-26 11:25:40 +01001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2018-2021, 2023 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 */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLFuseBatchNormalizationKernel.h"
Georgios Pinitasc9369172018-09-26 11:25:40 +010025
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"
Matthew Bentham314d3e22023-06-23 10:53:52 +000032#include "arm_compute/core/utils/StringUtils.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/CL/CLValidate.h"
34#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
Georgios Pinitasc9369172018-09-26 11:25:40 +010036
Matthew Bentham758b5ba2020-03-05 23:37:48 +000037#include "support/StringSupport.h"
Georgios Pinitasc9369172018-09-26 11:25:40 +010038
39namespace arm_compute
40{
41namespace
42{
Manuel Bottini2732cca2019-05-28 11:44:41 +010043Status validate_arguments(const ITensorInfo *input_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var,
Georgios Pinitasc9369172018-09-26 11:25:40 +010044 const ITensorInfo *fused_weights, const ITensorInfo *fused_bias,
Manuel Bottini2732cca2019-05-28 11:44:41 +010045 const ITensorInfo *input_bias, const ITensorInfo *bn_beta, const ITensorInfo *bn_gamma,
46 float epsilon, FuseBatchNormalizationType fbn_type)
Georgios Pinitasc9369172018-09-26 11:25:40 +010047{
48 ARM_COMPUTE_UNUSED(epsilon);
Manuel Bottini2732cca2019-05-28 11:44:41 +010049 ARM_COMPUTE_ERROR_ON_NULLPTR(input_weights, bn_mean, bn_var);
50 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input_weights);
51 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input_weights, 1, DataType::F16, DataType::F32);
Georgios Pinitasc9369172018-09-26 11:25:40 +010052 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_var);
Manuel Bottini2732cca2019-05-28 11:44:41 +010053 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, bn_mean, bn_var);
54 ARM_COMPUTE_RETURN_ERROR_ON(input_bias == nullptr && fused_bias == nullptr);
Gian Marco Iodice761c8d02019-06-10 14:46:49 +010055 ARM_COMPUTE_RETURN_ERROR_ON(bn_mean->num_dimensions() > 1);
Georgios Pinitasc9369172018-09-26 11:25:40 +010056
Manuel Bottini2732cca2019-05-28 11:44:41 +010057 if(fbn_type == FuseBatchNormalizationType::CONVOLUTION)
Georgios Pinitasc9369172018-09-26 11:25:40 +010058 {
Manuel Bottini2732cca2019-05-28 11:44:41 +010059 ARM_COMPUTE_RETURN_ERROR_ON(input_weights->dimension(3) != bn_mean->dimension(0));
60 }
61 else
62 {
63 const size_t channel_idx = get_data_layout_dimension_index(input_weights->data_layout(), DataLayoutDimension::CHANNEL);
64 ARM_COMPUTE_RETURN_ERROR_ON(input_weights->dimension(channel_idx) != bn_mean->dimension(0));
65 }
66
67 // Validate bias
68 if(input_bias != nullptr)
69 {
70 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, input_bias);
71 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, input_bias);
Georgios Pinitasc9369172018-09-26 11:25:40 +010072 }
73 // Validate beta
74 if(bn_beta != nullptr)
75 {
76 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_beta);
Manuel Bottini2732cca2019-05-28 11:44:41 +010077 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, bn_beta);
Georgios Pinitasc9369172018-09-26 11:25:40 +010078 }
79 // Validate gamma
80 if(bn_gamma != nullptr)
81 {
82 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_gamma);
Manuel Bottini2732cca2019-05-28 11:44:41 +010083 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, bn_gamma);
Georgios Pinitasc9369172018-09-26 11:25:40 +010084 }
Georgios Pinitasc9369172018-09-26 11:25:40 +010085 // Validate output weights
86 if(fused_weights != nullptr && fused_weights->total_size() != 0)
87 {
Manuel Bottini2732cca2019-05-28 11:44:41 +010088 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input_weights, fused_weights);
89 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input_weights, fused_weights);
90 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, fused_weights);
Georgios Pinitasc9369172018-09-26 11:25:40 +010091 }
92 // Validate output bias
93 if(fused_bias != nullptr && fused_bias->total_size() != 0)
94 {
95 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, fused_bias);
Manuel Bottini2732cca2019-05-28 11:44:41 +010096 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, fused_bias);
Georgios Pinitasc9369172018-09-26 11:25:40 +010097 }
98
99 return Status{};
100}
101} // namespace
102
103CLFuseBatchNormalizationKernel::CLFuseBatchNormalizationKernel()
Manuel Bottini2732cca2019-05-28 11:44:41 +0100104 : _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 +0100105 _run_in_place_weights(false), _run_in_place_bias(false)
106{
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100107 _type = CLKernelType::ELEMENTWISE;
Georgios Pinitasc9369172018-09-26 11:25:40 +0100108}
109
Manuel Bottini2732cca2019-05-28 11:44:41 +0100110void CLFuseBatchNormalizationKernel::configure(const ICLTensor *input_weights, const ICLTensor *bn_mean, const ICLTensor *bn_var,
Georgios Pinitasc9369172018-09-26 11:25:40 +0100111 ICLTensor *fused_weights, ICLTensor *fused_bias,
Manuel Bottini2732cca2019-05-28 11:44:41 +0100112 const ICLTensor *input_bias, const ICLTensor *bn_beta, const ICLTensor *bn_gamma,
113 float epsilon, FuseBatchNormalizationType fbn_type)
Georgios Pinitasc9369172018-09-26 11:25:40 +0100114{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100115 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);
116}
117
Manuel Bottini256c0b92020-04-21 13:29:30 +0100118void 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 +0100119 ICLTensor *fused_weights, ICLTensor *fused_bias,
120 const ICLTensor *input_bias, const ICLTensor *bn_beta, const ICLTensor *bn_gamma,
121 float epsilon, FuseBatchNormalizationType fbn_type)
122{
Manuel Bottini2732cca2019-05-28 11:44:41 +0100123 ARM_COMPUTE_ERROR_ON_NULLPTR(input_weights, bn_mean, bn_var);
Georgios Pinitasc9369172018-09-26 11:25:40 +0100124
Giorgio Arena861c2ec2020-10-21 14:29:51 +0100125 auto padding_info = get_padding_info({ input_weights, bn_mean, bn_var, fused_weights, fused_bias, input_bias, bn_beta, bn_gamma });
126
Manuel Bottini2732cca2019-05-28 11:44:41 +0100127 _input_weights = input_weights;
128 _input_bias = input_bias;
Georgios Pinitasc9369172018-09-26 11:25:40 +0100129 _bn_mean = bn_mean;
130 _bn_var = bn_var;
131 _bn_beta = bn_beta;
132 _bn_gamma = bn_gamma;
133 _fused_weights = fused_weights;
134 _fused_bias = fused_bias;
135 _epsilon = epsilon;
136
Manuel Bottini2732cca2019-05-28 11:44:41 +0100137 _run_in_place_weights = (fused_weights == nullptr) || (fused_weights == input_weights);
138 _run_in_place_bias = (input_bias != nullptr && fused_bias == nullptr) || (input_bias != nullptr && fused_bias == input_bias);
Georgios Pinitasc9369172018-09-26 11:25:40 +0100139
140 // Auto initialize outputs
141 if(_fused_weights != nullptr)
142 {
143 // Output tensor auto initialization if not yet initialized
Manuel Bottini2732cca2019-05-28 11:44:41 +0100144 auto_init_if_empty(*_fused_weights->info(), *_input_weights->info()->clone());
Georgios Pinitasc9369172018-09-26 11:25:40 +0100145 }
146 if(_fused_bias != nullptr)
147 {
148 // Output tensor auto initialization if not yet initialized
149 auto_init_if_empty(*_fused_bias->info(), *_bn_mean->info()->clone());
Georgios Pinitasc9369172018-09-26 11:25:40 +0100150 }
151
152 // Validate arguments
Manuel Bottini2732cca2019-05-28 11:44:41 +0100153 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input_weights->info(), bn_mean->info(), bn_var->info(),
Georgios Pinitasc9369172018-09-26 11:25:40 +0100154 (fused_weights != nullptr) ? fused_weights->info() : nullptr,
155 (fused_bias != nullptr) ? fused_bias->info() : nullptr,
Manuel Bottini2732cca2019-05-28 11:44:41 +0100156 (input_bias != nullptr) ? input_bias->info() : nullptr,
Georgios Pinitasc9369172018-09-26 11:25:40 +0100157 (bn_beta != nullptr) ? bn_beta->info() : nullptr,
158 (bn_gamma != nullptr) ? bn_gamma->info() : nullptr,
Manuel Bottini2732cca2019-05-28 11:44:41 +0100159 epsilon, fbn_type));
Georgios Pinitasc9369172018-09-26 11:25:40 +0100160
161 // Configure kernel window
Manuel Bottini2732cca2019-05-28 11:44:41 +0100162 Window win = calculate_max_window(*input_weights->info());
Georgios Pinitasc9369172018-09-26 11:25:40 +0100163 ICLKernel::configure_internal(win);
164
165 // Set build options
166 CLBuildOptions build_opts;
Manuel Bottini2732cca2019-05-28 11:44:41 +0100167 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input_weights->info()->data_type()));
168 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 +0100169 build_opts.add_option("-DEPSILON=" + float_to_string_with_full_precision(epsilon));
Manuel Bottini2732cca2019-05-28 11:44:41 +0100170 build_opts.add_option_if(_input_weights->info()->data_layout() == DataLayout::NHWC, "-DNHWC");
Georgios Pinitasc9369172018-09-26 11:25:40 +0100171 build_opts.add_option_if(_run_in_place_weights, "-DIN_PLACE_W");
172 build_opts.add_option_if(_run_in_place_bias, "-DIN_PLACE_B");
Manuel Bottini2732cca2019-05-28 11:44:41 +0100173 build_opts.add_option_if(input_bias != nullptr, "-DBIAS");
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100174 build_opts.add_option_if(bn_beta != nullptr, "-DBETA");
175 build_opts.add_option_if(bn_gamma != nullptr, "-DGAMMA");
Georgios Pinitasc9369172018-09-26 11:25:40 +0100176
177 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100178 _kernel = create_kernel(compile_context, "fuse_batchnormalization_layer", build_opts.options());
Giorgio Arena861c2ec2020-10-21 14:29:51 +0100179
180 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Georgios Pinitasc9369172018-09-26 11:25:40 +0100181}
182
Manuel Bottini2732cca2019-05-28 11:44:41 +0100183Status CLFuseBatchNormalizationKernel::validate(const ITensorInfo *input_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var,
Georgios Pinitasc9369172018-09-26 11:25:40 +0100184 const ITensorInfo *fused_weights, const ITensorInfo *fused_bias,
Manuel Bottini2732cca2019-05-28 11:44:41 +0100185 const ITensorInfo *input_bias, const ITensorInfo *bn_beta, const ITensorInfo *bn_gamma,
186 float epsilon, FuseBatchNormalizationType fbn_type)
Georgios Pinitasc9369172018-09-26 11:25:40 +0100187{
Manuel Bottini2732cca2019-05-28 11:44:41 +0100188 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 +0100189 return Status{};
190}
191
192void CLFuseBatchNormalizationKernel::run(const arm_compute::Window &window, cl::CommandQueue &queue)
193{
194 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
195 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
196
197 // Create window slice
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100198 Window collapsed_window = window.collapse(window, Window::DimZ);
199 Window slice_1d = window.first_slice_window_1D();
200 Window slice_3d = collapsed_window.first_slice_window_3D();
Georgios Pinitasc9369172018-09-26 11:25:40 +0100201
202 // Add kernel arguments
203 unsigned int idx = 0;
Manuel Bottini2732cca2019-05-28 11:44:41 +0100204 add_3D_tensor_argument(idx, _input_weights, slice_3d);
205 if(_input_bias != nullptr)
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100206 {
Manuel Bottini2732cca2019-05-28 11:44:41 +0100207 add_1D_tensor_argument(idx, _input_bias, slice_1d);
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100208 }
209 add_1D_tensor_argument(idx, _bn_mean, slice_1d);
210 add_1D_tensor_argument(idx, _bn_var, slice_1d);
Georgios Pinitasc9369172018-09-26 11:25:40 +0100211 if(!_run_in_place_weights)
212 {
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100213 add_3D_tensor_argument(idx, _fused_weights, slice_3d);
Georgios Pinitasc9369172018-09-26 11:25:40 +0100214 }
215 if(!_run_in_place_bias)
216 {
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100217 add_1D_tensor_argument(idx, _fused_bias, slice_1d);
Georgios Pinitasc9369172018-09-26 11:25:40 +0100218 }
219 if(_bn_beta != nullptr)
220 {
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100221 add_1D_tensor_argument(idx, _bn_beta, slice_1d);
Georgios Pinitasc9369172018-09-26 11:25:40 +0100222 }
223 if(_bn_gamma != nullptr)
224 {
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100225 add_1D_tensor_argument(idx, _bn_gamma, slice_1d);
Georgios Pinitasc9369172018-09-26 11:25:40 +0100226 }
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100227 enqueue(queue, *this, slice_3d, lws_hint());
Georgios Pinitasc9369172018-09-26 11:25:40 +0100228}
229} // namespace arm_compute