blob: 5d6e6a4f807f6089879d309858074683fe61c9be [file] [log] [blame]
Dana Zlotnika538ae52022-02-21 13:12:41 +02001/*
Pablo Marquez Tello7ce8a832023-08-31 16:00:50 +01002 * Copyright (c) 2021-2023 Arm Limited.
Dana Zlotnika538ae52022-02-21 13:12: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/softmax/generic/neon/impl.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010025
Dana Zlotnika538ae52022-02-21 13:12:41 +020026#include "support/SaturateCast.h"
27
28namespace arm_compute
29{
30namespace cpu
31{
Dana Zlotnika538ae52022-02-21 13:12:41 +020032template void neon_logits_1d_max<qasymm8_signed_t>(const ITensor *in, ITensor *out, const Window &window);
33template void neon_logits_1d_max<qasymm8_t>(const ITensor *in, ITensor *out, const Window &window);
34
35template <typename T>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010036void neon_softmax_logits_1d_quantized(
37 const ITensor *in, const ITensor *max, void *const tmp, ITensor *out, float beta, bool is_log, const Window &window)
Dana Zlotnika538ae52022-02-21 13:12:41 +020038{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010039 static_assert(std::is_same<T, qasymm8_t>::value || std::is_same<T, qasymm8_signed_t>::value,
Dana Zlotnika538ae52022-02-21 13:12:41 +020040 "quantized type should be either qasymm8_t or qasymm8_signed_t.");
41
42 const int start_x = in->info()->valid_region().anchor.x();
43 const int input_width = in->info()->valid_region().shape.x();
44
45 const float scale_beta = -beta * in->info()->quantization_info().uniform().scale;
46 const auto scale_beta_vec = vdupq_n_f32(scale_beta);
47
48 Iterator in_it(in, window);
49 Iterator max_it(max, window);
50 Iterator out_it(out, window);
51 constexpr int vec_size = 16;
52
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010053 execute_window_loop(
54 window,
55 [&](const Coordinates &)
Dana Zlotnika538ae52022-02-21 13:12:41 +020056 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010057 /* Get pointers */
58 const auto in_ptr = reinterpret_cast<const T *>(in_it.ptr()) + start_x;
59 const auto out_ptr = reinterpret_cast<T *>(out_it.ptr()) + start_x;
60 const auto tmp_ptr = reinterpret_cast<float *>(tmp);
Dana Zlotnika538ae52022-02-21 13:12:41 +020061
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010062 float sum{};
63 float sum_inversed{};
64
65 /* Compute exponentials and sum */
Dana Zlotnika538ae52022-02-21 13:12:41 +020066 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010067 /* Get max value */
68 const auto max_val = *reinterpret_cast<const T *>(max_it.ptr());
69 const auto vec_max = wrapper::vdup_n(max_val, wrapper::traits::vector_128_tag{});
Dana Zlotnika538ae52022-02-21 13:12:41 +020070
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010071 /* Init sum to zero */
72 float32x4x4_t vec_sum = {
73 vdupq_n_f32(0.f),
74 vdupq_n_f32(0.f),
75 vdupq_n_f32(0.f),
76 vdupq_n_f32(0.f),
77 };
Dana Zlotnika538ae52022-02-21 13:12:41 +020078
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010079 /* Loop over row and compute exponentials and sum */
80 int x = 0;
81 for (; x <= (input_width - vec_size); x += vec_size)
Dana Zlotnika538ae52022-02-21 13:12:41 +020082 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010083 auto vec_elements = wrapper::vloadq(in_ptr + x);
84 vec_elements = wrapper::vqsub(vec_max, vec_elements);
85 auto vec_elements_flt = convert_int_to_float<float32x4x4_t>(vec_elements);
Dana Zlotnika538ae52022-02-21 13:12:41 +020086
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010087 if (is_log)
Dana Zlotnika538ae52022-02-21 13:12:41 +020088 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010089 vec_elements_flt.val[0] = vmulq_f32(vec_elements_flt.val[0], scale_beta_vec);
90 vec_elements_flt.val[1] = vmulq_f32(vec_elements_flt.val[1], scale_beta_vec);
91 vec_elements_flt.val[2] = vmulq_f32(vec_elements_flt.val[2], scale_beta_vec);
92 vec_elements_flt.val[3] = vmulq_f32(vec_elements_flt.val[3], scale_beta_vec);
93 vec_sum.val[0] = vaddq_f32(vec_sum.val[0], vexpq_f32(vec_elements_flt.val[0]));
94 vec_sum.val[1] = vaddq_f32(vec_sum.val[1], vexpq_f32(vec_elements_flt.val[1]));
95 vec_sum.val[2] = vaddq_f32(vec_sum.val[2], vexpq_f32(vec_elements_flt.val[2]));
96 vec_sum.val[3] = vaddq_f32(vec_sum.val[3], vexpq_f32(vec_elements_flt.val[3]));
97 }
98 else
Dana Zlotnika538ae52022-02-21 13:12:41 +020099 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100100 vec_elements_flt.val[0] = vexpq_f32(vmulq_f32(vec_elements_flt.val[0], scale_beta_vec));
101 vec_elements_flt.val[1] = vexpq_f32(vmulq_f32(vec_elements_flt.val[1], scale_beta_vec));
102 vec_elements_flt.val[2] = vexpq_f32(vmulq_f32(vec_elements_flt.val[2], scale_beta_vec));
103 vec_elements_flt.val[3] = vexpq_f32(vmulq_f32(vec_elements_flt.val[3], scale_beta_vec));
104 vec_sum.val[0] = vaddq_f32(vec_sum.val[0], vec_elements_flt.val[0]);
105 vec_sum.val[1] = vaddq_f32(vec_sum.val[1], vec_elements_flt.val[1]);
106 vec_sum.val[2] = vaddq_f32(vec_sum.val[2], vec_elements_flt.val[2]);
107 vec_sum.val[3] = vaddq_f32(vec_sum.val[3], vec_elements_flt.val[3]);
Dana Zlotnika538ae52022-02-21 13:12:41 +0200108 }
109
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100110 vst4q_f32(tmp_ptr + x, vec_elements_flt);
Dana Zlotnika538ae52022-02-21 13:12:41 +0200111 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100112
113 /* Reduce sum */
114 const auto sum_16_byte =
115 vaddq_f32(vaddq_f32(vec_sum.val[0], vec_sum.val[1]), vaddq_f32(vec_sum.val[2], vec_sum.val[3]));
116 auto sum_res = vpadd_f32(vget_high_f32(sum_16_byte), vget_low_f32(sum_16_byte));
117 sum_res = vpadd_f32(sum_res, sum_res);
118 sum = wrapper::vgetlane(sum_res, 0);
119
120 /* Run remaining elements */
121 for (; x < input_width; ++x)
Dana Zlotnika538ae52022-02-21 13:12:41 +0200122 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100123 float element{};
124 if (is_log)
125 {
126 element = (max_val - in_ptr[x]) * scale_beta;
127 sum += std::exp(element);
128 }
129 else
130 {
131 element = std::exp((max_val - in_ptr[x]) * scale_beta);
132 sum += element;
133 }
134
135 tmp_ptr[x] = element;
136 }
137
138 if (!is_log)
139 {
140 sum_inversed = 256.f / sum;
Dana Zlotnika538ae52022-02-21 13:12:41 +0200141 }
142 else
143 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100144 sum = std::log(sum);
Dana Zlotnika538ae52022-02-21 13:12:41 +0200145 }
146 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100147
148 /* Normalize exponentials */
149 {
150 constexpr bool is_qasymm8_signed = std::is_same<T, qasymm8_signed_t>::value;
151 /* Loop over row and compute softmax */
152 int x = 0;
153 for (; x <= (input_width - vec_size); x += vec_size)
154 {
155 using int_vec_type = wrapper::traits::neon_vector_t<T, 16>;
156 float32x4x4_t vec_in = vld4q_f32(tmp_ptr + x);
157 int_vec_type normalized_value{};
158 if (is_log)
159 {
160 const float32x4x4_t sub = {
161 vsubq_f32(vec_in.val[0], vdupq_n_f32(sum)),
162 vsubq_f32(vec_in.val[1], vdupq_n_f32(sum)),
163 vsubq_f32(vec_in.val[2], vdupq_n_f32(sum)),
164 vsubq_f32(vec_in.val[3], vdupq_n_f32(sum)),
165 };
166 normalized_value = convert_float_to_int<float32x4x4_t, int_vec_type>(sub);
167 }
168 else
169 {
170 float32x4x4_t mul = {
171 vmulq_f32(vec_in.val[0], vdupq_n_f32(sum_inversed)),
172 vmulq_f32(vec_in.val[1], vdupq_n_f32(sum_inversed)),
173 vmulq_f32(vec_in.val[2], vdupq_n_f32(sum_inversed)),
174 vmulq_f32(vec_in.val[3], vdupq_n_f32(sum_inversed)),
175 };
176
177 if (is_qasymm8_signed)
178 {
179 const auto offset_vec = wrapper::vdup_n(128.f, wrapper::traits::vector_128_tag{});
180 mul.val[0] = wrapper::vsub(mul.val[0], offset_vec);
181 mul.val[1] = wrapper::vsub(mul.val[1], offset_vec);
182 mul.val[2] = wrapper::vsub(mul.val[2], offset_vec);
183 mul.val[3] = wrapper::vsub(mul.val[3], offset_vec);
184 }
185
186 normalized_value = convert_float_to_int<float32x4x4_t, int_vec_type>(mul);
187 }
188 wrapper::vstore(out_ptr + x, normalized_value);
189 }
190 /* Run remaining elements */
191 for (; x < input_width; ++x)
192 {
193 if (is_log)
194 {
195 out_ptr[x] = utils::cast::saturate_cast<T>(tmp_ptr[x] - sum);
196 }
197 else
198 {
199 out_ptr[x] = utils::cast::saturate_cast<T>((tmp_ptr[x] * sum_inversed) -
200 (is_qasymm8_signed ? 128.f : 0));
201 }
202 }
203 }
204 },
205 in_it, max_it, out_it);
Dana Zlotnika538ae52022-02-21 13:12:41 +0200206}
207
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100208template void neon_softmax_logits_1d_quantized<qasymm8_signed_t>(const ITensor *in,
209 const ITensor *max,
210 void *const tmp,
211 ITensor *out,
212 float beta,
213 bool is_log,
214 const Window &window);
215template void neon_softmax_logits_1d_quantized<qasymm8_t>(const ITensor *in,
216 const ITensor *max,
217 void *const tmp,
218 ITensor *out,
219 float beta,
220 bool is_log,
221 const Window &window);
Dana Zlotnika538ae52022-02-21 13:12:41 +0200222} // namespace cpu
223} // namespace arm_compute