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 | |
| 41 | void batch_normalization_q8(const ITensor *in, ITensor *out, const ITensor *mean, const ITensor *var, const ITensor *beta, const ITensor *gamma, float epsilon, const Window &window) |
| 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 | |
| 50 | int fixed_point_position = in->info()->fixed_point_position(); |
| 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 | |
| 85 | void batch_normalization_fp32(const ITensor *in, ITensor *out, const ITensor *mean, const ITensor *var, const ITensor *beta, const ITensor *gamma, float epsilon, const Window &window) |
| 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 auto input_mean = reinterpret_cast<const float *>(mean->ptr_to_element(Coordinates(0, 0))); |
| 95 | const auto input_var = reinterpret_cast<const float *>(var->ptr_to_element(Coordinates(0, 0))); |
| 96 | const auto input_gamma = reinterpret_cast<const float *>(gamma->ptr_to_element(Coordinates(0, 0))); |
| 97 | const auto input_beta = reinterpret_cast<const float *>(beta->ptr_to_element(Coordinates(0, 0))); |
| 98 | |
| 99 | float32x4_t mean_vec = vdupq_n_f32(0.0); |
| 100 | float32x4_t var_vec = vdupq_n_f32(0.0); |
| 101 | float32x4_t gamma_vec = vdupq_n_f32(0.0); |
| 102 | float32x4_t beta_vec = vdupq_n_f32(0.0); |
| 103 | float32x4_t denominator = vdupq_n_f32(0.0); |
| 104 | const float32x4_t epsilon_vec = vdupq_n_f32(epsilon); |
| 105 | execute_window_loop(window, [&](const Coordinates & id) |
| 106 | { |
| 107 | if(slice != id.z()) |
| 108 | { |
| 109 | // Conctruct vectors |
| 110 | mean_vec = vdupq_n_f32(*(input_mean + id.z())); |
| 111 | var_vec = vdupq_n_f32(*(input_var + id.z())); |
| 112 | gamma_vec = vdupq_n_f32(*(input_gamma + id.z())); |
| 113 | beta_vec = vdupq_n_f32(*(input_beta + id.z())); |
| 114 | |
| 115 | // Calculate denominator |
| 116 | denominator = vinvsqrtq_f32(vaddq_f32(var_vec, epsilon_vec)); |
| 117 | slice = id.z(); |
| 118 | } |
| 119 | |
| 120 | // Calculate x bar and store results |
| 121 | const float32x4_t numerator = vsubq_f32(vld1q_f32(reinterpret_cast<const float *>(input.ptr())), mean_vec); |
| 122 | const float32x4_t x_bar = vmulq_f32(numerator, denominator); |
| 123 | vst1q_f32(reinterpret_cast<float *>(output.ptr()), vmlaq_f32(beta_vec, x_bar, gamma_vec)); |
| 124 | }, |
| 125 | input, output); |
| 126 | } |
| 127 | |
| 128 | void NEBatchNormalizationLayerKernel::configure(const ITensor *input, ITensor *output, const ITensor *mean, const ITensor *var, const ITensor *beta, const ITensor *gamma, float epsilon) |
| 129 | { |
| 130 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::F32); |
Georgios Pinitas | b76346d | 2017-07-03 17:10:39 +0100 | [diff] [blame] | 131 | ARM_COMPUTE_ERROR_ON_NULLPTR(output); |
| 132 | |
| 133 | // Output tensor auto initialization if not yet initialized |
| 134 | auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position()); |
| 135 | |
| 136 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, mean, var, beta, gamma); |
| 137 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output, mean, var, beta, gamma); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 138 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); |
Georgios Pinitas | b76346d | 2017-07-03 17:10:39 +0100 | [diff] [blame] | 139 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(mean, var, beta, gamma); |
| 140 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) != mean->info()->dimension(0)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 141 | |
| 142 | _input = input; |
| 143 | _output = output; |
| 144 | _mean = mean; |
| 145 | _var = var; |
| 146 | _gamma = gamma; |
| 147 | _beta = beta; |
| 148 | _epsilon = epsilon; |
| 149 | |
| 150 | unsigned int num_elems_processed_per_iteration = 0; |
| 151 | |
| 152 | switch(input->info()->data_type()) |
| 153 | { |
| 154 | case DataType::QS8: |
| 155 | _func = &batch_normalization_q8; |
| 156 | num_elems_processed_per_iteration = 16; |
| 157 | break; |
| 158 | case DataType::F32: |
| 159 | _func = &batch_normalization_fp32; |
| 160 | num_elems_processed_per_iteration = 4; |
| 161 | break; |
| 162 | default: |
| 163 | ARM_COMPUTE_ERROR("Element size not supported"); |
| 164 | break; |
| 165 | } |
| 166 | |
| 167 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 168 | |
| 169 | AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); |
| 170 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
| 171 | |
| 172 | update_window_and_padding(win, input_access, output_access); |
| 173 | |
| 174 | output_access.set_valid_region(win, input->info()->valid_region()); |
| 175 | |
| 176 | INEKernel::configure(win); |
| 177 | } |
| 178 | |
| 179 | void NEBatchNormalizationLayerKernel::run(const Window &window) |
| 180 | { |
| 181 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 182 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 183 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 184 | |
| 185 | (*_func)(_input, _output, _mean, _var, _beta, _gamma, _epsilon, window); |
| 186 | } |