blob: b444c9d4df14e6add839483dfd4676b8dfbb86ba [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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
35using namespace arm_compute;
36
37NENormalizationLayerKernel::NENormalizationLayerKernel()
38 : _func(nullptr), _input(nullptr), _input_squared(nullptr), _output(nullptr), _norm_info(NormType::IN_MAP_1D), _border_size()
39{
40}
41
42BorderSize NENormalizationLayerKernel::border_size() const
43{
44 return _border_size;
45}
46
47void NENormalizationLayerKernel::configure(const ITensor *input, const ITensor *input_squared, ITensor *output, NormalizationLayerInfo norm_info)
48{
Michele Di Giorgiod5e65c72017-07-26 17:09:17 +010049 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
Georgios Pinitas09004ca2017-07-03 17:30:14 +010050 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
Georgios Pinitas09004ca2017-07-03 17:30:14 +010051 // Output tensor auto initialization if not yet initialized
52 auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, input_squared, output);
Georgios Pinitas09004ca2017-07-03 17:30:14 +010054 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, input_squared, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010055 ARM_COMPUTE_ERROR_ON_MSG(!(norm_info.norm_size() % 2), "Normalization size should be odd");
Michele Di Giorgio6c928342017-06-22 16:55:57 +010056 if(is_data_type_fixed_point(input->info()->data_type()))
57 {
58 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, input_squared, output);
59 ARM_COMPUTE_ERROR_ON_VALUE_NOT_REPRESENTABLE_IN_FIXED_POINT(norm_info.beta(), input);
60 ARM_COMPUTE_ERROR_ON_VALUE_NOT_REPRESENTABLE_IN_FIXED_POINT(norm_info.kappa(), input);
61 ARM_COMPUTE_ERROR_ON_VALUE_NOT_REPRESENTABLE_IN_FIXED_POINT(norm_info.scale_coeff(), input);
62 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063
64 const unsigned int border_width = (norm_info.type() == NormType::CROSS_MAP) ? 0 : std::min(norm_info.norm_size() / 2, 3U);
65
66 _input = input;
67 _input_squared = input_squared;
68 _output = output;
69 _norm_info = norm_info;
70 _border_size = BorderSize(0, border_width);
71
Pablo Tellodf246182017-07-03 16:25:09 +010072 unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073
Pablo Tellodf246182017-07-03 16:25:09 +010074 switch(_input->info()->data_type())
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075 {
Pablo Tellodf246182017-07-03 16:25:09 +010076 case DataType::F32:
77 {
78 num_elems_processed_per_iteration = 4;
79 switch(norm_info.type())
80 {
81 case NormType::IN_MAP_1D:
82 _func = &NENormalizationLayerKernel::normalize_float<DataType::F32, 0, false>;
83 break;
84 case NormType::IN_MAP_2D:
85 // Normalize over X and Y
86 _func = &NENormalizationLayerKernel::normalize_float<DataType::F32, 0, true>;
87 break;
88 case NormType::CROSS_MAP:
89 _func = &NENormalizationLayerKernel::normalize_float<DataType::F32, 2, false>;
90 break;
91 default:
92 ARM_COMPUTE_ERROR("Not supported");
93 break;
94 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095 break;
Pablo Tellodf246182017-07-03 16:25:09 +010096 }
97 case DataType::F16:
98 {
99 num_elems_processed_per_iteration = 8;
100 switch(norm_info.type())
101 {
102 case NormType::IN_MAP_1D:
103 _func = &NENormalizationLayerKernel::normalize_float<DataType::F16, 0, false>;
104 break;
105 case NormType::IN_MAP_2D:
106 // Normalize over X and Y
107 _func = &NENormalizationLayerKernel::normalize_float<DataType::F16, 0, true>;
108 break;
109 case NormType::CROSS_MAP:
110 _func = &NENormalizationLayerKernel::normalize_float<DataType::F16, 2, false>;
111 break;
112 default:
113 ARM_COMPUTE_ERROR("Not supported");
114 break;
115 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100116 break;
Pablo Tellodf246182017-07-03 16:25:09 +0100117 }
118 case DataType::QS8:
119 {
120 num_elems_processed_per_iteration = 16;
121 switch(norm_info.type())
122 {
123 case NormType::IN_MAP_1D:
Michele Di Giorgiod5e65c72017-07-26 17:09:17 +0100124 _func = &NENormalizationLayerKernel::normalize_fixed_point<DataType::QS8, 0, false>;
Pablo Tellodf246182017-07-03 16:25:09 +0100125 break;
126 case NormType::IN_MAP_2D:
127 // Normalize over X and Y
Michele Di Giorgiod5e65c72017-07-26 17:09:17 +0100128 _func = &NENormalizationLayerKernel::normalize_fixed_point<DataType::QS8, 0, true>;
Pablo Tellodf246182017-07-03 16:25:09 +0100129 break;
130 case NormType::CROSS_MAP:
Michele Di Giorgiod5e65c72017-07-26 17:09:17 +0100131 _func = &NENormalizationLayerKernel::normalize_fixed_point<DataType::QS8, 2, false>;
132 break;
133 default:
134 ARM_COMPUTE_ERROR("Not supported");
135 break;
136 }
137 break;
138 }
139 case DataType::QS16:
140 {
141 num_elems_processed_per_iteration = 8;
142 switch(norm_info.type())
143 {
144 case NormType::IN_MAP_1D:
145 _func = &NENormalizationLayerKernel::normalize_fixed_point<DataType::QS16, 0, false>;
146 break;
147 case NormType::IN_MAP_2D:
148 // Normalize over X and Y
149 _func = &NENormalizationLayerKernel::normalize_fixed_point<DataType::QS16, 0, true>;
150 break;
151 case NormType::CROSS_MAP:
152 _func = &NENormalizationLayerKernel::normalize_fixed_point<DataType::QS16, 2, false>;
Pablo Tellodf246182017-07-03 16:25:09 +0100153 break;
154 default:
155 ARM_COMPUTE_ERROR("Not supported");
156 break;
157 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158 break;
Pablo Tellodf246182017-07-03 16:25:09 +0100159 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160 default:
161 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
162 }
163
Pablo Tellodf246182017-07-03 16:25:09 +0100164 const unsigned int num_elems_read_per_iteration = num_elems_processed_per_iteration + 2 * (norm_info.norm_size() / 2);
165 const unsigned int num_rows = (norm_info.type() == NormType::IN_MAP_2D) ? norm_info.norm_size() : 1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100166
167 // Configure window
168 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
169
170 AccessWindowRectangle input_access(input->info(), -_border_size.left, 0, num_elems_read_per_iteration, num_rows);
171 AccessWindowRectangle input_squared_access(input_squared->info(), -_border_size.left, 0, num_elems_read_per_iteration, num_rows);
172 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
173
174 update_window_and_padding(win, input_access, input_squared_access, output_access);
175
176 output_access.set_valid_region(win, input->info()->valid_region());
177
178 INEKernel::configure(win);
179}
180
Pablo Tellodf246182017-07-03 16:25:09 +0100181template <DataType dt, unsigned int dim, bool do_2D_norm>
182void NENormalizationLayerKernel::normalize_float(const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100183{
184 Iterator input(_input, window);
185 Iterator input_squared(_input_squared, window);
186 Iterator output(_output, window);
187
188 const int dim_y = 1;
189 const int radius = _norm_info.norm_size() / 2;
190 const int total_size = _input->info()->dimension(dim) - 1;
191 const int input_squared_stride = _input_squared->info()->strides_in_bytes()[dim];
192 // We account padding across X only and we iterate over rows
193 const int min_left = (dim == 2) ? 0 : -static_cast<int>(border_size().left);
194 const int max_right = (dim == 2) ? total_size : total_size + border_size().left;
195 const int min_top = 0;
196 const int max_bottom = _input->info()->dimension(dim_y) - 1;
197
Pablo Tellodf246182017-07-03 16:25:09 +0100198 if(dt == DataType::F32)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100199 {
Pablo Tellodf246182017-07-03 16:25:09 +0100200 const float32x4_t coeff_vec = vdupq_n_f32(_norm_info.scale_coeff());
201 const float32x4_t beta_vec = vdupq_n_f32(_norm_info.beta());
202 const float32x4_t kappa_vec = vdupq_n_f32(_norm_info.kappa());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100203
Pablo Tellodf246182017-07-03 16:25:09 +0100204 execute_window_loop(window, [&](const Coordinates & id)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100205 {
Pablo Tellodf246182017-07-03 16:25:09 +0100206 // Get range to normalize
207 const int current_row = do_2D_norm ? id[dim_y] : 0;
208 const int current_slice = id[dim];
209 const int first_row = do_2D_norm ? std::max(current_row - radius, min_top) : 0;
210 const int last_row = do_2D_norm ? std::min(current_row + radius, max_bottom) : 0;
211 const int first_slice = std::max(current_slice - radius, min_left);
212 const int last_slice = std::min(current_slice + radius, max_right);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100213
Pablo Tellodf246182017-07-03 16:25:09 +0100214 // Accumulate 2D In-Map values
215 float32x4_t accu = vdupq_n_f32(0.f);
216 for(int j = first_row; j <= last_row; j++)
217 {
218 // Compute row displacement
219 const int row = (j - current_row) * _input_squared->info()->strides_in_bytes()[dim_y];
220 const uint8_t *const input_squared_ptr = input_squared.ptr() + row - (current_slice * input_squared_stride);
221 for(int i = first_slice; i <= last_slice; ++i)
222 {
223 accu = vaddq_f32(accu, vld1q_f32(reinterpret_cast<const float *>(input_squared_ptr + i * input_squared_stride)));
224 }
225 }
226
227 // Normalize
228 const float32x4_t normalized = vpowq_f32(vmlaq_f32(kappa_vec, coeff_vec, accu), beta_vec);
229 const float32x4_t normalized_pixel = vmulq_f32(vld1q_f32(reinterpret_cast<const float *>(input.ptr())), vinvq_f32(normalized));
230 vst1q_f32(reinterpret_cast<float *>(output.ptr()), normalized_pixel);
231 },
232 input, input_squared, output);
233 }
234#ifdef ARM_COMPUTE_ENABLE_FP16
235 else if(dt == DataType::F16)
236 {
237 const float16x8_t coeff_vec = vdupq_n_f16(_norm_info.scale_coeff());
238 const float16x8_t beta_vec_f16 = vdupq_n_f16(_norm_info.beta());
239 const float16x8_t kappa_vec = vdupq_n_f16(_norm_info.kappa());
240
241 execute_window_loop(window, [&](const Coordinates & id)
242 {
243 // Get range to normalize
244 const int current_row = do_2D_norm ? id[dim_y] : 0;
245 const int current_slice = id[dim];
246 const int first_row = do_2D_norm ? std::max(current_row - radius, min_top) : 0;
247 const int last_row = do_2D_norm ? std::min(current_row + radius, max_bottom) : 0;
248 const int first_slice = std::max(current_slice - radius, min_left);
249 const int last_slice = std::min(current_slice + radius, max_right);
250
251 // Accumulate 2D In-Map values
252 float16x8_t accu = vdupq_n_f16(0.f);
253 for(int j = first_row; j <= last_row; j++)
254 {
255 // Compute row displacement
256 const int row = (j - current_row) * _input_squared->info()->strides_in_bytes()[dim_y];
257 const uint8_t *const input_squared_ptr = input_squared.ptr() + row - (current_slice * input_squared_stride);
258 for(int i = first_slice; i <= last_slice; ++i)
259 {
260 accu = vaddq_f16(accu, vld1q_f16(reinterpret_cast<const float16_t *>(input_squared_ptr + i * input_squared_stride)));
261 }
262 }
263
264 const float16x8_t norm_f16 = vpowq_f16(vaddq_f16(kappa_vec, vmulq_f16(coeff_vec, accu)), beta_vec_f16);
265 const float16x8_t normalized_pixel = vmulq_f16(vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr())), vinvq_f16(norm_f16));
266 vst1q_f16(reinterpret_cast<float16_t *>(output.ptr()), normalized_pixel);
267 },
268 input, input_squared, output);
269 }
270#endif /* ARM_COMPUTE_ENABLE_FP16 */
271 else
272 {
273 ARM_COMPUTE_ERROR("Not supported");
274 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100275}
276
Michele Di Giorgiod5e65c72017-07-26 17:09:17 +0100277template <DataType dt, unsigned int dim, bool do_2D_norm>
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100278void NENormalizationLayerKernel::normalize_fixed_point(const Window &window)
279{
280 Iterator input(_input, window);
281 Iterator input_squared(_input_squared, window);
282 Iterator output(_output, window);
283
284 const int dim_y = 1;
285 const int radius = _norm_info.norm_size() / 2;
286 const int total_size = _input->info()->dimension(dim) - 1;
287 const int input_squared_stride = _input_squared->info()->strides_in_bytes()[dim];
288 // We account padding across X only and we iterate over rows
289 const int min_left = (dim == 2) ? 0 : -static_cast<int>(border_size().left);
290 const int max_right = (dim == 2) ? total_size : total_size + border_size().left;
291 const int min_top = 0;
292 const int max_bottom = _input->info()->dimension(dim_y) - 1;
293
294 const int fixed_point_position = _input->info()->fixed_point_position();
295
Michele Di Giorgiod5e65c72017-07-26 17:09:17 +0100296 if(dt == DataType::QS8)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100297 {
Michele Di Giorgiod5e65c72017-07-26 17:09:17 +0100298 const qint8x16_t coeff_vec = vdupq_n_qs8_f32(_norm_info.scale_coeff(), fixed_point_position);
299 const qint8x16_t beta_vec = vdupq_n_qs8_f32(_norm_info.beta(), fixed_point_position);
300 const qint8x16_t kappa_vec = vdupq_n_qs8_f32(_norm_info.kappa(), fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100301
Michele Di Giorgiod5e65c72017-07-26 17:09:17 +0100302 execute_window_loop(window, [&](const Coordinates & id)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100303 {
Michele Di Giorgiod5e65c72017-07-26 17:09:17 +0100304 // Get range to normalize
305 const int current_row = do_2D_norm ? id[dim_y] : 0;
306 const int current_slice = id[dim];
307 const int first_row = do_2D_norm ? std::max(current_row - radius, min_top) : 0;
308 const int last_row = do_2D_norm ? std::min(current_row + radius, max_bottom) : 0;
309 const int first_slice = std::max(current_slice - radius, min_left);
310 const int last_slice = std::min(current_slice + radius, max_right);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100311
Michele Di Giorgiod5e65c72017-07-26 17:09:17 +0100312 // Accumulate 2D In-Map values
313 qint8x16_t accu = vdupq_n_qs8(0);
314 for(int j = first_row; j <= last_row; ++j)
315 {
316 // Compute row displacement
317 const int row = (j - current_row) * _input_squared->info()->strides_in_bytes()[dim_y];
318 const uint8_t *const input_squared_ptr = input_squared.ptr() + row - (current_slice * input_squared_stride);
319 for(int i = first_slice; i <= last_slice; ++i)
320 {
321 accu = vqaddq_qs8(accu, vld1q_qs8(reinterpret_cast<const qint8_t *>(input_squared_ptr + i * input_squared_stride)));
322 }
323 }
324
325 // Normalize
326 const qint8x16_t accu_scale = vqmlaq_qs8(kappa_vec, coeff_vec, accu, fixed_point_position);
327 const qint8x16_t normalized = vqpowq_qs8(accu_scale, beta_vec, fixed_point_position);
328 const qint8x16_t normalized_pixel = vdivq_qs8(vld1q_qs8(reinterpret_cast<const qint8_t *>(input.ptr())), normalized, fixed_point_position);
329 vst1q_qs8(reinterpret_cast<qint8_t *>(output.ptr()), normalized_pixel);
330 },
331 input, input_squared, output);
332 }
333 else if(dt == DataType::QS16)
334 {
335 const qint16x8_t coeff_vec = vdupq_n_qs16_f32(_norm_info.scale_coeff(), fixed_point_position);
336 const qint16x8_t beta_vec = vdupq_n_qs16_f32(_norm_info.beta(), fixed_point_position);
337 const qint16x8_t kappa_vec = vdupq_n_qs16_f32(_norm_info.kappa(), fixed_point_position);
338
339 execute_window_loop(window, [&](const Coordinates & id)
340 {
341 // Get range to normalize
342 const int current_row = do_2D_norm ? id[dim_y] : 0;
343 const int current_slice = id[dim];
344 const int first_row = do_2D_norm ? std::max(current_row - radius, min_top) : 0;
345 const int last_row = do_2D_norm ? std::min(current_row + radius, max_bottom) : 0;
346 const int first_slice = std::max(current_slice - radius, min_left);
347 const int last_slice = std::min(current_slice + radius, max_right);
348
349 // Accumulate 2D In-Map values
350 qint16x8_t accu = vdupq_n_qs16(0);
351 for(int j = first_row; j <= last_row; ++j)
352 {
353 // Compute row displacement
354 const int row = (j - current_row) * _input_squared->info()->strides_in_bytes()[dim_y];
355 const uint8_t *const input_squared_ptr = input_squared.ptr() + row - (current_slice * input_squared_stride);
356 for(int i = first_slice; i <= last_slice; ++i)
357 {
358 accu = vqaddq_qs16(accu, vld1q_qs16(reinterpret_cast<const qint16_t *>(input_squared_ptr + i * input_squared_stride)));
359 }
360 }
361
362 // Normalize
363 const qint16x8_t accu_scale = vqmlaq_qs16(kappa_vec, coeff_vec, accu, fixed_point_position);
364 const qint16x8_t normalized = vqpowq_qs16(accu_scale, beta_vec, fixed_point_position);
365 const qint16x8_t normalized_pixel = vdivq_qs16(vld1q_qs16(reinterpret_cast<const qint16_t *>(input.ptr())), normalized, fixed_point_position);
366 vst1q_qs16(reinterpret_cast<qint16_t *>(output.ptr()), normalized_pixel);
367 },
368 input, input_squared, output);
369 }
370 else
371 {
372 ARM_COMPUTE_ERROR("Not supported");
373 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100374}
375
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100376void NENormalizationLayerKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100377{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100378 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100379 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
380 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
381 ARM_COMPUTE_ERROR_ON(_func == nullptr);
382
383 // Run function
384 (this->*_func)(window);
385}