blob: 6470f391e21c3b5a3401cc3d9446641de878c1b9 [file] [log] [blame]
Dana Zlotnik027bcef2021-12-27 17:35:00 +02001/*
Matthew Bentham7d9a78e2023-05-31 13:18:33 +00002 * Copyright (c) 2022-2023 Arm Limited.
Dana Zlotnik027bcef2021-12-27 17:35:00 +02003 *
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#if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
Pablo Marquez Tello3912f472023-09-04 14:18:37 +010025
Pablo Marquez Tello3912f472023-09-04 14:18:37 +010026#include "src/core/NEON/wrapper/wrapper.h"
27#include "src/cpu/CpuTypes.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028#include "src/cpu/kernels/meanstddevnorm/generic/neon/impl.h"
Dana Zlotnik027bcef2021-12-27 17:35:00 +020029
30namespace arm_compute
31{
32namespace cpu
33{
Pablo Marquez Tello3912f472023-09-04 14:18:37 +010034template <>
35void mean_stddev_normalization<float16_t, 8>(ITensor *input, ITensor *output, float epsilon, const Window &window)
36{
37 // Set build options
38 Window win = window;
39 win.set(Window::DimX, Window::Dimension(0, 1, 1));
40
41 const int window_step_x = 8;
42 const auto window_start_x = static_cast<int>(window.x().start());
43 const auto window_end_x = static_cast<int>(window.x().end());
44
45 Iterator input_itr(input, win);
46 Iterator output_itr(output, win);
47
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010048 execute_window_loop(
49 win,
50 [&](const Coordinates &)
Pablo Marquez Tello3912f472023-09-04 14:18:37 +010051 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010052 int x = window_start_x;
53 auto in_ptr = reinterpret_cast<const float16_t *>(input_itr.ptr());
54 auto out_ptr = reinterpret_cast<float16_t *>(output_itr.ptr());
Pablo Marquez Tello3912f472023-09-04 14:18:37 +010055
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010056 float16x8_t sum_vec = vdupq_n_f16(static_cast<float16_t>(0.0f));
57 float32x4_t sum_sq_vec = vdupq_n_f32(0.0f);
Pablo Marquez Tello3912f472023-09-04 14:18:37 +010058
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010059 for (; x <= (window_end_x - window_step_x); x += window_step_x)
60 {
61 float16x8_t data = vld1q_f16(in_ptr + x);
62 sum_vec = vaddq_f16(sum_vec, data);
63 float32x4_t dl = vcvt_f32_f16(vget_low_f16(data));
64 float32x4_t dh = vcvt_f32_f16(vget_high_f16(data));
65 sum_sq_vec = vaddq_f32(sum_sq_vec, vmulq_f32(dl, dl));
66 sum_sq_vec = vaddq_f32(sum_sq_vec, vmulq_f32(dh, dh));
67 }
Pablo Marquez Tello3912f472023-09-04 14:18:37 +010068
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010069 float16x4_t sum_carry_res = vpadd_f16(vget_high_f16(sum_vec), vget_low_f16(sum_vec));
70 sum_carry_res = vpadd_f16(sum_carry_res, sum_carry_res);
71 sum_carry_res = vpadd_f16(sum_carry_res, sum_carry_res);
Pablo Marquez Tello3912f472023-09-04 14:18:37 +010072
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010073 float32x4_t sum_sq_carry_res = vpaddq_f32(sum_sq_vec, sum_sq_vec);
74 sum_sq_carry_res = vpaddq_f32(sum_sq_carry_res, sum_sq_carry_res);
Pablo Marquez Tello3912f472023-09-04 14:18:37 +010075
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010076 float16_t sum = vget_lane_f16(sum_carry_res, 0);
77 float sum_sq = vgetq_lane_f32(sum_sq_carry_res, 0);
Pablo Marquez Tello3912f472023-09-04 14:18:37 +010078
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010079 // Compute left-over elements
80 for (; x < window_end_x; ++x)
81 {
82 float16_t data = *(in_ptr + x);
83 sum += data;
84 float fdata = static_cast<float>(data);
85 sum_sq += fdata * fdata;
86 }
Pablo Marquez Tello3912f472023-09-04 14:18:37 +010087
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010088 float16_t mean = sum / input->info()->dimension(0);
89 float var = (sum_sq / input->info()->dimension(0)) - (mean * mean);
90 float16_t stddev_inv = static_cast<float16_t>(1.f / sqrt(var + epsilon));
91
92 float16x8_t mean_vec = vdupq_n_f16(mean);
93 float16x8_t stddev_inv_vec = vdupq_n_f16(stddev_inv);
94
95 for (x = window_start_x; x <= (window_end_x - window_step_x); x += window_step_x)
96 {
97 float16x8_t data = vld1q_f16(in_ptr + x);
98 float16x8_t res = vmulq_f16(vsubq_f16(data, mean_vec), stddev_inv_vec);
99 // Store results
100 vst1q_f16(out_ptr + x, res);
101 }
102 for (; x < window_end_x; ++x)
103 {
104 *(out_ptr + x) = (*(in_ptr + x) - mean) * stddev_inv;
105 }
106 },
107 input_itr, output_itr);
Pablo Marquez Tello3912f472023-09-04 14:18:37 +0100108}
109
Dana Zlotnik027bcef2021-12-27 17:35:00 +0200110void neon_fp16_meanstddevnorm(ITensor *input, ITensor *output, float epsilon, const Window &window)
111{
112 return mean_stddev_normalization<float16_t, 8>(input, output, epsilon, window);
113}
114} // namespace cpu
115} // namespace arm_compute
116#endif /* defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS) */