Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 1 | /* |
Pablo Marquez Tello | 145e82e | 2023-09-04 16:08:33 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2023 Arm Limited. |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +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 | #include "src/cpu/kernels/instancenorm/generic/neon/impl.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 25 | |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 26 | #include "src/core/NEON/wrapper/wrapper.h" |
| 27 | |
| 28 | namespace arm_compute |
| 29 | { |
| 30 | class ITensor; |
| 31 | class Window; |
| 32 | namespace cpu |
| 33 | { |
| 34 | template <typename InputType, typename AccType> |
| 35 | void vector_float_sum(AccType &result, AccType &result_square, const InputType &inputs) |
| 36 | { |
| 37 | result = wrapper::vadd(result, inputs); |
| 38 | result_square = wrapper::vadd(result_square, wrapper::vmul(inputs, inputs)); |
| 39 | } |
| 40 | |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 41 | template <typename InputType, typename AccType> |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 42 | InputType |
| 43 | vector_float_norm(const InputType &inputs, const AccType &vec_mean, const AccType &vec_multip, const AccType &vec_beta) |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 44 | { |
| 45 | return wrapper::vadd(wrapper::vmul(wrapper::vsub(inputs, vec_mean), vec_multip), vec_beta); |
| 46 | } |
| 47 | |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 48 | template <typename T, typename AccType> |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 49 | void instance_normalization_nchw( |
| 50 | ITensor *input, ITensor *output, float gamma, float beta, float epsilon, const Window &window) |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 51 | { |
| 52 | /** SIMD vector tag type. */ |
| 53 | using ExactTagType = typename wrapper::traits::neon_bitvector_tag_t<T, wrapper::traits::BitWidth::W128>; |
| 54 | |
| 55 | // Clear X/Y dimensions on execution window as we handle the planes manually |
| 56 | Window win = window; |
| 57 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 58 | win.set(Window::DimY, Window::Dimension(0, 1, 1)); |
| 59 | |
| 60 | constexpr int window_step_x = 16 / sizeof(T); |
| 61 | const unsigned int elements_plane = input->info()->dimension(0) * output->info()->dimension(1); |
| 62 | |
| 63 | Iterator input_it(input, win); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 64 | execute_window_loop( |
| 65 | win, |
| 66 | [&](const Coordinates &id) |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 67 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 68 | Window win_plane = window; |
| 69 | win_plane.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 70 | win_plane.set(Window::DimZ, Window::Dimension(id[2], id[2] + 1, 1)); |
| 71 | win_plane.set(3, Window::Dimension(id[3], id[3] + 1, 1)); |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 72 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 73 | Iterator input_plane_it(input, win_plane); |
| 74 | Iterator output_plane_it(output, win_plane); |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 75 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 76 | auto sum_h_w = static_cast<AccType>(0.f); |
| 77 | auto sum_squares_h_w = static_cast<AccType>(0.f); |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 78 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 79 | execute_window_loop( |
| 80 | win_plane, |
| 81 | [&](const Coordinates &) |
| 82 | { |
| 83 | const auto input_ptr = reinterpret_cast<const T *>(input_plane_it.ptr()); |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 84 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 85 | auto vec_sum_h_w = wrapper::vdup_n(static_cast<AccType>(0.f), ExactTagType{}); |
| 86 | auto vec_sum_squares_h_w = wrapper::vdup_n(static_cast<AccType>(0.f), ExactTagType{}); |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 87 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 88 | // Compute S elements per iteration |
| 89 | int x = window.x().start(); |
| 90 | for (; x <= (window.x().end() - window_step_x); x += window_step_x) |
| 91 | { |
| 92 | auto vec_input_val = wrapper::vloadq(input_ptr + x); |
| 93 | vector_float_sum(vec_sum_h_w, vec_sum_squares_h_w, vec_input_val); |
| 94 | } |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 95 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 96 | auto vec2_sum_h_w = wrapper::vpadd(wrapper::vgethigh(vec_sum_h_w), wrapper::vgetlow(vec_sum_h_w)); |
| 97 | auto vec2_sum_squares_h_w = |
| 98 | wrapper::vpadd(wrapper::vgethigh(vec_sum_squares_h_w), wrapper::vgetlow(vec_sum_squares_h_w)); |
| 99 | |
| 100 | vec2_sum_h_w = wrapper::vpadd(vec2_sum_h_w, vec2_sum_h_w); |
| 101 | vec2_sum_squares_h_w = wrapper::vpadd(vec2_sum_squares_h_w, vec2_sum_squares_h_w); |
| 102 | |
| 103 | sum_h_w += wrapper::vgetlane(vec2_sum_h_w, 0); |
| 104 | sum_squares_h_w += wrapper::vgetlane(vec2_sum_squares_h_w, 0); |
| 105 | |
| 106 | // Compute left-over elements |
| 107 | for (; x < window.x().end(); ++x) |
| 108 | { |
| 109 | const auto value = static_cast<AccType>(*(input_ptr + x)); |
| 110 | sum_h_w += value; |
| 111 | sum_squares_h_w += value * value; |
| 112 | } |
| 113 | }, |
| 114 | input_plane_it, output_plane_it); |
| 115 | |
| 116 | const auto mean_h_w = sum_h_w / elements_plane; |
| 117 | const auto var_h_w = sum_squares_h_w / elements_plane - mean_h_w * mean_h_w; |
| 118 | |
| 119 | const auto multip_h_w = gamma / std::sqrt(var_h_w + epsilon); |
| 120 | const auto vec_mean_h_w = wrapper::vdup_n(static_cast<AccType>(mean_h_w), ExactTagType{}); |
| 121 | const auto vec_multip_h_w = wrapper::vdup_n(static_cast<AccType>(multip_h_w), ExactTagType{}); |
| 122 | const auto vec_beta = wrapper::vdup_n(static_cast<AccType>(beta), ExactTagType{}); |
| 123 | |
| 124 | execute_window_loop( |
| 125 | win_plane, |
| 126 | [&](const Coordinates &) |
| 127 | { |
| 128 | auto input_ptr = reinterpret_cast<T *>(input_plane_it.ptr()); |
| 129 | auto output_ptr = reinterpret_cast<T *>(output_plane_it.ptr()); |
| 130 | |
| 131 | // Compute S elements per iteration |
| 132 | int x = window.x().start(); |
| 133 | //auto vec_val = wrapper::vdup_n(static_cast<T>(0.0f), ExactTagType{}); |
| 134 | for (; x <= (window.x().end() - window_step_x); x += window_step_x) |
| 135 | { |
| 136 | const auto vec_val = wrapper::vloadq(input_ptr + x); |
| 137 | const auto normalized_vec = vector_float_norm(vec_val, vec_mean_h_w, vec_multip_h_w, vec_beta); |
| 138 | wrapper::vstore(output_ptr + x, normalized_vec); |
| 139 | } |
| 140 | |
| 141 | // Compute left-over elements |
| 142 | for (; x < window.x().end(); ++x) |
| 143 | { |
| 144 | const auto val = static_cast<AccType>(*(input_ptr + x)); |
| 145 | *(output_ptr + x) = static_cast<T>((val - mean_h_w) * multip_h_w + beta); |
| 146 | } |
| 147 | }, |
| 148 | input_plane_it, output_plane_it); |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 149 | }, |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 150 | input_it); |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 151 | } |
| 152 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 153 | template void instance_normalization_nchw<float>( |
| 154 | ITensor *input, ITensor *output, float gamma, float beta, float epsilon, const Window &window); |
Dana Zlotnik | d7e2ec5 | 2022-01-03 10:59:41 +0200 | [diff] [blame] | 155 | } // namespace cpu |
| 156 | } // namespace arm_compute |