Skip to content

Commit e95085a

Browse files
Ronsorggerganov
authored andcommitted
Don't use vdotq_s32 if it's not available (#139)
* Don't use vdotq_s32 if it's not available `dotprod` extensions aren't available on some ARM CPUs (e.g. Raspberry Pi 4), so check for them and only use them if they're available. Reintroduces the code removed in 84d9015 if `__ARM_FEATURE_DOTPROD` isn't defined. * Update ggml.c --------- Co-authored-by: Georgi Gerganov <[email protected]>
1 parent ef1a9b3 commit e95085a

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

ggml.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1364,8 +1364,8 @@ inline static void ggml_vec_dot_q4_0(const int n, float * restrict s, const void
13641364
const int8x16_t v0_1hs = vsubq_s8(v0_1h, s8b);
13651365
const int8x16_t v1_1hs = vsubq_s8(v1_1h, s8b);
13661366

1367+
#if defined(__ARM_FEATURE_DOTPROD)
13671368
// dot product into int16x8_t
1368-
// assume that vdotq_s32 is always available, if not, should check for __ARM_FEATURE_DOTPROD
13691369
int32x4_t p_0 = vdotq_s32(vdupq_n_s32(0), v0_0ls, v1_0ls);
13701370
int32x4_t p_1 = vdotq_s32(vdupq_n_s32(0), v0_1ls, v1_1ls);
13711371

@@ -1379,6 +1379,37 @@ inline static void ggml_vec_dot_q4_0(const int n, float * restrict s, const void
13791379
#else
13801380
sum0 += d0_0*d1_0*(vgetq_lane_s32(p_0, 0) + vgetq_lane_s32(p_0, 1) + vgetq_lane_s32(p_0, 2) + vgetq_lane_s32(p_0, 3));
13811381
sum1 += d0_1*d1_1*(vgetq_lane_s32(p_1, 0) + vgetq_lane_s32(p_1, 1) + vgetq_lane_s32(p_1, 2) + vgetq_lane_s32(p_1, 3));
1382+
#endif
1383+
#else
1384+
const int16x8_t pl0l = vmull_s8(vget_low_s8 (v0_0ls), vget_low_s8 (v1_0ls));
1385+
const int16x8_t pl0h = vmull_s8(vget_high_s8(v0_0ls), vget_high_s8(v1_0ls));
1386+
1387+
const int16x8_t ph0l = vmull_s8(vget_low_s8 (v0_0hs), vget_low_s8 (v1_0hs));
1388+
const int16x8_t ph0h = vmull_s8(vget_high_s8(v0_0hs), vget_high_s8(v1_0hs));
1389+
1390+
const int16x8_t pl1l = vmull_s8(vget_low_s8 (v0_1ls), vget_low_s8 (v1_1ls));
1391+
const int16x8_t pl1h = vmull_s8(vget_high_s8(v0_1ls), vget_high_s8(v1_1ls));
1392+
1393+
const int16x8_t ph1l = vmull_s8(vget_low_s8 (v0_1hs), vget_low_s8 (v1_1hs));
1394+
const int16x8_t ph1h = vmull_s8(vget_high_s8(v0_1hs), vget_high_s8(v1_1hs));
1395+
1396+
const int16x8_t pl_0 = vaddq_s16(pl0l, pl0h);
1397+
const int16x8_t ph_0 = vaddq_s16(ph0l, ph0h);
1398+
1399+
const int16x8_t pl_1 = vaddq_s16(pl1l, pl1h);
1400+
const int16x8_t ph_1 = vaddq_s16(ph1l, ph1h);
1401+
1402+
const int16x8_t p_0 = vaddq_s16(pl_0, ph_0);
1403+
const int16x8_t p_1 = vaddq_s16(pl_1, ph_1);
1404+
1405+
// scalar
1406+
#if defined(__ARM_FEATURE_QRDMX)
1407+
sum0 += d0_0*d1_0*vaddvq_s16(p_0);
1408+
sum1 += d0_1*d1_1*vaddvq_s16(p_1);
1409+
#else
1410+
sum0 += d0_0*d1_0*(vgetq_lane_s16(p_0, 0) + vgetq_lane_s16(p_0, 1) + vgetq_lane_s16(p_0, 2) + vgetq_lane_s16(p_0, 3) + vgetq_lane_s16(p_0, 4) + vgetq_lane_s16(p_0, 5) + vgetq_lane_s16(p_0, 6) + vgetq_lane_s16(p_0, 7));
1411+
sum1 += d0_1*d1_1*(vgetq_lane_s16(p_1, 0) + vgetq_lane_s16(p_1, 1) + vgetq_lane_s16(p_1, 2) + vgetq_lane_s16(p_1, 3) + vgetq_lane_s16(p_1, 4) + vgetq_lane_s16(p_1, 5) + vgetq_lane_s16(p_1, 6) + vgetq_lane_s16(p_1, 7));
1412+
#endif
13821413
#endif
13831414
}
13841415

0 commit comments

Comments
 (0)