q8_0_r4: NEON

We get PP-512(LLaMA-3.1-8B) = 112.6 t/s on M2-Max.
This commit is contained in:
Iwan Kawrakow
2024-12-02 18:40:31 +01:00
parent e1b922d3f3
commit fc781beec0

View File

@@ -6916,6 +6916,43 @@ void mul_mat_q4_0_r4_q8_0(int n, const void * vx, size_t bx, const DataInfo& inf
}
}
template <int nrc_y>
void mul_mat_q8_0_r4_q8_0(int n, const void * vx, size_t bx, const DataInfo& info, int nrc_x) {
GGML_ASSERT(nrc_x%4 == 0);
Q8<nrc_y, block_q8_0_x4> q8(info);
int nb = n / QK8_0;
GGML_ASSERT(nb%4 == 0);
float32x4_t acc[nrc_y] = {};
for (int ix = 0; ix < nrc_x; ix += 4) {
const block_q8_0_x4 * iq8 = (const block_q8_0_x4 *)((const char *)vx + ix*bx);
for (int ib4 = 0; ib4 < nb/4; ++ib4) {
for (int k = 0; k < 4; ++k) {
auto scales = vcvt_f32_f16(vld1_f16((const float16_t *)iq8[4*ib4+k].d));
auto qx1 = vld1q_s8_x4(iq8[4*ib4+k].qs);
auto qx2 = vld1q_s8_x4(iq8[4*ib4+k].qs+64);
for (int iy = 0; iy < nrc_y; ++iy) {
auto y = vld1q_s8_x2(q8.y[iy][ib4].qs+32*k);
auto sumi = vdupq_n_s32(0);
sumi = vdotq_laneq_s32(sumi, qx1.val[0], y.val[0], 0);
sumi = vdotq_laneq_s32(sumi, qx1.val[1], y.val[1], 0);
sumi = vdotq_laneq_s32(sumi, qx1.val[2], y.val[0], 1);
sumi = vdotq_laneq_s32(sumi, qx1.val[3], y.val[1], 1);
sumi = vdotq_laneq_s32(sumi, qx2.val[0], y.val[0], 2);
sumi = vdotq_laneq_s32(sumi, qx2.val[1], y.val[1], 2);
sumi = vdotq_laneq_s32(sumi, qx2.val[2], y.val[0], 3);
sumi = vdotq_laneq_s32(sumi, qx2.val[3], y.val[1], 3);
auto d4d8 = vmulq_f32(scales, vdupq_n_f32(GGML_FP16_TO_FP32(q8.y[iy][ib4].d[k])));
acc[iy] = vfmaq_f32(acc[iy], d4d8, vcvtq_f32_s32(sumi));
}
}
}
for (int iy = 0; iy < nrc_y; ++iy) {
info.store(ix, iy, acc[iy]);
acc[iy] = vdupq_n_f32(0.f);
}
}
}
template <typename Dequantizer> void MulMat::set_functions(MulMat& m) {
if constexpr (std::is_same_v<Dequantizer, DequantizerQ40> || std::is_same_v<Dequantizer, DequantizerQ50> ||
std::is_same_v<Dequantizer, DequantizerQ80> || std::is_same_v<Dequantizer, DequantizerIQ4NL> ||
@@ -7107,6 +7144,17 @@ bool MulMat::prepare(int typeA, int typeB, int ne00, MulMat& m, int /*Ny*/) {
m.funcs[7] = mul_mat_q4_0_r4_q8_0<8>;
expected_Btype = GGML_TYPE_Q8_0;
break;
case GGML_TYPE_Q8_0_R4:
m.funcs[0] = mul_mat_q8_0_r4_q8_0<1>;
m.funcs[1] = mul_mat_q8_0_r4_q8_0<2>;
m.funcs[2] = mul_mat_q8_0_r4_q8_0<3>;
m.funcs[3] = mul_mat_q8_0_r4_q8_0<4>;
m.funcs[4] = mul_mat_q8_0_r4_q8_0<5>;
m.funcs[5] = mul_mat_q8_0_r4_q8_0<6>;
m.funcs[6] = mul_mat_q8_0_r4_q8_0<7>;
m.funcs[7] = mul_mat_q8_0_r4_q8_0<8>;
expected_Btype = GGML_TYPE_Q8_0;
break;
default:
return false;
}