1 /*
2 * Copyright (c) 2017-2022 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 */
24 #include "src/core/NEON/kernels/NEL2NormalizeLayerKernel.h"
25
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/TensorInfo.h"
30 #include "arm_compute/core/Utils.h"
31 #include "arm_compute/core/Validate.h"
32 #include "arm_compute/core/Window.h"
33 #include "src/common/cpuinfo/CpuIsaInfo.h"
34 #include "src/core/NEON/NEMath.h"
35 #include "src/core/common/Registrars.h"
36 #include "src/core/helpers/AutoConfiguration.h"
37 #include "src/core/helpers/WindowHelpers.h"
38 #include "src/cpu/kernels/l2normlayer/list.h"
39
40 #include <arm_neon.h>
41 #include <cmath>
42
43 namespace arm_compute
44 {
45 namespace
46 {
47 constexpr int max_input_tensor_dim = 3;
48
49 struct L2NormalizeLayerSelectorData
50 {
51 DataType dt;
52 unsigned int actual_axis;
53 cpuinfo::CpuIsaInfo isa;
54 };
55
56 using L2NormalizeLayerKernelSelctorPtr = std::add_pointer<bool(const L2NormalizeLayerSelectorData &data)>::type;
57
58 using L2NormalizeLayerPtr = std::add_pointer<void(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window, size_t axis)>::type;
59
60 struct L2NormalizeLayerKernel
61 {
62 const char *name;
63 const L2NormalizeLayerKernelSelctorPtr is_selected;
64 L2NormalizeLayerPtr ukernel;
65 };
66
67 static const L2NormalizeLayerKernel available_kernels[] =
68 {
69 {
70 "fp32_neon_l2normalize_x",
__anonedc45faa0202() 71 [](const L2NormalizeLayerSelectorData & data) { return data.dt == DataType::F32 && data.actual_axis == Window::DimX; },
72 REGISTER_FP32_NEON(arm_compute::cpu::neon_fp32_l2_normalize_x)
73 },
74 {
75 "fp32_neon_l2normalize_yz",
__anonedc45faa0302() 76 [](const L2NormalizeLayerSelectorData & data) { return data.dt == DataType::F32 && data.actual_axis != Window::DimX; },
77 REGISTER_FP32_NEON(arm_compute::cpu::neon_fp32_l2_normalize_yz)
78 },
79 {
80 "fp16_neon_l2normalize_x",
__anonedc45faa0402() 81 [](const L2NormalizeLayerSelectorData & data) { return data.dt == DataType::F16 && data.isa.fp16 && data.actual_axis == Window::DimX; },
82 REGISTER_FP16_NEON(arm_compute::cpu::neon_fp16_l2_normalize_x),
83 },
84 {
85 "fp16_neon_l2normalize_yz",
__anonedc45faa0502() 86 [](const L2NormalizeLayerSelectorData & data) { return data.dt == DataType::F16 && data.isa.fp16 && data.actual_axis != Window::DimX; },
87 REGISTER_FP16_NEON(arm_compute::cpu::neon_fp16_l2_normalize_yz),
88 },
89 };
90
91 /** Micro-kernel selector
92 *
93 * @param[in] data Selection data passed to help pick the appropriate micro-kernel
94 *
95 * @return A matching micro-kernel else nullptr
96 */
get_implementation(const L2NormalizeLayerSelectorData & data)97 const L2NormalizeLayerKernel *get_implementation(const L2NormalizeLayerSelectorData &data)
98 {
99 for(const auto &uk : available_kernels)
100 {
101 if(uk.is_selected(data))
102 {
103 return &uk;
104 }
105 }
106 return nullptr;
107 }
108
validate_arguments(const ITensorInfo * input,const ITensorInfo * sum,const ITensorInfo * output,int axis,float epsilon)109 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, int axis, float epsilon)
110 {
111 ARM_COMPUTE_UNUSED(epsilon);
112
113 const uint32_t actual_axis = wrap_around(axis, max_input_tensor_dim);
114 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, sum, output);
115 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum);
116 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
117 ARM_COMPUTE_RETURN_ERROR_ON_MSG(actual_axis > 2, "Actual axis greater than 2 is not supported");
118 ARM_COMPUTE_RETURN_ERROR_ON_MSG(actual_axis >= TensorShape::num_max_dimensions, "Actual normalization axis greater than max number of dimensions");
119
120 // Reduce shape on axis
121 TensorShape sum_shape = input->tensor_shape();
122 sum_shape.set(actual_axis, 1);
123 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(sum->tensor_shape(), sum_shape);
124
125 if(output->total_size() != 0)
126 {
127 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
128 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
129 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output->tensor_shape());
130 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
131 }
132
133 return Status{};
134 }
135
validate_and_configure_window(ITensorInfo * input,ITensorInfo * output)136 std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
137 {
138 Window win = calculate_max_window(*input, Steps());
139
140 // Output auto initialization if not yet initialized
141 auto_init_if_empty(*output, input->tensor_shape(), 1, input->data_type());
142
143 // NEL2NormalizeLayerKernel doesn't need padding so update_window_and_padding() can be skipped
144
145 return std::make_tuple(Status{}, win);
146 }
147 } // namespace
148
NEL2NormalizeLayerKernel()149 NEL2NormalizeLayerKernel::NEL2NormalizeLayerKernel()
150 : _input(nullptr), _sum(nullptr), _output(nullptr), _actual_axis(0), _epsilon(1e-12)
151 {
152 }
153
configure(const ITensor * input,const ITensor * sum,ITensor * output,int axis,float epsilon)154 void NEL2NormalizeLayerKernel::configure(const ITensor *input, const ITensor *sum, ITensor *output, int axis, float epsilon)
155 {
156 ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output);
157 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), sum->info(), output->info(), axis, epsilon));
158
159 _input = input;
160 _sum = sum;
161 _output = output;
162 _actual_axis = wrap_around(axis, max_input_tensor_dim);
163 _epsilon = epsilon;
164
165 // Configure kernel window
166 auto win_config = validate_and_configure_window(_input->info(), _output->info());
167 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
168
169 INEKernel::configure(std::get<1>(win_config));
170 }
171
validate(const ITensorInfo * input,const ITensorInfo * sum,const ITensorInfo * output,int axis,float epsilon)172 Status NEL2NormalizeLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, int axis, float epsilon)
173 {
174 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, sum, output, axis, epsilon));
175 ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get())));
176
177 return Status{};
178 }
179
run(const Window & window,const ThreadInfo & info)180 void NEL2NormalizeLayerKernel::run(const Window &window, const ThreadInfo &info)
181 {
182 ARM_COMPUTE_UNUSED(info);
183 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
184 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
185
186 if(_actual_axis > 2)
187 {
188 ARM_COMPUTE_ERROR("Unsupported normalization axis");
189 }
190
191 const auto *uk = get_implementation(L2NormalizeLayerSelectorData{ _output->info()->data_type(), _actual_axis, CPUInfo::get().get_isa() });
192 ARM_COMPUTE_ERROR_ON(uk == nullptr);
193 ARM_COMPUTE_ERROR_ON(uk->ukernel == nullptr);
194
195 uk->ukernel(_input, _sum, _output, _epsilon, window, _actual_axis);
196 }
197 } // namespace arm_compute
198