blob: 76ace91c208e27785fca75624ead9345b1f48851 [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{
Pablo Tellodf246182017-07-03 16:25:09 +010049 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32, DataType::QS8);
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);
54 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, input_squared, output);
Georgios Pinitas09004ca2017-07-03 17:30:14 +010055 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, input_squared, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056 ARM_COMPUTE_ERROR_ON_MSG(!(norm_info.norm_size() % 2), "Normalization size should be odd");
57 ARM_COMPUTE_ERROR_ON_VALUE_NOT_REPRESENTABLE_IN_FIXED_POINT(norm_info.beta(), input);
58 ARM_COMPUTE_ERROR_ON_VALUE_NOT_REPRESENTABLE_IN_FIXED_POINT(norm_info.kappa(), input);
59 ARM_COMPUTE_ERROR_ON_VALUE_NOT_REPRESENTABLE_IN_FIXED_POINT(norm_info.scale_coeff(), input);
60
61 const unsigned int border_width = (norm_info.type() == NormType::CROSS_MAP) ? 0 : std::min(norm_info.norm_size() / 2, 3U);
62
63 _input = input;
64 _input_squared = input_squared;
65 _output = output;
66 _norm_info = norm_info;
67 _border_size = BorderSize(0, border_width);
68
Pablo Tellodf246182017-07-03 16:25:09 +010069 unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070
Pablo Tellodf246182017-07-03 16:25:09 +010071 switch(_input->info()->data_type())
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072 {
Pablo Tellodf246182017-07-03 16:25:09 +010073 case DataType::F32:
74 {
75 num_elems_processed_per_iteration = 4;
76 switch(norm_info.type())
77 {
78 case NormType::IN_MAP_1D:
79 _func = &NENormalizationLayerKernel::normalize_float<DataType::F32, 0, false>;
80 break;
81 case NormType::IN_MAP_2D:
82 // Normalize over X and Y
83 _func = &NENormalizationLayerKernel::normalize_float<DataType::F32, 0, true>;
84 break;
85 case NormType::CROSS_MAP:
86 _func = &NENormalizationLayerKernel::normalize_float<DataType::F32, 2, false>;
87 break;
88 default:
89 ARM_COMPUTE_ERROR("Not supported");
90 break;
91 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092 break;
Pablo Tellodf246182017-07-03 16:25:09 +010093 }
94 case DataType::F16:
95 {
96 num_elems_processed_per_iteration = 8;
97 switch(norm_info.type())
98 {
99 case NormType::IN_MAP_1D:
100 _func = &NENormalizationLayerKernel::normalize_float<DataType::F16, 0, false>;
101 break;
102 case NormType::IN_MAP_2D:
103 // Normalize over X and Y
104 _func = &NENormalizationLayerKernel::normalize_float<DataType::F16, 0, true>;
105 break;
106 case NormType::CROSS_MAP:
107 _func = &NENormalizationLayerKernel::normalize_float<DataType::F16, 2, false>;
108 break;
109 default:
110 ARM_COMPUTE_ERROR("Not supported");
111 break;
112 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113 break;
Pablo Tellodf246182017-07-03 16:25:09 +0100114 }
115 case DataType::QS8:
116 {
117 num_elems_processed_per_iteration = 16;
118 switch(norm_info.type())
119 {
120 case NormType::IN_MAP_1D:
121 _func = &NENormalizationLayerKernel::normalize_fixed_point<0, false>;
122 break;
123 case NormType::IN_MAP_2D:
124 // Normalize over X and Y
125 _func = &NENormalizationLayerKernel::normalize_fixed_point<0, true>;
126 break;
127 case NormType::CROSS_MAP:
128 _func = &NENormalizationLayerKernel::normalize_fixed_point<2, false>;
129 break;
130 default:
131 ARM_COMPUTE_ERROR("Not supported");
132 break;
133 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134 break;
Pablo Tellodf246182017-07-03 16:25:09 +0100135 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136 default:
137 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
138 }
139
Pablo Tellodf246182017-07-03 16:25:09 +0100140 const unsigned int num_elems_read_per_iteration = num_elems_processed_per_iteration + 2 * (norm_info.norm_size() / 2);
141 const unsigned int num_rows = (norm_info.type() == NormType::IN_MAP_2D) ? norm_info.norm_size() : 1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100142
143 // Configure window
144 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
145
146 AccessWindowRectangle input_access(input->info(), -_border_size.left, 0, num_elems_read_per_iteration, num_rows);
147 AccessWindowRectangle input_squared_access(input_squared->info(), -_border_size.left, 0, num_elems_read_per_iteration, num_rows);
148 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
149
150 update_window_and_padding(win, input_access, input_squared_access, output_access);
151
152 output_access.set_valid_region(win, input->info()->valid_region());
153
154 INEKernel::configure(win);
155}
156
Pablo Tellodf246182017-07-03 16:25:09 +0100157template <DataType dt, unsigned int dim, bool do_2D_norm>
158void NENormalizationLayerKernel::normalize_float(const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159{
160 Iterator input(_input, window);
161 Iterator input_squared(_input_squared, window);
162 Iterator output(_output, window);
163
164 const int dim_y = 1;
165 const int radius = _norm_info.norm_size() / 2;
166 const int total_size = _input->info()->dimension(dim) - 1;
167 const int input_squared_stride = _input_squared->info()->strides_in_bytes()[dim];
168 // We account padding across X only and we iterate over rows
169 const int min_left = (dim == 2) ? 0 : -static_cast<int>(border_size().left);
170 const int max_right = (dim == 2) ? total_size : total_size + border_size().left;
171 const int min_top = 0;
172 const int max_bottom = _input->info()->dimension(dim_y) - 1;
173
Pablo Tellodf246182017-07-03 16:25:09 +0100174 if(dt == DataType::F32)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100175 {
Pablo Tellodf246182017-07-03 16:25:09 +0100176 const float32x4_t coeff_vec = vdupq_n_f32(_norm_info.scale_coeff());
177 const float32x4_t beta_vec = vdupq_n_f32(_norm_info.beta());
178 const float32x4_t kappa_vec = vdupq_n_f32(_norm_info.kappa());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179
Pablo Tellodf246182017-07-03 16:25:09 +0100180 execute_window_loop(window, [&](const Coordinates & id)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181 {
Pablo Tellodf246182017-07-03 16:25:09 +0100182 // Get range to normalize
183 const int current_row = do_2D_norm ? id[dim_y] : 0;
184 const int current_slice = id[dim];
185 const int first_row = do_2D_norm ? std::max(current_row - radius, min_top) : 0;
186 const int last_row = do_2D_norm ? std::min(current_row + radius, max_bottom) : 0;
187 const int first_slice = std::max(current_slice - radius, min_left);
188 const int last_slice = std::min(current_slice + radius, max_right);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100189
Pablo Tellodf246182017-07-03 16:25:09 +0100190 // Accumulate 2D In-Map values
191 float32x4_t accu = vdupq_n_f32(0.f);
192 for(int j = first_row; j <= last_row; j++)
193 {
194 // Compute row displacement
195 const int row = (j - current_row) * _input_squared->info()->strides_in_bytes()[dim_y];
196 const uint8_t *const input_squared_ptr = input_squared.ptr() + row - (current_slice * input_squared_stride);
197 for(int i = first_slice; i <= last_slice; ++i)
198 {
199 accu = vaddq_f32(accu, vld1q_f32(reinterpret_cast<const float *>(input_squared_ptr + i * input_squared_stride)));
200 }
201 }
202
203 // Normalize
204 const float32x4_t normalized = vpowq_f32(vmlaq_f32(kappa_vec, coeff_vec, accu), beta_vec);
205 const float32x4_t normalized_pixel = vmulq_f32(vld1q_f32(reinterpret_cast<const float *>(input.ptr())), vinvq_f32(normalized));
206 vst1q_f32(reinterpret_cast<float *>(output.ptr()), normalized_pixel);
207 },
208 input, input_squared, output);
209 }
210#ifdef ARM_COMPUTE_ENABLE_FP16
211 else if(dt == DataType::F16)
212 {
213 const float16x8_t coeff_vec = vdupq_n_f16(_norm_info.scale_coeff());
214 const float16x8_t beta_vec_f16 = vdupq_n_f16(_norm_info.beta());
215 const float16x8_t kappa_vec = vdupq_n_f16(_norm_info.kappa());
216
217 execute_window_loop(window, [&](const Coordinates & id)
218 {
219 // Get range to normalize
220 const int current_row = do_2D_norm ? id[dim_y] : 0;
221 const int current_slice = id[dim];
222 const int first_row = do_2D_norm ? std::max(current_row - radius, min_top) : 0;
223 const int last_row = do_2D_norm ? std::min(current_row + radius, max_bottom) : 0;
224 const int first_slice = std::max(current_slice - radius, min_left);
225 const int last_slice = std::min(current_slice + radius, max_right);
226
227 // Accumulate 2D In-Map values
228 float16x8_t accu = vdupq_n_f16(0.f);
229 for(int j = first_row; j <= last_row; j++)
230 {
231 // Compute row displacement
232 const int row = (j - current_row) * _input_squared->info()->strides_in_bytes()[dim_y];
233 const uint8_t *const input_squared_ptr = input_squared.ptr() + row - (current_slice * input_squared_stride);
234 for(int i = first_slice; i <= last_slice; ++i)
235 {
236 accu = vaddq_f16(accu, vld1q_f16(reinterpret_cast<const float16_t *>(input_squared_ptr + i * input_squared_stride)));
237 }
238 }
239
240 const float16x8_t norm_f16 = vpowq_f16(vaddq_f16(kappa_vec, vmulq_f16(coeff_vec, accu)), beta_vec_f16);
241 const float16x8_t normalized_pixel = vmulq_f16(vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr())), vinvq_f16(norm_f16));
242 vst1q_f16(reinterpret_cast<float16_t *>(output.ptr()), normalized_pixel);
243 },
244 input, input_squared, output);
245 }
246#endif /* ARM_COMPUTE_ENABLE_FP16 */
247 else
248 {
249 ARM_COMPUTE_ERROR("Not supported");
250 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100251}
252
253template <unsigned int dim, bool do_2D_norm>
254void NENormalizationLayerKernel::normalize_fixed_point(const Window &window)
255{
256 Iterator input(_input, window);
257 Iterator input_squared(_input_squared, window);
258 Iterator output(_output, window);
259
260 const int dim_y = 1;
261 const int radius = _norm_info.norm_size() / 2;
262 const int total_size = _input->info()->dimension(dim) - 1;
263 const int input_squared_stride = _input_squared->info()->strides_in_bytes()[dim];
264 // We account padding across X only and we iterate over rows
265 const int min_left = (dim == 2) ? 0 : -static_cast<int>(border_size().left);
266 const int max_right = (dim == 2) ? total_size : total_size + border_size().left;
267 const int min_top = 0;
268 const int max_bottom = _input->info()->dimension(dim_y) - 1;
269
270 const int fixed_point_position = _input->info()->fixed_point_position();
271
272 const qint8x16_t coeff_vec = vdupq_n_qs8_f32(_norm_info.scale_coeff(), fixed_point_position);
273 const qint8x16_t beta_vec = vdupq_n_qs8_f32(_norm_info.beta(), fixed_point_position);
274 const qint8x16_t kappa_vec = vdupq_n_qs8_f32(_norm_info.kappa(), fixed_point_position);
275
276 execute_window_loop(window, [&](const Coordinates & id)
277 {
278 // Get range to normalize
279 const int current_row = do_2D_norm ? id[dim_y] : 0;
280 const int current_slice = id[dim];
281 const int first_row = do_2D_norm ? std::max(current_row - radius, min_top) : 0;
282 const int last_row = do_2D_norm ? std::min(current_row + radius, max_bottom) : 0;
283 const int first_slice = std::max(current_slice - radius, min_left);
284 const int last_slice = std::min(current_slice + radius, max_right);
285
286 // Accumulate 2D In-Map values
287 qint8x16_t accu = vdupq_n_qs8(0);
288 for(int j = first_row; j <= last_row; ++j)
289 {
290 // Compute row displacement
291 const int row = (j - current_row) * _input_squared->info()->strides_in_bytes()[dim_y];
292 const uint8_t *const input_squared_ptr = input_squared.ptr() + row - (current_slice * input_squared_stride);
293 for(int i = first_slice; i <= last_slice; ++i)
294 {
295 accu = vqaddq_qs8(accu, vld1q_qs8(reinterpret_cast<const qint8_t *>(input_squared_ptr + i * input_squared_stride)));
296 }
297 }
298
299 // Normalize
300 const qint8x16_t accu_scale = vqmlaq_qs8(kappa_vec, coeff_vec, accu, fixed_point_position);
301 const qint8x16_t normalized = vqpowq_qs8(accu_scale, beta_vec, fixed_point_position);
302 const qint8x16_t normalized_pixel = vdivq_qs8(vld1q_qs8(reinterpret_cast<const qint8_t *>(input.ptr())), normalized, fixed_point_position);
303 vst1q_qs8(reinterpret_cast<qint8_t *>(output.ptr()), normalized_pixel);
304 },
305 input, input_squared, output);
306}
307
308void NENormalizationLayerKernel::run(const Window &window)
309{
310 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
311 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
312 ARM_COMPUTE_ERROR_ON(_func == nullptr);
313
314 // Run function
315 (this->*_func)(window);
316}