Georgios Pinitas | c936917 | 2018-09-26 11:25:40 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2018 ARM Limited. |
| 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 | { |
| 41 | Status validate_arguments(const ITensorInfo *conv_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var, |
| 42 | const ITensorInfo *fused_weights, const ITensorInfo *fused_bias, |
| 43 | const ITensorInfo *conv_bias, const ITensorInfo *bn_beta, const ITensorInfo *bn_gamma, |
| 44 | float epsilon) |
| 45 | { |
| 46 | ARM_COMPUTE_UNUSED(epsilon); |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(conv_weights); |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(conv_weights, 1, DataType::F16, DataType::F32); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_var); |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(conv_weights, bn_mean, bn_var); |
| 51 | |
| 52 | unsigned int kernels_idx = get_data_layout_dimension_index(conv_weights->data_layout(), DataLayoutDimension::BATCHES); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON(conv_weights->dimension(kernels_idx) != bn_mean->dimension(0)); |
| 54 | |
| 55 | // Validate bias |
| 56 | if(conv_bias != nullptr) |
| 57 | { |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, conv_bias); |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(conv_weights, conv_bias); |
| 60 | } |
| 61 | // Validate beta |
| 62 | if(bn_beta != nullptr) |
| 63 | { |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_beta); |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(conv_weights, bn_beta); |
| 66 | } |
| 67 | // Validate gamma |
| 68 | if(bn_gamma != nullptr) |
| 69 | { |
| 70 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_gamma); |
| 71 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(conv_weights, bn_gamma); |
| 72 | } |
| 73 | |
| 74 | // Validate output weights |
| 75 | if(fused_weights != nullptr && fused_weights->total_size() != 0) |
| 76 | { |
| 77 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(conv_weights, fused_weights); |
| 78 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(conv_weights, fused_weights); |
| 79 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(conv_weights, fused_weights); |
| 80 | } |
| 81 | // Validate output bias |
| 82 | if(fused_bias != nullptr && fused_bias->total_size() != 0) |
| 83 | { |
| 84 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, fused_bias); |
| 85 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(conv_weights, fused_bias); |
| 86 | } |
| 87 | |
| 88 | return Status{}; |
| 89 | } |
| 90 | } // namespace |
| 91 | |
| 92 | CLFuseBatchNormalizationKernel::CLFuseBatchNormalizationKernel() |
| 93 | : _conv_weights(nullptr), _conv_bias(nullptr), _bn_mean(nullptr), _bn_var(nullptr), _bn_gamma(nullptr), _bn_beta(nullptr), _fused_weights(nullptr), _fused_bias(nullptr), _epsilon(), |
| 94 | _run_in_place_weights(false), _run_in_place_bias(false) |
| 95 | { |
| 96 | } |
| 97 | |
| 98 | void CLFuseBatchNormalizationKernel::configure(const ICLTensor *conv_weights, const ICLTensor *bn_mean, const ICLTensor *bn_var, |
| 99 | ICLTensor *fused_weights, ICLTensor *fused_bias, |
| 100 | const ICLTensor *conv_bias, const ICLTensor *bn_beta, const ICLTensor *bn_gamma, |
| 101 | float epsilon) |
| 102 | { |
| 103 | ARM_COMPUTE_ERROR_ON_NULLPTR(conv_weights, bn_mean, bn_var); |
| 104 | |
| 105 | _conv_weights = conv_weights; |
| 106 | _conv_bias = conv_bias; |
| 107 | _bn_mean = bn_mean; |
| 108 | _bn_var = bn_var; |
| 109 | _bn_beta = bn_beta; |
| 110 | _bn_gamma = bn_gamma; |
| 111 | _fused_weights = fused_weights; |
| 112 | _fused_bias = fused_bias; |
| 113 | _epsilon = epsilon; |
| 114 | |
| 115 | _run_in_place_weights = (fused_weights == nullptr) || (fused_weights == conv_weights); |
| 116 | _run_in_place_bias = (fused_bias == nullptr) || (conv_bias != nullptr && fused_bias == conv_bias); |
| 117 | |
| 118 | // Auto initialize outputs |
| 119 | if(_fused_weights != nullptr) |
| 120 | { |
| 121 | // Output tensor auto initialization if not yet initialized |
| 122 | auto_init_if_empty(*_fused_weights->info(), *_conv_weights->info()->clone()); |
| 123 | fused_weights->info()->set_valid_region(conv_weights->info()->valid_region()); |
| 124 | } |
| 125 | if(_fused_bias != nullptr) |
| 126 | { |
| 127 | // Output tensor auto initialization if not yet initialized |
| 128 | auto_init_if_empty(*_fused_bias->info(), *_bn_mean->info()->clone()); |
| 129 | _fused_bias->info()->set_valid_region(bn_mean->info()->valid_region()); |
| 130 | } |
| 131 | |
| 132 | // Validate arguments |
| 133 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(conv_weights->info(), bn_mean->info(), bn_var->info(), |
| 134 | (fused_weights != nullptr) ? fused_weights->info() : nullptr, |
| 135 | (fused_bias != nullptr) ? fused_bias->info() : nullptr, |
| 136 | (conv_bias != nullptr) ? conv_bias->info() : nullptr, |
| 137 | (bn_beta != nullptr) ? bn_beta->info() : nullptr, |
| 138 | (bn_gamma != nullptr) ? bn_gamma->info() : nullptr, |
| 139 | epsilon)); |
| 140 | |
| 141 | // Configure kernel window |
| 142 | const unsigned int num_elems_processed_per_iteration_x = 16 / conv_weights->info()->element_size(); |
| 143 | const int output_width_x = conv_weights->info()->tensor_shape().x(); |
| 144 | const bool multi_access_x = (output_width_x / num_elems_processed_per_iteration_x > 0); |
| 145 | |
| 146 | Window win = calculate_max_window(*conv_weights->info()); |
| 147 | if(multi_access_x) |
| 148 | { |
| 149 | win.set(Window::DimX, Window::Dimension(win.x().start(), |
| 150 | ceil_to_multiple(win.x().end(), num_elems_processed_per_iteration_x), |
| 151 | num_elems_processed_per_iteration_x)); |
| 152 | } |
| 153 | ICLKernel::configure_internal(win); |
| 154 | |
| 155 | // Set build options |
| 156 | CLBuildOptions build_opts; |
| 157 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(conv_weights->info()->data_type())); |
| 158 | build_opts.add_option("-DSELECT_DATA_TYPE=" + get_cl_select_type_from_data_type(conv_weights->info()->data_type())); |
| 159 | build_opts.add_option("-DNUM_CHANNELS=" + support::cpp11::to_string(conv_weights->info()->dimension(2))); |
| 160 | build_opts.add_option("-DEPSILON=" + float_to_string_with_full_precision(epsilon)); |
| 161 | build_opts.add_option_if(multi_access_x, "-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration_x)); |
| 162 | build_opts.add_option_if(multi_access_x, "-DLAST_ACCESSED_X=" + support::cpp11::to_string(std::max<int>(output_width_x - num_elems_processed_per_iteration_x, 0))); |
| 163 | build_opts.add_option_if(_run_in_place_weights, "-DIN_PLACE_W"); |
| 164 | build_opts.add_option_if(_run_in_place_bias, "-DIN_PLACE_B"); |
| 165 | build_opts.add_option_if(conv_bias != nullptr, "-DHAS_BIAS"); |
| 166 | build_opts.add_option_if(bn_beta == nullptr, "-DUSE_DEFAULT_BETA"); |
| 167 | build_opts.add_option_if(bn_gamma == nullptr, "-DUSE_DEFAULT_GAMMA"); |
| 168 | |
| 169 | // Create kernel |
| 170 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("fuse_batchnormalization_layer", build_opts.options())); |
| 171 | } |
| 172 | |
| 173 | Status CLFuseBatchNormalizationKernel::validate(const ITensorInfo *conv_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var, |
| 174 | const ITensorInfo *fused_weights, const ITensorInfo *fused_bias, |
| 175 | const ITensorInfo *conv_bias, const ITensorInfo *bn_beta, const ITensorInfo *bn_gamma, |
| 176 | float epsilon) |
| 177 | { |
| 178 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(conv_weights, bn_mean, bn_var, fused_weights, fused_bias, conv_bias, bn_beta, bn_gamma, epsilon)); |
| 179 | return Status{}; |
| 180 | } |
| 181 | |
| 182 | void CLFuseBatchNormalizationKernel::run(const arm_compute::Window &window, cl::CommandQueue &queue) |
| 183 | { |
| 184 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 185 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 186 | |
| 187 | // Create window slice |
| 188 | Window collapsed_window = window.collapse_if_possible(window, Window::DimZ); |
| 189 | Window slice = collapsed_window.first_slice_window_4D(); |
| 190 | |
| 191 | Window vector_slice = window.first_slice_window_1D(); |
| 192 | vector_slice.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 193 | |
| 194 | // Add kernel arguments |
| 195 | unsigned int idx = 0; |
| 196 | add_4D_tensor_argument(idx, _conv_weights, slice); |
| 197 | add_1D_tensor_argument(idx, _bn_mean, vector_slice); |
| 198 | add_1D_tensor_argument(idx, _bn_var, vector_slice); |
| 199 | if(!_run_in_place_weights) |
| 200 | { |
| 201 | add_4D_tensor_argument(idx, _fused_weights, slice); |
| 202 | } |
| 203 | if(!_run_in_place_bias) |
| 204 | { |
| 205 | add_1D_tensor_argument(idx, _fused_bias, vector_slice); |
| 206 | } |
| 207 | if(_conv_bias != nullptr) |
| 208 | { |
| 209 | add_1D_tensor_argument(idx, _conv_bias, vector_slice); |
| 210 | } |
| 211 | if(_bn_beta != nullptr) |
| 212 | { |
| 213 | add_1D_tensor_argument(idx, _bn_beta, vector_slice); |
| 214 | } |
| 215 | if(_bn_gamma != nullptr) |
| 216 | { |
| 217 | add_1D_tensor_argument(idx, _bn_gamma, vector_slice); |
| 218 | } |
| 219 | enqueue(queue, *this, slice, lws_hint()); |
| 220 | } |
| 221 | } // namespace arm_compute |