Skip to content

ggml-hrx: NVFP4 prompt matmuls on the int8 q8_1 x4 WMMA kernel (pp512 107 -> 323) - #78

Closed
bong-water-water-bong wants to merge 4 commits into
1bit/hrx-getrows-iqfrom
1bit/hrx-nvfp4-prefill
Closed

bong-water-water-bong wants to merge 4 commits into
1bit/hrx-getrows-iqfrom
1bit/hrx-nvfp4-prefill

Conversation

@bong-water-water-bong

Copy link
Copy Markdown

NVFP4 prompt matmuls on HRX's int8 q8_1 x4 WMMA kernel instead of the generic f16 WMMA path. Stacked on #77 (on #76, on #75).

NVFP4 prefill no longer takes the f16 WMMA kernels (common.mul_mat.f32_f32_wmma, common.mul_mat_swiglu.f32_f32_wmma), so the #284 f16-accumulation fix can skip NVFP4.

How

  • The q8_1 x4 kernel (ops/mul_mat_q5_k_q8_plane_wmma.loom, export ggml_mul_mat_q5_k_iq4_xs_q8_1_x4_wmma_token256) stages 64-value K chunks, which is exactly one NVFP4 block. Our motifs/nvfp4_q8_1_x4.loom stages it as signed E2M1 codes (kvalues_mxfp4, -12..12, exact in i8) plus its four halved UE4M3 scales. NVFP4 has no minimum, so the kernel's correction slots hold the second 16-value scale of each K32 block.
  • A K32 block holds two NVFP4 scale groups. At format 43, each output tile keeps its two 16-wide int8 WMMA products separate and scales each by its own scale before applying the q8_1 activation scale. The other formats sum the two in i32 under one K32 scale.
  • Hooks in AMD's file: the format flag, signed staged weights, a staging branch, and that per-tile select at the 16 contraction sites of the unpaired path. The flag is a compile-time constant. Our code is in our own files (motifs/nvfp4_q8_1_x4.loom, dispatch-mul-mat-nvfp4.{h,cpp}, the test).
  • common.mul_mat.nvfp4_q8_1_x4_prefill (priority 300) claims NVFP4 MUL_MAT when: tokens are 256..2048 in multiples of 256, K is a multiple of 256, output is a multiple of 64. Activations go through the existing common_prepare_q8_1_x4_input. Everything else stays on the current paths.
    • No split tail (the token-tail policy in 1bit/hrx-q8-tail is not merged).
    • Gate/up claim their matmuls separately, consistent with 1bit/hrx-q8-prefill-policy.
    • None of the three unmerged q8 branches is touched.

Validation, gfx1151, balanced power mode:

Check Result
test-hrx-nvfp4-prefill, known answer: integer activations (exact q8_1), raw blocks cycling every UE4M3 byte, f64 reference from dequantize_row_nvfp4; K 256-5120, 256-1024 tokens worst error 2.3e-7 of sum |w x| (limit 1e-5)
same shapes, random weights vs CPU backend NMSE 7e-15 .. 1.5e-9
Other formats on the x4 kernel: Qwen3-0.6B Q4_K_M and UD-Q4_K_XL perplexity, before vs after identical to every printed digit, through the Q4_K, Q5_K/IQ4_XS and paired SwiGLU x4 kernels
test-backend-ops -b HRX0 1129/1129
test-hrx-{nvfp4,q2_0,get-rows-iq,mxfp4,decode-stride} pass
Qwen3.8-27B NVFP4 (cdiamond), routing graph splits = 1; prefill matmuls on nvfp4_q8_1_x4_prefill, no NVFP4 on mul_mat(_swiglu).f32_f32_wmma
same, pp512 107 -> 323 tok/s
same, KLD vs CPU (wikitext-2 8 x 512) 0.00462 -> 0.00423, same top token 95.8 -> 96.1%
same, tg128 7.98 +/- 0.76 (decode path unchanged)

Not for the release pin.

🤖 Generated with Claude Code

bong-water-water-bong and others added 3 commits October 2, 2026 20:28
The q8_1 x4 kernel (ops/mul_mat_q5_k_q8_plane_wmma.loom) stages 64-value K chunks, which is exactly one NVFP4
block. motifs/nvfp4_q8_1_x4.loom stages it as signed E2M1 codes (kvalues_mxfp4, -12..12) and its four halved
UE4M3 scales; NVFP4 has no minimum, so the correction slots carry the second 16-value scale of each K32 block.

A K32 block holds two NVFP4 scale groups, so at weight format 43 each output tile keeps its two 16-wide int8
WMMA products apart and scales each by its own scale before the q8_1 activation scale (the other formats sum
them in i32 under one K32 scale). The hooks in AMD's file are the format flag, signed staged weights, the
staging branch and that per-tile select; the flag is a compile-time constant, so other formats compile as
before (Qwen3-0.6B Q4_K_M and UD-Q4_K_XL perplexity on HRX0 identical to the digit, through the Q4_K, Q5_K /
IQ4_XS and paired SwiGLU x4 kernels).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
common.mul_mat.nvfp4_q8_1_x4_prefill (dispatch-mul-mat-nvfp4.cpp, priority 300) claims NVFP4 MUL_MAT at 256..2048
tokens in multiples of 256, input sizes in multiples of 256 and output sizes in multiples of 64: the activations
are quantized to q8_1 x4 once (common_prepare_q8_1_x4_input) and the kernel runs at weight format 43. NVFP4
prompts no longer take the generic f16 WMMA kernels (common.mul_mat.f32_f32_wmma,
common.mul_mat_swiglu.f32_f32_wmma), so the f16 accumulation of engine #284 no longer applies to them.

