2 * Copyright (c) 2017 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #include "arm_compute/core/NEON/kernels/NENormalizationLayerKernel.h"
26 #include "arm_compute/core/Helpers.h"
27 #include "arm_compute/core/NEON/NEMath.h"
28 #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"
33 using namespace arm_compute;
35 NENormalizationLayerKernel::NENormalizationLayerKernel()
36 : _func(nullptr), _input(nullptr), _input_squared(nullptr), _output(nullptr), _norm_info(NormType::IN_MAP), _border_size()
40 BorderSize NENormalizationLayerKernel::border_size() const
45 void NENormalizationLayerKernel::configure(const ITensor *input, const ITensor *input_squared, ITensor *output, NormalizationLayerInfo norm_info)
47 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
48 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F32);
49 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
50 ARM_COMPUTE_ERROR_ON_MSG(!(norm_info.norm_size() % 2), "Normalization size should be odd");
52 const unsigned int border_width = (_norm_info.type() == NormType::IN_MAP) ? 3 : 0;
55 _input_squared = input_squared;
57 _norm_info = norm_info;
58 _func = (norm_info.type() == NormType::IN_MAP) ? &NENormalizationLayerKernel::normalize<0> : &NENormalizationLayerKernel::normalize<2>;
59 _border_size = BorderSize(0, border_width);
61 const unsigned int num_elems_processed_per_iteration = 4;
62 const unsigned int num_elems_read_per_iteration = num_elems_processed_per_iteration + 2 * (norm_info.norm_size() / 2);
64 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
66 AccessWindowHorizontal input_access(input->info(), -_border_size.left, num_elems_read_per_iteration);
67 AccessWindowHorizontal input_squared_access(input_squared->info(), -_border_size.left, num_elems_read_per_iteration);
68 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
70 update_window_and_padding(win, input_access, input_squared_access, output_access);
72 output_access.set_valid_region(win, input->info()->valid_region());
74 INEKernel::configure(win);
77 template <unsigned int dim>
78 void NENormalizationLayerKernel::normalize(const Window &window)
80 Iterator input(_input, window);
81 Iterator input_squared(_input_squared, window);
82 Iterator output(_output, window);
84 const int radius = _norm_info.norm_size() / 2;
85 const int total_size = _input->info()->dimension(dim) - 1;
86 const int input_squared_stride = _input_squared->info()->strides_in_bytes()[dim];
87 // We account padding when we normalize across X
88 const int min_left = (dim == 0) ? -static_cast<int>(border_size().left) : 0;
89 const int max_right = (dim == 0) ? total_size + border_size().left : total_size;
91 const float32x4_t coeff_vec = vdupq_n_f32(_norm_info.scale_coeff());
92 const float32x4_t beta_vec = vdupq_n_f32(_norm_info.beta());
93 const float32x4_t kappa_vec = vdupq_n_f32(_norm_info.kappa());
95 execute_window_loop(window, [&](const Coordinates & id)
97 // Get range to normalize
98 const int current_slice = id[dim];
99 const int first_slice = std::max(current_slice - radius, min_left);
100 const int last_slice = std::min(current_slice + radius, max_right);
102 // Accumulate cross map values
103 float32x4_t accu = vdupq_n_f32(0.f);
104 for(int i = first_slice; i <= last_slice; ++i)
106 accu = vaddq_f32(accu, vld1q_f32(reinterpret_cast<float *>(input_squared.ptr() + (i - current_slice) * input_squared_stride)));
110 const float32x4_t normalized = vpowq_f32(vmlaq_f32(kappa_vec, coeff_vec, accu), beta_vec);
111 const float32x4_t normalized_pixel = vmulq_f32(vld1q_f32(reinterpret_cast<float *>(input.ptr())), vinv_f32(normalized));
112 vst1q_f32(reinterpret_cast<float *>(output.ptr()), normalized_pixel);
114 input, input_squared, output);
117 void NENormalizationLayerKernel::run(const Window &window)
119 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
120 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
121 ARM_COMPUTE_ERROR_ON(_func == nullptr);
124 (this->*_func)(window);