blob: f1c5d3f6e6abd8de7ecbee9974f10c8ad8081ea8 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sheri Zhangac6499a2021-02-10 15:32:38 +00002 * Copyright (c) 2017-2021 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 */
Michalis Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NENormalizationLayerKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/TensorInfo.h"
29#include "arm_compute/core/Utils.h"
30#include "arm_compute/core/Validate.h"
31#include "arm_compute/core/Window.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/core/AccessWindowStatic.h"
33#include "src/core/CPP/Validate.h"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010034#include "src/core/NEON/NEFixedPoint.h"
35#include "src/core/NEON/NEMath.h"
36#include "src/core/NEON/wrapper/wrapper.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010037#include "src/core/helpers/AutoConfiguration.h"
38#include "src/core/helpers/NormalizationHelpers.h"
39#include "src/core/helpers/WindowHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040
Manuel Bottinif2c714b2020-06-15 16:50:35 +010041namespace arm_compute
42{
Michalis Spyrouafa5d812017-11-30 14:25:57 +000043namespace
44{
45Status validate_arguments(const ITensorInfo *input, const ITensorInfo *input_squared, const ITensorInfo *output, const NormalizationLayerInfo &norm_info)
46{
47 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, input_squared, output);
Anthony Barbiereaefd002018-07-20 17:49:35 +010048 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010049 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
Michalis Spyrouafa5d812017-11-30 14:25:57 +000050
51 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, input_squared);
52 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, input_squared);
53 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!(norm_info.norm_size() % 2), "Normalization size should be odd");
54
Michalis Spyrouafa5d812017-11-30 14:25:57 +000055 // Checks performed when output is configured
56 if(output->total_size() != 0)
57 {
58 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
59 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
Michalis Spyrou0c71d0b2018-11-22 11:22:18 +000060 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
Michalis Spyrouafa5d812017-11-30 14:25:57 +000061 }
62
63 return Status{};
64}
65
Michalis Spyrouafa5d812017-11-30 14:25:57 +000066} // namespace
67
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068NENormalizationLayerKernel::NENormalizationLayerKernel()
Manuel Bottinif2c714b2020-06-15 16:50:35 +010069 : _func(nullptr), _input(nullptr), _input_squared(nullptr), _output(nullptr), _norm_info(NormType::IN_MAP_1D)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070{
71}
72
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073void NENormalizationLayerKernel::configure(const ITensor *input, const ITensor *input_squared, ITensor *output, NormalizationLayerInfo norm_info)
74{
Michalis Spyrouafa5d812017-11-30 14:25:57 +000075 ARM_COMPUTE_ERROR_ON_NULLPTR(input, input_squared, output);
Georgios Pinitas09004ca2017-07-03 17:30:14 +010076 // Output tensor auto initialization if not yet initialized
Michalis Spyrouafa5d812017-11-30 14:25:57 +000077 auto_init_if_empty(*output->info(), *input->info());
78
79 // Perform validation step
80 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), input_squared->info(), output->info(), norm_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010081
Manuel Bottinif2c714b2020-06-15 16:50:35 +010082 const unsigned int norm_idx = get_normalization_dimension_index(input->info()->data_layout(), norm_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083
84 _input = input;
85 _input_squared = input_squared;
86 _output = output;
87 _norm_info = norm_info;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088
Pablo Tellodf246182017-07-03 16:25:09 +010089 switch(_input->info()->data_type())
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090 {
Pablo Tellodf246182017-07-03 16:25:09 +010091 case DataType::F32:
92 {
Georgios Pinitase2220552018-07-20 13:23:44 +010093 switch(norm_idx)
Pablo Tellodf246182017-07-03 16:25:09 +010094 {
Georgios Pinitase2220552018-07-20 13:23:44 +010095 case 0:
96 {
97 if(norm_info.type() == NormType::IN_MAP_2D)
98 {
Michalis Spyrou0c71d0b2018-11-22 11:22:18 +000099 _func = &NENormalizationLayerKernel::normalize_float<float, 4, 0, true>;
Georgios Pinitase2220552018-07-20 13:23:44 +0100100 }
101 else
102 {
Michalis Spyrou0c71d0b2018-11-22 11:22:18 +0000103 _func = &NENormalizationLayerKernel::normalize_float<float, 4, 0, false>;
Georgios Pinitase2220552018-07-20 13:23:44 +0100104 }
Pablo Tellodf246182017-07-03 16:25:09 +0100105 break;
Georgios Pinitase2220552018-07-20 13:23:44 +0100106 }
Michalis Spyrou0c71d0b2018-11-22 11:22:18 +0000107 case 1:
108 if(norm_info.type() == NormType::IN_MAP_2D)
109 {
110 _func = &NENormalizationLayerKernel::normalize_float<float, 4, 1, true>;
111 }
112 else
113 {
114 _func = &NENormalizationLayerKernel::normalize_float<float, 4, 1, false>;
115 }
116 break;
Georgios Pinitase2220552018-07-20 13:23:44 +0100117 case 2:
Michalis Spyrou0c71d0b2018-11-22 11:22:18 +0000118 _func = &NENormalizationLayerKernel::normalize_float<float, 4, 2, false>;
Pablo Tellodf246182017-07-03 16:25:09 +0100119 break;
120 default:
Pablo Tellodf246182017-07-03 16:25:09 +0100121 break;
122 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123 break;
Pablo Tellodf246182017-07-03 16:25:09 +0100124 }
Gian Marco Iodicef2cde9b2018-08-23 15:29:16 +0100125#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tellodf246182017-07-03 16:25:09 +0100126 case DataType::F16:
127 {
Georgios Pinitase2220552018-07-20 13:23:44 +0100128 switch(norm_idx)
Pablo Tellodf246182017-07-03 16:25:09 +0100129 {
Georgios Pinitase2220552018-07-20 13:23:44 +0100130 case 0:
131 {
132 if(norm_info.type() == NormType::IN_MAP_2D)
133 {
Michalis Spyrou0c71d0b2018-11-22 11:22:18 +0000134 _func = &NENormalizationLayerKernel::normalize_float<float16_t, 8, 0, true>;
Georgios Pinitase2220552018-07-20 13:23:44 +0100135 }
136 else
137 {
Michalis Spyrou0c71d0b2018-11-22 11:22:18 +0000138 _func = &NENormalizationLayerKernel::normalize_float<float16_t, 8, 0, false>;
Georgios Pinitase2220552018-07-20 13:23:44 +0100139 }
Pablo Tellodf246182017-07-03 16:25:09 +0100140 break;
Georgios Pinitase2220552018-07-20 13:23:44 +0100141 }
Michalis Spyrou0c71d0b2018-11-22 11:22:18 +0000142 case 1:
143 if(norm_info.type() == NormType::IN_MAP_2D)
144 {
145 _func = &NENormalizationLayerKernel::normalize_float<float16_t, 8, 1, true>;
146 }
147 else
148 {
149 _func = &NENormalizationLayerKernel::normalize_float<float16_t, 8, 1, false>;
150 }
151 break;
Georgios Pinitase2220552018-07-20 13:23:44 +0100152 case 2:
Michalis Spyrou0c71d0b2018-11-22 11:22:18 +0000153 _func = &NENormalizationLayerKernel::normalize_float<float16_t, 8, 2, false>;
Pablo Tellodf246182017-07-03 16:25:09 +0100154 break;
155 default:
Pablo Tellodf246182017-07-03 16:25:09 +0100156 break;
157 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158 break;
Pablo Tellodf246182017-07-03 16:25:09 +0100159 }
Gian Marco Iodicef2cde9b2018-08-23 15:29:16 +0100160#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161 default:
162 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
163 }
164
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000165 // Configure kernel window
SiCongLib88272e2021-02-24 15:40:57 +0000166 Window win = calculate_max_window(*input->info(), Steps());
Manuel Bottinif2c714b2020-06-15 16:50:35 +0100167 INEKernel::configure(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100168}
169
Michalis Spyrou0c71d0b2018-11-22 11:22:18 +0000170template <typename T, unsigned int S, unsigned int dim, bool do_2D_norm>
Pablo Tellodf246182017-07-03 16:25:09 +0100171void NENormalizationLayerKernel::normalize_float(const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172{
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +0000173 /** SIMD vector tag type. */
Michalis Spyrou0c71d0b2018-11-22 11:22:18 +0000174 using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
175
Manuel Bottinif2c714b2020-06-15 16:50:35 +0100176 Window win(window);
177 win.set(Window::DimX, Window::Dimension(0, 1, 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100178
Manuel Bottinif2c714b2020-06-15 16:50:35 +0100179 const auto window_start_x = static_cast<int>(window.x().start());
180 const auto window_end_x = static_cast<int>(window.x().end());
181 const int window_step_x = S;
182
183 Iterator input(_input, win);
184 Iterator input_squared(_input_squared, win);
185 Iterator output(_output, win);
186
187 const int dim_y = _input->info()->data_layout() == DataLayout::NCHW ? 1 : 2;
188 const int radius = _norm_info.norm_size() / 2;
189 const int input_squared_stride_x = _input_squared->info()->strides_in_bytes()[0];
190 const int input_squared_stride_slice = _input_squared->info()->strides_in_bytes()[dim];
191 const int input_squared_stride_row = _input_squared->info()->strides_in_bytes()[dim_y];
192
Georgios Pinitas7f32d012018-10-11 18:41:19 +0100193 const int max_right = _input->info()->dimension(dim) - 1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100194 const int max_bottom = _input->info()->dimension(dim_y) - 1;
195
Michalis Spyrou0c71d0b2018-11-22 11:22:18 +0000196 const auto coeff_vec = wrapper::vdup_n(static_cast<T>(_norm_info.scale_coeff()), ExactTagType{});
197 const auto beta_vec = wrapper::vdup_n(static_cast<T>(_norm_info.beta()), ExactTagType{});
198 const auto kappa_vec = wrapper::vdup_n(static_cast<T>(_norm_info.kappa()), ExactTagType{});
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100199
Manuel Bottinif2c714b2020-06-15 16:50:35 +0100200 auto sequential_normalization = [&](const int x, const Coordinates & id, const int current_row, const int first_row, const int last_row, const T * input_ptr, const uint8_t *input_squared_start_ptr,
201 T * output_ptr)
Michalis Spyrou0c71d0b2018-11-22 11:22:18 +0000202 {
Manuel Bottinif2c714b2020-06-15 16:50:35 +0100203 const int current_slice = dim == 0 ? x : id[dim];
204 const int first_slice = std::max(current_slice - radius, 0);
Michalis Spyrou0c71d0b2018-11-22 11:22:18 +0000205 const int last_slice = std::min(current_slice + radius, max_right);
206
Manuel Bottinif2c714b2020-06-15 16:50:35 +0100207 const uint8_t *const input_squared_x_ptr = input_squared_start_ptr + x * input_squared_stride_x;
Michalis Spyrou0c71d0b2018-11-22 11:22:18 +0000208 // Accumulate 2D In-Map values
Manuel Bottinif2c714b2020-06-15 16:50:35 +0100209 auto accu = static_cast<T>(0.f);
210 for(int j = first_row; j <= last_row; ++j)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100211 {
Michalis Spyrou0c71d0b2018-11-22 11:22:18 +0000212 // Compute row displacement
Manuel Bottinif2c714b2020-06-15 16:50:35 +0100213 const uint8_t *const input_squared_ptr = input_squared_x_ptr + (j - current_row) * input_squared_stride_row;
Michalis Spyrou0c71d0b2018-11-22 11:22:18 +0000214 for(int i = first_slice; i <= last_slice; ++i)
Pablo Tellodf246182017-07-03 16:25:09 +0100215 {
Manuel Bottinif2c714b2020-06-15 16:50:35 +0100216 accu += *reinterpret_cast<const T *>(input_squared_ptr + (i - current_slice) * input_squared_stride_slice);
Pablo Tellodf246182017-07-03 16:25:09 +0100217 }
Michalis Spyrou0c71d0b2018-11-22 11:22:18 +0000218 }
Pablo Tellodf246182017-07-03 16:25:09 +0100219
Michalis Spyrou0c71d0b2018-11-22 11:22:18 +0000220 // Normalize
Manuel Bottinif2c714b2020-06-15 16:50:35 +0100221 const auto normalized = std::pow(accu * static_cast<T>(_norm_info.scale_coeff()) + static_cast<T>(_norm_info.kappa()), _norm_info.beta());
222 const auto normalized_pixel = (*(input_ptr + x)) / normalized;
223 *(output_ptr + x) = normalized_pixel;
224 };
225
226 execute_window_loop(win, [&](const Coordinates & id)
227 {
228 const auto input_ptr = reinterpret_cast<const T *>(input.ptr());
229 auto output_ptr = reinterpret_cast<T *>(output.ptr());
230
231 // Get range to normalize
232 const int current_row = do_2D_norm ? id[dim_y] : 0;
233 const int first_row = do_2D_norm ? std::max(current_row - radius, 0) : 0;
234 const int last_row = do_2D_norm ? std::min(current_row + radius, max_bottom) : 0;
235
236 int x = window_start_x;
237 // Compute serially starting elements for the case x dimension is width
238 for(; x < radius && x < window_end_x && dim == 0; ++x)
239 {
240 sequential_normalization(x, id, current_row, first_row, last_row, input_ptr, input_squared.ptr(), output_ptr);
241 }
242
243 // Compute vectorized
244 for(; x <= window_end_x - window_step_x - radius; x += window_step_x)
245 {
246 const int current_slice = dim == 0 ? x : id[dim];
247 const int first_slice = std::max(current_slice - radius, 0);
248 const int last_slice = std::min(current_slice + radius, max_right);
249
250 const uint8_t *const input_squared_x_ptr = input_squared.ptr() + x * input_squared_stride_x;
251 // Accumulate 2D In-Map values
252 auto accu = wrapper::vdup_n(static_cast<T>(0.f), ExactTagType{});
253 for(int j = first_row; j <= last_row; ++j)
254 {
255 // Compute row displacement
256 const uint8_t *const input_squared_ptr = input_squared_x_ptr + (j - current_row) * input_squared_stride_row;
257 for(int i = first_slice; i <= last_slice; ++i)
258 {
259 accu = wrapper::vadd(accu, wrapper::vloadq(reinterpret_cast<const T *>(input_squared_ptr + (i - current_slice) * input_squared_stride_slice)));
260 }
261 }
262
263 // Normalize
264 const auto normalized = wrapper::vpow(wrapper::vmla(kappa_vec, coeff_vec, accu), beta_vec);
265 const auto normalized_pixel = wrapper::vmul(wrapper::vloadq(input_ptr + x), wrapper::vinv(normalized));
266 wrapper::vstore(reinterpret_cast<T *>(output_ptr + x), normalized_pixel);
267 }
268
269 // Compute left-over elements
270 for(; x < window_end_x; ++x)
271 {
272 sequential_normalization(x, id, current_row, first_row, last_row, input_ptr, input_squared.ptr(), output_ptr);
273 }
Michalis Spyrou0c71d0b2018-11-22 11:22:18 +0000274 },
275 input, input_squared, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100276}
277
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000278Status NENormalizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *input_squared, const ITensorInfo *output, const NormalizationLayerInfo norm_info)
279{
280 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, input_squared, output, norm_info));
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000281
282 return Status{};
283}
284
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100285void NENormalizationLayerKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100286{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100287 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100288 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
289 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
290 ARM_COMPUTE_ERROR_ON(_func == nullptr);
291
292 // Run function
293 (this->*_func)(window);
294}
Sheri Zhangac6499a2021-02-10 15:32:38 +0000295} // namespace arm_compute