Skip to content

Flash Next CUDA: routed NVFP4 experts in the checkpoint's own math (FP4 x FP4), prompts −9…−10 % - #211

Closed
jschmied wants to merge 2 commits into
ashhart:mainfrom
jschmied:flashnext-fp4-experts
Closed

jschmied wants to merge 2 commits into
ashhart:mainfrom
jschmied:flashnext-fp4-experts

Conversation

@jschmied

@jschmied jschmied commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #173. In 0.6.1 --precision checkpoint reaches the 27B only: precision.mode() and
cuda/nvfp4/checkpoint.py are used by families/qwen3_5, and Flash Next's routed experts still run
nvfp4_expert_kernel on bf16 rows whatever the flag. This puts them under the same contract.

  • Rows enter gate|up once a layer in NVFP4 (quant4) under the experts' static input scale. The checkpoint stores
    one per expert; ours holds a single value for all 512 in every layer but layer 0 (two values), so the layer takes the
    largest, as FlashInfer's CUTLASS MoE does.
  • cuda/nvfp4/experts_ck.cu: a warp per (plan item, 32 columns) on the block-scaled FP4 mma (mma4.cuh), against
    the lane matmul's words and block scales (pack4) stacked per projection and re-laid lane-major, four K steps in
    flight. gate|up's epilogue is SiLU(gate) * up in fp32, quantized to down's NVFP4 input under each expert's own down
    input scale (nvfp4q.cuh, as gemm_gu_ck does); down writes each pair's row in fp32 or bf16.
  • One fp32 chain over K a pair, so a pair's bits never depend on the others: drafted == serial and resumed == fresh
    hold. Same weight bytes as the W4A16 blocks.
  • One rank on SM 12.x. --precision full, two ranks and other GPUs keep nvfp4_expert_kernel; so do the MTP
    layer's draft-only experts. The startup line names the experts' math. A checkpoint without the experts' input scales
    is refused under checkpoint (as the 27B's loader does).

Measured

One DGX Spark (GB10), our ModelOpt export (NVFP4 routed experts, block-FP8 dense layers), --precision full vs
checkpoint on this branch, one process an arm
(data):

full checkpoint
8k prompt, prefill (s), 3 alternating rounds 6.11 / 6.13 / 6.18 5.57 / 5.60 / 5.56 −8.7…−9.9 %
32k prompt, prefill (s) 24.51 / 24.62 / 24.78 22.32 / 22.41 / 22.33 −8.9…−9.9 %
8k prompt, routed experts' kernels 2,154 ms 1,597 ms −26 %
decode, 8 prompts x 256 greedy tokens, drafts on 52.45 tok/s 52.1 tok/s −0.6 %

The first 16 tokens after both prompts are the same in both modes. Decode rounds are 3 % faster (54.6 vs 56.3 ms) and
carry 3.8 % fewer tokens: the replies differ, and so do their drafts.

Quality, teacher-forced, 8 sequences of 4,096 positions (wikitext-2 test x 4, CPython source x 4), the decode path,
against --precision full (Flash Next has no fp32 forward):

KL(full ‖ checkpoint) top-1 agreement perplexity
wikitext 0.051 93.0 % +1.29 %
code 0.026 98.3 % +1.26 %
all 0.038 95.6 % +1.27 %

KL takes the reference's top 32 tokens and the rest as one bucket.

Tests

  • tests/cuda/test_nvfp4_experts_ck.py: gate|up's NVFP4 bytes against a torch quantizer of the fp64 SwiGLU (fewer
    than 1 % of bytes differ, where an fp32 sum sits on an e2m1 edge); down equals the fp64 product of its NVFP4 rows to
    1e-5, bf16 = fp32 rounded; a pair's bits alone and in decode or prompt plans (16- and 64-pair items); the scales
    make_ck takes.
  • tests/cuda/test_flashnext_nvfp4_loader.py: the W4A16 face tests pin --precision full; a new one loads the tiny
    checkpoint under checkpoint.
  • On GB10: tests/cuda 1,208 passed, 98 skipped. Drafted == serial on the real checkpoint under checkpoint: 4 of
    4 prompts, 160 tokens each.

Not run: two ranks, other SM 12.x cards, RadixArk's and local-inference-lab's exports (both store per-expert input
scales; I haven't checked whether gate/up's are uniform there).

Written with AI assistance (Claude Code); the author reviewed every change.

…ath (FP4 x FP4) under --precision checkpoint

