Skip to content

Commit bde1b96

Browse files
committed
ggml-cpu: replace AArch64 NEON assembly with intrinsics in ggml_gemv_q4_0_4x4_q8_0()
Signed-off-by: Adrien Gallouët <[email protected]>
1 parent 76b27d2 commit bde1b96

File tree

1 file changed

+36
-56
lines changed

1 file changed

+36
-56
lines changed

ggml/src/ggml-cpu/ggml-cpu-aarch64.c

Lines changed: 36 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -531,62 +531,42 @@ void ggml_gemv_q4_0_4x4_q8_0(int n, float * restrict s, size_t bs, const void *
531531

532532
#if ! ((defined(_MSC_VER)) && ! defined(__clang__)) && defined(__aarch64__) && defined(__ARM_NEON)
533533
if (ggml_cpu_has_neon() && ggml_cpu_has_dotprod()) {
534-
const void * b_ptr = vx;
535-
const void * a_ptr = vy;
536-
float * res_ptr = s;
537-
538-
__asm__ __volatile__(
539-
"movi v31.16b, #0x4\n"
540-
"movi v30.16b, #0xf0\n"
541-
"add %x[b_ptr], %x[b_ptr], #0x8\n"
542-
"1:" // Column loop
543-
"add x22, %x[a_ptr], #0x2\n"
544-
"movi v29.16b, #0x0\n"
545-
"mov x21, %x[nb]\n"
546-
"2:" // Block loop
547-
"ldr q28, [%x[b_ptr], #0x0]\n"
548-
"ldr q27, [x22, #0x0]\n"
549-
"movi v26.4s, #0x0\n"
550-
"sub x20, x22, #0x2\n"
551-
"ldr q25, [x22, #0x10]\n"
552-
"ldr q24, [%x[b_ptr], #0x10]\n"
553-
"sub x21, x21, #0x1\n"
554-
"add x22, x22, #0x22\n"
555-
"ldr q23, [%x[b_ptr], #0x20]\n"
556-
"ldr q22, [%x[b_ptr], #0x30]\n"
557-
"ld1r { v21.8h }, [x20]\n"
558-
"ldr q20, [%x[b_ptr], #-0x8]\n"
559-
"sshl v16.16b, v28.16b, v31.16b\n"
560-
"and v28.16b, v28.16b, v30.16b\n"
561-
"sshl v19.16b, v24.16b, v31.16b\n"
562-
"and v24.16b, v24.16b, v30.16b\n"
563-
"add %x[b_ptr], %x[b_ptr], #0x48\n"
564-
"sshl v18.16b, v23.16b, v31.16b\n"
565-
"and v23.16b, v23.16b, v30.16b\n"
566-
".inst 0x4f9be21a // sdot v26.4s, v16.16b, v27.4b[0]\n"
567-
"sshl v17.16b, v22.16b, v31.16b\n"
568-
"and v22.16b, v22.16b, v30.16b\n"
569-
"fcvtl v21.4s, v21.4h\n"
570-
"fcvtl v16.4s, v20.4h\n"
571-
".inst 0x4f99e39a // sdot v26.4s, v28.16b, v25.4b[0]\n"
572-
"fmul v16.4s, v16.4s, v21.4s\n"
573-
".inst 0x4fbbe27a // sdot v26.4s, v19.16b, v27.4b[1]\n"
574-
".inst 0x4fb9e31a // sdot v26.4s, v24.16b, v25.4b[1]\n"
575-
".inst 0x4f9bea5a // sdot v26.4s, v18.16b, v27.4b[2]\n"
576-
".inst 0x4f99eafa // sdot v26.4s, v23.16b, v25.4b[2]\n"
577-
".inst 0x4fbbea3a // sdot v26.4s, v17.16b, v27.4b[3]\n"
578-
".inst 0x4fb9eada // sdot v26.4s, v22.16b, v25.4b[3]\n"
579-
"scvtf v26.4s, v26.4s, #0x4\n"
580-
"fmla v29.4s, v26.4s, v16.4s\n"
581-
"cbnz x21, 2b\n"
582-
"sub %x[nc], %x[nc], #0x4\n"
583-
"str q29, [%x[res_ptr], #0x0]\n"
584-
"add %x[res_ptr], %x[res_ptr], #0x10\n"
585-
"cbnz %x[nc], 1b\n"
586-
: [b_ptr] "+&r" (b_ptr), [res_ptr] "+&r" (res_ptr), [nc] "+&r" (nc)
587-
: [a_ptr] "r" (a_ptr), [nb] "r" (nb)
588-
: "memory", "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31", "x20", "x21", "x22"
589-
);
534+
const block_q4_0x4 *b_ptr = (const block_q4_0x4 *)vx;
535+
536+
for (int c = 0; c < nc; c += ncols_interleaved) {
537+
const block_q8_0 *a_ptr = (const block_q8_0 *)vy;
538+
float32x4_t acc = vdupq_n_f32(0);
539+
for (int b = 0; b < nb; b += 1) {
540+
int8x16_t b0 = vld1q_s8((const int8_t *)b_ptr->qs);
541+
int8x16_t b1 = vld1q_s8((const int8_t *)b_ptr->qs + 16);
542+
int8x16_t b2 = vld1q_s8((const int8_t *)b_ptr->qs + 32);
543+
int8x16_t b3 = vld1q_s8((const int8_t *)b_ptr->qs + 48);
544+
float16x4_t bd = vld1_f16((const __fp16 *)b_ptr->d);
545+
546+
int8x16_t a0 = vld1q_s8(a_ptr->qs);
547+
int8x16_t a1 = vld1q_s8(a_ptr->qs + qk/2);
548+
float16x4_t ad = vld1_dup_f16((const __fp16 *)&a_ptr->d);
549+
550+
int32x4_t ret = vdupq_n_s32(0);
551+
552+
ret = vdotq_laneq_s32(ret, b0 << 4, a0, 0);
553+
ret = vdotq_laneq_s32(ret, b1 << 4, a0, 1);
554+
ret = vdotq_laneq_s32(ret, b2 << 4, a0, 2);
555+
ret = vdotq_laneq_s32(ret, b3 << 4, a0, 3);
556+
557+
ret = vdotq_laneq_s32(ret, b0 & 0xf0U, a1, 0);
558+
ret = vdotq_laneq_s32(ret, b1 & 0xf0U, a1, 1);
559+
ret = vdotq_laneq_s32(ret, b2 & 0xf0U, a1, 2);
560+
ret = vdotq_laneq_s32(ret, b3 & 0xf0U, a1, 3);
561+
562+
acc = vfmaq_f32(acc, vcvtq_n_f32_s32(ret, 4),
563+
vmulq_f32(vcvt_f32_f16(ad), vcvt_f32_f16(bd)));
564+
a_ptr++;
565+
b_ptr++;
566+
}
567+
vst1q_f32(s, acc);
568+
s += ncols_interleaved;
569+
}
590570
return;
591571
}
592572
#endif // #if ! ((defined(_MSC_VER)) && ! defined(__clang__)) && defined(__aarch64__) && defined(__ARM_NEON)

0 commit comments

Comments
 (0)