blob: 9bda82d4167633187b4f5146c359d73e46fa0ac7 [file] [log] [blame]
Georgios Pinitasd9769582017-08-03 10:19:40 +01001/*
SiCongLib88272e2021-02-24 15:40:57 +00002 * Copyright (c) 2017-2021 Arm Limited.
Georgios Pinitasd9769582017-08-03 10:19:40 +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/NEL2NormalizeLayerKernel.h"
Georgios Pinitasd9769582017-08-03 10:19:40 +010025
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
Georgios Pinitasd9769582017-08-03 10:19:40 +010029#include "arm_compute/core/TensorInfo.h"
30#include "arm_compute/core/Utils.h"
31#include "arm_compute/core/Validate.h"
32#include "arm_compute/core/Window.h"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010033#include "src/core/NEON/NEMath.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
Georgios Pinitasd9769582017-08-03 10:19:40 +010036
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010037#include "src/core/NEON/wrapper/wrapper.h"
Georgios Pinitasd9769582017-08-03 10:19:40 +010038#include <arm_neon.h>
39#include <cmath>
40
Michalis Spyrou2897e612018-11-20 18:38:29 +000041namespace arm_compute
42{
Georgios Pinitasd9769582017-08-03 10:19:40 +010043namespace
44{
Manuel Bottini4b5c5882019-05-14 10:38:30 +010045constexpr int max_input_tensor_dim = 3;
46
Michalis Spyrou2897e612018-11-20 18:38:29 +000047template <typename T, int S>
Georgios Pinitasd9769582017-08-03 10:19:40 +010048void l2_normalize_X(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window)
49{
Michalis Spyrou2897e612018-11-20 18:38:29 +000050 using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
51
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +010052 const int window_step_x = 16 / data_size_from_type(in->info()->data_type());
53 const auto window_start_x = static_cast<int>(window.x().start());
54 const auto window_end_x = static_cast<int>(window.x().end());
Georgios Pinitasd9769582017-08-03 10:19:40 +010055
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +010056 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
57 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
Georgios Pinitasd9769582017-08-03 10:19:40 +010058
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +010059 Iterator input_it(in, win_collapsed);
60 Iterator sum_it(sum, win_collapsed);
61 Iterator output_it(out, win_collapsed);
62
63 execute_window_loop(win_collapsed, [&](const Coordinates &)
Georgios Pinitasd9769582017-08-03 10:19:40 +010064 {
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +010065 const auto in_ptr = reinterpret_cast<const T *>(input_it.ptr());
66 const auto out_ptr = reinterpret_cast<T *>(output_it.ptr());
Georgios Pinitasd9769582017-08-03 10:19:40 +010067
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +010068 const T sum_value = *reinterpret_cast<const T *>(sum_it.ptr());
69 const T norm_value = static_cast<T>(1.f) / std::sqrt(std::max(sum_value, static_cast<T>(epsilon)));
70 const auto vec_norm_value = wrapper::vdup_n(norm_value, ExactTagType{});
Georgios Pinitasd9769582017-08-03 10:19:40 +010071
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +010072 // Compute elements over vector steps
73 int x = window_start_x;
74 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Georgios Pinitasd9769582017-08-03 10:19:40 +010075 {
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +010076 wrapper::vstore(out_ptr + x, wrapper::vmul(wrapper::vloadq(in_ptr + x), vec_norm_value));
77 }
Georgios Pinitasd9769582017-08-03 10:19:40 +010078
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +010079 // Compute left-over elements
80 for(; x < window_end_x; ++x)
81 {
82 out_ptr[x] = in_ptr[x] * norm_value;
83 }
84 },
85 input_it, sum_it, output_it);
Georgios Pinitasd9769582017-08-03 10:19:40 +010086}
John Richardson73d4aef2018-05-08 14:34:33 +010087
Michalis Spyrou2897e612018-11-20 18:38:29 +000088template <typename T, int S>
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +010089void l2_normalize_YZ(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window, size_t axis)
Michalis Spyrou2897e612018-11-20 18:38:29 +000090{
Michalis Spyrou2897e612018-11-20 18:38:29 +000091 using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
92
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +010093 const int window_step_x = 16 / data_size_from_type(in->info()->data_type());
94 const auto window_start_x = static_cast<int>(window.x().start());
95 const auto window_end_x = static_cast<int>(window.x().end());
Michalis Spyrou2897e612018-11-20 18:38:29 +000096
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +010097 Window win = window;
98 win.set(Window::DimX, Window::Dimension(0, 1, 1));
Michalis Spyrou2897e612018-11-20 18:38:29 +000099
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +0100100 Window window_sum(win);
101 window_sum.set(axis, Window::Dimension(0, 0, 0));
102
103 Iterator input_it(in, win);
104 Iterator sum_it(sum, window_sum);
105 Iterator output_it(out, win);
106
107 const auto vec_eps = wrapper::vdup_n(static_cast<T>(epsilon), ExactTagType{});
108
109 execute_window_loop(win, [&](const Coordinates &)
Michalis Spyrou2897e612018-11-20 18:38:29 +0000110 {
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +0100111 const auto in_ptr = reinterpret_cast<const T *>(input_it.ptr());
112 const auto sum_ptr = reinterpret_cast<const T *>(sum_it.ptr());
113 const auto out_ptr = reinterpret_cast<T *>(output_it.ptr());
Michalis Spyrou2897e612018-11-20 18:38:29 +0000114
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +0100115 // Compute elements over vector steps
116 int x = window_start_x;
117 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Michalis Spyrou2897e612018-11-20 18:38:29 +0000118 {
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +0100119 const auto vec_norm_value = wrapper::vinvsqrt(wrapper::vmax(wrapper::vloadq(sum_ptr + x), vec_eps));
120 wrapper::vstore(out_ptr + x, wrapper::vmul(wrapper::vloadq(in_ptr + x), vec_norm_value));
121 }
Michalis Spyrou2897e612018-11-20 18:38:29 +0000122
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +0100123 // Compute left-over elements
124 for(; x < window_end_x; ++x)
Michalis Spyrou2897e612018-11-20 18:38:29 +0000125 {
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +0100126 const T norm_value = static_cast<T>(1.f) / std::sqrt(std::max(sum_ptr[x], static_cast<T>(epsilon)));
127 out_ptr[x] = in_ptr[x] * norm_value;
128 }
129 },
130 input_it, sum_it, output_it);
Michalis Spyrou2897e612018-11-20 18:38:29 +0000131}
132
Manuel Bottini4b5c5882019-05-14 10:38:30 +0100133Status validate_arguments(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, int axis, float epsilon)
John Richardson73d4aef2018-05-08 14:34:33 +0100134{
135 ARM_COMPUTE_UNUSED(epsilon);
136
Manuel Bottini4b5c5882019-05-14 10:38:30 +0100137 const uint32_t actual_axis = wrap_around(axis, max_input_tensor_dim);
John Richardson73d4aef2018-05-08 14:34:33 +0100138 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, sum, output);
139 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum);
Michalis Spyrou2897e612018-11-20 18:38:29 +0000140 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
Manuel Bottini4b5c5882019-05-14 10:38:30 +0100141 ARM_COMPUTE_RETURN_ERROR_ON_MSG(actual_axis > 2, "Actual axis greater than 2 is not supported");
142 ARM_COMPUTE_RETURN_ERROR_ON_MSG(actual_axis >= TensorShape::num_max_dimensions, "Actual normalization axis greater than max number of dimensions");
John Richardson73d4aef2018-05-08 14:34:33 +0100143
144 // Reduce shape on axis
145 TensorShape sum_shape = input->tensor_shape();
Manuel Bottini4b5c5882019-05-14 10:38:30 +0100146 sum_shape.set(actual_axis, 1);
John Richardson73d4aef2018-05-08 14:34:33 +0100147 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(sum->tensor_shape(), sum_shape);
148
149 if(output->total_size() != 0)
150 {
151 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
152 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
153 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output->tensor_shape());
Michalis Spyrou2897e612018-11-20 18:38:29 +0000154 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
John Richardson73d4aef2018-05-08 14:34:33 +0100155 }
156
157 return Status{};
158}
159
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +0100160std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
John Richardson73d4aef2018-05-08 14:34:33 +0100161{
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +0100162 Window win = calculate_max_window(*input, Steps());
John Richardson73d4aef2018-05-08 14:34:33 +0100163
164 // Output auto initialization if not yet initialized
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100165 auto_init_if_empty(*output, input->tensor_shape(), 1, input->data_type());
John Richardson73d4aef2018-05-08 14:34:33 +0100166
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +0100167 // NEL2NormalizeLayerKernel doesn't need padding so update_window_and_padding() can be skipped
John Richardson73d4aef2018-05-08 14:34:33 +0100168
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +0100169 return std::make_tuple(Status{}, win);
John Richardson73d4aef2018-05-08 14:34:33 +0100170}
Georgios Pinitasd9769582017-08-03 10:19:40 +0100171} // namespace
172
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000173NEL2NormalizeLayerKernel::NEL2NormalizeLayerKernel()
Manuel Bottini4b5c5882019-05-14 10:38:30 +0100174 : _input(nullptr), _sum(nullptr), _output(nullptr), _actual_axis(0), _epsilon(1e-12)
Georgios Pinitasd9769582017-08-03 10:19:40 +0100175{
176}
177
Manuel Bottini4b5c5882019-05-14 10:38:30 +0100178void NEL2NormalizeLayerKernel::configure(const ITensor *input, const ITensor *sum, ITensor *output, int axis, float epsilon)
Georgios Pinitasd9769582017-08-03 10:19:40 +0100179{
180 ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output);
John Richardson73d4aef2018-05-08 14:34:33 +0100181 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), sum->info(), output->info(), axis, epsilon));
Georgios Pinitasd9769582017-08-03 10:19:40 +0100182
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +0100183 _input = input;
184 _sum = sum;
185 _output = output;
186 _actual_axis = wrap_around(axis, max_input_tensor_dim);
187 _epsilon = epsilon;
Georgios Pinitasd9769582017-08-03 10:19:40 +0100188
189 // Configure kernel window
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +0100190 auto win_config = validate_and_configure_window(_input->info(), _output->info());
John Richardson73d4aef2018-05-08 14:34:33 +0100191 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
Georgios Pinitasd9769582017-08-03 10:19:40 +0100192
John Richardson73d4aef2018-05-08 14:34:33 +0100193 INEKernel::configure(std::get<1>(win_config));
194}
Georgios Pinitasd9769582017-08-03 10:19:40 +0100195
Manuel Bottini4b5c5882019-05-14 10:38:30 +0100196Status NEL2NormalizeLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, int axis, float epsilon)
John Richardson73d4aef2018-05-08 14:34:33 +0100197{
198 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, sum, output, axis, epsilon));
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +0100199 ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get())));
Georgios Pinitasd9769582017-08-03 10:19:40 +0100200
John Richardson73d4aef2018-05-08 14:34:33 +0100201 return Status{};
Georgios Pinitasd9769582017-08-03 10:19:40 +0100202}
203
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000204void NEL2NormalizeLayerKernel::run(const Window &window, const ThreadInfo &info)
Georgios Pinitasd9769582017-08-03 10:19:40 +0100205{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100206 ARM_COMPUTE_UNUSED(info);
Georgios Pinitasd9769582017-08-03 10:19:40 +0100207 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
208 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
209
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +0100210 if(_actual_axis > 2)
Georgios Pinitasd9769582017-08-03 10:19:40 +0100211 {
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +0100212 ARM_COMPUTE_ERROR("Unsupported normalization axis");
213 }
214
215 switch(_input->info()->data_type())
216 {
217 case DataType::F32:
218 (_actual_axis == Window::DimX) ? l2_normalize_X<float, 4>(_input, _sum, _output, _epsilon, window) : l2_normalize_YZ<float, 4>(_input, _sum, _output, _epsilon, window, _actual_axis);
Michalis Spyrou2897e612018-11-20 18:38:29 +0000219 break;
Michalis Spyrou2897e612018-11-20 18:38:29 +0000220#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +0100221 case DataType::F16:
222 (_actual_axis == Window::DimX) ? l2_normalize_X<float16_t, 8>(_input, _sum, _output, _epsilon, window) : l2_normalize_YZ<float16_t, 8>(_input, _sum, _output, _epsilon, window, _actual_axis);
Michalis Spyrou2897e612018-11-20 18:38:29 +0000223 break;
Michalis Spyrou2897e612018-11-20 18:38:29 +0000224#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Georgios Pinitasd9769582017-08-03 10:19:40 +0100225 default:
Georgios Pinitas6cb26ce2020-06-24 17:20:23 +0100226 ARM_COMPUTE_ERROR("Not implemented");
Georgios Pinitasd9769582017-08-03 10:19:40 +0100227 }
228}
Michalis Spyrou2897e612018-11-20 18:38:29 +0000229} // namespace arm_compute