blob: f07fd2fb277078248706ebe35b225e2d504544f7 [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"
Dana Zlotnika538ae52022-02-21 13:12:41 +020025#include "support/SaturateCast.h"
26
27namespace arm_compute
28{
29namespace cpu
30{
Dana Zlotnika538ae52022-02-21 13:12:41 +020031template void neon_logits_1d_max<qasymm8_signed_t>(const ITensor *in, ITensor *out, const Window &window);
32template void neon_logits_1d_max<qasymm8_t>(const ITensor *in, ITensor *out, const Window &window);
33
34template <typename T>
35void neon_softmax_logits_1d_quantized(const ITensor *in, const ITensor *max, void *const tmp,
36 ITensor *out, float beta, bool is_log, const Window &window)
37{
38 static_assert(std::is_same<T, qasymm8_t>::value
39 || std::is_same<T, qasymm8_signed_t>::value,
40 "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
53 execute_window_loop(window, [&](const Coordinates &)
54 {
55 /* Get pointers */
56 const auto in_ptr = reinterpret_cast<const T *>(in_it.ptr()) + start_x;
57 const auto out_ptr = reinterpret_cast<T *>(out_it.ptr()) + start_x;
58 const auto tmp_ptr = reinterpret_cast<float *>(tmp);
59
60 float sum{};
61 float sum_inversed{};
62
63 /* Compute exponentials and sum */
64 {
65 /* Get max value */
66 const auto max_val = *reinterpret_cast<const T *>(max_it.ptr());
67 const auto vec_max = wrapper::vdup_n(max_val, wrapper::traits::vector_128_tag{});
68
69 /* Init sum to zero */
70 float32x4x4_t vec_sum =
71 {
72 vdupq_n_f32(0.f),
73 vdupq_n_f32(0.f),
74 vdupq_n_f32(0.f),
75 vdupq_n_f32(0.f),
76 };
77
78 /* Loop over row and compute exponentials and sum */
79 int x = 0;
80 for(; x <= (input_width - vec_size); x += vec_size)
81 {
82 auto vec_elements = wrapper::vloadq(in_ptr + x);
83 vec_elements = wrapper::vqsub(vec_max, vec_elements);
84 auto vec_elements_flt = convert_int_to_float<float32x4x4_t>(vec_elements);
85
86 if(is_log)
87 {
88 vec_elements_flt.val[0] = vmulq_f32(vec_elements_flt.val[0], scale_beta_vec);
89 vec_elements_flt.val[1] = vmulq_f32(vec_elements_flt.val[1], scale_beta_vec);
90 vec_elements_flt.val[2] = vmulq_f32(vec_elements_flt.val[2], scale_beta_vec);
91 vec_elements_flt.val[3] = vmulq_f32(vec_elements_flt.val[3], scale_beta_vec);
92 vec_sum.val[0] = vaddq_f32(vec_sum.val[0], vexpq_f32(vec_elements_flt.val[0]));
93 vec_sum.val[1] = vaddq_f32(vec_sum.val[1], vexpq_f32(vec_elements_flt.val[1]));
94 vec_sum.val[2] = vaddq_f32(vec_sum.val[2], vexpq_f32(vec_elements_flt.val[2]));
95 vec_sum.val[3] = vaddq_f32(vec_sum.val[3], vexpq_f32(vec_elements_flt.val[3]));
96 }
97 else
98 {
99 vec_elements_flt.val[0] = vexpq_f32(vmulq_f32(vec_elements_flt.val[0], scale_beta_vec));
100 vec_elements_flt.val[1] = vexpq_f32(vmulq_f32(vec_elements_flt.val[1], scale_beta_vec));
101 vec_elements_flt.val[2] = vexpq_f32(vmulq_f32(vec_elements_flt.val[2], scale_beta_vec));
102 vec_elements_flt.val[3] = vexpq_f32(vmulq_f32(vec_elements_flt.val[3], scale_beta_vec));
103 vec_sum.val[0] = vaddq_f32(vec_sum.val[0], vec_elements_flt.val[0]);
104 vec_sum.val[1] = vaddq_f32(vec_sum.val[1], vec_elements_flt.val[1]);
105 vec_sum.val[2] = vaddq_f32(vec_sum.val[2], vec_elements_flt.val[2]);
106 vec_sum.val[3] = vaddq_f32(vec_sum.val[3], vec_elements_flt.val[3]);
107 }
108
109 vst4q_f32(tmp_ptr + x, vec_elements_flt);
110 }
111
112 /* Reduce sum */
113 const auto sum_16_byte = vaddq_f32(vaddq_f32(vec_sum.val[0], vec_sum.val[1]), vaddq_f32(vec_sum.val[2], vec_sum.val[3]));
114 auto sum_res = vpadd_f32(vget_high_f32(sum_16_byte), vget_low_f32(sum_16_byte));
115 sum_res = vpadd_f32(sum_res, sum_res);
116 sum = wrapper::vgetlane(sum_res, 0);
117
118 /* Run remaining elements */
119 for(; x < input_width; ++x)
120 {
121 float element{};
122 if(is_log)
123 {
124 element = (max_val - in_ptr[x]) * scale_beta;
125 sum += std::exp(element);
126 }
127 else
128 {
129 element = std::exp((max_val - in_ptr[x]) * scale_beta);
130 sum += element;
131 }
132
133 tmp_ptr[x] = element;
134 }
135
136 if(!is_log)
137 {
138 sum_inversed = 256.f / sum;
139 }
140 else
141 {
142 sum = std::log(sum);
143 }
144 }
145
146 /* Normalize exponentials */
147 {
148 constexpr bool is_qasymm8_signed = std::is_same<T, qasymm8_signed_t>::value;
149 /* Loop over row and compute softmax */
150 int x = 0;
151 for(; x <= (input_width - vec_size); x += vec_size)
152 {
153 using int_vec_type = wrapper::traits::neon_vector_t<T, 16>;
154 float32x4x4_t vec_in = vld4q_f32(tmp_ptr + x);
155 int_vec_type normalized_value{};
156 if(is_log)
157 {
158 const float32x4x4_t sub =
159 {
160 vsubq_f32(vec_in.val[0], vdupq_n_f32(sum)),
161 vsubq_f32(vec_in.val[1], vdupq_n_f32(sum)),
162 vsubq_f32(vec_in.val[2], vdupq_n_f32(sum)),
163 vsubq_f32(vec_in.val[3], vdupq_n_f32(sum)),
164 };
165 normalized_value = convert_float_to_int<float32x4x4_t, int_vec_type>(sub);
166 }
167 else
168 {
169 float32x4x4_t mul =
170 {
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) - (is_qasymm8_signed ? 128.f : 0));
200 }
201 }
202 }
203 },
204 in_it, max_it, out_it);
205}
206
207template void neon_softmax_logits_1d_quantized<qasymm8_signed_t>(const ITensor *in, const ITensor *max, void *const tmp,
208 ITensor *out, float beta, bool is_log, const Window &window);
209template void neon_softmax_logits_1d_quantized<qasymm8_t>(const ITensor *in, const ITensor *max, void *const tmp,
210 ITensor *out, float beta, bool is_log, const Window &window);
Dana Zlotnika538ae52022-02-21 13:12:41 +0200211} // namespace cpu
212} // namespace arm_compute