blob: 91776d8100d68019c24e73cd40db72516f97fd6b [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
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}
John Richardson73d4aef2018-05-08 14:34:33 +010070
71Status validate_arguments(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, unsigned int axis, float epsilon)
72{
73 ARM_COMPUTE_UNUSED(epsilon);
74
75 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, sum, output);
76 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum);
77 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
78 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() != DataLayout::NCHW);
79 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 0, "Unsupported normalization axis, Supported axis is 0");
80 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Normalization axis greater than max number of dimensions");
81
82 // Reduce shape on axis
83 TensorShape sum_shape = input->tensor_shape();
84 sum_shape.set(axis, 1);
85 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(sum->tensor_shape(), sum_shape);
86
87 if(output->total_size() != 0)
88 {
89 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
90 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
91 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output->tensor_shape());
92 ARM_COMPUTE_RETURN_ERROR_ON(output->data_layout() != DataLayout::NCHW);
93 }
94
95 return Status{};
96}
97
98std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *sum, ITensorInfo *output, unsigned int axis)
99{
100 const unsigned int num_elems_processed_per_iteration = 16 / data_size_from_type(input->data_type());
101 const unsigned int num_elems_processed_per_iteration_sum = (axis == 0) ? 1 : num_elems_processed_per_iteration;
102
103 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
104
105 // Output auto initialization if not yet initialized
106 auto_init_if_empty(*output, input->tensor_shape(), 1, input->data_type(), input->fixed_point_position());
107
108 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
109 AccessWindowHorizontal sum_access(sum, 0, num_elems_processed_per_iteration_sum);
110 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
111
112 bool window_changed = update_window_and_padding(win, input_access, sum_access, output_access);
113 output_access.set_valid_region(win, input->valid_region());
114
115 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
116
117 return std::make_tuple(err, win);
118}
Georgios Pinitasd9769582017-08-03 10:19:40 +0100119} // namespace
120
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000121NEL2NormalizeLayerKernel::NEL2NormalizeLayerKernel()
Georgios Pinitasd9769582017-08-03 10:19:40 +0100122 : _input(nullptr), _sum(nullptr), _output(nullptr), _axis(0), _epsilon(1e-12)
123{
124}
125
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000126void NEL2NormalizeLayerKernel::configure(const ITensor *input, const ITensor *sum, ITensor *output, unsigned int axis, float epsilon)
Georgios Pinitasd9769582017-08-03 10:19:40 +0100127{
128 ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output);
John Richardson73d4aef2018-05-08 14:34:33 +0100129 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), sum->info(), output->info(), axis, epsilon));
Georgios Pinitasd9769582017-08-03 10:19:40 +0100130
131 _input = input;
132 _sum = sum;
133 _output = output;
134 _axis = axis;
135 _epsilon = epsilon;
136
137 // Configure kernel window
John Richardson73d4aef2018-05-08 14:34:33 +0100138 auto win_config = validate_and_configure_window(_input->info(), _sum->info(), _output->info(), axis);
139 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
Georgios Pinitasd9769582017-08-03 10:19:40 +0100140
John Richardson73d4aef2018-05-08 14:34:33 +0100141 INEKernel::configure(std::get<1>(win_config));
142}
Georgios Pinitasd9769582017-08-03 10:19:40 +0100143
John Richardson73d4aef2018-05-08 14:34:33 +0100144Status NEL2NormalizeLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, unsigned int axis, float epsilon)
145{
146 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, sum, output, axis, epsilon));
147 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 +0100148
John Richardson73d4aef2018-05-08 14:34:33 +0100149 return Status{};
Georgios Pinitasd9769582017-08-03 10:19:40 +0100150}
151
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000152void NEL2NormalizeLayerKernel::run(const Window &window, const ThreadInfo &info)
Georgios Pinitasd9769582017-08-03 10:19:40 +0100153{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100154 ARM_COMPUTE_UNUSED(info);
Georgios Pinitasd9769582017-08-03 10:19:40 +0100155 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
156 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
157
158 switch(_axis)
159 {
160 case 0:
161 l2_normalize_X(_input, _sum, _output, _epsilon, window);
162 break;
163 default:
164 ARM_COMPUTE_ERROR("Unsupported normalization axis");
165 }
166}