blob: 515079e1b54a1c308399e5474d76c78276c1b8a2 [file] [log] [blame]
Dana Zlotnikd7e2ec52022-01-03 10:59:41 +02001/*
Pablo Marquez Tello145e82e2023-09-04 16:08:33 +01002 * Copyright (c) 2019-2023 Arm Limited.
Dana Zlotnikd7e2ec52022-01-03 10:59:41 +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#include "src/cpu/kernels/instancenorm/generic/neon/impl.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010025
Dana Zlotnikd7e2ec52022-01-03 10:59:41 +020026#include "src/core/NEON/wrapper/wrapper.h"
27
28namespace arm_compute
29{
30class ITensor;
31class Window;
32namespace cpu
33{
34template <typename InputType, typename AccType>
35void 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 Zlotnikd7e2ec52022-01-03 10:59:41 +020041template <typename InputType, typename AccType>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010042InputType
43vector_float_norm(const InputType &inputs, const AccType &vec_mean, const AccType &vec_multip, const AccType &vec_beta)
Dana Zlotnikd7e2ec52022-01-03 10:59:41 +020044{
45 return wrapper::vadd(wrapper::vmul(wrapper::vsub(inputs, vec_mean), vec_multip), vec_beta);
46}
47
Dana Zlotnikd7e2ec52022-01-03 10:59:41 +020048template <typename T, typename AccType>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010049void instance_normalization_nchw(
50 ITensor *input, ITensor *output, float gamma, float beta, float epsilon, const Window &window)
Dana Zlotnikd7e2ec52022-01-03 10:59:41 +020051{
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010064 execute_window_loop(
65 win,
66 [&](const Coordinates &id)
Dana Zlotnikd7e2ec52022-01-03 10:59:41 +020067 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010068 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 Zlotnikd7e2ec52022-01-03 10:59:41 +020072
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010073 Iterator input_plane_it(input, win_plane);
74 Iterator output_plane_it(output, win_plane);
Dana Zlotnikd7e2ec52022-01-03 10:59:41 +020075
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010076 auto sum_h_w = static_cast<AccType>(0.f);
77 auto sum_squares_h_w = static_cast<AccType>(0.f);
Dana Zlotnikd7e2ec52022-01-03 10:59:41 +020078
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010079 execute_window_loop(
80 win_plane,
81 [&](const Coordinates &)
82 {
83 const auto input_ptr = reinterpret_cast<const T *>(input_plane_it.ptr());
Dana Zlotnikd7e2ec52022-01-03 10:59:41 +020084
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010085 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 Zlotnikd7e2ec52022-01-03 10:59:41 +020087
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010088 // 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 Zlotnikd7e2ec52022-01-03 10:59:41 +020095
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010096 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 Zlotnikd7e2ec52022-01-03 10:59:41 +0200149 },
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100150 input_it);
Dana Zlotnikd7e2ec52022-01-03 10:59:41 +0200151}
152
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100153template void instance_normalization_nchw<float>(
154 ITensor *input, ITensor *output, float gamma, float beta, float epsilon, const Window &window);
Dana Zlotnikd7e2ec52022-01-03 10:59:41 +0200155} // namespace cpu
156} // namespace arm_compute