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/NENormalizationLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/NEON/NEFixedPoint.h" |
| 29 | #include "arm_compute/core/NEON/NEMath.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/Utils.h" |
| 32 | #include "arm_compute/core/Validate.h" |
| 33 | #include "arm_compute/core/Window.h" |
| 34 | |
| 35 | using namespace arm_compute; |
| 36 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 37 | namespace |
| 38 | { |
| 39 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *input_squared, const ITensorInfo *output, const NormalizationLayerInfo &norm_info) |
| 40 | { |
| 41 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, input_squared, output); |
| 42 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
| 43 | |
| 44 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, input_squared); |
| 45 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, input_squared); |
| 46 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!(norm_info.norm_size() % 2), "Normalization size should be odd"); |
| 47 | |
| 48 | if(is_data_type_fixed_point(input->data_type())) |
| 49 | { |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, input_squared); |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON_VALUE_NOT_REPRESENTABLE_IN_FIXED_POINT(norm_info.beta(), input); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON_VALUE_NOT_REPRESENTABLE_IN_FIXED_POINT(norm_info.kappa(), input); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_VALUE_NOT_REPRESENTABLE_IN_FIXED_POINT(norm_info.scale_coeff(), input); |
| 54 | } |
| 55 | |
| 56 | // Checks performed when output is configured |
| 57 | if(output->total_size() != 0) |
| 58 | { |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); |
| 62 | } |
| 63 | |
| 64 | return Status{}; |
| 65 | } |
| 66 | |
| 67 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *input_squared, ITensorInfo *output, const NormalizationLayerInfo &norm_info) |
| 68 | { |
| 69 | unsigned int num_elems_processed_per_iteration = 16 / input->element_size(); |
| 70 | const unsigned int num_elems_read_per_iteration = num_elems_processed_per_iteration + 2 * (norm_info.norm_size() / 2); |
| 71 | const unsigned int num_rows = (norm_info.type() == NormType::IN_MAP_2D) ? norm_info.norm_size() : 1; |
| 72 | const unsigned int border_width = (norm_info.is_cross_map()) ? 0 : std::min<unsigned int>(norm_info.norm_size() / 2, 3U); |
| 73 | BorderSize border_size = BorderSize(0, border_width); |
| 74 | bool window_changed = false; |
| 75 | |
| 76 | // Configure window |
| 77 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 78 | |
| 79 | AccessWindowRectangle input_access(input, -border_size.left, 0, num_elems_read_per_iteration, num_rows); |
| 80 | AccessWindowRectangle input_squared_access(input_squared, -border_size.left, 0, num_elems_read_per_iteration, num_rows); |
| 81 | |
| 82 | if(output->total_size() != 0) |
| 83 | { |
| 84 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 85 | window_changed = update_window_and_padding(win, input_access, input_squared_access, output_access); |
| 86 | output_access.set_valid_region(win, input->valid_region()); |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | window_changed = update_window_and_padding(win, input_access, input_squared_access); |
| 91 | } |
| 92 | |
| 93 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 94 | return std::make_pair(err, win); |
| 95 | } |
| 96 | } // namespace |
| 97 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 98 | NENormalizationLayerKernel::NENormalizationLayerKernel() |
| 99 | : _func(nullptr), _input(nullptr), _input_squared(nullptr), _output(nullptr), _norm_info(NormType::IN_MAP_1D), _border_size() |
| 100 | { |
| 101 | } |
| 102 | |
| 103 | BorderSize NENormalizationLayerKernel::border_size() const |
| 104 | { |
| 105 | return _border_size; |
| 106 | } |
| 107 | |
| 108 | void NENormalizationLayerKernel::configure(const ITensor *input, const ITensor *input_squared, ITensor *output, NormalizationLayerInfo norm_info) |
| 109 | { |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 110 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, input_squared, output); |
Georgios Pinitas | 09004ca | 2017-07-03 17:30:14 +0100 | [diff] [blame] | 111 | // Output tensor auto initialization if not yet initialized |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 112 | auto_init_if_empty(*output->info(), *input->info()); |
| 113 | |
| 114 | // Perform validation step |
| 115 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), input_squared->info(), output->info(), norm_info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 116 | |
Georgios Pinitas | 41caa62 | 2017-11-16 14:37:08 +0000 | [diff] [blame] | 117 | const unsigned int border_width = (norm_info.is_cross_map()) ? 0 : std::min<unsigned int>(norm_info.norm_size() / 2, 3U); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 118 | |
| 119 | _input = input; |
| 120 | _input_squared = input_squared; |
| 121 | _output = output; |
| 122 | _norm_info = norm_info; |
| 123 | _border_size = BorderSize(0, border_width); |
| 124 | |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 125 | switch(_input->info()->data_type()) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 126 | { |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 127 | case DataType::F32: |
| 128 | { |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 129 | switch(norm_info.type()) |
| 130 | { |
| 131 | case NormType::IN_MAP_1D: |
| 132 | _func = &NENormalizationLayerKernel::normalize_float<DataType::F32, 0, false>; |
| 133 | break; |
| 134 | case NormType::IN_MAP_2D: |
| 135 | // Normalize over X and Y |
| 136 | _func = &NENormalizationLayerKernel::normalize_float<DataType::F32, 0, true>; |
| 137 | break; |
| 138 | case NormType::CROSS_MAP: |
| 139 | _func = &NENormalizationLayerKernel::normalize_float<DataType::F32, 2, false>; |
| 140 | break; |
| 141 | default: |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 142 | break; |
| 143 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 144 | break; |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 145 | } |
| 146 | case DataType::F16: |
| 147 | { |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 148 | switch(norm_info.type()) |
| 149 | { |
| 150 | case NormType::IN_MAP_1D: |
| 151 | _func = &NENormalizationLayerKernel::normalize_float<DataType::F16, 0, false>; |
| 152 | break; |
| 153 | case NormType::IN_MAP_2D: |
| 154 | // Normalize over X and Y |
| 155 | _func = &NENormalizationLayerKernel::normalize_float<DataType::F16, 0, true>; |
| 156 | break; |
| 157 | case NormType::CROSS_MAP: |
| 158 | _func = &NENormalizationLayerKernel::normalize_float<DataType::F16, 2, false>; |
| 159 | break; |
| 160 | default: |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 161 | break; |
| 162 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 163 | break; |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 164 | } |
| 165 | case DataType::QS8: |
| 166 | { |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 167 | switch(norm_info.type()) |
| 168 | { |
| 169 | case NormType::IN_MAP_1D: |
Michele Di Giorgio | d5e65c7 | 2017-07-26 17:09:17 +0100 | [diff] [blame] | 170 | _func = &NENormalizationLayerKernel::normalize_fixed_point<DataType::QS8, 0, false>; |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 171 | break; |
| 172 | case NormType::IN_MAP_2D: |
| 173 | // Normalize over X and Y |
Michele Di Giorgio | d5e65c7 | 2017-07-26 17:09:17 +0100 | [diff] [blame] | 174 | _func = &NENormalizationLayerKernel::normalize_fixed_point<DataType::QS8, 0, true>; |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 175 | break; |
| 176 | case NormType::CROSS_MAP: |
Michele Di Giorgio | d5e65c7 | 2017-07-26 17:09:17 +0100 | [diff] [blame] | 177 | _func = &NENormalizationLayerKernel::normalize_fixed_point<DataType::QS8, 2, false>; |
| 178 | break; |
| 179 | default: |
Michele Di Giorgio | d5e65c7 | 2017-07-26 17:09:17 +0100 | [diff] [blame] | 180 | break; |
| 181 | } |
| 182 | break; |
| 183 | } |
| 184 | case DataType::QS16: |
| 185 | { |
Michele Di Giorgio | d5e65c7 | 2017-07-26 17:09:17 +0100 | [diff] [blame] | 186 | switch(norm_info.type()) |
| 187 | { |
| 188 | case NormType::IN_MAP_1D: |
| 189 | _func = &NENormalizationLayerKernel::normalize_fixed_point<DataType::QS16, 0, false>; |
| 190 | break; |
| 191 | case NormType::IN_MAP_2D: |
| 192 | // Normalize over X and Y |
| 193 | _func = &NENormalizationLayerKernel::normalize_fixed_point<DataType::QS16, 0, true>; |
| 194 | break; |
| 195 | case NormType::CROSS_MAP: |
| 196 | _func = &NENormalizationLayerKernel::normalize_fixed_point<DataType::QS16, 2, false>; |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 197 | break; |
| 198 | default: |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 199 | break; |
| 200 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 201 | break; |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 202 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 203 | default: |
| 204 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 205 | } |
| 206 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 207 | // Configure kernel window |
| 208 | auto win_config = validate_and_configure_window(input->info(), input_squared->info(), output->info(), norm_info); |
| 209 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 210 | INEKernel::configure(win_config.second); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 213 | template <DataType dt, unsigned int dim, bool do_2D_norm> |
| 214 | void NENormalizationLayerKernel::normalize_float(const Window &window) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 215 | { |
| 216 | Iterator input(_input, window); |
| 217 | Iterator input_squared(_input_squared, window); |
| 218 | Iterator output(_output, window); |
| 219 | |
| 220 | const int dim_y = 1; |
| 221 | const int radius = _norm_info.norm_size() / 2; |
| 222 | const int total_size = _input->info()->dimension(dim) - 1; |
| 223 | const int input_squared_stride = _input_squared->info()->strides_in_bytes()[dim]; |
| 224 | // We account padding across X only and we iterate over rows |
| 225 | const int min_left = (dim == 2) ? 0 : -static_cast<int>(border_size().left); |
| 226 | const int max_right = (dim == 2) ? total_size : total_size + border_size().left; |
| 227 | const int min_top = 0; |
| 228 | const int max_bottom = _input->info()->dimension(dim_y) - 1; |
| 229 | |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 230 | if(dt == DataType::F32) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 231 | { |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 232 | const float32x4_t coeff_vec = vdupq_n_f32(_norm_info.scale_coeff()); |
| 233 | const float32x4_t beta_vec = vdupq_n_f32(_norm_info.beta()); |
| 234 | const float32x4_t kappa_vec = vdupq_n_f32(_norm_info.kappa()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 235 | |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 236 | execute_window_loop(window, [&](const Coordinates & id) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 237 | { |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 238 | // Get range to normalize |
| 239 | const int current_row = do_2D_norm ? id[dim_y] : 0; |
| 240 | const int current_slice = id[dim]; |
| 241 | const int first_row = do_2D_norm ? std::max(current_row - radius, min_top) : 0; |
| 242 | const int last_row = do_2D_norm ? std::min(current_row + radius, max_bottom) : 0; |
| 243 | const int first_slice = std::max(current_slice - radius, min_left); |
| 244 | const int last_slice = std::min(current_slice + radius, max_right); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 245 | |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 246 | // Accumulate 2D In-Map values |
| 247 | float32x4_t accu = vdupq_n_f32(0.f); |
| 248 | for(int j = first_row; j <= last_row; j++) |
| 249 | { |
| 250 | // Compute row displacement |
| 251 | const int row = (j - current_row) * _input_squared->info()->strides_in_bytes()[dim_y]; |
| 252 | const uint8_t *const input_squared_ptr = input_squared.ptr() + row - (current_slice * input_squared_stride); |
| 253 | for(int i = first_slice; i <= last_slice; ++i) |
| 254 | { |
| 255 | accu = vaddq_f32(accu, vld1q_f32(reinterpret_cast<const float *>(input_squared_ptr + i * input_squared_stride))); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // Normalize |
| 260 | const float32x4_t normalized = vpowq_f32(vmlaq_f32(kappa_vec, coeff_vec, accu), beta_vec); |
| 261 | const float32x4_t normalized_pixel = vmulq_f32(vld1q_f32(reinterpret_cast<const float *>(input.ptr())), vinvq_f32(normalized)); |
| 262 | vst1q_f32(reinterpret_cast<float *>(output.ptr()), normalized_pixel); |
| 263 | }, |
| 264 | input, input_squared, output); |
| 265 | } |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 266 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 267 | else if(dt == DataType::F16) |
| 268 | { |
| 269 | const float16x8_t coeff_vec = vdupq_n_f16(_norm_info.scale_coeff()); |
| 270 | const float16x8_t beta_vec_f16 = vdupq_n_f16(_norm_info.beta()); |
| 271 | const float16x8_t kappa_vec = vdupq_n_f16(_norm_info.kappa()); |
| 272 | |
| 273 | execute_window_loop(window, [&](const Coordinates & id) |
| 274 | { |
| 275 | // Get range to normalize |
| 276 | const int current_row = do_2D_norm ? id[dim_y] : 0; |
| 277 | const int current_slice = id[dim]; |
| 278 | const int first_row = do_2D_norm ? std::max(current_row - radius, min_top) : 0; |
| 279 | const int last_row = do_2D_norm ? std::min(current_row + radius, max_bottom) : 0; |
| 280 | const int first_slice = std::max(current_slice - radius, min_left); |
| 281 | const int last_slice = std::min(current_slice + radius, max_right); |
| 282 | |
| 283 | // Accumulate 2D In-Map values |
| 284 | float16x8_t accu = vdupq_n_f16(0.f); |
| 285 | for(int j = first_row; j <= last_row; j++) |
| 286 | { |
| 287 | // Compute row displacement |
| 288 | const int row = (j - current_row) * _input_squared->info()->strides_in_bytes()[dim_y]; |
| 289 | const uint8_t *const input_squared_ptr = input_squared.ptr() + row - (current_slice * input_squared_stride); |
| 290 | for(int i = first_slice; i <= last_slice; ++i) |
| 291 | { |
| 292 | accu = vaddq_f16(accu, vld1q_f16(reinterpret_cast<const float16_t *>(input_squared_ptr + i * input_squared_stride))); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | const float16x8_t norm_f16 = vpowq_f16(vaddq_f16(kappa_vec, vmulq_f16(coeff_vec, accu)), beta_vec_f16); |
| 297 | const float16x8_t normalized_pixel = vmulq_f16(vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr())), vinvq_f16(norm_f16)); |
| 298 | vst1q_f16(reinterpret_cast<float16_t *>(output.ptr()), normalized_pixel); |
| 299 | }, |
| 300 | input, input_squared, output); |
| 301 | } |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 302 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 303 | else |
| 304 | { |
| 305 | ARM_COMPUTE_ERROR("Not supported"); |
| 306 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 307 | } |
| 308 | |
Michele Di Giorgio | d5e65c7 | 2017-07-26 17:09:17 +0100 | [diff] [blame] | 309 | template <DataType dt, unsigned int dim, bool do_2D_norm> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 310 | void NENormalizationLayerKernel::normalize_fixed_point(const Window &window) |
| 311 | { |
| 312 | Iterator input(_input, window); |
| 313 | Iterator input_squared(_input_squared, window); |
| 314 | Iterator output(_output, window); |
| 315 | |
| 316 | const int dim_y = 1; |
| 317 | const int radius = _norm_info.norm_size() / 2; |
| 318 | const int total_size = _input->info()->dimension(dim) - 1; |
| 319 | const int input_squared_stride = _input_squared->info()->strides_in_bytes()[dim]; |
| 320 | // We account padding across X only and we iterate over rows |
| 321 | const int min_left = (dim == 2) ? 0 : -static_cast<int>(border_size().left); |
| 322 | const int max_right = (dim == 2) ? total_size : total_size + border_size().left; |
| 323 | const int min_top = 0; |
| 324 | const int max_bottom = _input->info()->dimension(dim_y) - 1; |
| 325 | |
| 326 | const int fixed_point_position = _input->info()->fixed_point_position(); |
| 327 | |
Michele Di Giorgio | d5e65c7 | 2017-07-26 17:09:17 +0100 | [diff] [blame] | 328 | if(dt == DataType::QS8) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 329 | { |
Michele Di Giorgio | d5e65c7 | 2017-07-26 17:09:17 +0100 | [diff] [blame] | 330 | const qint8x16_t coeff_vec = vdupq_n_qs8_f32(_norm_info.scale_coeff(), fixed_point_position); |
| 331 | const qint8x16_t beta_vec = vdupq_n_qs8_f32(_norm_info.beta(), fixed_point_position); |
| 332 | const qint8x16_t kappa_vec = vdupq_n_qs8_f32(_norm_info.kappa(), fixed_point_position); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 333 | |
Michele Di Giorgio | d5e65c7 | 2017-07-26 17:09:17 +0100 | [diff] [blame] | 334 | execute_window_loop(window, [&](const Coordinates & id) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 335 | { |
Michele Di Giorgio | d5e65c7 | 2017-07-26 17:09:17 +0100 | [diff] [blame] | 336 | // Get range to normalize |
| 337 | const int current_row = do_2D_norm ? id[dim_y] : 0; |
| 338 | const int current_slice = id[dim]; |
| 339 | const int first_row = do_2D_norm ? std::max(current_row - radius, min_top) : 0; |
| 340 | const int last_row = do_2D_norm ? std::min(current_row + radius, max_bottom) : 0; |
| 341 | const int first_slice = std::max(current_slice - radius, min_left); |
| 342 | const int last_slice = std::min(current_slice + radius, max_right); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 343 | |
Michele Di Giorgio | d5e65c7 | 2017-07-26 17:09:17 +0100 | [diff] [blame] | 344 | // Accumulate 2D In-Map values |
| 345 | qint8x16_t accu = vdupq_n_qs8(0); |
| 346 | for(int j = first_row; j <= last_row; ++j) |
| 347 | { |
| 348 | // Compute row displacement |
| 349 | const int row = (j - current_row) * _input_squared->info()->strides_in_bytes()[dim_y]; |
| 350 | const uint8_t *const input_squared_ptr = input_squared.ptr() + row - (current_slice * input_squared_stride); |
| 351 | for(int i = first_slice; i <= last_slice; ++i) |
| 352 | { |
| 353 | accu = vqaddq_qs8(accu, vld1q_qs8(reinterpret_cast<const qint8_t *>(input_squared_ptr + i * input_squared_stride))); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | // Normalize |
| 358 | const qint8x16_t accu_scale = vqmlaq_qs8(kappa_vec, coeff_vec, accu, fixed_point_position); |
| 359 | const qint8x16_t normalized = vqpowq_qs8(accu_scale, beta_vec, fixed_point_position); |
| 360 | const qint8x16_t normalized_pixel = vdivq_qs8(vld1q_qs8(reinterpret_cast<const qint8_t *>(input.ptr())), normalized, fixed_point_position); |
| 361 | vst1q_qs8(reinterpret_cast<qint8_t *>(output.ptr()), normalized_pixel); |
| 362 | }, |
| 363 | input, input_squared, output); |
| 364 | } |
| 365 | else if(dt == DataType::QS16) |
| 366 | { |
| 367 | const qint16x8_t coeff_vec = vdupq_n_qs16_f32(_norm_info.scale_coeff(), fixed_point_position); |
| 368 | const qint16x8_t beta_vec = vdupq_n_qs16_f32(_norm_info.beta(), fixed_point_position); |
| 369 | const qint16x8_t kappa_vec = vdupq_n_qs16_f32(_norm_info.kappa(), fixed_point_position); |
| 370 | |
| 371 | execute_window_loop(window, [&](const Coordinates & id) |
| 372 | { |
| 373 | // Get range to normalize |
| 374 | const int current_row = do_2D_norm ? id[dim_y] : 0; |
| 375 | const int current_slice = id[dim]; |
| 376 | const int first_row = do_2D_norm ? std::max(current_row - radius, min_top) : 0; |
| 377 | const int last_row = do_2D_norm ? std::min(current_row + radius, max_bottom) : 0; |
| 378 | const int first_slice = std::max(current_slice - radius, min_left); |
| 379 | const int last_slice = std::min(current_slice + radius, max_right); |
| 380 | |
| 381 | // Accumulate 2D In-Map values |
| 382 | qint16x8_t accu = vdupq_n_qs16(0); |
| 383 | for(int j = first_row; j <= last_row; ++j) |
| 384 | { |
| 385 | // Compute row displacement |
| 386 | const int row = (j - current_row) * _input_squared->info()->strides_in_bytes()[dim_y]; |
| 387 | const uint8_t *const input_squared_ptr = input_squared.ptr() + row - (current_slice * input_squared_stride); |
| 388 | for(int i = first_slice; i <= last_slice; ++i) |
| 389 | { |
| 390 | accu = vqaddq_qs16(accu, vld1q_qs16(reinterpret_cast<const qint16_t *>(input_squared_ptr + i * input_squared_stride))); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | // Normalize |
| 395 | const qint16x8_t accu_scale = vqmlaq_qs16(kappa_vec, coeff_vec, accu, fixed_point_position); |
| 396 | const qint16x8_t normalized = vqpowq_qs16(accu_scale, beta_vec, fixed_point_position); |
| 397 | const qint16x8_t normalized_pixel = vdivq_qs16(vld1q_qs16(reinterpret_cast<const qint16_t *>(input.ptr())), normalized, fixed_point_position); |
| 398 | vst1q_qs16(reinterpret_cast<qint16_t *>(output.ptr()), normalized_pixel); |
| 399 | }, |
| 400 | input, input_squared, output); |
| 401 | } |
| 402 | else |
| 403 | { |
| 404 | ARM_COMPUTE_ERROR("Not supported"); |
| 405 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 406 | } |
| 407 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 408 | Status NENormalizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *input_squared, const ITensorInfo *output, const NormalizationLayerInfo norm_info) |
| 409 | { |
| 410 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, input_squared, output, norm_info)); |
| 411 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), input_squared->clone().get(), output->clone().get(), norm_info).first); |
| 412 | |
| 413 | return Status{}; |
| 414 | } |
| 415 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 416 | void NENormalizationLayerKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 417 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 418 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 419 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 420 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 421 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 422 | |
| 423 | // Run function |
| 424 | (this->*_func)(window); |
| 425 | } |