Debian Codegraph @

external/whispercpp/vec.cpp

raw symbols SCIP
1 #cxx . . $ `<file>/external/whispercpp/vec.cpp`/include "vec.h"cxx . . $ `<file>/external/whispercpp/vec.h`/
2
3 #include <cassert>
4
5 // precomputed gelu table for f16 (128 KB)
6 ggml_fp16_t ggml_table_gelu_f16cxx . . $ ggml_table_gelu_f16.[1 << 16];
7
8 // precomputed quick gelu table for f16 (128 KB)
9 ggml_fp16_t ggml_table_gelu_quick_f16cxx . . $ ggml_table_gelu_quick_f16.[1 << 16];
10
11 void ggml_vec_dot_f32cxx . . $ ggml_vec_dot_f32(12b0df4573b2cd62).(int n, float * GGML_RESTRICTcxx . . $ `external/whispercpp/ggml.h:2710:17`! s, size_t bs, const float * GGML_RESTRICTcxx . . $ `external/whispercpp/ggml.h:2710:17`! x, size_t bx, const float * GGML_RESTRICTcxx . . $ `external/whispercpp/ggml.h:2710:17`! y, size_t by, int nrc) {
12 assertcxx . . $ `/usr/include/assert.h:102:11`!(nrc == 1);
13 GGML_UNUSEDcxx . . $ `external/whispercpp/ggml.h:270:9`!(nrc);
14 GGML_UNUSEDcxx . . $ `external/whispercpp/ggml.h:270:9`!(bx);
15 GGML_UNUSEDcxx . . $ `external/whispercpp/ggml.h:270:9`!(by);
16 GGML_UNUSEDcxx . . $ `external/whispercpp/ggml.h:270:9`!(bs);
17
18 #if defined(GGML_SIMD)
19 float sumf = 0.0f;
20
21 #if defined(__ARM_FEATURE_SVE)
22 const int sve_register_length = ggml_cpu_get_sve_cnt() * 8;
23 const int ggml_f32_epr = sve_register_length / 32;//8;//svcntw(); // SVE128:4, SVE256:8, SVE512:16
24 const int ggml_f32_step = 8 * ggml_f32_epr; // choose 8 SVE registers
25
26 const int np = (n & ~(ggml_f32_step - 1));
27 svfloat32_t sum1 = svdup_n_f32(0.0f);
28 svfloat32_t sum2 = svdup_n_f32(0.0f);
29 svfloat32_t sum3 = svdup_n_f32(0.0f);
30 svfloat32_t sum4 = svdup_n_f32(0.0f);
31 svfloat32_t sum5 = svdup_n_f32(0.0f);
32 svfloat32_t sum6 = svdup_n_f32(0.0f);
33 svfloat32_t sum7 = svdup_n_f32(0.0f);
34 svfloat32_t sum8 = svdup_n_f32(0.0f);
35 svfloat32_t ax1,ax2,ax3,ax4,ax5,ax6,ax7,ax8;
36 svfloat32_t ay1,ay2,ay3,ay4,ay5,ay6,ay7,ay8;
37 for (int i = 0; i < np; i += ggml_f32_step) {
38 ax1 = GGML_F32_VEC_LOAD(x + i);
39 ay1 = GGML_F32_VEC_LOAD(y + i);
40 sum1 = GGML_F32_VEC_FMA(sum1, ax1, ay1);
41
42 ax2 = GGML_F32_VEC_LOAD(x + i + 1*ggml_f32_epr);
43 ay2 = GGML_F32_VEC_LOAD(y + i + 1*ggml_f32_epr);
44 sum2 = GGML_F32_VEC_FMA(sum2, ax2, ay2);
45
46 ax3 = GGML_F32_VEC_LOAD(x + i + 2*ggml_f32_epr);
47 ay3 = GGML_F32_VEC_LOAD(y + i + 2*ggml_f32_epr);
48 sum3 = GGML_F32_VEC_FMA(sum3, ax3, ay3);
49
50 ax4 = GGML_F32_VEC_LOAD(x + i + 3*ggml_f32_epr);
51 ay4 = GGML_F32_VEC_LOAD(y + i + 3*ggml_f32_epr);
52 sum4 = GGML_F32_VEC_FMA(sum4, ax4, ay4);
53
54 ax5 = GGML_F32_VEC_LOAD(x + i + 4*ggml_f32_epr);
55 ay5 = GGML_F32_VEC_LOAD(y + i + 4*ggml_f32_epr);
56 sum5 = GGML_F32_VEC_FMA(sum5, ax5, ay5);
57
58 ax6 = GGML_F32_VEC_LOAD(x + i + 5*ggml_f32_epr);
59 ay6 = GGML_F32_VEC_LOAD(y + i + 5*ggml_f32_epr);
60 sum6 = GGML_F32_VEC_FMA(sum6, ax6, ay6);
61
62 ax7 = GGML_F32_VEC_LOAD(x + i + 6*ggml_f32_epr);
63 ay7 = GGML_F32_VEC_LOAD(y + i + 6*ggml_f32_epr);
64 sum7 = GGML_F32_VEC_FMA(sum7, ax7, ay7);
65
66 ax8 = GGML_F32_VEC_LOAD(x + i + 7*ggml_f32_epr);
67 ay8 = GGML_F32_VEC_LOAD(y + i + 7*ggml_f32_epr);
68 sum8 = GGML_F32_VEC_FMA(sum8, ax8, ay8);
69 }
70 // leftovers
71 // Since 8 unrolls are done in above loop, leftovers lie in range [0, ggml_f32_step] which is handled in below loop
72 const int np2 = (n & ~(ggml_f32_epr - 1));
73 for (int i = np; i < np2; i += ggml_f32_epr) {
74 ax1 = GGML_F32_VEC_LOAD(x + i);
75 ay1 = GGML_F32_VEC_LOAD(y + i);
76 sum1 = GGML_F32_VEC_FMA(sum1, ax1, ay1);
77 }
78 // maximum number of leftover elements will be less that ggml_f32_epr. Apply predicated svmad on available elements only
79 if (np2 < n) {
80 svbool_t pg = svwhilelt_b32(np2, n);
81 ax1 = svld1_f32(pg, x + np2);
82 ay1 = svld1_f32(pg, y + np2);
83 sum1 = svmad_f32_m(pg, ax1, ay1, sum1);
84 }
85 // reduce sum1,sum2 to sum1
86 GGML_F32_VEC_REDUCE(sumf, sum1, sum2, sum3, sum4, sum5, sum6, sum7, sum8);
87 #elif defined(__riscv_v_intrinsic)
88 int vl = __riscv_vsetvlmax_e32m8();
89 vfloat32m1_t vs = __riscv_vfmv_v_f_f32m1(0.0f, 1);
90 vfloat32m8_t vsum;
91 vfloat32m8_t ax;
92 vfloat32m8_t ay;
93 vsum = __riscv_vfmv_v_f_f32m8_tu(vsum, 0.0f, vl);
94 for (int i = 0; i < n; i += vl) {
95 vl = __riscv_vsetvl_e32m8(n - i);
96 ax = __riscv_vle32_v_f32m8_tu(ax, &x[i], vl);
97 ay = __riscv_vle32_v_f32m8_tu(ay, &y[i], vl);
98 vsum = __riscv_vfmacc_vv_f32m8_tu(vsum, ax, ay, vl);
99 }
100 vl = __riscv_vsetvlmax_e32m8();
101 vs = __riscv_vfredusum_vs_f32m8_f32m1(vsum, vs, vl);
102 sumf += __riscv_vfmv_f_s_f32m1_f32(vs);
103 #else
104 const int np = (n & ~(GGML_F32_STEP - 1));
105
106 GGML_F32_VEC sum[GGML_F32_ARR] = { GGML_F32_VEC_ZERO };
107
108 GGML_F32_VEC ax[GGML_F32_ARR];
109 GGML_F32_VEC ay[GGML_F32_ARR];
110
111 for (int i = 0; i < np; i += GGML_F32_STEP) {
112 for (int j = 0; j < GGML_F32_ARR; j++) {
113 ax[j] = GGML_F32_VEC_LOAD(x + i + j*GGML_F32_EPR);
114 ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
115
116 sum[j] = GGML_F32_VEC_FMA(sum[j], ax[j], ay[j]);
117 }
118 }
119
120 // reduce sum0..sum3 to sum0
121 GGML_F32_VEC_REDUCE(sumf, sum);
122
123 // leftovers
124 for (int i = np; i < n; ++i) {
125 sumf += x[i]*y[i];
126 }
127 #endif
128 #else
129 // scalar
130 ggml_floatcxx . . $ ggml_float# sumf = 0.0;
131 for (int i = 0; i < n; ++i) {
132 sumf += (ggml_floatcxx . . $ ggml_float#)(x[i]*y[i]);
133 }
134 #endif
135
136 *s = sumf;
137 }
138
139 void ggml_vec_dot_bf16cxx . . $ ggml_vec_dot_bf16(8843e7e229370688).(int n, float * GGML_RESTRICTcxx . . $ `external/whispercpp/ggml.h:2710:17`! s, size_t bs, ggml_bf16_t * GGML_RESTRICTcxx . . $ `external/whispercpp/ggml.h:2710:17`! x, size_t bx, ggml_bf16_t * GGML_RESTRICTcxx . . $ `external/whispercpp/ggml.h:2710:17`! y, size_t by, int nrc) {
140 assertcxx . . $ `/usr/include/assert.h:102:11`!(nrc == 1);
141 GGML_UNUSEDcxx . . $ `external/whispercpp/ggml.h:270:9`!(nrc);
142 GGML_UNUSEDcxx . . $ `external/whispercpp/ggml.h:270:9`!(bx);
143 GGML_UNUSEDcxx . . $ `external/whispercpp/ggml.h:270:9`!(by);
144 GGML_UNUSEDcxx . . $ `external/whispercpp/ggml.h:270:9`!(bs);
145 int i = 0;
146 ggml_floatcxx . . $ ggml_float# sumf = 0;
147
148 #if defined(__AVX512BF16__)
149 __m512 c1 = _mm512_setzero_ps();
150 __m512 c2 = _mm512_setzero_ps();
151 for (; i + 64 <= n; i += 64) {
152 c1 = _mm512_dpbf16_ps(c1, m512bh(_mm512_loadu_si512((x + i))),
153 m512bh(_mm512_loadu_si512((y + i))));
154 c2 = _mm512_dpbf16_ps(c2, m512bh(_mm512_loadu_si512((x + i + 32))),
155 m512bh(_mm512_loadu_si512((y + i + 32))));
156 }
157 sumf += (ggml_float)_mm512_reduce_add_ps(c1);
158 sumf += (ggml_float)_mm512_reduce_add_ps(c2);
159
160 #elif defined(__AVX512F__)
161 #define LOAD(p) _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(_mm256_loadu_si256((const __m256i *)(p))), 16))
162 __m512 c1 = _mm512_setzero_ps();
163 __m512 c2 = _mm512_setzero_ps();
164 for (; i + 32 <= n; i += 32) {
165 c1 = _mm512_add_ps(_mm512_mul_ps(LOAD(x + i), LOAD(y + i)), c1);
166 c2 = _mm512_add_ps(_mm512_mul_ps(LOAD(x + i + 16), LOAD(y + i + 16)), c2);
167 }
168 sumf += (ggml_float)_mm512_reduce_add_ps(c1);
169 sumf += (ggml_float)_mm512_reduce_add_ps(c2);
170
171 #undef LOAD
172 #elif defined(__AVX2__) || defined(__AVX__)
173 #if defined(__AVX2__)
174 #define LOAD(p) _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_cvtepu16_epi32(_mm_loadu_si128((const __m128i *)(p))), 16))
175 #else
176 #define LOAD(p) _mm256_castsi256_ps(_mm256_insertf128_si256(_mm256_castsi128_si256(_mm_slli_epi32(_mm_cvtepu16_epi32(_mm_loadu_si128((const __m128i *)(p))), 16)), (_mm_slli_epi32(_mm_cvtepu16_epi32(_mm_bsrli_si128(_mm_loadu_si128((const __m128i *)(p)), 8)), 16)), 1))
177 #endif
178 __m256 c1 = _mm256_setzero_ps();
179 __m256 c2 = _mm256_setzero_ps();
180 __m256 c3 = _mm256_setzero_ps();
181 __m256 c4 = _mm256_setzero_ps();
182 for (; i + 32 <= n; i += 32) {
183 c1 = _mm256_add_ps(_mm256_mul_ps(LOAD(x + i), LOAD(y + i)), c1);
184 c2 = _mm256_add_ps(_mm256_mul_ps(LOAD(x + i + 8), LOAD(y + i + 8)), c2);
185 c3 = _mm256_add_ps(_mm256_mul_ps(LOAD(x + i + 16), LOAD(y + i + 16)), c3);
186 c4 = _mm256_add_ps(_mm256_mul_ps(LOAD(x + i + 24), LOAD(y + i + 24)), c4);
187 }
188 __m128 g;
189 c1 = _mm256_add_ps(_mm256_add_ps(c1, c3),
190 _mm256_add_ps(c2, c4));
191 g = _mm_add_ps(_mm256_extractf128_ps(c1, 1),
192 _mm256_castps256_ps128(c1));
193 g = _mm_add_ps(g, _mm_movehl_ps(g, g));
194 g = _mm_add_ss(g, _mm_movehdup_ps(g));
195 sumf += (ggml_float)_mm_cvtss_f32(g);
196
197 #undef LOAD
198 #elif defined(__riscv_v_intrinsic) && defined(__riscv_zvfbfwma)
199 size_t vl = __riscv_vsetvlmax_e32m4();
200
201 // initialize accumulators to all zeroes
202 vfloat32m4_t vsum0 = __riscv_vfmv_v_f_f32m4(0.0f, vl);
203 vfloat32m4_t vsum1 = __riscv_vfmv_v_f_f32m4(0.0f, vl);
204
205 // calculate step size
206 const size_t epr = __riscv_vsetvlmax_e16m2();
207 const size_t step = epr * 2;
208 const int np = (n & ~(step - 1));
209
210 // unroll by 2
211 for (; i < np; i += step) {
212 vbfloat16m2_t ax0 = __riscv_vle16_v_bf16m2((const __bf16 *)&x[i], epr);
213 vbfloat16m2_t ay0 = __riscv_vle16_v_bf16m2((const __bf16 *)&y[i], epr);
214 vsum0 = __riscv_vfwmaccbf16_vv_f32m4(vsum0, ax0, ay0, epr);
215 __asm__ __volatile__ ("" ::: "memory");
216
217 vbfloat16m2_t ax1 = __riscv_vle16_v_bf16m2((const __bf16 *)&x[i + epr], epr);
218 vbfloat16m2_t ay1 = __riscv_vle16_v_bf16m2((const __bf16 *)&y[i + epr], epr);
219 vsum1 = __riscv_vfwmaccbf16_vv_f32m4(vsum1, ax1, ay1, epr);
220 __asm__ __volatile__ ("" ::: "memory");
221 }
222
223 // accumulate in 1 register
224 vsum0 = __riscv_vfadd_vv_f32m4(vsum0, vsum1, vl);
225
226 // leftovers
227 for (i = np; i < n; i += vl) {
228 vl = __riscv_vsetvl_e16m2(n - i);
229 vbfloat16m2_t ax0 = __riscv_vle16_v_bf16m2((const __bf16 *)&x[i], vl);
230 vbfloat16m2_t ay0 = __riscv_vle16_v_bf16m2((const __bf16 *)&y[i], vl);
231 vsum0 = __riscv_vfwmaccbf16_vv_f32m4(vsum0, ax0, ay0, vl);
232 }
233
234 // reduce
235 vl = __riscv_vsetvlmax_e32m4();
236 vfloat32m1_t redsum = __riscv_vfredusum_vs_f32m4_f32m1(vsum0, __riscv_vfmv_v_f_f32m1(0.0f, 1), vl);
237 sumf += __riscv_vfmv_f_s_f32m1_f32(redsum);
238
239 #elif defined(__POWER9_VECTOR__) || defined(__VXE__) || defined(__VXE2__)
240 const int np = (n & ~(GGML_BF16_STEP - 1));
241 if (np > 0) {
242 GGML_F32_VEC sum[4] = {GGML_F32_VEC_ZERO};
243 for (; i < np; i += GGML_BF16_STEP) {
244 GGML_BF16_VEC vx0 = GGML_BF16_VEC_LOAD(x + i);
245 GGML_BF16_VEC vx1 = GGML_BF16_VEC_LOAD(x + i + 8);
246 GGML_BF16_VEC vy0 = GGML_BF16_VEC_LOAD(y + i);
247 GGML_BF16_VEC vy1 = GGML_BF16_VEC_LOAD(y + i + 8);
248 GGML_BF16_FMA_LO(sum[0], vx0, vy0);
249 GGML_BF16_FMA_HI(sum[1], vx0, vy0);
250 GGML_BF16_FMA_LO(sum[2], vx1, vy1);
251 GGML_BF16_FMA_HI(sum[3], vx1, vy1);
252 }
253 GGML_F32x4_REDUCE_4(sumf, sum[0], sum[1], sum[2], sum[3]);
254 }
255 #endif
256
257 for (; i < n; ++i) {
258 sumf += (ggml_floatcxx . . $ ggml_float#)(GGML_BF16_TO_FP32cxx . . $ `external/whispercpp/ggml-impl.h:571:9`!(x[i]) *
259 GGML_BF16_TO_FP32cxx . . $ `external/whispercpp/ggml-impl.h:571:9`!(y[i]));
260 }
261 *s = sumf;
262 }
263
264 void ggml_vec_dot_f16cxx . . $ ggml_vec_dot_f16(69b23beeb64163f3).(int n, float * GGML_RESTRICTcxx . . $ `external/whispercpp/ggml.h:2710:17`! s, size_t bs, ggml_fp16_t * GGML_RESTRICTcxx . . $ `external/whispercpp/ggml.h:2710:17`! x, size_t bx, ggml_fp16_t * GGML_RESTRICTcxx . . $ `external/whispercpp/ggml.h:2710:17`! y, size_t by, int nrc) {
265 assertcxx . . $ `/usr/include/assert.h:102:11`!(nrc == 1);
266 GGML_UNUSEDcxx . . $ `external/whispercpp/ggml.h:270:9`!(nrc);
267 GGML_UNUSEDcxx . . $ `external/whispercpp/ggml.h:270:9`!(bx);
268 GGML_UNUSEDcxx . . $ `external/whispercpp/ggml.h:270:9`!(by);
269 GGML_UNUSEDcxx . . $ `external/whispercpp/ggml.h:270:9`!(bs);
270
271 ggml_floatcxx . . $ ggml_float# sumf = 0.0;
272
273
274 #if defined(GGML_SIMD)
275 #if defined(__ARM_FEATURE_SVE)
276 const int sve_register_length = svcntb() * 8; //get vector length
277 const int ggml_f16_epr = sve_register_length / 16; // running when 16
278 const int ggml_f16_step = 8 * ggml_f16_epr; // choose 8 SVE registers
279
280 const int np= (n & ~(ggml_f16_step - 1));
281 svfloat16_t sum1 = svdup_n_f16(0.0f);
282 svfloat16_t sum2 = svdup_n_f16(0.0f);
283 svfloat16_t sum3 = svdup_n_f16(0.0f);
284 svfloat16_t sum4 = svdup_n_f16(0.0f);
285
286 svfloat16_t ax1, ax2, ax3, ax4, ax5, ax6, ax7, ax8;
287 svfloat16_t ay1, ay2, ay3, ay4, ay5, ay6, ay7, ay8;
288 for (int i = 0; i < np; i += ggml_f16_step) {
289 ax1 = GGML_F16x_VEC_LOAD(x + i + 0 * ggml_f16_epr, 0);
290 ay1 = GGML_F16x_VEC_LOAD(y + i + 0 * ggml_f16_epr, 0);
291 sum1 = GGML_F16x_VEC_FMA(sum1, ax1, ay1);
292
293 ax2 = GGML_F16x_VEC_LOAD(x + i + 1 * ggml_f16_epr, 1);
294 ay2 = GGML_F16x_VEC_LOAD(y + i + 1 * ggml_f16_epr, 1);
295 sum2 = GGML_F16x_VEC_FMA(sum2, ax2, ay2);
296
297 ax3 = GGML_F16x_VEC_LOAD(x + i + 2 * ggml_f16_epr, 2);
298 ay3 = GGML_F16x_VEC_LOAD(y + i + 2 * ggml_f16_epr, 2);
299 sum3 = GGML_F16x_VEC_FMA(sum3, ax3, ay3);
300
301 ax4 = GGML_F16x_VEC_LOAD(x + i + 3 * ggml_f16_epr, 3);
302 ay4 = GGML_F16x_VEC_LOAD(y + i + 3 * ggml_f16_epr, 3);
303 sum4 = GGML_F16x_VEC_FMA(sum4, ax4, ay4);
304
305 ax5 = GGML_F16x_VEC_LOAD(x + i + 4 * ggml_f16_epr, 4);
306 ay5 = GGML_F16x_VEC_LOAD(y + i + 4 * ggml_f16_epr, 4);
307 sum1 = GGML_F16x_VEC_FMA(sum1, ax5, ay5);
308
309 ax6 = GGML_F16x_VEC_LOAD(x + i + 5 * ggml_f16_epr, 5);
310 ay6 = GGML_F16x_VEC_LOAD(y + i + 5 * ggml_f16_epr, 5);
311 sum2 = GGML_F16x_VEC_FMA(sum2, ax6, ay6);
312
313 ax7 = GGML_F16x_VEC_LOAD(x + i + 6 * ggml_f16_epr, 6);
314 ay7 = GGML_F16x_VEC_LOAD(y + i + 6 * ggml_f16_epr, 6);
315 sum3 = GGML_F16x_VEC_FMA(sum3, ax7, ay7);
316
317 ax8 = GGML_F16x_VEC_LOAD(x + i + 7 * ggml_f16_epr, 7);
318 ay8 = GGML_F16x_VEC_LOAD(y + i + 7 * ggml_f16_epr, 7);
319 sum4 = GGML_F16x_VEC_FMA(sum4, ax8, ay8);
320 }
321
322 const int np2 = (n & ~(ggml_f16_epr - 1)); // round down to multiple of 8
323 for (int k = np; k < np2; k += ggml_f16_epr) {
324 svfloat16_t rx = GGML_F16x_VEC_LOAD(x + k, 0);
325 svfloat16_t ry = GGML_F16x_VEC_LOAD(y + k, 0);
326 sum1 = GGML_F16x_VEC_FMA(sum1, rx, ry);
327 }
328
329 if (np2 < n) {
330 svbool_t pg = svwhilelt_b16(np2, n);
331 svfloat16_t hx = svld1_f16(pg, (const __fp16 *)(x + np2));
332 svfloat16_t hy = svld1_f16(pg, (const __fp16 *)(y + np2));
333
334 sum1 = svmad_f16_x(pg, hx, hy, sum1);
335 }
336 GGML_F16x_VEC_REDUCE(sumf, sum1, sum2, sum3, sum4);
337 #elif defined(__riscv_v_intrinsic)
338 #if defined(__riscv_zvfh)
339 int vl = __riscv_vsetvlmax_e32m2();
340 vfloat32m1_t vs = __riscv_vfmv_v_f_f32m1(0.0f, 1);
341 vfloat32m2_t vsum;
342 vfloat16m1_t ax;
343 vfloat16m1_t ay;
344 vsum = __riscv_vreinterpret_v_u32m2_f32m2(__riscv_vmv_v_x_u32m2(0, vl));
345 for (int i = 0; i < n; i += vl) {
346 vl = __riscv_vsetvl_e16m1(n - i);
347 ax = __riscv_vle16_v_f16m1_tu(ax, (const _Float16 *)&x[i], vl);
348 ay = __riscv_vle16_v_f16m1_tu(ay, (const _Float16 *)&y[i], vl);
349 vsum = __riscv_vfwmacc_vv_f32m2_tu(vsum, ax, ay, vl);
350 }
351 vl = __riscv_vsetvlmax_e32m1();
352 vfloat32m1_t ac0 = __riscv_vfadd_vv_f32m1(__riscv_vget_v_f32m2_f32m1(vsum, 0), __riscv_vget_v_f32m2_f32m1(vsum, 1), vl);
353 vs = __riscv_vfredusum_vs_f32m1_f32m1(ac0, vs, vl);
354 sumf += __riscv_vfmv_f_s_f32m1_f32(vs);
355 #else
356 for (int i = 0; i < n; ++i) {
357 sumf += (ggml_float)(GGML_CPU_FP16_TO_FP32(x[i])*GGML_CPU_FP16_TO_FP32(y[i]));
358 }
359 #endif // __riscv_zvfh
360 #else
361 const int np = (n & ~(GGML_F16_STEP - 1));
362
363 GGML_F16_VEC sum[GGML_F16_ARR] = { GGML_F16_VEC_ZERO };
364
365 GGML_F16_VEC ax[GGML_F16_ARR];
366 GGML_F16_VEC ay[GGML_F16_ARR];
367
368 for (int i = 0; i < np; i += GGML_F16_STEP) {
369 for (int j = 0; j < GGML_F16_ARR; j++) {
370 ax[j] = GGML_F16_VEC_LOAD(x + i + j*GGML_F16_EPR, j);
371 ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
372
373 sum[j] = GGML_F16_VEC_FMA(sum[j], ax[j], ay[j]);
374 }
375 }
376
377 // reduce sum0..sum3 to sum0
378 GGML_F16_VEC_REDUCE(sumf, sum);
379
380 // leftovers
381 for (int i = np; i < n; ++i) {
382 sumf += (ggml_float)(GGML_CPU_FP16_TO_FP32(x[i])*GGML_CPU_FP16_TO_FP32(y[i]));
383 }
384 // if you hit this, you are likely running outside the FP range
385 assert(!isnan(sumf) && !isinf(sumf));
386 #endif
387 #else
388 for (int i = 0; i < n; ++i) {
389 sumf += (ggml_floatcxx . . $ ggml_float#)(GGML_CPU_FP16_TO_FP32cxx . . $ `external/whispercpp/simd-mappings.h:140:9`!(x[i])*GGML_CPU_FP16_TO_FP32cxx . . $ `external/whispercpp/simd-mappings.h:140:9`!(y[i]));
390 }
391 #endif // GGML_SIMD
392
393 *s = sumf;
394 }
395
396 void ggml_vec_silu_f32cxx . . $ ggml_vec_silu_f32(ad1e47f708f087d4).(const int n, float * y, const float * x) {
397 int i = 0;
398 #if defined(__AVX512F__) && defined(__AVX512DQ__)
399 for (; i + 15 < n; i += 16) {
400 _mm512_storeu_ps(y + i, ggml_v_silu(_mm512_loadu_ps(x + i)));
401 }
402 #elif defined(__AVX2__) && defined(__FMA__)
403 for (; i + 7 < n; i += 8) {
404 _mm256_storeu_ps(y + i, ggml_v_silu(_mm256_loadu_ps(x + i)));
405 }
406 #elif defined(__SSE2__cxx . . $ `<built-in>:450:9`!)
407 for (; i + 3 < n; i += 4) {
408 _mm_storeu_ps(y + i, ggml_v_silu(_mm_loadu_ps(x + i)));
409 }
410 #elif defined(__ARM_FEATURE_SVE) && defined(__aarch64__)
411 const int vlen = svcntw();
412 for (; i < n; i += vlen) {
413 const svbool_t pg = svwhilelt_b32_s32(i, n);
414 svst1_f32(pg, y + i, ggml_v_silu(pg, svld1_f32(pg, x + i)));
415 }
416 #elif defined(__ARM_NEON) && defined(__aarch64__)
417 for (; i + 3 < n; i += 4) {
418 vst1q_f32(y + i, ggml_v_silu(vld1q_f32(x + i)));
419 }
420 #elif defined(__riscv_v_intrinsic)
421 for (int vl; i < n; i += vl) {
422 vl = __riscv_vsetvl_e32m2(n - i);
423 vfloat32m2_t vx = __riscv_vle32_v_f32m2(&x[i], vl);
424 vfloat32m2_t vy = ggml_v_silu_m2(vx, vl);
425 __riscv_vse32_v_f32m2(&y[i], vy, vl);
426 }
427 #endif
428 for (; i < n; ++i) {
429 y[i] = ggml_silu_f32(x[i]);
430 }
431 }
432
433 void ggml_vec_swiglu_f32cxx . . $ ggml_vec_swiglu_f32(b4e6d09e1ee28f20).(const int n, float * y, const float * x, const float * g) {
434 int i = 0;
435 #if defined(__AVX512F__) && defined(__AVX512DQ__)
436 for (; i + 15 < n; i += 16) {
437 _mm512_storeu_ps(y + i, _mm512_mul_ps(ggml_v_silu(_mm512_loadu_ps(x + i)), _mm512_loadu_ps(g + i)));
438 }
439 #elif defined(__AVX2__) && defined(__FMA__)
440 for (; i + 7 < n; i += 8) {
441 _mm256_storeu_ps(y + i, _mm256_mul_ps(ggml_v_silu(_mm256_loadu_ps(x + i)), _mm256_loadu_ps(g + i)));
442 }
443 #elif defined(__SSE2__cxx . . $ `<built-in>:450:9`!)
444 for (; i + 3 < n; i += 4) {
445 _mm_storeu_ps(y + i, _mm_mul_ps(ggml_v_silu(_mm_loadu_ps(x + i)), _mm_loadu_ps(g + i)));
446 }
447 #elif defined(__ARM_FEATURE_SVE) && defined(__aarch64__)
448 const int vlen = svcntw();
449 for (; i < n; i += vlen) {
450 const svbool_t pg = svwhilelt_b32_s32(i, n);
451 svst1_f32(pg, y + i, svmul_f32_x(pg, ggml_v_silu(pg, svld1_f32(pg, x + i)), svld1_f32(pg, g + i)));
452 }
453 #elif defined(__ARM_NEON) && defined(__aarch64__)
454 for (; i + 3 < n; i += 4) {
455 vst1q_f32(y + i, vmulq_f32(ggml_v_silu(vld1q_f32(x + i)), vld1q_f32(g + i)));
456 }
457 #elif defined(__riscv_v_intrinsic)
458 for (int vl; i < n; i += vl) {
459 vl = __riscv_vsetvl_e32m2(n - i);
460 vfloat32m2_t vx = __riscv_vle32_v_f32m2(&x[i], vl);
461 vfloat32m2_t vg = __riscv_vle32_v_f32m2(&g[i], vl);
462 vfloat32m2_t vy = __riscv_vfmul_vv_f32m2(ggml_v_silu_m2(vx, vl), vg, vl);
463 __riscv_vse32_v_f32m2(&y[i], vy, vl);
464 }
465 #endif
466 for (; i < n; ++i) {
467 y[i] = ggml_silu_f32(x[i]) * g[i];
468 }
469 }
470
471 ggml_floatcxx . . $ ggml_float# ggml_vec_cvar_f32cxx . . $ ggml_vec_cvar_f32(4f1e42740f7851eb).(const int n, float * y, const float * x, const float mean) {
472 int i = 0;
473 ggml_floatcxx . . $ ggml_float# sum = 0;
474 // TODO: optimize to process the remaining elements in groups using the smaller vector sizes from AVX2 and SSE
475 // ref: https://github.com/ggml-org/llama.cpp/pull/15953#pullrequestreview-3310928344
476 #if defined(__AVX512F__) && defined(__AVX512DQ__)
477 for (; i + 15 < n; i += 16) {
478 __m512 val = _mm512_sub_ps(_mm512_loadu_ps(x + i),
479 _mm512_set1_ps(mean));
480 _mm512_storeu_ps(y + i, val);
481 sum += (ggml_float)_mm512_reduce_add_ps(_mm512_mul_ps(val, val));
482 }
483 #elif defined(__AVX2__) && defined(__FMA__)
484 for (; i + 7 < n; i += 8) {
485 __m256 val = _mm256_sub_ps(_mm256_loadu_ps(x + i),
486 _mm256_set1_ps(mean));
487 _mm256_storeu_ps(y + i, val);
488 val = _mm256_mul_ps(val,val);
489 __m128 val2 = _mm_add_ps(_mm256_extractf128_ps(val, 1),
490 _mm256_castps256_ps128(val));
491 val2 = _mm_add_ps(val2, _mm_movehl_ps(val2, val2));
492 val2 = _mm_add_ss(val2, _mm_movehdup_ps(val2));
493 sum += (ggml_float)_mm_cvtss_f32(val2);
494 }
495 #elif defined(__SSE2__cxx . . $ `<built-in>:450:9`!)
496 for (; i + 3 < n; i += 4) {
497 __m128 val = _mm_sub_ps(_mm_loadu_ps(x + i),
498 _mm_set1_ps(mean));
499 _mm_storeu_ps(y + i, val);
500 val = _mm_mul_ps(val, val);
501 #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__)
502 val = _mm_add_ps(val, _mm_movehl_ps(val, val));
503 val = _mm_add_ss(val, _mm_movehdup_ps(val));
504 #else
505 __m128 tmp = _mm_shuffle_ps(val, val, _MM_SHUFFLEcxx . . $ `<external>/8701576479610391844/xmmintrin.h:82:9`!(2, 3, 0, 1));
506 val = _mm_add_ps(val, tmp);
507 tmp = _mm_movehl_ps(tmp, val);
508 val = _mm_add_ss(val, tmp);
509 #endif // __AVX__ || __AVX2__ || __AVX512F__
510 sum += (ggml_floatcxx . . $ ggml_float#)_mm_cvtss_f32(val);
511 }
512 #elif defined(__ARM_NEON) && defined(__aarch64__)
513 for (; i + 3 < n; i += 4) {
514 float32x4_t val = vsubq_f32(vld1q_f32(x + i),
515 vdupq_n_f32(mean));
516 vst1q_f32(y + i, val);
517 val = vmulq_f32(val, val);
518 sum += (ggml_float)vaddvq_f32(val);
519 }
520 #elif defined(__VXE__) || defined(__VXE2__)
521 for (; i + 3 < n; i += 4) {
522 float32x4_t val = vec_sub(vec_xl(0, x + i), vec_splats(mean));
523 vec_xst(val, 0, y + i);
524 val = vec_mul(val, val);
525 sum += (ggml_float)vec_hsum_f32x4(val);
526 }
527 #elif defined(__riscv_v_intrinsic)
528 vfloat64m1_t vsum = __riscv_vfmv_v_f_f64m1(0, 1);
529 for (int vl; i < n; i += vl) {
530 vl = __riscv_vsetvl_e32m2(n - i);
531 vfloat32m2_t val = __riscv_vfsub_vf_f32m2(__riscv_vle32_v_f32m2(&x[i], vl), mean, vl);
532 __riscv_vse32_v_f32m2(&y[i], val, vl);
533 val = __riscv_vfmul_vv_f32m2(val, val, vl);
534 vsum = __riscv_vfwredusum_vs_f32m2_f64m1(val, vsum, vl);
535 }
536 sum = (ggml_float)__riscv_vfmv_f_s_f64m1_f64(vsum);
537 #endif
538 for (; i < n; ++i) {
539 float val = x[i] - mean;
540 y[i] = val;
541 val *= val;
542 sum += (ggml_floatcxx . . $ ggml_float#)val;
543 }
544 return sum/n;
545 }
546
547 ggml_floatcxx . . $ ggml_float# ggml_vec_soft_max_f32cxx . . $ ggml_vec_soft_max_f32(4f1e42740f7851eb).(const int n, float * y, const float * x, float max) {
548 int i = 0;
549 ggml_floatcxx . . $ ggml_float# sum = 0;
550 #if defined(__AVX512F__) && defined(__AVX512DQ__)
551 for (; i + 15 < n; i += 16) {
552 __m512 val = ggml_v_expf(_mm512_sub_ps(_mm512_loadu_ps(x + i),
553 _mm512_set1_ps(max)));
554 _mm512_storeu_ps(y + i, val);
555 sum += (ggml_float)_mm512_reduce_add_ps(val);
556 }
557 #elif defined(__AVX2__) && defined(__FMA__)
558 for (; i + 7 < n; i += 8) {
559 __m256 val = ggml_v_expf(_mm256_sub_ps(_mm256_loadu_ps(x + i),
560 _mm256_set1_ps(max)));
561 _mm256_storeu_ps(y + i, val);
562 __m128 val2 = _mm_add_ps(_mm256_extractf128_ps(val, 1),
563 _mm256_castps256_ps128(val));
564 val2 = _mm_add_ps(val2, _mm_movehl_ps(val2, val2));
565 val2 = _mm_add_ss(val2, _mm_movehdup_ps(val2));
566 sum += (ggml_float)_mm_cvtss_f32(val2);
567 }
568 #elif defined(__SSE2__cxx . . $ `<built-in>:450:9`!)
569 for (; i + 3 < n; i += 4) {
570 __m128 val = ggml_v_expf(_mm_sub_ps(_mm_loadu_ps(x + i),
571 _mm_set1_ps(max)));
572 _mm_storeu_ps(y + i, val);
573 #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__)
574 val = _mm_add_ps(val, _mm_movehl_ps(val, val));
575 val = _mm_add_ss(val, _mm_movehdup_ps(val));
576 #else
577 __m128 tmp = _mm_shuffle_ps(val, val, _MM_SHUFFLEcxx . . $ `<external>/8701576479610391844/xmmintrin.h:82:9`!(2, 3, 0, 1));
578 val = _mm_add_ps(val, tmp);
579 tmp = _mm_movehl_ps(tmp, val);
580 val = _mm_add_ss(val, tmp);
581 #endif
582 sum += (ggml_floatcxx . . $ ggml_float#)_mm_cvtss_f32(val);
583 }
584 #elif defined(__ARM_FEATURE_SVE) && defined(__aarch64__)
585 const int vlen = svcntw();
586 for (; i < n; i += vlen) {
587 const svbool_t pg = svwhilelt_b32_s32(i, n);
588 svfloat32_t val = ggml_v_expf(pg, svsub_f32_x(pg, svld1_f32(pg, x + i),
589 svdup_n_f32_x(pg, max)));
590 svst1_f32(pg, y + i, val);
591 sum += (ggml_float)svaddv_f32(pg, val);
592 }
593 #elif defined(__ARM_NEON) && defined(__aarch64__)
594 for (; i + 3 < n; i += 4) {
595 float32x4_t val = ggml_v_expf(vsubq_f32(vld1q_f32(x + i),
596 vdupq_n_f32(max)));
597 vst1q_f32(y + i, val);
598 sum += (ggml_float)vaddvq_f32(val);
599 }
600 #elif defined(__riscv_v_intrinsic)
601 vfloat64m1_t vsum = __riscv_vfmv_v_f_f64m1(0, 1);
602 for (int avl; i < n; i += avl) {
603 avl = __riscv_vsetvl_e32m2(n - i);
604 vfloat32m2_t val = ggml_v_expf_m2(__riscv_vfsub_vf_f32m2(__riscv_vle32_v_f32m2(&x[i], avl), max, avl), avl);
605 __riscv_vse32_v_f32m2(&y[i], val, avl);
606 vsum = __riscv_vfwredusum_vs_f32m2_f64m1(val, vsum, avl);
607 }
608 return (ggml_float)__riscv_vfmv_f_s_f64m1_f64(vsum);
609 #endif
610 for (; i < n; ++i) {
611 float val = expf(x[i] - max);
612 sum += (ggml_floatcxx . . $ ggml_float#)val;
613 y[i] = val;
614 }
615 return sum;
616 }
617
618 ggml_floatcxx . . $ ggml_float# ggml_vec_log_soft_max_f32cxx . . $ ggml_vec_log_soft_max_f32(4f1e42740f7851eb).(const int n, float * y, const float * x, float max) {
619 // log(soft_max) = log(soft_max_i / soft_max_sum) = log(soft_max_i) - log(soft_max_sum) = (logit_i - max) - log(soft_max_i)
620
621 int i = 0;
622 ggml_floatcxx . . $ ggml_float# sum = 0;
623 for (; i < n; ++i) {
624 float val = x[i] - max;
625 y[i] = val;
626 sum += (ggml_floatcxx . . $ ggml_float#)expf(val);
627 }
628 return sum = (ggml_floatcxx . . $ ggml_float#)logf(sum);
629 }