blob: 27af121ce5cef3426c6fbc5d467ff0c3ab5e72f7 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +01002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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
Georgios Pinitas7f32d012018-10-11 18:41:19 +010026#include "arm_compute/core/AccessWindowStatic.h"
Anthony Barbiereaefd002018-07-20 17:49:35 +010027#include "arm_compute/core/CPP/Validate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/Error.h"
29#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/NEON/NEFixedPoint.h"
31#include "arm_compute/core/NEON/NEMath.h"
32#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Utils.h"
34#include "arm_compute/core/Validate.h"
35#include "arm_compute/core/Window.h"
36
37using namespace arm_compute;
38
Michalis Spyrouafa5d812017-11-30 14:25:57 +000039namespace
40{
41Status validate_arguments(const ITensorInfo *input, const ITensorInfo *input_squared, const ITensorInfo *output, const NormalizationLayerInfo &norm_info)
42{
43 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, input_squared, output);
Anthony Barbiereaefd002018-07-20 17:49:35 +010044 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010045 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
Michalis Spyrouafa5d812017-11-30 14:25:57 +000046
Georgios Pinitase2220552018-07-20 13:23:44 +010047 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_layout() == DataLayout::NHWC && norm_info.type() == NormType::IN_MAP_2D,
48 "Only Cross-map and 1D In-map normalization is supported for NHWC layout");
Michalis Spyrouafa5d812017-11-30 14:25:57 +000049 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, input_squared);
50 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, input_squared);
51 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!(norm_info.norm_size() % 2), "Normalization size should be odd");
52
Michalis Spyrouafa5d812017-11-30 14:25:57 +000053 // Checks performed when output is configured
54 if(output->total_size() != 0)
55 {
56 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
57 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
Michalis Spyrouafa5d812017-11-30 14:25:57 +000058 }
59
60 return Status{};
61}
62
63std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *input_squared, ITensorInfo *output, const NormalizationLayerInfo &norm_info)
64{
Georgios Pinitas7f32d012018-10-11 18:41:19 +010065 // Output tensor auto initialization if not yet initialized
66 auto_init_if_empty(*output, *input->clone());
67
68 const unsigned int num_elems_processed_per_iteration = 16 / input->element_size();
69
70 const unsigned int norm_idx = get_normalization_dimension_index(input->data_layout(), norm_info);
71 const bool is_norm_accross_width = norm_idx == 0;
72
73 const unsigned int border_width = is_norm_accross_width ? num_elems_processed_per_iteration - 1 : 0;
74 const BorderSize border_size = BorderSize(0, border_width);
Michalis Spyrouafa5d812017-11-30 14:25:57 +000075
76 // Configure window
Georgios Pinitas7f32d012018-10-11 18:41:19 +010077 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
78 bool window_changed = false;
Michalis Spyrouafa5d812017-11-30 14:25:57 +000079
Georgios Pinitas7f32d012018-10-11 18:41:19 +010080 if(is_norm_accross_width)
81 {
82 AccessWindowStatic input_access(input, -border_size.left, 0, input->dimension(0) + border_size.right, 0);
83 AccessWindowStatic input_squared_access(input_squared, -border_size.left, 0, input->dimension(0) + border_size.right, 0);
84 window_changed = window_changed || update_window_and_padding(win, input_access, input_squared_access);
85 }
86 else
87 {
88 AccessWindowHorizontal input_access(input, -border_size.left, num_elems_processed_per_iteration);
89 AccessWindowHorizontal input_squared_access(input_squared, -border_size.left, num_elems_processed_per_iteration);
90 window_changed = window_changed || update_window_and_padding(win, input_access, input_squared_access);
91 }
Michalis Spyrouafa5d812017-11-30 14:25:57 +000092
93 if(output->total_size() != 0)
94 {
95 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
Georgios Pinitas7f32d012018-10-11 18:41:19 +010096 window_changed = window_changed || update_window_and_padding(win, output_access);
Michalis Spyrouafa5d812017-11-30 14:25:57 +000097 output_access.set_valid_region(win, input->valid_region());
98 }
Michalis Spyrouafa5d812017-11-30 14:25:57 +000099
100 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
101 return std::make_pair(err, win);
102}
103} // namespace
104
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105NENormalizationLayerKernel::NENormalizationLayerKernel()
106 : _func(nullptr), _input(nullptr), _input_squared(nullptr), _output(nullptr), _norm_info(NormType::IN_MAP_1D), _border_size()
107{
108}
109
110BorderSize NENormalizationLayerKernel::border_size() const
111{
112 return _border_size;
113}
114
115void NENormalizationLayerKernel::configure(const ITensor *input, const ITensor *input_squared, ITensor *output, NormalizationLayerInfo norm_info)
116{
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000117 ARM_COMPUTE_ERROR_ON_NULLPTR(input, input_squared, output);
Georgios Pinitas09004ca2017-07-03 17:30:14 +0100118 // Output tensor auto initialization if not yet initialized
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000119 auto_init_if_empty(*output->info(), *input->info());
120
121 // Perform validation step
122 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), input_squared->info(), output->info(), norm_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123
Georgios Pinitas7f32d012018-10-11 18:41:19 +0100124 const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size();
125
126 const unsigned int norm_idx = get_normalization_dimension_index(input->info()->data_layout(), norm_info);
127 const bool is_norm_accross_width = norm_idx == 0;
128 const unsigned int border_width = is_norm_accross_width ? num_elems_processed_per_iteration - 1 : 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129
130 _input = input;
131 _input_squared = input_squared;
132 _output = output;
133 _norm_info = norm_info;
134 _border_size = BorderSize(0, border_width);
135
Pablo Tellodf246182017-07-03 16:25:09 +0100136 switch(_input->info()->data_type())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137 {
Pablo Tellodf246182017-07-03 16:25:09 +0100138 case DataType::F32:
139 {
Georgios Pinitase2220552018-07-20 13:23:44 +0100140 switch(norm_idx)
Pablo Tellodf246182017-07-03 16:25:09 +0100141 {
Georgios Pinitase2220552018-07-20 13:23:44 +0100142 case 0:
143 {
144 if(norm_info.type() == NormType::IN_MAP_2D)
145 {
146 _func = &NENormalizationLayerKernel::normalize_float<DataType::F32, 0, true>;
147 }
148 else
149 {
150 _func = &NENormalizationLayerKernel::normalize_float<DataType::F32, 0, false>;
151 }
Pablo Tellodf246182017-07-03 16:25:09 +0100152 break;
Georgios Pinitase2220552018-07-20 13:23:44 +0100153 }
154 case 2:
Pablo Tellodf246182017-07-03 16:25:09 +0100155 _func = &NENormalizationLayerKernel::normalize_float<DataType::F32, 2, false>;
156 break;
157 default:
Pablo Tellodf246182017-07-03 16:25:09 +0100158 break;
159 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160 break;
Pablo Tellodf246182017-07-03 16:25:09 +0100161 }
Gian Marco Iodicef2cde9b2018-08-23 15:29:16 +0100162#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tellodf246182017-07-03 16:25:09 +0100163 case DataType::F16:
164 {
Georgios Pinitase2220552018-07-20 13:23:44 +0100165 switch(norm_idx)
Pablo Tellodf246182017-07-03 16:25:09 +0100166 {
Georgios Pinitase2220552018-07-20 13:23:44 +0100167 case 0:
168 {
169 if(norm_info.type() == NormType::IN_MAP_2D)
170 {
171 _func = &NENormalizationLayerKernel::normalize_float<DataType::F16, 0, true>;
172 }
173 else
174 {
175 _func = &NENormalizationLayerKernel::normalize_float<DataType::F16, 0, false>;
176 }
Pablo Tellodf246182017-07-03 16:25:09 +0100177 break;
Georgios Pinitase2220552018-07-20 13:23:44 +0100178 }
179 case 2:
Pablo Tellodf246182017-07-03 16:25:09 +0100180 _func = &NENormalizationLayerKernel::normalize_float<DataType::F16, 2, false>;
181 break;
182 default:
Pablo Tellodf246182017-07-03 16:25:09 +0100183 break;
184 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185 break;
Pablo Tellodf246182017-07-03 16:25:09 +0100186 }
Gian Marco Iodicef2cde9b2018-08-23 15:29:16 +0100187#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100188 default:
189 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
190 }
191
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000192 // Configure kernel window
193 auto win_config = validate_and_configure_window(input->info(), input_squared->info(), output->info(), norm_info);
194 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
195 INEKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100196}
197
Pablo Tellodf246182017-07-03 16:25:09 +0100198template <DataType dt, unsigned int dim, bool do_2D_norm>
199void NENormalizationLayerKernel::normalize_float(const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100200{
201 Iterator input(_input, window);
202 Iterator input_squared(_input_squared, window);
203 Iterator output(_output, window);
204
205 const int dim_y = 1;
206 const int radius = _norm_info.norm_size() / 2;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100207 const int input_squared_stride = _input_squared->info()->strides_in_bytes()[dim];
208 // We account padding across X only and we iterate over rows
209 const int min_left = (dim == 2) ? 0 : -static_cast<int>(border_size().left);
Georgios Pinitas7f32d012018-10-11 18:41:19 +0100210 const int max_right = _input->info()->dimension(dim) - 1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100211 const int max_bottom = _input->info()->dimension(dim_y) - 1;
212
Pablo Tellodf246182017-07-03 16:25:09 +0100213 if(dt == DataType::F32)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100214 {
Pablo Tellodf246182017-07-03 16:25:09 +0100215 const float32x4_t coeff_vec = vdupq_n_f32(_norm_info.scale_coeff());
216 const float32x4_t beta_vec = vdupq_n_f32(_norm_info.beta());
217 const float32x4_t kappa_vec = vdupq_n_f32(_norm_info.kappa());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100218
Pablo Tellodf246182017-07-03 16:25:09 +0100219 execute_window_loop(window, [&](const Coordinates & id)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100220 {
Pablo Tellodf246182017-07-03 16:25:09 +0100221 // Get range to normalize
222 const int current_row = do_2D_norm ? id[dim_y] : 0;
223 const int current_slice = id[dim];
Georgios Pinitas67d94d22018-10-09 18:48:37 +0100224 const int first_row = do_2D_norm ? std::max(current_row - radius, 0) : 0;
Pablo Tellodf246182017-07-03 16:25:09 +0100225 const int last_row = do_2D_norm ? std::min(current_row + radius, max_bottom) : 0;
226 const int first_slice = std::max(current_slice - radius, min_left);
227 const int last_slice = std::min(current_slice + radius, max_right);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100228
Pablo Tellodf246182017-07-03 16:25:09 +0100229 // Accumulate 2D In-Map values
230 float32x4_t accu = vdupq_n_f32(0.f);
231 for(int j = first_row; j <= last_row; j++)
232 {
233 // Compute row displacement
234 const int row = (j - current_row) * _input_squared->info()->strides_in_bytes()[dim_y];
235 const uint8_t *const input_squared_ptr = input_squared.ptr() + row - (current_slice * input_squared_stride);
236 for(int i = first_slice; i <= last_slice; ++i)
237 {
238 accu = vaddq_f32(accu, vld1q_f32(reinterpret_cast<const float *>(input_squared_ptr + i * input_squared_stride)));
239 }
240 }
241
242 // Normalize
243 const float32x4_t normalized = vpowq_f32(vmlaq_f32(kappa_vec, coeff_vec, accu), beta_vec);
244 const float32x4_t normalized_pixel = vmulq_f32(vld1q_f32(reinterpret_cast<const float *>(input.ptr())), vinvq_f32(normalized));
245 vst1q_f32(reinterpret_cast<float *>(output.ptr()), normalized_pixel);
246 },
247 input, input_squared, output);
248 }
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000249#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tellodf246182017-07-03 16:25:09 +0100250 else if(dt == DataType::F16)
251 {
252 const float16x8_t coeff_vec = vdupq_n_f16(_norm_info.scale_coeff());
253 const float16x8_t beta_vec_f16 = vdupq_n_f16(_norm_info.beta());
254 const float16x8_t kappa_vec = vdupq_n_f16(_norm_info.kappa());
255
256 execute_window_loop(window, [&](const Coordinates & id)
257 {
258 // Get range to normalize
259 const int current_row = do_2D_norm ? id[dim_y] : 0;
260 const int current_slice = id[dim];
Georgios Pinitas67d94d22018-10-09 18:48:37 +0100261 const int first_row = do_2D_norm ? std::max(current_row - radius, 0) : 0;
Pablo Tellodf246182017-07-03 16:25:09 +0100262 const int last_row = do_2D_norm ? std::min(current_row + radius, max_bottom) : 0;
263 const int first_slice = std::max(current_slice - radius, min_left);
264 const int last_slice = std::min(current_slice + radius, max_right);
265
266 // Accumulate 2D In-Map values
267 float16x8_t accu = vdupq_n_f16(0.f);
268 for(int j = first_row; j <= last_row; j++)
269 {
270 // Compute row displacement
271 const int row = (j - current_row) * _input_squared->info()->strides_in_bytes()[dim_y];
272 const uint8_t *const input_squared_ptr = input_squared.ptr() + row - (current_slice * input_squared_stride);
273 for(int i = first_slice; i <= last_slice; ++i)
274 {
275 accu = vaddq_f16(accu, vld1q_f16(reinterpret_cast<const float16_t *>(input_squared_ptr + i * input_squared_stride)));
276 }
277 }
278
279 const float16x8_t norm_f16 = vpowq_f16(vaddq_f16(kappa_vec, vmulq_f16(coeff_vec, accu)), beta_vec_f16);
280 const float16x8_t normalized_pixel = vmulq_f16(vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr())), vinvq_f16(norm_f16));
281 vst1q_f16(reinterpret_cast<float16_t *>(output.ptr()), normalized_pixel);
282 },
283 input, input_squared, output);
284 }
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000285#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tellodf246182017-07-03 16:25:09 +0100286 else
287 {
288 ARM_COMPUTE_ERROR("Not supported");
289 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100290}
291
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000292Status NENormalizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *input_squared, const ITensorInfo *output, const NormalizationLayerInfo norm_info)
293{
294 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, input_squared, output, norm_info));
295 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), input_squared->clone().get(), output->clone().get(), norm_info).first);
296
297 return Status{};
298}
299
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100300void NENormalizationLayerKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100301{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100302 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100303 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
304 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
305 ARM_COMPUTE_ERROR_ON(_func == nullptr);
306
307 // Run function
308 (this->*_func)(window);
309}