blob: b983609e49ca49625eaa2b1b3ae2c682d6b7572e [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
Georgios Pinitas41caa622017-11-16 14:37:08 +000064 const unsigned int border_width = (norm_info.is_cross_map()) ? 0 : std::min<unsigned int>(norm_info.norm_size() / 2, 3U);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065
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();
Georgios Pinitas30f02152017-09-27 11:20:48 +010073 ARM_COMPUTE_UNUSED(num_elems_processed_per_iteration);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074
Pablo Tellodf246182017-07-03 16:25:09 +010075 switch(_input->info()->data_type())
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076 {
Pablo Tellodf246182017-07-03 16:25:09 +010077 case DataType::F32:
78 {
79 num_elems_processed_per_iteration = 4;
80 switch(norm_info.type())
81 {
82 case NormType::IN_MAP_1D:
83 _func = &NENormalizationLayerKernel::normalize_float<DataType::F32, 0, false>;
84 break;
85 case NormType::IN_MAP_2D:
86 // Normalize over X and Y
87 _func = &NENormalizationLayerKernel::normalize_float<DataType::F32, 0, true>;
88 break;
89 case NormType::CROSS_MAP:
90 _func = &NENormalizationLayerKernel::normalize_float<DataType::F32, 2, false>;
91 break;
92 default:
93 ARM_COMPUTE_ERROR("Not supported");
94 break;
95 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096 break;
Pablo Tellodf246182017-07-03 16:25:09 +010097 }
98 case DataType::F16:
99 {
100 num_elems_processed_per_iteration = 8;
101 switch(norm_info.type())
102 {
103 case NormType::IN_MAP_1D:
104 _func = &NENormalizationLayerKernel::normalize_float<DataType::F16, 0, false>;
105 break;
106 case NormType::IN_MAP_2D:
107 // Normalize over X and Y
108 _func = &NENormalizationLayerKernel::normalize_float<DataType::F16, 0, true>;
109 break;
110 case NormType::CROSS_MAP:
111 _func = &NENormalizationLayerKernel::normalize_float<DataType::F16, 2, false>;
112 break;
113 default:
114 ARM_COMPUTE_ERROR("Not supported");
115 break;
116 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117 break;
Pablo Tellodf246182017-07-03 16:25:09 +0100118 }
119 case DataType::QS8:
120 {
121 num_elems_processed_per_iteration = 16;
122 switch(norm_info.type())
123 {
124 case NormType::IN_MAP_1D:
Michele Di Giorgiod5e65c72017-07-26 17:09:17 +0100125 _func = &NENormalizationLayerKernel::normalize_fixed_point<DataType::QS8, 0, false>;
Pablo Tellodf246182017-07-03 16:25:09 +0100126 break;
127 case NormType::IN_MAP_2D:
128 // Normalize over X and Y
Michele Di Giorgiod5e65c72017-07-26 17:09:17 +0100129 _func = &NENormalizationLayerKernel::normalize_fixed_point<DataType::QS8, 0, true>;
Pablo Tellodf246182017-07-03 16:25:09 +0100130 break;
131 case NormType::CROSS_MAP:
Michele Di Giorgiod5e65c72017-07-26 17:09:17 +0100132 _func = &NENormalizationLayerKernel::normalize_fixed_point<DataType::QS8, 2, false>;
133 break;
134 default:
135 ARM_COMPUTE_ERROR("Not supported");
136 break;
137 }
138 break;
139 }
140 case DataType::QS16:
141 {
142 num_elems_processed_per_iteration = 8;
143 switch(norm_info.type())
144 {
145 case NormType::IN_MAP_1D:
146 _func = &NENormalizationLayerKernel::normalize_fixed_point<DataType::QS16, 0, false>;
147 break;
148 case NormType::IN_MAP_2D:
149 // Normalize over X and Y
150 _func = &NENormalizationLayerKernel::normalize_fixed_point<DataType::QS16, 0, true>;
151 break;
152 case NormType::CROSS_MAP:
153 _func = &NENormalizationLayerKernel::normalize_fixed_point<DataType::QS16, 2, false>;
Pablo Tellodf246182017-07-03 16:25:09 +0100154 break;
155 default:
156 ARM_COMPUTE_ERROR("Not supported");
157 break;
158 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159 break;
Pablo Tellodf246182017-07-03 16:25:09 +0100160 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161 default:
162 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
163 }
164
Pablo Tellodf246182017-07-03 16:25:09 +0100165 const unsigned int num_elems_read_per_iteration = num_elems_processed_per_iteration + 2 * (norm_info.norm_size() / 2);
166 const unsigned int num_rows = (norm_info.type() == NormType::IN_MAP_2D) ? norm_info.norm_size() : 1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167
168 // Configure window
169 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
170
171 AccessWindowRectangle input_access(input->info(), -_border_size.left, 0, num_elems_read_per_iteration, num_rows);
172 AccessWindowRectangle input_squared_access(input_squared->info(), -_border_size.left, 0, num_elems_read_per_iteration, num_rows);
173 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
174
175 update_window_and_padding(win, input_access, input_squared_access, output_access);
176
177 output_access.set_valid_region(win, input->info()->valid_region());
178
179 INEKernel::configure(win);
180}
181
Pablo Tellodf246182017-07-03 16:25:09 +0100182template <DataType dt, unsigned int dim, bool do_2D_norm>
183void NENormalizationLayerKernel::normalize_float(const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100184{
185 Iterator input(_input, window);
186 Iterator input_squared(_input_squared, window);
187 Iterator output(_output, window);
188
189 const int dim_y = 1;
190 const int radius = _norm_info.norm_size() / 2;
191 const int total_size = _input->info()->dimension(dim) - 1;
192 const int input_squared_stride = _input_squared->info()->strides_in_bytes()[dim];
193 // We account padding across X only and we iterate over rows
194 const int min_left = (dim == 2) ? 0 : -static_cast<int>(border_size().left);
195 const int max_right = (dim == 2) ? total_size : total_size + border_size().left;
196 const int min_top = 0;
197 const int max_bottom = _input->info()->dimension(dim_y) - 1;
198
Pablo Tellodf246182017-07-03 16:25:09 +0100199 if(dt == DataType::F32)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100200 {
Pablo Tellodf246182017-07-03 16:25:09 +0100201 const float32x4_t coeff_vec = vdupq_n_f32(_norm_info.scale_coeff());
202 const float32x4_t beta_vec = vdupq_n_f32(_norm_info.beta());
203 const float32x4_t kappa_vec = vdupq_n_f32(_norm_info.kappa());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204
Pablo Tellodf246182017-07-03 16:25:09 +0100205 execute_window_loop(window, [&](const Coordinates & id)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100206 {
Pablo Tellodf246182017-07-03 16:25:09 +0100207 // Get range to normalize
208 const int current_row = do_2D_norm ? id[dim_y] : 0;
209 const int current_slice = id[dim];
210 const int first_row = do_2D_norm ? std::max(current_row - radius, min_top) : 0;
211 const int last_row = do_2D_norm ? std::min(current_row + radius, max_bottom) : 0;
212 const int first_slice = std::max(current_slice - radius, min_left);
213 const int last_slice = std::min(current_slice + radius, max_right);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100214
Pablo Tellodf246182017-07-03 16:25:09 +0100215 // Accumulate 2D In-Map values
216 float32x4_t accu = vdupq_n_f32(0.f);
217 for(int j = first_row; j <= last_row; j++)
218 {
219 // Compute row displacement
220 const int row = (j - current_row) * _input_squared->info()->strides_in_bytes()[dim_y];
221 const uint8_t *const input_squared_ptr = input_squared.ptr() + row - (current_slice * input_squared_stride);
222 for(int i = first_slice; i <= last_slice; ++i)
223 {
224 accu = vaddq_f32(accu, vld1q_f32(reinterpret_cast<const float *>(input_squared_ptr + i * input_squared_stride)));
225 }
226 }
227
228 // Normalize
229 const float32x4_t normalized = vpowq_f32(vmlaq_f32(kappa_vec, coeff_vec, accu), beta_vec);
230 const float32x4_t normalized_pixel = vmulq_f32(vld1q_f32(reinterpret_cast<const float *>(input.ptr())), vinvq_f32(normalized));
231 vst1q_f32(reinterpret_cast<float *>(output.ptr()), normalized_pixel);
232 },
233 input, input_squared, output);
234 }
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000235#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tellodf246182017-07-03 16:25:09 +0100236 else if(dt == DataType::F16)
237 {
238 const float16x8_t coeff_vec = vdupq_n_f16(_norm_info.scale_coeff());
239 const float16x8_t beta_vec_f16 = vdupq_n_f16(_norm_info.beta());
240 const float16x8_t kappa_vec = vdupq_n_f16(_norm_info.kappa());
241
242 execute_window_loop(window, [&](const Coordinates & id)
243 {
244 // Get range to normalize
245 const int current_row = do_2D_norm ? id[dim_y] : 0;
246 const int current_slice = id[dim];
247 const int first_row = do_2D_norm ? std::max(current_row - radius, min_top) : 0;
248 const int last_row = do_2D_norm ? std::min(current_row + radius, max_bottom) : 0;
249 const int first_slice = std::max(current_slice - radius, min_left);
250 const int last_slice = std::min(current_slice + radius, max_right);
251
252 // Accumulate 2D In-Map values
253 float16x8_t accu = vdupq_n_f16(0.f);
254 for(int j = first_row; j <= last_row; j++)
255 {
256 // Compute row displacement
257 const int row = (j - current_row) * _input_squared->info()->strides_in_bytes()[dim_y];
258 const uint8_t *const input_squared_ptr = input_squared.ptr() + row - (current_slice * input_squared_stride);
259 for(int i = first_slice; i <= last_slice; ++i)
260 {
261 accu = vaddq_f16(accu, vld1q_f16(reinterpret_cast<const float16_t *>(input_squared_ptr + i * input_squared_stride)));
262 }
263 }
264
265 const float16x8_t norm_f16 = vpowq_f16(vaddq_f16(kappa_vec, vmulq_f16(coeff_vec, accu)), beta_vec_f16);
266 const float16x8_t normalized_pixel = vmulq_f16(vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr())), vinvq_f16(norm_f16));
267 vst1q_f16(reinterpret_cast<float16_t *>(output.ptr()), normalized_pixel);
268 },
269 input, input_squared, output);
270 }
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000271#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tellodf246182017-07-03 16:25:09 +0100272 else
273 {
274 ARM_COMPUTE_ERROR("Not supported");
275 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100276}
277
Michele Di Giorgiod5e65c72017-07-26 17:09:17 +0100278template <DataType dt, unsigned int dim, bool do_2D_norm>
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100279void NENormalizationLayerKernel::normalize_fixed_point(const Window &window)
280{
281 Iterator input(_input, window);
282 Iterator input_squared(_input_squared, window);
283 Iterator output(_output, window);
284
285 const int dim_y = 1;
286 const int radius = _norm_info.norm_size() / 2;
287 const int total_size = _input->info()->dimension(dim) - 1;
288 const int input_squared_stride = _input_squared->info()->strides_in_bytes()[dim];
289 // We account padding across X only and we iterate over rows
290 const int min_left = (dim == 2) ? 0 : -static_cast<int>(border_size().left);
291 const int max_right = (dim == 2) ? total_size : total_size + border_size().left;
292 const int min_top = 0;
293 const int max_bottom = _input->info()->dimension(dim_y) - 1;
294
295 const int fixed_point_position = _input->info()->fixed_point_position();
296
Michele Di Giorgiod5e65c72017-07-26 17:09:17 +0100297 if(dt == DataType::QS8)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100298 {
Michele Di Giorgiod5e65c72017-07-26 17:09:17 +0100299 const qint8x16_t coeff_vec = vdupq_n_qs8_f32(_norm_info.scale_coeff(), fixed_point_position);
300 const qint8x16_t beta_vec = vdupq_n_qs8_f32(_norm_info.beta(), fixed_point_position);
301 const qint8x16_t kappa_vec = vdupq_n_qs8_f32(_norm_info.kappa(), fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100302
Michele Di Giorgiod5e65c72017-07-26 17:09:17 +0100303 execute_window_loop(window, [&](const Coordinates & id)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100304 {
Michele Di Giorgiod5e65c72017-07-26 17:09:17 +0100305 // Get range to normalize
306 const int current_row = do_2D_norm ? id[dim_y] : 0;
307 const int current_slice = id[dim];
308 const int first_row = do_2D_norm ? std::max(current_row - radius, min_top) : 0;
309 const int last_row = do_2D_norm ? std::min(current_row + radius, max_bottom) : 0;
310 const int first_slice = std::max(current_slice - radius, min_left);
311 const int last_slice = std::min(current_slice + radius, max_right);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100312
Michele Di Giorgiod5e65c72017-07-26 17:09:17 +0100313 // Accumulate 2D In-Map values
314 qint8x16_t accu = vdupq_n_qs8(0);
315 for(int j = first_row; j <= last_row; ++j)
316 {
317 // Compute row displacement
318 const int row = (j - current_row) * _input_squared->info()->strides_in_bytes()[dim_y];
319 const uint8_t *const input_squared_ptr = input_squared.ptr() + row - (current_slice * input_squared_stride);
320 for(int i = first_slice; i <= last_slice; ++i)
321 {
322 accu = vqaddq_qs8(accu, vld1q_qs8(reinterpret_cast<const qint8_t *>(input_squared_ptr + i * input_squared_stride)));
323 }
324 }
325
326 // Normalize
327 const qint8x16_t accu_scale = vqmlaq_qs8(kappa_vec, coeff_vec, accu, fixed_point_position);
328 const qint8x16_t normalized = vqpowq_qs8(accu_scale, beta_vec, fixed_point_position);
329 const qint8x16_t normalized_pixel = vdivq_qs8(vld1q_qs8(reinterpret_cast<const qint8_t *>(input.ptr())), normalized, fixed_point_position);
330 vst1q_qs8(reinterpret_cast<qint8_t *>(output.ptr()), normalized_pixel);
331 },
332 input, input_squared, output);
333 }
334 else if(dt == DataType::QS16)
335 {
336 const qint16x8_t coeff_vec = vdupq_n_qs16_f32(_norm_info.scale_coeff(), fixed_point_position);
337 const qint16x8_t beta_vec = vdupq_n_qs16_f32(_norm_info.beta(), fixed_point_position);
338 const qint16x8_t kappa_vec = vdupq_n_qs16_f32(_norm_info.kappa(), fixed_point_position);
339
340 execute_window_loop(window, [&](const Coordinates & id)
341 {
342 // Get range to normalize
343 const int current_row = do_2D_norm ? id[dim_y] : 0;
344 const int current_slice = id[dim];
345 const int first_row = do_2D_norm ? std::max(current_row - radius, min_top) : 0;
346 const int last_row = do_2D_norm ? std::min(current_row + radius, max_bottom) : 0;
347 const int first_slice = std::max(current_slice - radius, min_left);
348 const int last_slice = std::min(current_slice + radius, max_right);
349
350 // Accumulate 2D In-Map values
351 qint16x8_t accu = vdupq_n_qs16(0);
352 for(int j = first_row; j <= last_row; ++j)
353 {
354 // Compute row displacement
355 const int row = (j - current_row) * _input_squared->info()->strides_in_bytes()[dim_y];
356 const uint8_t *const input_squared_ptr = input_squared.ptr() + row - (current_slice * input_squared_stride);
357 for(int i = first_slice; i <= last_slice; ++i)
358 {
359 accu = vqaddq_qs16(accu, vld1q_qs16(reinterpret_cast<const qint16_t *>(input_squared_ptr + i * input_squared_stride)));
360 }
361 }
362
363 // Normalize
364 const qint16x8_t accu_scale = vqmlaq_qs16(kappa_vec, coeff_vec, accu, fixed_point_position);
365 const qint16x8_t normalized = vqpowq_qs16(accu_scale, beta_vec, fixed_point_position);
366 const qint16x8_t normalized_pixel = vdivq_qs16(vld1q_qs16(reinterpret_cast<const qint16_t *>(input.ptr())), normalized, fixed_point_position);
367 vst1q_qs16(reinterpret_cast<qint16_t *>(output.ptr()), normalized_pixel);
368 },
369 input, input_squared, output);
370 }
371 else
372 {
373 ARM_COMPUTE_ERROR("Not supported");
374 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100375}
376
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100377void NENormalizationLayerKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100378{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100379 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100380 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
381 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
382 ARM_COMPUTE_ERROR_ON(_func == nullptr);
383
384 // Run function
385 (this->*_func)(window);
386}