giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 1 | /* |
Michalis Spyrou | aeebe4a | 2019-01-09 14:21:03 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [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 |
Michalis Spyrou | aeebe4a | 2019-01-09 14:21:03 +0000 | [diff] [blame^] | 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Michalis Spyrou | aeebe4a | 2019-01-09 14:21:03 +0000 | [diff] [blame^] | 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 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/NEON/kernels/NEFuseBatchNormalizationKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/TensorInfo.h" |
| 27 | #include "arm_compute/core/Utils.h" |
| 28 | #include "arm_compute/core/Validate.h" |
| 29 | #include "arm_compute/core/Window.h" |
| 30 | |
| 31 | #include "arm_compute/core/Helpers.h" |
| 32 | #include "arm_compute/core/ITensor.h" |
| 33 | #include "arm_compute/core/TensorInfo.h" |
| 34 | #include "arm_compute/core/Utils.h" |
| 35 | #include "arm_compute/core/Window.h" |
| 36 | |
| 37 | #include "support/ToolchainSupport.h" |
| 38 | |
| 39 | #include "arm_compute/core/NEON/wrapper/wrapper.h" |
| 40 | #include "utils/TypePrinter.h" |
| 41 | namespace arm_compute |
| 42 | { |
| 43 | namespace |
| 44 | { |
| 45 | Status validate_arguments(const ITensorInfo *conv_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var, |
| 46 | const ITensorInfo *fused_weights, const ITensorInfo *fused_bias, |
| 47 | const ITensorInfo *conv_bias, const ITensorInfo *bn_beta, const ITensorInfo *bn_gamma, |
| 48 | float epsilon) |
| 49 | { |
| 50 | ARM_COMPUTE_UNUSED(epsilon); |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(conv_weights, 1, DataType::F16, DataType::F32); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_var); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(conv_weights, bn_mean, bn_var); |
| 54 | |
| 55 | unsigned int kernels_idx = get_data_layout_dimension_index(conv_weights->data_layout(), DataLayoutDimension::BATCHES); |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON(conv_weights->dimension(kernels_idx) != bn_mean->dimension(0)); |
| 57 | |
| 58 | // Validate bias |
| 59 | if(conv_bias != nullptr) |
| 60 | { |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, conv_bias); |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(conv_weights, conv_bias); |
| 63 | } |
| 64 | // Validate beta |
| 65 | if(bn_beta != nullptr) |
| 66 | { |
| 67 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_beta); |
| 68 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(conv_weights, bn_beta); |
| 69 | } |
| 70 | // Validate gamma |
| 71 | if(bn_gamma != nullptr) |
| 72 | { |
| 73 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_gamma); |
| 74 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(conv_weights, bn_gamma); |
| 75 | } |
| 76 | |
| 77 | // Validate output weights |
| 78 | if(fused_weights != nullptr && fused_weights->total_size() != 0) |
| 79 | { |
| 80 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(conv_weights, fused_weights); |
| 81 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(conv_weights, fused_weights); |
| 82 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(conv_weights, fused_weights); |
| 83 | } |
| 84 | // Validate output bias |
| 85 | if(fused_bias != nullptr && fused_bias->total_size() != 0) |
| 86 | { |
| 87 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, fused_bias); |
| 88 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(conv_weights, fused_bias); |
| 89 | } |
| 90 | |
| 91 | return Status{}; |
| 92 | } |
| 93 | |
| 94 | template <typename ScalarType, int size> |
| 95 | void fused_batch_normmalization(const ITensor *conv_weights, const ITensor *conv_bias, ITensor *fused_weights, ITensor *fused_bias, |
| 96 | const ITensor *bn_mean, const ITensor *bn_var, const ITensor *bn_beta, const ITensor *bn_gamma, float epsilon, const Window &window) |
| 97 | { |
| 98 | using ExactTagType = typename wrapper::traits::neon_vector<ScalarType, size>::tag_type; |
| 99 | |
| 100 | const bool run_in_place_weights = (fused_weights == nullptr) || (fused_weights == conv_weights); |
| 101 | const bool run_in_place_bias = (fused_bias == nullptr) || (conv_bias != nullptr && fused_bias == conv_bias); |
| 102 | |
| 103 | // Set build options |
| 104 | Window win = window; |
| 105 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 106 | |
| 107 | const int window_step_x = size; |
| 108 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 109 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 110 | |
| 111 | Iterator conv_w_in(conv_weights, win); |
| 112 | Iterator conv_w_out(run_in_place_weights ? conv_weights : fused_weights, win); |
| 113 | |
| 114 | const auto conv_bias_in = (conv_bias != nullptr ? reinterpret_cast<ScalarType *>(conv_bias->ptr_to_element(Coordinates(0, 0))) : nullptr); |
| 115 | auto conv_bias_out = (run_in_place_bias ? conv_bias_in : reinterpret_cast<ScalarType *>(fused_bias->ptr_to_element(Coordinates(0, 0)))); |
| 116 | |
| 117 | int slice = -1; |
| 118 | |
| 119 | const auto input_mean = reinterpret_cast<const ScalarType *>(bn_mean->ptr_to_element(Coordinates(0, 0))); |
| 120 | const auto input_var = reinterpret_cast<const ScalarType *>(bn_var->ptr_to_element(Coordinates(0, 0))); |
| 121 | const auto input_gamma = (bn_gamma != nullptr) ? reinterpret_cast<const ScalarType *>(bn_gamma->ptr_to_element(Coordinates(0, 0))) : nullptr; |
| 122 | const auto input_beta = (bn_beta != nullptr) ? reinterpret_cast<const ScalarType *>(bn_beta->ptr_to_element(Coordinates(0, 0))) : nullptr; |
| 123 | |
| 124 | auto mean_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{}); |
| 125 | auto var_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{}); |
| 126 | auto gamma_vec = wrapper::vdup_n(ScalarType(1), ExactTagType{}); |
| 127 | auto beta_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{}); |
| 128 | auto rvar_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{}); |
| 129 | const auto epsilon_vec = wrapper::vdup_n(ScalarType(epsilon), ExactTagType{}); |
| 130 | |
| 131 | auto mean = ScalarType(0.0); |
| 132 | auto var = ScalarType(0.0); |
| 133 | auto gamma = ScalarType(1.0); |
| 134 | auto beta = ScalarType(0.0); |
| 135 | auto conv_bias_in_scalar = ScalarType(0.0); |
| 136 | execute_window_loop(win, [&](const Coordinates & id) |
| 137 | { |
| 138 | if(slice != id[3]) |
| 139 | { |
| 140 | slice = id[3]; |
| 141 | mean = input_mean[slice]; |
| 142 | var = input_var[slice]; |
| 143 | gamma = ScalarType(1.0); |
| 144 | beta = ScalarType(0.0); |
| 145 | |
| 146 | // Construct vectors |
| 147 | mean_vec = wrapper::vdup_n(mean, ExactTagType{}); |
| 148 | var_vec = wrapper::vdup_n(var, ExactTagType{}); |
| 149 | if(input_gamma != nullptr) |
| 150 | { |
| 151 | gamma = input_gamma[slice]; |
| 152 | gamma_vec = wrapper::vdup_n(gamma, ExactTagType{}); |
| 153 | } |
| 154 | if(input_beta != nullptr) |
| 155 | { |
| 156 | beta = input_beta[slice]; |
| 157 | beta_vec = wrapper::vdup_n(beta, ExactTagType{}); |
| 158 | } |
| 159 | if(conv_bias_in != nullptr) |
| 160 | { |
| 161 | conv_bias_in_scalar = conv_bias_in[slice]; |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | conv_bias_in_scalar = ScalarType(0); |
| 166 | } |
| 167 | |
| 168 | conv_bias_in_scalar = (conv_bias_in_scalar - mean) / sqrt(var + ScalarType(epsilon)); |
| 169 | conv_bias_in_scalar = (conv_bias_in_scalar * gamma) + beta; |
| 170 | conv_bias_out[slice] = conv_bias_in_scalar; |
| 171 | rvar_vec = wrapper::vinvsqrt(wrapper::vadd(var_vec, epsilon_vec)); |
| 172 | } |
| 173 | |
| 174 | int x = window_start_x; |
| 175 | auto conv_w_in_ptr = reinterpret_cast<const ScalarType *>(conv_w_in.ptr()); |
| 176 | auto conv_w_out_ptr = reinterpret_cast<ScalarType *>(conv_w_out.ptr()); |
| 177 | |
| 178 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 179 | { |
| 180 | auto wn = wrapper::vloadq(conv_w_in_ptr + x); |
| 181 | wn = wrapper::vmul(wn, rvar_vec); |
| 182 | wn = wrapper::vmul(wn, gamma_vec); |
| 183 | |
| 184 | // Store results |
| 185 | wrapper::vstore(conv_w_out_ptr + x, wn); |
| 186 | } |
| 187 | |
| 188 | // Compute left-over elements |
| 189 | for(; x < window_end_x; ++x) |
| 190 | { |
| 191 | *(conv_w_out_ptr + x) = *(conv_w_in_ptr + x) / sqrt(var + ScalarType(epsilon)) * gamma; |
| 192 | } |
| 193 | }, |
| 194 | conv_w_in, conv_w_out); |
| 195 | } |
| 196 | } // namespace |
| 197 | |
| 198 | NEFuseBatchNormalizationKernel::NEFuseBatchNormalizationKernel() |
| 199 | : _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(), |
| 200 | _run_in_place_weights(false), _run_in_place_bias(false), _func(nullptr) |
| 201 | { |
| 202 | } |
| 203 | |
| 204 | void NEFuseBatchNormalizationKernel::configure(const ITensor *conv_weights, const ITensor *bn_mean, const ITensor *bn_var, |
| 205 | ITensor *fused_weights, ITensor *fused_bias, |
| 206 | const ITensor *conv_bias, const ITensor *bn_beta, const ITensor *bn_gamma, |
| 207 | float epsilon) |
| 208 | { |
| 209 | ARM_COMPUTE_ERROR_ON_NULLPTR(conv_weights, bn_mean, bn_var); |
| 210 | |
| 211 | _conv_weights = conv_weights; |
| 212 | _conv_bias = conv_bias; |
| 213 | _bn_mean = bn_mean; |
| 214 | _bn_var = bn_var; |
| 215 | _bn_beta = bn_beta; |
| 216 | _bn_gamma = bn_gamma; |
| 217 | _fused_weights = fused_weights; |
| 218 | _fused_bias = fused_bias; |
| 219 | _epsilon = epsilon; |
| 220 | |
| 221 | _run_in_place_weights = (fused_weights == nullptr) || (fused_weights == conv_weights); |
| 222 | _run_in_place_bias = (fused_bias == nullptr) || (conv_bias != nullptr && fused_bias == conv_bias); |
| 223 | |
| 224 | // Auto initialize outputs |
| 225 | if(_fused_weights != nullptr) |
| 226 | { |
| 227 | // Output tensor auto initialization if not yet initialized |
| 228 | auto_init_if_empty(*_fused_weights->info(), *_conv_weights->info()->clone()); |
| 229 | fused_weights->info()->set_valid_region(conv_weights->info()->valid_region()); |
| 230 | } |
| 231 | if(_fused_bias != nullptr) |
| 232 | { |
| 233 | // Output tensor auto initialization if not yet initialized |
| 234 | auto_init_if_empty(*_fused_bias->info(), *_bn_mean->info()->clone()); |
| 235 | _fused_bias->info()->set_valid_region(bn_mean->info()->valid_region()); |
| 236 | } |
| 237 | |
| 238 | // Validate arguments |
| 239 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(conv_weights->info(), bn_mean->info(), bn_var->info(), |
| 240 | (fused_weights != nullptr) ? fused_weights->info() : nullptr, |
| 241 | (fused_bias != nullptr) ? fused_bias->info() : nullptr, |
| 242 | (conv_bias != nullptr) ? conv_bias->info() : nullptr, |
| 243 | (bn_beta != nullptr) ? bn_beta->info() : nullptr, |
| 244 | (bn_gamma != nullptr) ? bn_gamma->info() : nullptr, |
| 245 | epsilon)); |
| 246 | |
| 247 | // Configure kernel window |
| 248 | Window win = calculate_max_window(*conv_weights->info()); |
| 249 | INEKernel::configure(win); |
| 250 | |
| 251 | // Configure function to run based on different data types |
| 252 | const DataType data_type = _conv_weights->info()->data_type(); |
| 253 | switch(data_type) |
| 254 | { |
| 255 | case DataType::F32: |
| 256 | _func = &fused_batch_normmalization<float, 4>; |
| 257 | break; |
| 258 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 259 | case DataType::F16: |
| 260 | _func = &fused_batch_normmalization<float16_t, 8>; |
| 261 | break; |
| 262 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 263 | default: |
| 264 | ARM_COMPUTE_ERROR("Not Supported"); |
| 265 | break; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | Status NEFuseBatchNormalizationKernel::validate(const ITensorInfo *conv_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var, |
| 270 | const ITensorInfo *fused_weights, const ITensorInfo *fused_bias, |
| 271 | const ITensorInfo *conv_bias, const ITensorInfo *bn_beta, const ITensorInfo *bn_gamma, |
| 272 | float epsilon) |
| 273 | { |
| 274 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(conv_weights, bn_mean, bn_var, fused_weights, fused_bias, conv_bias, bn_beta, bn_gamma, epsilon)); |
| 275 | return Status{}; |
| 276 | } |
| 277 | |
| 278 | void NEFuseBatchNormalizationKernel::run(const Window &window, const ThreadInfo &info) |
| 279 | { |
| 280 | ARM_COMPUTE_UNUSED(info); |
| 281 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 282 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 283 | (*_func)(_conv_weights, _conv_bias, _fused_weights, _fused_bias, _bn_mean, _bn_var, _bn_beta, _bn_gamma, _epsilon, window); |
| 284 | } |
| 285 | } // namespace arm_compute |