blob: 3bf1d9400ecc43ccd73babec8b01863281861579 [file] [log] [blame]
Georgios Pinitasd9769582017-08-03 10:19:40 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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
35#include <arm_neon.h>
36#include <cmath>
37
38using namespace arm_compute;
39
40namespace
41{
42void l2_normalize_X(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window)
43{
44 Window window_sum(window);
45 window_sum.set(Window::DimX, Window::Dimension(0, 0, 0));
46
47 Window in_slice = window.first_slice_window_1D();
48 Window sum_slice = window_sum.first_slice_window_1D();
49
50 do
51 {
52 Iterator input_it(in, in_slice);
53 Iterator sum_it(sum, sum_slice);
54 Iterator output_it(out, in_slice);
55
56 const float sum_value = *reinterpret_cast<const float *>(sum_it.ptr());
57 const float32x4_t vec_normalize_value = vdupq_n_f32(1.f / std::sqrt(std::max(sum_value, epsilon)));
58
59 execute_window_loop(in_slice, [&](const Coordinates & id)
60 {
61 const auto in_ptr = reinterpret_cast<const float *>(input_it.ptr());
62 const auto out_ptr = reinterpret_cast<float *>(output_it.ptr());
63
64 vst1q_f32(out_ptr, vmulq_f32(vld1q_f32(in_ptr), vec_normalize_value));
65 },
66 input_it, output_it);
67 }
68 while(window.slide_window_slice_1D(in_slice) && window.slide_window_slice_1D(sum_slice));
69}
70} // namespace
71
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000072NEL2NormalizeLayerKernel::NEL2NormalizeLayerKernel()
Georgios Pinitasd9769582017-08-03 10:19:40 +010073 : _input(nullptr), _sum(nullptr), _output(nullptr), _axis(0), _epsilon(1e-12)
74{
75}
76
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000077void NEL2NormalizeLayerKernel::configure(const ITensor *input, const ITensor *sum, ITensor *output, unsigned int axis, float epsilon)
Georgios Pinitasd9769582017-08-03 10:19:40 +010078{
79 ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output);
80 ARM_COMPUTE_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Normalization axis greater than max number of dimensions");
81 ARM_COMPUTE_ERROR_ON_MSG(axis > 0, "Unsupported normalization axis, Supported axis is 0");
82
83 // Output auto initialization if not yet initialized
84 auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position());
85
86 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
87 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, sum);
88 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
89
90 unsigned int num_elems_processed_per_iteration = 16 / data_size_from_type(input->info()->data_type());
91 unsigned int num_elems_processed_per_iteration_sum = (axis == 0) ? 1 : num_elems_processed_per_iteration;
92
93 _input = input;
94 _sum = sum;
95 _output = output;
96 _axis = axis;
97 _epsilon = epsilon;
98
99 // Configure kernel window
100 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
101 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
102 AccessWindowHorizontal sum_access(sum->info(), 0, num_elems_processed_per_iteration_sum);
103 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
104
105 update_window_and_padding(win, input_access, sum_access, output_access);
106
107 output_access.set_valid_region(win, input->info()->valid_region());
108
109 INEKernel::configure(win);
110}
111
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000112void NEL2NormalizeLayerKernel::run(const Window &window, const ThreadInfo &info)
Georgios Pinitasd9769582017-08-03 10:19:40 +0100113{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100114 ARM_COMPUTE_UNUSED(info);
Georgios Pinitasd9769582017-08-03 10:19:40 +0100115 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
116 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
117
118 switch(_axis)
119 {
120 case 0:
121 l2_normalize_X(_input, _sum, _output, _epsilon, window);
122 break;
123 default:
124 ARM_COMPUTE_ERROR("Unsupported normalization axis");
125 }
126}