Skip to content

feat(qwen3.8 cuda): NVFP4 checkpoints on two ranks (--tp 2) - #355

Closed
gilby wants to merge 2 commits into
ashhart:mainfrom
gilby:qwen27-nvfp4-tp2
Closed

gilby wants to merge 2 commits into
ashhart:mainfrom
gilby:qwen27-nvfp4-tp2

Conversation

@gilby

@gilby gilby commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

A Qwen3.8-27B NVFP4 checkpoint (NVFP4 MLP, FP8 attention and DeltaNet, bf16 gates and head) now runs at --tp 2,
sharded as the MLX checkpoint is, every stored byte unchanged:

  • Fp4Linear / Fp8Linear gain outputs() (whole 64-output tiles of the loaded tiling; a zero tile pads an odd
    count) and inputs() (whole 64-input groups, four e4m3-scaled 16-input blocks each, the per-tensor scale kept). Each
    shard equals the projection loaded from the sliced checkpoint tensors, byte for byte.
  • partial(): the lane matmul with an unrounded fp32 epilogue, for the rank-ordered fp32 sum that rounds once to the
    bf16 output (row-invariant).
  • Plain / Plain8 (bf16 head and GDN gates, their e4m3 prompt copies) split by rows or columns as stored. A
    column shard of Plain8 remakes its e4m3 copy from its own bf16 rows (the copy's scales are per output row, so the
    bytes are the full copy's rows), so the 48-row GDN gates split to 24 a rank under --prefill-fp8 (second commit).
  • split_weights keeps the checkpoint's precision settings; the two-rank DFlash2 drafter takes its half of the draft
    vocabulary as views of the bf16 head.

The engine no longer refuses --tp 2 for NVFP4. EXL3 packs and checkpoint math (FP4/FP8 activations) still run on one
GPU and say so.

Receipt

  • Environment, with the TensorFold commit, MLX or PyTorch version, machine and GPU, checkpoint and revision: this
    change on 0.6.4 (before its rebase onto v0.6.5, with both porting fixes) against 0.6.4. PyTorch 2.13.0a0 (NGC 26.07 container, CUDA 13.3), Triton 3.7.1; one DGX Spark (GB10, sm_121); real checkpoint RadixArk/Qwen3.8-27B-NVFP4-BF16-LMHead @ 009632f with z-lab/Qwen3.8-27B-DFlash2 @ 50307d4.
  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false: in the tests: a
    two-rank verify window equals two-rank serial steps, prompts ignore chunking, logits match one GPU to bf16 rounding,
    and every shard equals the sliced checkpoint byte for byte (both ranks on one GPU in two threads). A served --tp 2
    run with upstream's tools: not run (see below). Resumed == fresh, on v0.6.5 (one DGX Spark): the same 2,682-token prompt three times on one server at temperature 0: fresh, resumed (2,677 tokens from the prompt cache) and resumed with "draft": false (one rank), all token_sha 03c531b17784.
  • Decode speed before and after, from tools/bench_openai.py: before is a refusal (--tp 2 refused for NVFP4); not run.
  • Prompt speed before and after, from tools/prefill_cold.py: one rank only, measured later on v0.6.5 (v0.6.5 → this): 2,610 / 2,848 / 2,713 → 2,603 / 2,844 / 2,734 tok/s at 2k / 8k / 16k. --tp 2: not run.
  • On v0.6.5: this branch rebased onto v0.6.5 (609ca41) as 3c3d9bb. Mac host suite (M5 Ultra): 5000 passed, 1 failed, 596 skipped; the one failure, test_glm5_prompt_kernels::test_fused_index_scores_are_the_three_ops, fails on v0.6.5 too. CUDA on v0.6.5 (one DGX Spark, GB10): pytest tests/cuda tests/test_cuda_build.py -k "qwen27 or qwen36 or nvfp4 or b16 or qmm or moe or build or distributed or dflash or dspark or exl3 or graph": 692 passed, 0 failed, 81 skipped.
  • Tests run, with pass, fail and skip counts: Mac host suite on this branch (M5 Ultra): 4692 passed, 1 failed, 595 skipped (the one failure, test_glm5_prompt_kernels::test_fused_index_scores_are_the_three_ops, fails on 0.6.4 too). On the DGX Spark, pytest tests/cuda tests/test_cuda_build.py -k "qwen27 or nvfp4 or b16 or qmm or build or distributed": 466 passed, 0 failed, 10
    skipped (0.6.4: 453 passed, 10 skipped). Fail-before: the new tests/cuda/test_qwen27_nvfp4_tp.py and the changed
    test_qwen27_nvfp4.py copied into the 0.6.4 tree: 11 failed, 3 errors, 13 passed. Two-process tests need two GPUs in
    one machine and skip.
  • Checked on v0.6.5 (2026-10-04): one DGX Spark (GB10), tools/prefill_cold.py (2k / 8k / 16k, 3 reps, median tok/s; runs v0.6.5, this, this, v0.6.5, each side the mean of its two) on RadixArk/Qwen3.8-27B-NVFP4-BF16-LMHead + z-lab/Qwen3.8-27B-DFlash2 at --context 131072 --parallel 1, a checkpoint v0.6.5 serves: v0.6.5 → this 2,601 → 2,597 / 2,853 → 2,854 / 2,736 → 2,732. Two NVLinked NVIDIA A40s (48 GB), this branch plus feat(cuda): Ampere (sm_80/86) runs the bf16 kernels; FP8 prompts stay at 8.9 and NVFP4 runs W4A16 below it #363's Ampere commit: --tp 2 cannot start the 27B even at --context 8192 ("0 tokens across the ranks"): startup still holds twice the checkpoint, which perf(cuda): two-rank NVFP4 layers shard as they load; row-parallel sums over a peer mapping #356 fixes. tests/cuda/test_qwen27_nvfp4_tp.py and test_qwen27_nvfp4.py on the two A40s (the two-process tests run there): copied into v0.6.5's tree 11 failed, 6 passed, 7 skipped, 3 errors; here 20 passed, 7 skipped.
  • Second commit (e453ecc, from review): --tp 2 with --prefill-fp8 refused to load because the 48-row gates' e4m3
    copy was cut on 64-output tiles. New test_bf16_gate_prompt_copies_split_at_any_row_count (48, 128 and 384 rows
    of 5120, split two ways): before the fix 48 fails with "a shard's outputs must be whole 64-output tiles", 128 and
    384 pass; after, all three pass. Run on a Tesla V100 (the test is tensor slicing only); the rest of
    test_qwen27_nvfp4_tp.py and test_qwen27_nvfp4.py needs sm_80+ and fails the same way on both sides there.
    A --prefill-fp8 prompt through a 24-row shard was not run: FP8 prompts need sm_89+, and the one-GPU path
    already runs the same copy with 48 rows in a 128-padded tile.
  • Not run, and why: a served --tp 2 receipt. Two DGX Sparks together were not set up, and two 48 GB A40s cannot start
    the 27B at this commit (see the v0.6.5 check); the two-rank serves are on perf(cuda): two-rank NVFP4 layers shard as they load; row-parallel sums over a peer mapping #356 and perf(cuda): two-rank sums through shared page-locked host memory where the GPUs cannot map each other #357.

Checklist

(Two ranks change the sum order relative to one GPU — rank-ordered fp32 partial sums — exactly as the existing MLX
--tp 2 does. Within --tp 2, the threaded tests show a verify window equal to serial steps; a served --tp 2 run needs #356's startup fix; see #356.)

An NVFP4 checkpoint (NVFP4 MLP, FP8 attention and DeltaNet, bf16 gates and head) now shards as the MLX checkpoint
does, every stored byte unchanged. Fp4Linear and Fp8Linear gain outputs() (whole 64-output tiles of the loaded
tiling, a zero tile padding an odd count) and inputs() (whole 64-input groups, four e4m3-scaled 16-input blocks
each, the per-tensor scale kept), and partial(), the lane matmul with an unrounded fp32 epilogue for the
rank-ordered sum. Plain and Plain8 split their rows or columns as stored. split_weights keeps the checkpoint's
precision settings, and the two-rank DFlash2 drafter takes its half of the draft vocabulary from the bf16 head.
The engine no longer refuses --tp 2 for NVFP4; EXL3 packs and checkpoint math still run on one GPU.
@BHCC2025

BHCC2025 commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

@gilby, one combination to look at: --tp 2 with --prefill-fp8 on the NVFP4 27B. The bf16 GDN gates in_proj_a and in_proj_b (48 rows) are loaded as Plain8 when fp8 is on (nvfp4_load.py:160), and split_output gives each rank 24 rows. Plain8.outputs then reaches Fp8Linear.outputs, whose _tiles_of needs whole 64-output tiles, so it raises at load. A direct call on a 48x5120 Plain8 reproduces it, and 128 and 384 rows split fine. The 48-row case in test_qwen27_nvfp4_tp.py uses Plain, not Plain8, so it isn't covered. Rebuilding rows8 from the sliced bf16 rows, or refusing the combination by name, would both work. --prefill-fp8 is off by default, so only that combination is affected. I haven't run a two-Spark serve.

…ount

With --prefill-fp8, the GDN gates a and b (48 rows) carry an e4m3 copy for prompts. A two-rank shard sliced that
copy by whole 64-output tiles, which 24 rows a rank are not, so --tp 2 with --prefill-fp8 refused to load. A shard
now makes its copy from its own bf16 rows; the copy's scales are per output row, so the bytes are the full copy's
rows at any row count.

tests/cuda/test_qwen27_nvfp4_tp.py: a 48x5120 gate (and 128, 384 rows) splits two ways into the copies made from
the sliced rows.
@gilby

gilby commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

@BHCC2025, thanks, you were right. Fixed in e453ecc: a column shard of Plain8 now remakes its e4m3 prompt copy from its own sliced bf16 rows instead of cutting the full copy on 64-output tiles. The copy's scales are per output row, so a shard's bytes are the full copy's rows at any row count, and the 48-row gates split to 24 a rank under --prefill-fp8. test_bf16_gate_prompt_copies_split_at_any_row_count in tests/cuda/test_qwen27_nvfp4_tp.py covers it: a 48x5120 Plain8 (plus 128 and 384 rows) split two ways must equal the copies made from the sliced rows. It fails before the fix at 48 rows with the error you saw and passes after. I haven't run a two-Spark --prefill-fp8 serve either. The PRs stacked on this one carry a copy of its first commit and will pick this fix up when they are restacked.

@ashhart

ashhart commented Oct 6, 2026

Copy link
Copy Markdown
Owner

Thank you, @gilby. Shards that equal the projection loaded from the sliced checkpoint byte for byte, and a partial with an unrounded fp32 epilogue so the rank sum rounds once, make two-rank NVFP4 exact against two-rank serial decoding.

The Python engine is frozen (#286), so this won't merge. On the Zig engine it waits for Zig CUDA, which runs one GPU today with no NVFP4 path, and for Qwen3.8-27B as a Zig family. The rounding rule already matches the Zig engine's multi-Mac cluster, which sums fp32 partials in a fixed order and rounds once. When ranks come to Zig CUDA, your outputs and inputs shards are where NVFP4 starts, with your credit.

@ashhart ashhart closed this Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants