Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | 879e8dd | 2019-05-08 17:49:28 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 3 | * |
| 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" |
| 28 | #include "arm_compute/core/CL/CLValidate.h" |
| 29 | #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" |
| 33 | #include "arm_compute/core/Window.h" |
| 34 | |
| 35 | #include "support/ToolchainSupport.h" |
| 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace |
| 40 | { |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 41 | Status validate_arguments(const ITensorInfo *input_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var, |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 42 | const ITensorInfo *fused_weights, const ITensorInfo *fused_bias, |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 43 | const ITensorInfo *input_bias, const ITensorInfo *bn_beta, const ITensorInfo *bn_gamma, |
| 44 | float epsilon, FuseBatchNormalizationType fbn_type) |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 45 | { |
| 46 | ARM_COMPUTE_UNUSED(epsilon); |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 47 | ARM_COMPUTE_ERROR_ON_NULLPTR(input_weights, bn_mean, bn_var); |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input_weights); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input_weights, 1, DataType::F16, DataType::F32); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_var); |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, bn_mean, bn_var); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON(input_bias == nullptr && fused_bias == nullptr); |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 53 | ARM_COMPUTE_RETURN_ERROR_ON(bn_mean->num_dimensions() > 1); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 54 | |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 55 | if(fbn_type == FuseBatchNormalizationType::CONVOLUTION) |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 56 | { |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 57 | ARM_COMPUTE_RETURN_ERROR_ON(input_weights->dimension(3) != bn_mean->dimension(0)); |
| 58 | } |
| 59 | else |
| 60 | { |
| 61 | const size_t channel_idx = get_data_layout_dimension_index(input_weights->data_layout(), DataLayoutDimension::CHANNEL); |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON(input_weights->dimension(channel_idx) != bn_mean->dimension(0)); |
| 63 | } |
| 64 | |
| 65 | // Validate bias |
| 66 | if(input_bias != nullptr) |
| 67 | { |
| 68 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, input_bias); |
| 69 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, input_bias); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 70 | } |
| 71 | // Validate beta |
| 72 | if(bn_beta != nullptr) |
| 73 | { |
| 74 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_beta); |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 75 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, bn_beta); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 76 | } |
| 77 | // Validate gamma |
| 78 | if(bn_gamma != nullptr) |
| 79 | { |
| 80 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_gamma); |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 81 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, bn_gamma); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 82 | } |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 83 | // Validate output weights |
| 84 | if(fused_weights != nullptr && fused_weights->total_size() != 0) |
| 85 | { |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 86 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input_weights, fused_weights); |
| 87 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input_weights, fused_weights); |
| 88 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, fused_weights); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 89 | } |
| 90 | // Validate output bias |
| 91 | if(fused_bias != nullptr && fused_bias->total_size() != 0) |
| 92 | { |
| 93 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, fused_bias); |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 94 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, fused_bias); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | return Status{}; |
| 98 | } |
| 99 | } // namespace |
| 100 | |
| 101 | CLFuseBatchNormalizationKernel::CLFuseBatchNormalizationKernel() |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 102 | : _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 Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 103 | _run_in_place_weights(false), _run_in_place_bias(false) |
| 104 | { |
| 105 | } |
| 106 | |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 107 | void CLFuseBatchNormalizationKernel::configure(const ICLTensor *input_weights, const ICLTensor *bn_mean, const ICLTensor *bn_var, |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 108 | ICLTensor *fused_weights, ICLTensor *fused_bias, |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 109 | const ICLTensor *input_bias, const ICLTensor *bn_beta, const ICLTensor *bn_gamma, |
| 110 | float epsilon, FuseBatchNormalizationType fbn_type) |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 111 | { |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 112 | ARM_COMPUTE_ERROR_ON_NULLPTR(input_weights, bn_mean, bn_var); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 113 | |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 114 | _input_weights = input_weights; |
| 115 | _input_bias = input_bias; |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 116 | _bn_mean = bn_mean; |
| 117 | _bn_var = bn_var; |
| 118 | _bn_beta = bn_beta; |
| 119 | _bn_gamma = bn_gamma; |
| 120 | _fused_weights = fused_weights; |
| 121 | _fused_bias = fused_bias; |
| 122 | _epsilon = epsilon; |
| 123 | |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 124 | _run_in_place_weights = (fused_weights == nullptr) || (fused_weights == input_weights); |
| 125 | _run_in_place_bias = (input_bias != nullptr && fused_bias == nullptr) || (input_bias != nullptr && fused_bias == input_bias); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 126 | |
| 127 | // Auto initialize outputs |
| 128 | if(_fused_weights != nullptr) |
| 129 | { |
| 130 | // Output tensor auto initialization if not yet initialized |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 131 | auto_init_if_empty(*_fused_weights->info(), *_input_weights->info()->clone()); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 132 | } |
| 133 | if(_fused_bias != nullptr) |
| 134 | { |
| 135 | // Output tensor auto initialization if not yet initialized |
| 136 | auto_init_if_empty(*_fused_bias->info(), *_bn_mean->info()->clone()); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | // Validate arguments |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 140 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input_weights->info(), bn_mean->info(), bn_var->info(), |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 141 | (fused_weights != nullptr) ? fused_weights->info() : nullptr, |
| 142 | (fused_bias != nullptr) ? fused_bias->info() : nullptr, |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 143 | (input_bias != nullptr) ? input_bias->info() : nullptr, |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 144 | (bn_beta != nullptr) ? bn_beta->info() : nullptr, |
| 145 | (bn_gamma != nullptr) ? bn_gamma->info() : nullptr, |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 146 | epsilon, fbn_type)); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 147 | |
| 148 | // Configure kernel window |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 149 | Window win = calculate_max_window(*input_weights->info()); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 150 | ICLKernel::configure_internal(win); |
| 151 | |
| 152 | // Set build options |
| 153 | CLBuildOptions build_opts; |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 154 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input_weights->info()->data_type())); |
| 155 | build_opts.add_option_if(fbn_type == FuseBatchNormalizationType::CONVOLUTION, "-DDIM2=" + support::cpp11::to_string(input_weights->info()->dimension(2))); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 156 | build_opts.add_option("-DEPSILON=" + float_to_string_with_full_precision(epsilon)); |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 157 | build_opts.add_option_if(_input_weights->info()->data_layout() == DataLayout::NHWC, "-DNHWC"); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 158 | build_opts.add_option_if(_run_in_place_weights, "-DIN_PLACE_W"); |
| 159 | build_opts.add_option_if(_run_in_place_bias, "-DIN_PLACE_B"); |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 160 | build_opts.add_option_if(input_bias != nullptr, "-DBIAS"); |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 161 | build_opts.add_option_if(bn_beta != nullptr, "-DBETA"); |
| 162 | build_opts.add_option_if(bn_gamma != nullptr, "-DGAMMA"); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 163 | |
| 164 | // Create kernel |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 165 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("fuse_batchnormalization_layer", build_opts.options())); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 168 | Status CLFuseBatchNormalizationKernel::validate(const ITensorInfo *input_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var, |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 169 | const ITensorInfo *fused_weights, const ITensorInfo *fused_bias, |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 170 | const ITensorInfo *input_bias, const ITensorInfo *bn_beta, const ITensorInfo *bn_gamma, |
| 171 | float epsilon, FuseBatchNormalizationType fbn_type) |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 172 | { |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 173 | 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 Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 174 | return Status{}; |
| 175 | } |
| 176 | |
| 177 | void CLFuseBatchNormalizationKernel::run(const arm_compute::Window &window, cl::CommandQueue &queue) |
| 178 | { |
| 179 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 180 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 181 | |
| 182 | // Create window slice |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 183 | Window collapsed_window = window.collapse(window, Window::DimZ); |
| 184 | Window slice_1d = window.first_slice_window_1D(); |
| 185 | Window slice_3d = collapsed_window.first_slice_window_3D(); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 186 | |
| 187 | // Add kernel arguments |
| 188 | unsigned int idx = 0; |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 189 | add_3D_tensor_argument(idx, _input_weights, slice_3d); |
| 190 | if(_input_bias != nullptr) |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 191 | { |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 192 | add_1D_tensor_argument(idx, _input_bias, slice_1d); |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 193 | } |
| 194 | add_1D_tensor_argument(idx, _bn_mean, slice_1d); |
| 195 | add_1D_tensor_argument(idx, _bn_var, slice_1d); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 196 | if(!_run_in_place_weights) |
| 197 | { |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 198 | add_3D_tensor_argument(idx, _fused_weights, slice_3d); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 199 | } |
| 200 | if(!_run_in_place_bias) |
| 201 | { |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 202 | add_1D_tensor_argument(idx, _fused_bias, slice_1d); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 203 | } |
| 204 | if(_bn_beta != nullptr) |
| 205 | { |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 206 | add_1D_tensor_argument(idx, _bn_beta, slice_1d); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 207 | } |
| 208 | if(_bn_gamma != nullptr) |
| 209 | { |
Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 210 | add_1D_tensor_argument(idx, _bn_gamma, slice_1d); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 211 | } |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame^] | 212 | enqueue(queue, *this, slice_3d, lws_hint()); |
Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame] | 213 | } |
| 214 | } // namespace arm_compute |