Skip to content

ggml-hrx: Q8_0/Q4_0/Q4_1/Q5_0/Q5_1/IQ4_NL/MXFP4 prompt matmuls on the int8 x4 kernel (4B Q8_0 pp512 889 -> 2657) - #80

Closed
bong-water-water-bong wants to merge 3 commits into
1bit/hrx-q2_0-prefillfrom
1bit/hrx-legacy-prefill
Closed

bong-water-water-bong wants to merge 3 commits into
1bit/hrx-q2_0-prefillfrom
1bit/hrx-legacy-prefill

Conversation

@bong-water-water-bong

Copy link
Copy Markdown

Prompt matmuls of the classic 32-value block formats, Q8_0, Q4_0, Q4_1, Q5_0, Q5_1, IQ4_NL and MXFP4, on the int8 q8_1 x4 WMMA kernel. Stacked on #79 (on #78). These formats no longer prefill on the generic f16 WMMA kernels, which also takes them out of the #284 f16-accumulation path.

How

  • One scale per 32 values matches one K32 block of the kernel, so its own contraction applies unchanged. motifs/nvfp4_q8_1_x4.loom stages two blocks per K chunk:
    • signed: Q8_0 as-is, Q4_0 (code - 8), Q5_0 (code - 16, fifth bits from qh), and the IQ4_NL and MXFP4 tables (the E8M0 half scale as ggml);
    • unsigned: Q4_1 and Q5_1 codes, with the block minimum m as the K32 correction, which the kernel already applies against the q8_1 activation sum.
  • AMD's mul_mat_q5_k_q8_plane_wmma.loom:
  • common.mul_mat.legacy_q8_1_x4_prefill (priority 300) uses the same matcher conditions as NVFP4 / Q2_0.

Validation, gfx1151, balanced power mode:

Check Result
test-hrx-legacy-prefill, known answer, all 7 formats x 4 shapes (integer activations with nonzero block sums, so a wrong offset or minimum fails) worst error 6.0e-8 of sum |w x| (MXFP4: exactly 0)
same, random weights vs CPU NMSE <= 1.3e-9
test-hrx-{nvfp4,q2_0}-prefill and all other HRX tests pass
test-backend-ops -b HRX0 1129/1129
Qwen3-0.6B Q4_K_M / UD-Q4_K_XL perplexity identical to before
Qwen3-4B Q8_0 (HF file) pp512 889 -> 2,657 tok/s
same, KLD vs CPU (wikitext-2 8 x 512) 0.00705 -> 0.00085, same top token 96.0 -> 98.6%

Qwen3-0.6B with each format's projections, pp512 before -> after and KLD vs CPU after (the generic f16 path gave 0.0032):

Q8_0 Q4_0 Q4_1 Q5_0 Q5_1 IQ4_NL MXFP4
pp512 12,436 -> 16,387 12,745 -> 16,728 12,524 -> 16,286 10,955 -> 16,225 10,887 -> 15,864 10,935 -> 12,601 10,652 -> 12,633
KLD 0.00152 0.00167 0.00184 0.00155 0.00167 0.00172 0.00164

The 0.6B is overhead-bound. The 4B Q8_0 row shows the size of the effect on a real model.

Not for the release pin.

🤖 Generated with Claude Code

bong-water-water-bong and others added 3 commits October 2, 2026 21:15
Q8_0, Q4_0, Q4_1, Q5_0, Q5_1, IQ4_NL and MXFP4 have one scale per 32 values, one K32 block of the q8_1 x4
kernel, so its own contraction applies: motifs/nvfp4_q8_1_x4.loom stages two blocks per K chunk. Q8_0, Q4_0
(code - 8), Q5_0 (code - 16), IQ4_NL and MXFP4 (their tables) as signed values; Q4_1 and Q5_1 as unsigned codes
with the block minimum as the K32 correction, which the kernel applies against the q8_1 activation sum.

AMD's file: the Q2_0 flag becomes a format selector (@ggml_own_x4_format), the signed-weight flag comes from
@ggml_own_x4_signed, the staging call takes the selector, and the weight_format config range widens to 80
(Q8_0); +52/-23 against its pre-#78 text.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ls take the q8_1 x4 kernel

common.mul_mat.legacy_q8_1_x4_prefill (priority 300) shares the NVFP4 / Q2_0 matcher conditions.
Qwen3-4B Q8_0, balanced mode: pp512 889 -> 2657 tok/s; KLD vs CPU 0.00705 -> 0.00085. Qwen3-0.6B with each
format's projections: pp512 +16..49%, KLD vs CPU 0.0015..0.0018 (the generic f16 path gave 0.0032).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-value block formats on the x4 path