Rows enter gate|up once a layer in NVFP4 under the experts' static input scale (the largest of a layer's, which
the checkpoints we know store once for every expert), on the block-scaled FP4 mma against the lane matmul's words
and block scales; SiLU(gate) * up leaves for down as NVFP4 rows under each expert's own down input scale. One K
chain a pair, so drafted == serial and resumed == fresh hold. One rank on SM 12.x; --precision full, two ranks and
other GPUs keep the W4A16 kernel. The MTP layer's draft-only experts keep theirs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_011SuBgdp87NbfLbiigmzn1z
@SvangenStudios

Copy link
Copy Markdown

Tested 99ccde3 on one DGX Spark (GB10, NGC pytorch:26.07-py3, one rank) with local-inference-lab/Qwen3.8-Flash-Next-NVFP4 @ 7c4f1bc1, against v0.6.1 (17c73e1).

It refuses this checkpoint at startup:

tensorfold: mtp.layers.0.mlp.gate_proj: no static input scales, so the experts' own math is unknown; serve it with --precision full

In this export the MTP layer's experts are per-expert NVFP4 (weight, weight_scale, weight_scale_2) with no input_scale (0 of the checkpoint's 73,728 input_scale tensors are under mtp.). So they take the same "per-expert FP4" branch as the main layers in families/qwen4_exp/cuda/weights.py and hit the new check, although the description says the MTP layer's draft-only experts keep nvfp4_expert_kernel. Keeping the MTP layer out of that branch fixed it for me:

-            if nvfp4_moe.own_math(world):                # rows in NVFP4 under the checkpoint's static input scales
+            if nvfp4_moe.own_math(world) and not name.startswith("mtp."):   # MTP draft-only experts keep nvfp4_expert_kernel

The branch merges cleanly onto v0.6.2, and the merged code reaches the same check (weights.py:306).

With that one line, the startup line reports "routed NVFP4 experts in the checkpoint's math (FP4 x FP4 under its static input scales)", and against v0.6.1, --parallel 4, median of two runs:

v0.6.1 #211 + fix Change
cold prefill, 1,962 tokens 1,560 tok/s 1,685 tok/s +8 %
cold prefill, 13,995 tokens 1,662 tok/s 1,861 tok/s +12 %
cold prefill, 56,000 tokens 1,576 tok/s 1,731 tok/s +10 %
decode, 1 stream (512 greedy tokens) 36.3 tok/s 37.2 tok/s +2.5 %
decode, 2 streams (aggregate) 56.3 tok/s 59.3 tok/s +5 %
decode, 4 streams (aggregate) 88.9 tok/s 91.1 tok/s +2.5 %

The prefill gain (+8 to +12 %) was consistent across both runs at each length on this checkpoint. The decode differences are uncertain: two runs each, and one-stream runs spread by about ±2.5 tok/s. Greedy replies differ from v0.6.1 on 6 of 6 fixed prompts, as expected for a change in arithmetic; a small known-answer set (10 questions: arithmetic, counting, facts) scored 10/10 on both. tests/cuda/test_nvfp4_experts_ck.py and tests/cuda/test_flashnext_nvfp4_loader.py: 47 passed on the GB10.

…input scales keep nvfp4_expert_kernel under --precision checkpoint
@jschmied

jschmied commented Oct 3, 2026

Copy link
Copy Markdown
Contributor Author

@SvangenStudios thanks for testing this and finding the bug. Your diagnosis was right, and the fix is your line: 5ad55f9. Our checkpoint stores the MTP layer's experts with input scales, so it never reached that check.

The commit also adds a test: the tiny checkpoint writer can now store MTP experts as per-expert NVFP4 without input scales, like your export. On that checkpoint, under --precision checkpoint, the main layers run FP4 x FP4 and the MTP experts keep nvfp4_expert_kernel. The test failed with your startup error before the fix. With it, test_flashnext_nvfp4_loader.py and test_nvfp4_experts_ck.py pass, 48 tests on one GB10.

@ashhart

ashhart commented Oct 6, 2026

Copy link
Copy Markdown
Owner

Thank you, @jschmied. Running Flash Next's routed experts in the checkpoint's own FP4 x FP4 math, with one fp32 chain over K for each pair, keeps a pair's bits independent of the others, and the KL, top-1 and perplexity table is the documentation that mode needs.

The Python engine is frozen (#286), so this won't merge. Zig CUDA runs only Nemotron's MLX 4-bit weights so far, on a copy of the same expert plan your kernel uses, and it has no NVFP4 kernels yet. When Flash Next's NVFP4 checkpoints serve on Zig CUDA, the block-scaled FP4 expert kernel and its SiLU-to-NVFP4 epilogue come across under --precision checkpoint, with your credit.

@ashhart ashhart closed this Oct 6, 2026
@jschmied
jschmied deleted the flashnext-fp4-experts branch October 11, 2026 17:36
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