ggml-cpu: faster AVX2 NVFP4 x Q8_0 dot product (two blocks per iteration, vector UE4M3 scales)

The x86 AVX2 ggml_vec_dot_nvfp4_q8_0 did one block per iteration with four
scalar UE4M3 table lookups, two scalar FP16 conversions, four scalar muls and
broadcasts per 64 weights, plus two FMAs on one accumulator chain.

New AVX2 path, two blocks (128 weights) per iteration:
- E2M1 decode with 256-bit pshufb on the whole 32-byte qs, unpack + permute2x128
  into the q8_0 element order (low nibbles are elements 0-7 of a sub-block,
  high nibbles 8-15);
- the 8 sub-block sums reduced exactly in int16 (a sub-block sum is at most
  16*12*127) with two levels of hadd_epi16 and one madd, one cvt;
- the 8 UE4M3 scales decoded in one vector (bit manipulation, bit-exact with
  ggml_ue4m3_to_fp32 for all 256 bytes: x0.5, 0x7F -> 0, sign bit ignored,
  subnormals), the 4 q8_0 FP16 scales assembled in a GPR and converted with one
  vcvtph2ps; one mul and one FMA per iteration.
An odd trailing block uses the existing scalar loop. The AVX (non-AVX2) path
and the generic reference are unchanged.

tests: test-backend-ops perf cases at Inkling-2-Mini expert shapes (n_embd 2304,
gate_up 2304, down 1152, 128 experts / 8 used; n = 1, 8, 16, 32) for nvfp4,
mxfp4 and q4_0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PZz44SLQvTXMyWGio6t9DZ
This commit is contained in:
LumpiastyandClaude Opus 5.5 committed 2026-11-23 00:01:59 +01:00
1 parent 27c23d20fe
commit af815458ca
2 files changed
+63 -37

No files matched your search

+10
View File
@@ -14226,6 +14226,16 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_perf() {
}
}
// Inkling-2-Mini experts (NVFP4 in UD-Q4_K_XL): 128 experts, 8 used, n_embd 2304, gate_up 2x1152 fused, down 1152
for (int bs : {1, 8, 16, 32}) {
for (ggml_type type_a : {GGML_TYPE_NVFP4, GGML_TYPE_MXFP4, GGML_TYPE_Q4_0}) {
test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 2304, bs, 2304, {1, 1}, {1, 1}));
test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 2304, bs, 1152, {1, 1}, {1, 1}));
test_cases.emplace_back(new test_mul_mat_id(type_a, GGML_TYPE_F32, 128, 8, false, 2304, bs, 2304));
test_cases.emplace_back(new test_mul_mat_id(type_a, GGML_TYPE_F32, 128, 8, false, 2304, bs, 1152));
}
}
for (int K : {3, 5}) {
for (int IC : {256, 2560}) {
for (int IW_IH : {32, 64, 256}) {