For Q8_0, Q4_0, Q4_1, Q5_0, Q5_1, IQ4_NL and MXFP4: integer activations with |max| 127 per K32 block (exact
q8_1, nonzero block sums, so a wrong offset or minimum term fails) against rows quantized at scales
2^-8..2^6; HRX must match an f64 reference from to_float to 1e-5 of sum |w x| (worst 6e-8). Then random weights
against the CPU backend (NMSE <= 1.3e-9).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@bong-water-water-bong

Copy link
Copy Markdown
Author

Review (PR-Agent duty): good change. Three model checks before I approve, and the merge comes after the release, stacked after #79.

Good:

  • One scale per K32 block fits the kernel's contraction unchanged.
  • Q4_1/Q5_1 use their min through the existing asum correction; the others are staged signed.
  • The known-answer test covers 7 formats x 4 shapes with nonzero block sums (worst 6e-8).
  • AMD's file changes by +14/-13 in this PR, and our test file has the full header.
  • Qwen3-4B Q8_0: pp512 889 -> 2657, KLD 0.00705 -> 0.00085. That is a real quality fix as well as a speedup.

Q4_0 and MXFP4 back release-pin models whose prompts now take this path, so please measure (HRX0 vs the #79 tip, same settings):

  1. Qwen3.8-27B-Q4_0-H32 (Hadamard-rotated Q4_0, prism.hadamard route): PPL was 6.021. The rotation is applied to activations outside the matmul, so it should hold or improve; confirm, plus pp512.
  2. Ternary-Bonsai-2-27B Q4_0 (exact converted file, Hadamard 1024): KLD vs CPU was 0.000129 at ub 8; confirm at a prompt-sized ub, plus pp512.
  3. gpt-oss-20b MXFP4: KLD vs CPU was 0.027 (our known gap, hrx: keep routed-expert activations inside the f16 range, so a large SwiGLU output is not inf (engine #1539) #284). Report whether it moves, since its dense projections may now take the int8 path while MUL_MAT_ID stays where it is. Splits must stay at 1.

@bong-water-water-bong

Copy link
Copy Markdown
Author

Verified on strixhalo (gfx1151, balanced power mode 85 W). Build: this branch at 89fdac0, HRX0.

"Before" is the same binary with GGML_HRX_DISABLE_DISPATCH=legacy_q8_1_x4, which turns off this PR's new dispatch and stands in for the #79 tip. I did not build #79 separately. The "after" runs log legacy_q8_1_x4_prefill; the "before" runs do not.

Settings: wikitext-2 test, c512, b512 (so ub 512, prompt-sized), 8 chunks, -fa on, --no-repack. pp512 is llama-bench, 3 reps. Graph splits are 1 at bs=1 on every run.

model metric before after
Qwen3.8-27B-Q4_0-H32 PPL 6.7635 ± 0.371 6.7708 ± 0.371
pp512 67.87 217.44 (3.2x)
Ternary-Bonsai-2-27B Q4_0 mean KLD vs CPU 0.002132 0.000150
same top 96.77% 99.46%
max KLD 0.0447 0.0046
pp512 93.09 373.09 (4.0x)
gpt-oss-20b MXFP4 mean KLD vs CPU 0.028448 ± 0.0025 0.032127 ± 0.0029
same top 87.75% 87.55%
max KLD 1.99 2.68
pp512 1015.79 1034.11 (+1.8%)
  1. H32: PPL holds; +0.1% is well inside the error. The 6.021 in the review came from a different run length, so compare before and after here, not against that number.
  2. Bonsai-2: at ub 512 the PR brings KLD to 0.000150, close to the 0.000129 measured at ub 8. The "before" path at ub 512 was 14x worse.
  3. gpt-oss-20b: KLD moves from 0.0284 to 0.0321. The two error bars overlap, but max KLD also rises, from 1.99 to 2.68. So the dense MXFP4 projections on the int8 path do not close the hrx: keep routed-expert activations inside the f16 range, so a large SwiGLU output is not inf (engine #1539) #284 gap and may widen it slightly. Splits stay at 1.

The CPU baseline for Bonsai-2 is -dev none -ngl 0 -t 4, same file and settings. gpt-oss used the existing CPU KLD base from the #81 checks.

@bong-water-water-bong

Copy link
Copy Markdown
Author

Closed under the 2026-10-03 direction: new GPU kernels are HIP in the private add-on, the public fork keeps plumbing and fixes. Q8_0 and MXFP4 prompt GEMMs already run on HIP there (hip.mul_mat_q8_0_prefill, the GEAK MXFP4/Q6_K GEMMs); Q2_0 / NVFP4 / Q4_0-Q5_1 prompt matmuls will be HIP kernels when a model needs them. The branch stays for reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant