Dana Zlotnik | 027bcef | 2021-12-27 17:35:00 +0200 | [diff] [blame] | 1 | /* |
Matthew Bentham | 7d9a78e | 2023-05-31 13:18:33 +0000 | [diff] [blame] | 2 | * Copyright (c) 2022-2023 Arm Limited. |
Dana Zlotnik | 027bcef | 2021-12-27 17:35:00 +0200 | [diff] [blame] | 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 | #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS) |
Pablo Marquez Tello | 3912f47 | 2023-09-04 14:18:37 +0100 | [diff] [blame] | 25 | |
Pablo Marquez Tello | 3912f47 | 2023-09-04 14:18:37 +0100 | [diff] [blame] | 26 | #include "src/core/NEON/wrapper/wrapper.h" |
| 27 | #include "src/cpu/CpuTypes.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 28 | #include "src/cpu/kernels/meanstddevnorm/generic/neon/impl.h" |
Dana Zlotnik | 027bcef | 2021-12-27 17:35:00 +0200 | [diff] [blame] | 29 | |
| 30 | namespace arm_compute |
| 31 | { |
| 32 | namespace cpu |
| 33 | { |
Pablo Marquez Tello | 3912f47 | 2023-09-04 14:18:37 +0100 | [diff] [blame] | 34 | template <> |
| 35 | void 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 48 | execute_window_loop( |
| 49 | win, |
| 50 | [&](const Coordinates &) |
Pablo Marquez Tello | 3912f47 | 2023-09-04 14:18:37 +0100 | [diff] [blame] | 51 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 52 | 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 Tello | 3912f47 | 2023-09-04 14:18:37 +0100 | [diff] [blame] | 55 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 56 | 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 Tello | 3912f47 | 2023-09-04 14:18:37 +0100 | [diff] [blame] | 58 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 59 | 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 Tello | 3912f47 | 2023-09-04 14:18:37 +0100 | [diff] [blame] | 68 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 69 | 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 Tello | 3912f47 | 2023-09-04 14:18:37 +0100 | [diff] [blame] | 72 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 73 | 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 Tello | 3912f47 | 2023-09-04 14:18:37 +0100 | [diff] [blame] | 75 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 76 | float16_t sum = vget_lane_f16(sum_carry_res, 0); |
| 77 | float sum_sq = vgetq_lane_f32(sum_sq_carry_res, 0); |
Pablo Marquez Tello | 3912f47 | 2023-09-04 14:18:37 +0100 | [diff] [blame] | 78 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 79 | // 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 Tello | 3912f47 | 2023-09-04 14:18:37 +0100 | [diff] [blame] | 87 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 88 | 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 Tello | 3912f47 | 2023-09-04 14:18:37 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Dana Zlotnik | 027bcef | 2021-12-27 17:35:00 +0200 | [diff] [blame] | 110 | void 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) */ |