Qwen3.8-27B NVFP4 (cdiamond), balanced mode: pp512 107 -> 323 tok/s; KLD vs CPU 0.00462 -> 0.00423.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…P4 q8_1 x4 path

Known answer: integer activations with |max| 127 in every K32 block (exact q8_1, d = 1) against raw NVFP4 blocks
whose scales cycle through every UE4M3 byte; HRX must match an f64 reference from dequantize_row_nvfp4 to 1e-5
of sum |w x| per output (it is within 2.4e-7). A shared or swapped 16-value scale fails by orders of magnitude.
Then quantized random weights against the CPU backend. Both must plan the q8_1 quantize + x4 kernel.

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

Copy link
Copy Markdown
Author

Review (PR-Agent duty): the design and results are good; one change requested before merge (after Sunday's release either way).

Good:

  • Reusing the q8_1 x4 kernel at format 43 is a good fit: its 64-value K chunk is one NVFP4 block.
  • The two 16-wide products are scaled separately at the 16/32 boundary.
  • Known-answer test: worst 2.3e-7 of sum|w x|. Other formats' PPL is identical before and after. Suite 1129/1129.
  • 27B NVFP4: pp512 107 -> 323, KLD 0.00462 -> 0.00423.
  • The matcher is in our own file, with our full header on all four new files.

Change requested: ops/mul_mat_q5_k_q8_plane_wmma.loom is AMD's file (no notice, AMD authorship), and the PR changes about 172 lines in it. Our fork rule is that our code lives in our own files, with only minimal hooks in AMD's. Please move the NVFP4 logic into motifs/nvfp4_q8_1_x4.loom (or a new file of ours) so the AMD file keeps only:

  • the compile-time flag;
  • one call per site into our motif (for example a single @nvfp4_scale_pair helper that takes the two 16-wide products and returns the scaled sum), not inline selects at all 16 contraction sites;
  • the staging branch as one call.

Don't copy AMD's kernel into our file to get around this; minimal hooks in theirs is the right shape. After the refactor, rerun the known-answer test, the Q4_K_M / UD-Q4_K_XL PPL identity check and the 27B pp512/KLD.

…nel file

Review on #78: the NVFP4 two-scale contraction and the staging gate move into motifs/nvfp4_q8_1_x4.loom
(@ggml_nvfp4_x4_tile, @ggml_nvfp4_x4_stage). AMD's ops/mul_mat_q5_k_q8_plane_wmma.loom keeps the format flag,
the signed-weight flag, one staging call in place of the lane comparison, and one helper call per contraction
site (the original affine result is passed in and kept for other formats): 46 lines added, 22 removed against
its pre-#78 text, down from 150 added. The helpers take only values, never calling back into the kernel file.

Same results as before: known answer within 2.3e-7 of sum |w x|, CPU NMSE <= 1.5e-9, Qwen3-0.6B Q4_K_M and
UD-Q4_K_XL perplexity identical, test-backend-ops 1129/1129, 27B NVFP4 KLD 0.004225 / PPL 6.651781 (identical),
pp512 321 +/- 9.

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

Copy link
Copy Markdown
Author

Review addressed in 16a03aa: the NVFP4 contraction and staging logic now live in motifs/nvfp4_q8_1_x4.loom (@ggml_nvfp4_x4_tile, @ggml_nvfp4_x4_stage). AMD's mul_mat_q5_k_q8_plane_wmma.loom keeps the format flag, the signed-weight flag, one staging call in place of the lane comparison, and one helper call per contraction site: 46 lines added / 22 removed against its pre-#78 text (was 150 / 22). Nothing of AMD's kernel is copied into our file.

Reran, all the same as before: known answer within 2.3e-7 of sum |w x|; vs CPU NMSE <= 1.5e-9; Qwen3-0.6B Q4_K_M and UD-Q4_K_XL perplexity identical (through the Q4_K, Q5_K/IQ4_XS and paired SwiGLU x4 kernels); test-backend-ops 1129/1129; the other HRX tests pass; 27B NVFP4 KLD 0.004225 and PPL 6.651781 (identical), pp512 321 +/- 9, tg128 7.96.

🤖 Generated with Claude Code

@bong-water-water-bong

Copy link
Copy Markdown
Author

Change made in 16a03aa, approved. AMD's mul_mat_q5_k_q8_plane_wmma.loom is now down to +46/-22: the format and signed-weight flags, one staging call, and one helper call per contraction site that keeps the original affine result for every other format. The NVFP4 logic is in our motif (@ggml_nvfp4_x4_tile / @ggml_nvfp4_x4_stage), and none of AMD's kernel is copied. Reruns are identical (known answer, NMSE, other-format PPL, 1129/1129, 27B KLD 0.004225, pp512 321). I'll merge after Sunday's release, stacked after #75-#77.

@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