Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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/NEON/kernels/NEBatchNormalizationLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Helpers.h" |
| 27 | #include "arm_compute/core/NEON/NEFixedPoint.h" |
| 28 | #include "arm_compute/core/NEON/NEMath.h" |
| 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Utils.h" |
| 31 | #include "arm_compute/core/Validate.h" |
| 32 | #include "arm_compute/core/Window.h" |
| 33 | |
| 34 | using namespace arm_compute; |
| 35 | |
| 36 | NEBatchNormalizationLayerKernel::NEBatchNormalizationLayerKernel() |
| 37 | : _func(nullptr), _input(nullptr), _output(nullptr), _mean(nullptr), _var(nullptr), _gamma(nullptr), _beta(nullptr), _epsilon() |
| 38 | { |
| 39 | } |
| 40 | |
Georgios Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 41 | void batch_normalization_q8(ITensor *in, ITensor *out, const ITensor *mean, const ITensor *var, const ITensor *beta, const ITensor *gamma, float epsilon, const Window &window) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 42 | { |
| 43 | Iterator input(in, window); |
| 44 | Iterator output(out, window); |
| 45 | |
| 46 | // Hold information about the current feature map we are iterating. |
| 47 | // Only compute denominator and NEON vectors once per feature map. |
| 48 | int slice = -1; |
| 49 | |
Michalis Spyrou | bbd3d60 | 2017-06-21 17:29:40 +0100 | [diff] [blame] | 50 | const int fixed_point_position = in->info()->fixed_point_position(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 51 | const auto input_mean = reinterpret_cast<const qint8_t *>(mean->ptr_to_element(Coordinates(0, 0))); |
| 52 | const auto input_var = reinterpret_cast<const qint8_t *>(var->ptr_to_element(Coordinates(0, 0))); |
| 53 | const auto input_gamma = reinterpret_cast<const qint8_t *>(gamma->ptr_to_element(Coordinates(0, 0))); |
| 54 | const auto input_beta = reinterpret_cast<const qint8_t *>(beta->ptr_to_element(Coordinates(0, 0))); |
| 55 | |
| 56 | qint8x16_t mean_vec = vdupq_n_qs8(0); |
| 57 | qint8x16_t var_vec = vdupq_n_qs8(0); |
| 58 | qint8x16_t gamma_vec = vdupq_n_qs8(0); |
| 59 | qint8x16_t beta_vec = vdupq_n_qs8(0); |
| 60 | qint8x16_t denominator = vdupq_n_qs8(0); |
Georgios Pinitas | 21efeb4 | 2017-07-04 12:47:17 +0100 | [diff] [blame] | 61 | const qint8x16_t epsilon_vec = vdupq_n_qs8(sqcvt_qs8_f32(epsilon, fixed_point_position)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 62 | execute_window_loop(window, [&](const Coordinates & id) |
| 63 | { |
| 64 | if(slice != id.z()) |
| 65 | { |
| 66 | // Conctruct vectors |
| 67 | mean_vec = vdupq_n_qs8(*(input_mean + id.z())); |
| 68 | var_vec = vdupq_n_qs8(*(input_var + id.z())); |
| 69 | gamma_vec = vdupq_n_qs8(*(input_gamma + id.z())); |
| 70 | beta_vec = vdupq_n_qs8(*(input_beta + id.z())); |
| 71 | |
| 72 | // Calculate denominator |
| 73 | denominator = vqinvsqrtq_qs8(vqaddq_qs8(var_vec, epsilon_vec), fixed_point_position); |
| 74 | slice = id.z(); |
| 75 | } |
| 76 | |
| 77 | // Calculate x bar and store results |
| 78 | const qint8x16_t numerator = vqsubq_qs8(vld1q_qs8(reinterpret_cast<const qint8_t *>(input.ptr())), mean_vec); |
| 79 | const qint8x16_t x_bar = vqmulq_qs8(numerator, denominator, fixed_point_position); |
| 80 | vst1q_qs8(reinterpret_cast<qint8_t *>(output.ptr()), vqmlaq_qs8(beta_vec, x_bar, gamma_vec, fixed_point_position)); |
| 81 | }, |
| 82 | input, output); |
| 83 | } |
| 84 | |
Georgios Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 85 | void batch_normalization_q16(ITensor *in, ITensor *out, const ITensor *mean, const ITensor *var, const ITensor *beta, const ITensor *gamma, float epsilon, const Window &window) |
Michalis Spyrou | bbd3d60 | 2017-06-21 17:29:40 +0100 | [diff] [blame] | 86 | { |
| 87 | Iterator input(in, window); |
| 88 | Iterator output(out, window); |
| 89 | |
| 90 | // Hold information about the current feature map we are iterating. |
| 91 | // Only compute denominator and NEON vectors once per feature map. |
| 92 | int slice = -1; |
| 93 | |
| 94 | const int fixed_point_position = in->info()->fixed_point_position(); |
| 95 | const auto input_mean = reinterpret_cast<const qint16_t *>(mean->ptr_to_element(Coordinates(0, 0))); |
| 96 | const auto input_var = reinterpret_cast<const qint16_t *>(var->ptr_to_element(Coordinates(0, 0))); |
| 97 | const auto input_gamma = reinterpret_cast<const qint16_t *>(gamma->ptr_to_element(Coordinates(0, 0))); |
| 98 | const auto input_beta = reinterpret_cast<const qint16_t *>(beta->ptr_to_element(Coordinates(0, 0))); |
| 99 | |
| 100 | qint16x8_t mean_vec = vdupq_n_qs16(0); |
| 101 | qint16x8_t var_vec = vdupq_n_qs16(0); |
| 102 | qint16x8_t gamma_vec = vdupq_n_qs16(0); |
| 103 | qint16x8_t beta_vec = vdupq_n_qs16(0); |
| 104 | qint16x8_t denominator = vdupq_n_qs16(0); |
| 105 | const qint16x8_t epsilon_vec = vdupq_n_qs16(sqcvt_qs16_f32(epsilon, fixed_point_position)); |
| 106 | execute_window_loop(window, [&](const Coordinates & id) |
| 107 | { |
| 108 | if(slice != id.z()) |
| 109 | { |
| 110 | // Conctruct vectors |
| 111 | mean_vec = vdupq_n_qs16(*(input_mean + id.z())); |
| 112 | var_vec = vdupq_n_qs16(*(input_var + id.z())); |
| 113 | gamma_vec = vdupq_n_qs16(*(input_gamma + id.z())); |
| 114 | beta_vec = vdupq_n_qs16(*(input_beta + id.z())); |
| 115 | |
| 116 | // Calculate denominator |
| 117 | denominator = vqinvsqrtq_qs16(vqaddq_qs16(var_vec, epsilon_vec), fixed_point_position); |
| 118 | slice = id.z(); |
| 119 | } |
| 120 | |
| 121 | // Calculate x bar and store results |
| 122 | const qint16x8_t numerator = vqsubq_qs16(vld1q_qs16(reinterpret_cast<const qint16_t *>(input.ptr())), mean_vec); |
| 123 | const qint16x8_t x_bar = vqmulq_qs16(numerator, denominator, fixed_point_position); |
| 124 | vst1q_qs16(reinterpret_cast<qint16_t *>(output.ptr()), vqmlaq_qs16(beta_vec, x_bar, gamma_vec, fixed_point_position)); |
| 125 | }, |
| 126 | input, output); |
| 127 | } |
| 128 | |
Georgios Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 129 | void batch_normalization_fp32(ITensor *in, ITensor *out, const ITensor *mean, const ITensor *var, const ITensor *beta, const ITensor *gamma, float epsilon, const Window &window) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 130 | { |
| 131 | Iterator input(in, window); |
| 132 | Iterator output(out, window); |
| 133 | |
| 134 | // Hold information about the current feature map we are iterating. |
| 135 | // Only compute denominator and NEON vectors once per feature map. |
| 136 | int slice = -1; |
| 137 | |
| 138 | const auto input_mean = reinterpret_cast<const float *>(mean->ptr_to_element(Coordinates(0, 0))); |
| 139 | const auto input_var = reinterpret_cast<const float *>(var->ptr_to_element(Coordinates(0, 0))); |
| 140 | const auto input_gamma = reinterpret_cast<const float *>(gamma->ptr_to_element(Coordinates(0, 0))); |
| 141 | const auto input_beta = reinterpret_cast<const float *>(beta->ptr_to_element(Coordinates(0, 0))); |
| 142 | |
| 143 | float32x4_t mean_vec = vdupq_n_f32(0.0); |
| 144 | float32x4_t var_vec = vdupq_n_f32(0.0); |
| 145 | float32x4_t gamma_vec = vdupq_n_f32(0.0); |
| 146 | float32x4_t beta_vec = vdupq_n_f32(0.0); |
| 147 | float32x4_t denominator = vdupq_n_f32(0.0); |
| 148 | const float32x4_t epsilon_vec = vdupq_n_f32(epsilon); |
| 149 | execute_window_loop(window, [&](const Coordinates & id) |
| 150 | { |
| 151 | if(slice != id.z()) |
| 152 | { |
| 153 | // Conctruct vectors |
| 154 | mean_vec = vdupq_n_f32(*(input_mean + id.z())); |
| 155 | var_vec = vdupq_n_f32(*(input_var + id.z())); |
| 156 | gamma_vec = vdupq_n_f32(*(input_gamma + id.z())); |
| 157 | beta_vec = vdupq_n_f32(*(input_beta + id.z())); |
| 158 | |
| 159 | // Calculate denominator |
| 160 | denominator = vinvsqrtq_f32(vaddq_f32(var_vec, epsilon_vec)); |
| 161 | slice = id.z(); |
| 162 | } |
| 163 | |
| 164 | // Calculate x bar and store results |
| 165 | const float32x4_t numerator = vsubq_f32(vld1q_f32(reinterpret_cast<const float *>(input.ptr())), mean_vec); |
| 166 | const float32x4_t x_bar = vmulq_f32(numerator, denominator); |
| 167 | vst1q_f32(reinterpret_cast<float *>(output.ptr()), vmlaq_f32(beta_vec, x_bar, gamma_vec)); |
| 168 | }, |
| 169 | input, output); |
| 170 | } |
| 171 | |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 172 | #ifdef ARM_COMPUTE_ENABLE_FP16 |
Georgios Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 173 | void batch_normalization_fp16(ITensor *in, ITensor *out, const ITensor *mean, const ITensor *var, const ITensor *beta, const ITensor *gamma, float epsilon, const Window &window) |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 174 | { |
| 175 | Iterator input(in, window); |
| 176 | Iterator output(out, window); |
| 177 | |
| 178 | // Hold information about the current feature map we are iterating. |
| 179 | // Only compute denominator and NEON vectors once per feature map. |
| 180 | int slice = -1; |
| 181 | |
| 182 | const auto input_mean = reinterpret_cast<const float16_t *>(mean->ptr_to_element(Coordinates(0, 0))); |
| 183 | const auto input_var = reinterpret_cast<const float16_t *>(var->ptr_to_element(Coordinates(0, 0))); |
| 184 | const auto input_gamma = reinterpret_cast<const float16_t *>(gamma->ptr_to_element(Coordinates(0, 0))); |
| 185 | const auto input_beta = reinterpret_cast<const float16_t *>(beta->ptr_to_element(Coordinates(0, 0))); |
| 186 | |
| 187 | float16x8_t mean_vec = vdupq_n_f16(0.0); |
| 188 | float16x8_t var_vec = vdupq_n_f16(0.0); |
| 189 | float16x8_t gamma_vec = vdupq_n_f16(0.0); |
| 190 | float16x8_t beta_vec = vdupq_n_f16(0.0); |
| 191 | float16x8_t denominator = vdupq_n_f16(0.0); |
| 192 | const float16x8_t epsilon_vec = vdupq_n_f16(epsilon); |
| 193 | execute_window_loop(window, [&](const Coordinates & id) |
| 194 | { |
| 195 | if(slice != id.z()) |
| 196 | { |
| 197 | // Conctruct vectors |
| 198 | mean_vec = vdupq_n_f16(*(input_mean + id.z())); |
| 199 | var_vec = vdupq_n_f16(*(input_var + id.z())); |
| 200 | gamma_vec = vdupq_n_f16(*(input_gamma + id.z())); |
| 201 | beta_vec = vdupq_n_f16(*(input_beta + id.z())); |
| 202 | |
| 203 | // Calculate denominator |
| 204 | denominator = vinvsqrtq_f16(vaddq_f16(var_vec, epsilon_vec)); |
| 205 | slice = id.z(); |
| 206 | } |
| 207 | |
| 208 | // Calculate x bar and store results |
| 209 | const float16x8_t numerator = vsubq_f16(vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr())), mean_vec); |
| 210 | const float16x8_t x_bar = vmulq_f16(numerator, denominator); |
| 211 | vst1q_f16(reinterpret_cast<float16_t *>(output.ptr()), vaddq_f16(beta_vec, vmulq_f16(x_bar, gamma_vec))); |
| 212 | }, |
| 213 | input, output); |
| 214 | } |
| 215 | #endif /* ARM_COMPUTE_ENABLE_FP16 */ |
| 216 | |
Georgios Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 217 | void NEBatchNormalizationLayerKernel::configure(ITensor *input, ITensor *output, const ITensor *mean, const ITensor *var, const ITensor *beta, const ITensor *gamma, float epsilon) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 218 | { |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 219 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 220 | |
| 221 | _input = input; |
Georgios Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 222 | _output = input; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 223 | _mean = mean; |
| 224 | _var = var; |
| 225 | _gamma = gamma; |
| 226 | _beta = beta; |
| 227 | _epsilon = epsilon; |
| 228 | |
Georgios Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 229 | if(output != nullptr) |
| 230 | { |
| 231 | // Output tensor auto initialization if not yet initialized |
| 232 | auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position()); |
| 233 | |
| 234 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 235 | |
| 236 | _output = output; |
| 237 | } |
| 238 | |
| 239 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, mean, var, beta, gamma); |
| 240 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output, mean, var, beta, gamma); |
| 241 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(mean, var, beta, gamma); |
| 242 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) != mean->info()->dimension(0)); |
| 243 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 244 | unsigned int num_elems_processed_per_iteration = 0; |
| 245 | |
| 246 | switch(input->info()->data_type()) |
| 247 | { |
| 248 | case DataType::QS8: |
| 249 | _func = &batch_normalization_q8; |
| 250 | num_elems_processed_per_iteration = 16; |
| 251 | break; |
Michalis Spyrou | bbd3d60 | 2017-06-21 17:29:40 +0100 | [diff] [blame] | 252 | case DataType::QS16: |
| 253 | _func = &batch_normalization_q16; |
| 254 | num_elems_processed_per_iteration = 8; |
| 255 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 256 | case DataType::F32: |
| 257 | _func = &batch_normalization_fp32; |
| 258 | num_elems_processed_per_iteration = 4; |
| 259 | break; |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 260 | case DataType::F16: |
| 261 | #ifdef ARM_COMPUTE_ENABLE_FP16 |
| 262 | _func = &batch_normalization_fp16; |
| 263 | num_elems_processed_per_iteration = 8; |
| 264 | break; |
| 265 | #endif /* ARM_COMPUTE_ENABLE_FP16 */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 266 | default: |
| 267 | ARM_COMPUTE_ERROR("Element size not supported"); |
| 268 | break; |
| 269 | } |
| 270 | |
Georgios Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 271 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 272 | AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); |
Georgios Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 273 | if(output != nullptr) |
| 274 | { |
| 275 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
| 276 | update_window_and_padding(win, input_access, output_access); |
| 277 | output_access.set_valid_region(win, input->info()->valid_region()); |
| 278 | } |
| 279 | else |
| 280 | { |
| 281 | update_window_and_padding(win, input_access); |
| 282 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 283 | INEKernel::configure(win); |
| 284 | } |
| 285 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 286 | void NEBatchNormalizationLayerKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 287 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 288 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 289 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 290 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 291 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 292 | |
| 293 | (*_func)(_input, _output, _mean, _var, _beta, _gamma, _epsilon, window); |
| 294 | } |