blob: fe6b69c455b36785e3d74796bc78a5d78c88a812 [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
Anthony Barbiereaefd002018-07-20 17:49:35 +010026#include "arm_compute/core/CPP/Validate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/NEON/NEFixedPoint.h"
30#include "arm_compute/core/NEON/NEMath.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Utils.h"
33#include "arm_compute/core/Validate.h"
34#include "arm_compute/core/Window.h"
35
36using namespace arm_compute;
37
Michalis Spyrouafa5d812017-11-30 14:25:57 +000038namespace
39{
40Status validate_arguments(const ITensorInfo *input, const ITensorInfo *input_squared, const ITensorInfo *output, const NormalizationLayerInfo &norm_info)
41{
42 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, input_squared, output);
Anthony Barbiereaefd002018-07-20 17:49:35 +010043 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010044 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
Michalis Spyrouafa5d812017-11-30 14:25:57 +000045
Georgios Pinitase2220552018-07-20 13:23:44 +010046 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_layout() == DataLayout::NHWC && norm_info.type() == NormType::IN_MAP_2D,
47 "Only Cross-map and 1D In-map normalization is supported for NHWC layout");
Michalis Spyrouafa5d812017-11-30 14:25:57 +000048 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, input_squared);
49 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, input_squared);
50 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!(norm_info.norm_size() % 2), "Normalization size should be odd");
51
Michalis Spyrouafa5d812017-11-30 14:25:57 +000052 // Checks performed when output is configured
53 if(output->total_size() != 0)
54 {
55 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
56 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
Michalis Spyrouafa5d812017-11-30 14:25:57 +000057 }
58
59 return Status{};
60}
61
62std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *input_squared, ITensorInfo *output, const NormalizationLayerInfo &norm_info)
63{
64 unsigned int num_elems_processed_per_iteration = 16 / input->element_size();
65 const unsigned int num_elems_read_per_iteration = num_elems_processed_per_iteration + 2 * (norm_info.norm_size() / 2);
Georgios Pinitase2220552018-07-20 13:23:44 +010066 const unsigned int norm_idx = get_normalization_dimension_index(input->data_layout(), norm_info);
Michalis Spyrouafa5d812017-11-30 14:25:57 +000067 const unsigned int num_rows = (norm_info.type() == NormType::IN_MAP_2D) ? norm_info.norm_size() : 1;
Georgios Pinitase2220552018-07-20 13:23:44 +010068 const unsigned int border_width = (norm_idx == 2) ? 0 : std::min<unsigned int>(norm_info.norm_size() / 2, 3U);
Michalis Spyrouafa5d812017-11-30 14:25:57 +000069 BorderSize border_size = BorderSize(0, border_width);
70 bool window_changed = false;
71
72 // Configure window
73 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
74
75 AccessWindowRectangle input_access(input, -border_size.left, 0, num_elems_read_per_iteration, num_rows);
76 AccessWindowRectangle input_squared_access(input_squared, -border_size.left, 0, num_elems_read_per_iteration, num_rows);
77
78 if(output->total_size() != 0)
79 {
80 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
81 window_changed = update_window_and_padding(win, input_access, input_squared_access, output_access);
82 output_access.set_valid_region(win, input->valid_region());
83 }
84 else
85 {
86 window_changed = update_window_and_padding(win, input_access, input_squared_access);
87 }
88
89 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
90 return std::make_pair(err, win);
91}
92} // namespace
93
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094NENormalizationLayerKernel::NENormalizationLayerKernel()
95 : _func(nullptr), _input(nullptr), _input_squared(nullptr), _output(nullptr), _norm_info(NormType::IN_MAP_1D), _border_size()
96{
97}
98
99BorderSize NENormalizationLayerKernel::border_size() const
100{
101 return _border_size;
102}
103
104void NENormalizationLayerKernel::configure(const ITensor *input, const ITensor *input_squared, ITensor *output, NormalizationLayerInfo norm_info)
105{
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000106 ARM_COMPUTE_ERROR_ON_NULLPTR(input, input_squared, output);
Georgios Pinitas09004ca2017-07-03 17:30:14 +0100107 // Output tensor auto initialization if not yet initialized
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000108 auto_init_if_empty(*output->info(), *input->info());
109
110 // Perform validation step
111 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), input_squared->info(), output->info(), norm_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112
Georgios Pinitase2220552018-07-20 13:23:44 +0100113 const unsigned int norm_idx = get_normalization_dimension_index(input->info()->data_layout(), norm_info);
114 const unsigned int border_width = (norm_idx == 2) ? 0 : std::min<unsigned int>(norm_info.norm_size() / 2, 3U);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115
116 _input = input;
117 _input_squared = input_squared;
118 _output = output;
119 _norm_info = norm_info;
120 _border_size = BorderSize(0, border_width);
121
Pablo Tellodf246182017-07-03 16:25:09 +0100122 switch(_input->info()->data_type())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123 {
Pablo Tellodf246182017-07-03 16:25:09 +0100124 case DataType::F32:
125 {
Georgios Pinitase2220552018-07-20 13:23:44 +0100126 switch(norm_idx)
Pablo Tellodf246182017-07-03 16:25:09 +0100127 {
Georgios Pinitase2220552018-07-20 13:23:44 +0100128 case 0:
129 {
130 if(norm_info.type() == NormType::IN_MAP_2D)
131 {
132 _func = &NENormalizationLayerKernel::normalize_float<DataType::F32, 0, true>;
133 }
134 else
135 {
136 _func = &NENormalizationLayerKernel::normalize_float<DataType::F32, 0, false>;
137 }
Pablo Tellodf246182017-07-03 16:25:09 +0100138 break;
Georgios Pinitase2220552018-07-20 13:23:44 +0100139 }
140 case 2:
Pablo Tellodf246182017-07-03 16:25:09 +0100141 _func = &NENormalizationLayerKernel::normalize_float<DataType::F32, 2, false>;
142 break;
143 default:
Pablo Tellodf246182017-07-03 16:25:09 +0100144 break;
145 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146 break;
Pablo Tellodf246182017-07-03 16:25:09 +0100147 }
Gian Marco Iodicef2cde9b2018-08-23 15:29:16 +0100148#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tellodf246182017-07-03 16:25:09 +0100149 case DataType::F16:
150 {
Georgios Pinitase2220552018-07-20 13:23:44 +0100151 switch(norm_idx)
Pablo Tellodf246182017-07-03 16:25:09 +0100152 {
Georgios Pinitase2220552018-07-20 13:23:44 +0100153 case 0:
154 {
155 if(norm_info.type() == NormType::IN_MAP_2D)
156 {
157 _func = &NENormalizationLayerKernel::normalize_float<DataType::F16, 0, true>;
158 }
159 else
160 {
161 _func = &NENormalizationLayerKernel::normalize_float<DataType::F16, 0, false>;
162 }
Pablo Tellodf246182017-07-03 16:25:09 +0100163 break;
Georgios Pinitase2220552018-07-20 13:23:44 +0100164 }
165 case 2:
Pablo Tellodf246182017-07-03 16:25:09 +0100166 _func = &NENormalizationLayerKernel::normalize_float<DataType::F16, 2, false>;
167 break;
168 default:
Pablo Tellodf246182017-07-03 16:25:09 +0100169 break;
170 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100171 break;
Pablo Tellodf246182017-07-03 16:25:09 +0100172 }
Gian Marco Iodicef2cde9b2018-08-23 15:29:16 +0100173#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174 default:
175 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
176 }
177
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000178 // Configure kernel window
179 auto win_config = validate_and_configure_window(input->info(), input_squared->info(), output->info(), norm_info);
180 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
181 INEKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100182}
183
Pablo Tellodf246182017-07-03 16:25:09 +0100184template <DataType dt, unsigned int dim, bool do_2D_norm>
185void NENormalizationLayerKernel::normalize_float(const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100186{
187 Iterator input(_input, window);
188 Iterator input_squared(_input_squared, window);
189 Iterator output(_output, window);
190
191 const int dim_y = 1;
192 const int radius = _norm_info.norm_size() / 2;
193 const int total_size = _input->info()->dimension(dim) - 1;
194 const int input_squared_stride = _input_squared->info()->strides_in_bytes()[dim];
195 // We account padding across X only and we iterate over rows
196 const int min_left = (dim == 2) ? 0 : -static_cast<int>(border_size().left);
197 const int max_right = (dim == 2) ? total_size : total_size + border_size().left;
198 const int min_top = 0;
199 const int max_bottom = _input->info()->dimension(dim_y) - 1;
200
Pablo Tellodf246182017-07-03 16:25:09 +0100201 if(dt == DataType::F32)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100202 {
Pablo Tellodf246182017-07-03 16:25:09 +0100203 const float32x4_t coeff_vec = vdupq_n_f32(_norm_info.scale_coeff());
204 const float32x4_t beta_vec = vdupq_n_f32(_norm_info.beta());
205 const float32x4_t kappa_vec = vdupq_n_f32(_norm_info.kappa());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100206
Pablo Tellodf246182017-07-03 16:25:09 +0100207 execute_window_loop(window, [&](const Coordinates & id)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100208 {
Pablo Tellodf246182017-07-03 16:25:09 +0100209 // Get range to normalize
210 const int current_row = do_2D_norm ? id[dim_y] : 0;
211 const int current_slice = id[dim];
212 const int first_row = do_2D_norm ? std::max(current_row - radius, min_top) : 0;
213 const int last_row = do_2D_norm ? std::min(current_row + radius, max_bottom) : 0;
214 const int first_slice = std::max(current_slice - radius, min_left);
215 const int last_slice = std::min(current_slice + radius, max_right);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100216
Pablo Tellodf246182017-07-03 16:25:09 +0100217 // Accumulate 2D In-Map values
218 float32x4_t accu = vdupq_n_f32(0.f);
219 for(int j = first_row; j <= last_row; j++)
220 {
221 // Compute row displacement
222 const int row = (j - current_row) * _input_squared->info()->strides_in_bytes()[dim_y];
223 const uint8_t *const input_squared_ptr = input_squared.ptr() + row - (current_slice * input_squared_stride);
224 for(int i = first_slice; i <= last_slice; ++i)
225 {
226 accu = vaddq_f32(accu, vld1q_f32(reinterpret_cast<const float *>(input_squared_ptr + i * input_squared_stride)));
227 }
228 }
229
230 // Normalize
231 const float32x4_t normalized = vpowq_f32(vmlaq_f32(kappa_vec, coeff_vec, accu), beta_vec);
232 const float32x4_t normalized_pixel = vmulq_f32(vld1q_f32(reinterpret_cast<const float *>(input.ptr())), vinvq_f32(normalized));
233 vst1q_f32(reinterpret_cast<float *>(output.ptr()), normalized_pixel);
234 },
235 input, input_squared, output);
236 }
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000237#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tellodf246182017-07-03 16:25:09 +0100238 else if(dt == DataType::F16)
239 {
240 const float16x8_t coeff_vec = vdupq_n_f16(_norm_info.scale_coeff());
241 const float16x8_t beta_vec_f16 = vdupq_n_f16(_norm_info.beta());
242 const float16x8_t kappa_vec = vdupq_n_f16(_norm_info.kappa());
243
244 execute_window_loop(window, [&](const Coordinates & id)
245 {
246 // Get range to normalize
247 const int current_row = do_2D_norm ? id[dim_y] : 0;
248 const int current_slice = id[dim];
249 const int first_row = do_2D_norm ? std::max(current_row - radius, min_top) : 0;
250 const int last_row = do_2D_norm ? std::min(current_row + radius, max_bottom) : 0;
251 const int first_slice = std::max(current_slice - radius, min_left);
252 const int last_slice = std::min(current_slice + radius, max_right);
253
254 // Accumulate 2D In-Map values
255 float16x8_t accu = vdupq_n_f16(0.f);
256 for(int j = first_row; j <= last_row; j++)
257 {
258 // Compute row displacement
259 const int row = (j - current_row) * _input_squared->info()->strides_in_bytes()[dim_y];
260 const uint8_t *const input_squared_ptr = input_squared.ptr() + row - (current_slice * input_squared_stride);
261 for(int i = first_slice; i <= last_slice; ++i)
262 {
263 accu = vaddq_f16(accu, vld1q_f16(reinterpret_cast<const float16_t *>(input_squared_ptr + i * input_squared_stride)));
264 }
265 }
266
267 const float16x8_t norm_f16 = vpowq_f16(vaddq_f16(kappa_vec, vmulq_f16(coeff_vec, accu)), beta_vec_f16);
268 const float16x8_t normalized_pixel = vmulq_f16(vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr())), vinvq_f16(norm_f16));
269 vst1q_f16(reinterpret_cast<float16_t *>(output.ptr()), normalized_pixel);
270 },
271 input, input_squared, output);
272 }
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000273#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tellodf246182017-07-03 16:25:09 +0100274 else
275 {
276 ARM_COMPUTE_ERROR("Not supported");
277 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100278}
279
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000280Status NENormalizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *input_squared, const ITensorInfo *output, const NormalizationLayerInfo norm_info)
281{
282 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, input_squared, output, norm_info));
283 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), input_squared->clone().get(), output->clone().get(), norm_info).first);
284
285 return Status{};
286}
287
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100288void NENormalizationLayerKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100289{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100290 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100291 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
292 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
293 ARM_COMPUTE_ERROR_ON(_func == nullptr);
294
295 // Run function
296 (this->*_func)(window);
297}