blob: cda041de668e45a0b36c3ecfdc664a2769a5b5e2 [file] [log] [blame]
Georgios Pinitasd9769582017-08-03 10:19:40 +01001/*
John Richardson73d4aef2018-05-08 14:34:33 +01002 * Copyright (c) 2017-2018 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 */
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000024#include "arm_compute/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"
29#include "arm_compute/core/NEON/NEMath.h"
30#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Utils.h"
32#include "arm_compute/core/Validate.h"
33#include "arm_compute/core/Window.h"
34
Michalis Spyrou2897e612018-11-20 18:38:29 +000035#include "arm_compute/core/NEON/wrapper/wrapper.h"
Georgios Pinitasd9769582017-08-03 10:19:40 +010036#include <arm_neon.h>
37#include <cmath>
38
Michalis Spyrou2897e612018-11-20 18:38:29 +000039namespace arm_compute
40{
Georgios Pinitasd9769582017-08-03 10:19:40 +010041namespace
42{
Michalis Spyrou2897e612018-11-20 18:38:29 +000043template <typename T, int S>
Georgios Pinitasd9769582017-08-03 10:19:40 +010044void l2_normalize_X(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window)
45{
Michalis Spyrou2897e612018-11-20 18:38:29 +000046 /** NEON vector tag type. */
47 using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
48
Georgios Pinitasd9769582017-08-03 10:19:40 +010049 Window window_sum(window);
50 window_sum.set(Window::DimX, Window::Dimension(0, 0, 0));
51
52 Window in_slice = window.first_slice_window_1D();
53 Window sum_slice = window_sum.first_slice_window_1D();
54
55 do
56 {
57 Iterator input_it(in, in_slice);
58 Iterator sum_it(sum, sum_slice);
59 Iterator output_it(out, in_slice);
60
Michalis Spyrou2897e612018-11-20 18:38:29 +000061 const auto sum_value = *reinterpret_cast<const T *>(sum_it.ptr());
62 const auto vec_normalize_value = wrapper::vdup_n(static_cast<T>(1.f / std::sqrt(std::max(sum_value, static_cast<T>(epsilon)))), ExactTagType{});
Georgios Pinitasd9769582017-08-03 10:19:40 +010063
64 execute_window_loop(in_slice, [&](const Coordinates & id)
65 {
Michalis Spyrou2897e612018-11-20 18:38:29 +000066 const auto in_ptr = reinterpret_cast<const T *>(input_it.ptr());
67 const auto out_ptr = reinterpret_cast<T *>(output_it.ptr());
Georgios Pinitasd9769582017-08-03 10:19:40 +010068
Michalis Spyrou2897e612018-11-20 18:38:29 +000069 wrapper::vstore(out_ptr, wrapper::vmul(wrapper::vloadq(in_ptr), vec_normalize_value));
Georgios Pinitasd9769582017-08-03 10:19:40 +010070 },
71 input_it, output_it);
72 }
73 while(window.slide_window_slice_1D(in_slice) && window.slide_window_slice_1D(sum_slice));
74}
John Richardson73d4aef2018-05-08 14:34:33 +010075
Michalis Spyrou2897e612018-11-20 18:38:29 +000076template <typename T, int S>
77void l2_normalize_Y(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window)
78{
79 /** NEON vector tag type. */
80 using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
81
82 Window window_sum(window);
83 window_sum.set(Window::DimY, Window::Dimension(0, 0, 0));
84
85 Window in_slice = window.first_slice_window_2D();
86 Window sum_slice = window_sum.first_slice_window_2D();
87
88 do
89 {
90 Iterator input_it(in, in_slice);
91 Iterator sum_it(sum, sum_slice);
92 Iterator output_it(out, in_slice);
93
94 auto eps = wrapper::vdup_n(static_cast<T>(epsilon), ExactTagType{});
95
96 execute_window_loop(in_slice, [&](const Coordinates & id)
97 {
98 const auto in_ptr = reinterpret_cast<const T *>(input_it.ptr());
99 const auto sum_ptr = reinterpret_cast<const T *>(sum_it.ptr());
100 const auto out_ptr = reinterpret_cast<T *>(output_it.ptr());
101
102 const auto vec_normalize_value = wrapper::vinvsqrt(wrapper::vmax(wrapper::vloadq(sum_ptr), eps));
103 wrapper::vstore(out_ptr, wrapper::vmul(wrapper::vloadq(in_ptr), vec_normalize_value));
104 },
105 input_it, sum_it, output_it);
106 }
107 while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(sum_slice));
108}
109
110template <typename T, int S>
111void l2_normalize_Z(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window)
112{
113 /** NEON vector tag type. */
114 using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
115
116 Window window_sum(window);
117 window_sum.set(Window::DimZ, Window::Dimension(0, 0, 0));
118
119 Window in_slice = window.first_slice_window_3D();
120 Window sum_slice = window_sum.first_slice_window_3D();
121
122 do
123 {
124 Iterator input_it(in, in_slice);
125 Iterator sum_it(sum, sum_slice);
126 Iterator output_it(out, in_slice);
127
128 auto eps = wrapper::vdup_n(static_cast<T>(epsilon), ExactTagType{});
129
130 execute_window_loop(in_slice, [&](const Coordinates & id)
131 {
132 const auto in_ptr = reinterpret_cast<const T *>(input_it.ptr());
133 const auto sum_ptr = reinterpret_cast<const T *>(sum_it.ptr());
134 const auto out_ptr = reinterpret_cast<T *>(output_it.ptr());
135
136 const auto vec_normalize_value = wrapper::vinvsqrt(wrapper::vmax(wrapper::vloadq(sum_ptr), eps));
137 wrapper::vstore(out_ptr, wrapper::vmul(wrapper::vloadq(in_ptr), vec_normalize_value));
138 },
139 input_it, sum_it, output_it);
140 }
141 while(window.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(sum_slice));
142}
143
John Richardson73d4aef2018-05-08 14:34:33 +0100144Status validate_arguments(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, unsigned int axis, float epsilon)
145{
146 ARM_COMPUTE_UNUSED(epsilon);
147
148 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, sum, output);
149 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum);
Michalis Spyrou2897e612018-11-20 18:38:29 +0000150 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
151 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 2, "Axis greater than 2 is not supported");
John Richardson73d4aef2018-05-08 14:34:33 +0100152 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Normalization axis greater than max number of dimensions");
153
154 // Reduce shape on axis
155 TensorShape sum_shape = input->tensor_shape();
156 sum_shape.set(axis, 1);
157 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(sum->tensor_shape(), sum_shape);
158
159 if(output->total_size() != 0)
160 {
161 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
162 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
163 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output->tensor_shape());
Michalis Spyrou2897e612018-11-20 18:38:29 +0000164 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
John Richardson73d4aef2018-05-08 14:34:33 +0100165 }
166
167 return Status{};
168}
169
170std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *sum, ITensorInfo *output, unsigned int axis)
171{
172 const unsigned int num_elems_processed_per_iteration = 16 / data_size_from_type(input->data_type());
173 const unsigned int num_elems_processed_per_iteration_sum = (axis == 0) ? 1 : num_elems_processed_per_iteration;
174
175 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
176
177 // Output auto initialization if not yet initialized
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100178 auto_init_if_empty(*output, input->tensor_shape(), 1, input->data_type());
John Richardson73d4aef2018-05-08 14:34:33 +0100179
180 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
181 AccessWindowHorizontal sum_access(sum, 0, num_elems_processed_per_iteration_sum);
182 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
183
184 bool window_changed = update_window_and_padding(win, input_access, sum_access, output_access);
185 output_access.set_valid_region(win, input->valid_region());
186
187 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
188
189 return std::make_tuple(err, win);
190}
Georgios Pinitasd9769582017-08-03 10:19:40 +0100191} // namespace
192
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000193NEL2NormalizeLayerKernel::NEL2NormalizeLayerKernel()
Georgios Pinitasd9769582017-08-03 10:19:40 +0100194 : _input(nullptr), _sum(nullptr), _output(nullptr), _axis(0), _epsilon(1e-12)
195{
196}
197
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000198void NEL2NormalizeLayerKernel::configure(const ITensor *input, const ITensor *sum, ITensor *output, unsigned int axis, float epsilon)
Georgios Pinitasd9769582017-08-03 10:19:40 +0100199{
200 ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output);
John Richardson73d4aef2018-05-08 14:34:33 +0100201 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), sum->info(), output->info(), axis, epsilon));
Georgios Pinitasd9769582017-08-03 10:19:40 +0100202
203 _input = input;
204 _sum = sum;
205 _output = output;
206 _axis = axis;
207 _epsilon = epsilon;
208
209 // Configure kernel window
John Richardson73d4aef2018-05-08 14:34:33 +0100210 auto win_config = validate_and_configure_window(_input->info(), _sum->info(), _output->info(), axis);
211 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
Georgios Pinitasd9769582017-08-03 10:19:40 +0100212
John Richardson73d4aef2018-05-08 14:34:33 +0100213 INEKernel::configure(std::get<1>(win_config));
214}
Georgios Pinitasd9769582017-08-03 10:19:40 +0100215
John Richardson73d4aef2018-05-08 14:34:33 +0100216Status NEL2NormalizeLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, unsigned int axis, float epsilon)
217{
218 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, sum, output, axis, epsilon));
219 ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), sum->clone().get(), output->clone().get(), axis)));
Georgios Pinitasd9769582017-08-03 10:19:40 +0100220
John Richardson73d4aef2018-05-08 14:34:33 +0100221 return Status{};
Georgios Pinitasd9769582017-08-03 10:19:40 +0100222}
223
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000224void NEL2NormalizeLayerKernel::run(const Window &window, const ThreadInfo &info)
Georgios Pinitasd9769582017-08-03 10:19:40 +0100225{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100226 ARM_COMPUTE_UNUSED(info);
Georgios Pinitasd9769582017-08-03 10:19:40 +0100227 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
228 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
229
230 switch(_axis)
231 {
232 case 0:
Michalis Spyrou2897e612018-11-20 18:38:29 +0000233 switch(_input->info()->data_type())
234 {
235 case DataType::F32:
236 l2_normalize_X<float, 4>(_input, _sum, _output, _epsilon, window);
237 break;
238#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
239 case DataType::F16:
240 l2_normalize_X<float16_t, 8>(_input, _sum, _output, _epsilon, window);
241 break;
242#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
243 default:
244 ARM_COMPUTE_ERROR("Not implemented");
245 }
246 break;
247 case 1:
248 switch(_input->info()->data_type())
249 {
250 case DataType::F32:
251 l2_normalize_Y<float, 4>(_input, _sum, _output, _epsilon, window);
252 break;
253#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
254 case DataType::F16:
255 l2_normalize_Y<float16_t, 8>(_input, _sum, _output, _epsilon, window);
256#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
257 break;
258 default:
259 ARM_COMPUTE_ERROR("Not implemented");
260 }
261 break;
262 case 2:
263 switch(_input->info()->data_type())
264 {
265 case DataType::F32:
266 l2_normalize_Z<float, 4>(_input, _sum, _output, _epsilon, window);
267 break;
268#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
269 case DataType::F16:
270 l2_normalize_Z<float16_t, 8>(_input, _sum, _output, _epsilon, window);
271 break;
272#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
273 default:
274 ARM_COMPUTE_ERROR("Not implemented");
275 }
Georgios Pinitasd9769582017-08-03 10:19:40 +0100276 break;
277 default:
278 ARM_COMPUTE_ERROR("Unsupported normalization axis");
279 }
280}
Michalis Spyrou2897e612018-11-20 18:38:29 +0000281} // namespace arm_compute