Skip to content

nvfp4 experts: Flash Next decode rows in equal waves, two warps a unit for one row - #1351

Merged
ashhart merged 2 commits into
ashhart:mainfrom
jschmied:fnx-decode
Oct 28, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
jschmied:fnx-decode

Conversation

@jschmied

@jschmied jschmied commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Flash Next's routed experts at decode rows (16-pair items, nvfp4_expert_kernel) run behind Humming on GB10. At one row gate-up is 21 % slower, and at four rows 20 % slower. This PR fixes two causes without changing a bit:

  • Four rows: a tail wave. 780 units on a 768-warp resident grid leave 12 units to run alone, at a fraction of the bandwidth. nvfp4_expert_waves deals the units in equal waves instead: ceil(units / waves) warps run and the rest exit at once. A plan with no items returns before the division.
  • One row: compute not hidden. 200 units give about one warp per scheduler, and each warp's dequant and mma sit between its loads. Deeper prefetch does not help: 3 to 8 stages give the same time, and a pure streaming kernel with this layout reads the same bytes in 82.6 µs at any depth. When every half unit fits the grid, nvfp4_expert_half gives a unit's 32 columns to two warps of 16 columns each.
  • Dispatch. Half applies while 2 x units fit the resident warps; past that edge it is +19 %. Waves applies only when the plan spills past one wave. On a plan that fits one wave with no half fit (Flash Next rows 2 and 3), the classic kernel stays, because waves there is identical work and cost +2 %.

Every output keeps the same per-16 mma from zero, the same fmaf into its sum in ascending K, the same global scale and the same epilogue. The classic kernel's SASS is unchanged, so Nemotron and GLM are untouched. Only the four waves instances changed SASS in the rebase, from the zero-items return. Only Flash Next's decode tile routes to experts_waves.run: cuda_nvfp4_tile.run(k, pt, ...) keeps main's signature and K32 prompt path, and takes experts_waves.run for pt == 16.

GEMM time on GB10 (top-10 of 512 experts, cold L2, median of 50, µs)

stage rows before after Humming 0.1.18 after/before after/Humming
gate-up 1 93.2 86.6 77.3 0.929 1.12
gate-up 2 161.6 162.2 154.9 1.004 1.05
gate-up 4 357.7 314.7 297.8 0.881 1.06
gate-up 8 609.6 597.3 564.5 0.980 1.06
gate-up 16 1097.5 1092.5 1033.4 0.996 1.06
down 1 41.1 41.2 39.4 1.001 1.05
down 4 158.0 156.5 145.6 0.989 1.07
down 16 537.1 535.4 503.0 0.999 1.06

Row 2 now takes the classic kernel, so its 161.6 -> 162.2 is the same kernel within noise (spread 0.5 %). The rest of the gap to Humming is Humming's stream-K, which splits K. Splitting K would change the summation order, so this PR does not use it.

Served decode

Flash Next FP4 with MTP drafting at 1, 2, 4 and 8 streams (fresh servers, 2+2 alternating) is flat within noise against main. I do not claim a served gain. Every decode text is identical between the arms, and TTFT is unchanged. The GEMM-level gain at one and four rows is real, but too small a share of a served step to clear run-to-run noise.

Gates

  • Kernel contract, in the nvfp4 group (contract_nvfp4_tile.Group). The group loads the decode kernels at 4 warps (the baseline, whatever the environment says) and at 1. Besides main's prompt-tile and K32 kinds, it holds:
    • the waves and half kernels;
    • all seven instances that TF_NVFP4_DECODE_WARPS=1 selects;
    • each byte-compared by gate 3 with the 4-warp classic kernel;
    • a 1,024-row shape that runs at least two waves;
    • two controls cut to one wave, which go red only there.
  • Launch records at sm_80/86/89/120/121 for nvcc 13.0, plus a 13.2 set for the six 1-warp instances that release compiles differently.
  • Synthetic sweep against main's image: all bit-equal (768/768: random, large, tiny and non-finite data; spread and hot routing; rows 1 to 16; with and without Flash Next's skipped shared slot).
  • On this head: zig build test passes (including the tile dispatch test, which now expects half, classic and waves by plan size with the classic kernel's arguments); lean check 0 new.
  • Device gates on this rebased head have not been rerun here.

@jschmied

Copy link
Copy Markdown
Contributor Author

I measured the dispatch between classic, waves and half on GB10 for M = 1..16 (spread, mixed and hot routing, with and without the shared slot, cold L2, median of 50 replays x 3 repeats, 4 and 1 warps a CTA).

Half wins whenever every half unit fits (M=1: -7 %, and still -2 to -8 % at smaller plans); its edge is exactly 2 x active units <= resident warps, +19 % one point past it, so that rule stays. Waves only pays when the plan spills past one wave (M=4 spread -9 %, M=8/16 mixed -5 to -7 %); on a plan that fits one wave it is identical work and cost +2 % on gate-up in the hot case (M=2/3, 95 -> 97 us). The M=2 161.6 -> 162.2 in the table is noise (+0.3 %, spread 0.5 %). TF_NVFP4_DECODE_WARPS 1 vs 4 is within +-1 % on the same grids, so the default stays.

So pick() now returns the classic kernel while items x blocks fit one wave and no half fit applies (Flash Next rows 2 and 3); everything else is unchanged, same bits (768/768 sweep, kernel contract 343/343). Served decode 1/2/4/8 streams is flat within noise (2+2 alternating), so I do not claim a served gain.

@ashhart

ashhart commented Oct 26, 2026

Copy link
Copy Markdown
Owner

Review against main e9b69ed. The wave split covers every unit exactly once, and the half kernel's sums and output columns match the classic kernel's. The gap is in the contract: its fixture has 4 experts and at most about 480 units against 768 resident warps, so the waves kernel never runs a second wave there, and the byte-equality check only exercises the case that is identical to the classic kernel. The served 4-row case (780 units) is covered only by your sweep. Please add a contract shape that forces at least two waves, and register the 1-warp instances that TF_NVFP4_DECODE_WARPS=1 selects (with limit records at every SM). Then we'll run Flash Next FP4 with MTP at --parallel 8 under both warp settings, plus Nemotron and GLM unchanged.

@jschmied

Copy link
Copy Markdown
Contributor Author

Both are in a8683df, contract only. No kernel or dispatch change.

Two waves. The waves kinds' served shapes gain a 1,024-row shape: 2,048 pairs over 4 experts make at least 128 items of 16 pairs. That is 2,560 or more gate-up units and 10,240 or more down units, against 768 resident warps on GB10, so gate-up runs at least 4 waves and down at least 14. Gate 3 still compares the bytes with the classic kernel's at every shape. Two planted controls show the shape really needs a second wave. Each one runs the waves entry with the plan's item count cut to what one wave of its grid holds. The cut changes nothing at shapes that fit one wave, so a control can only go red where a later wave exists:

  • nvfp4_expert_waves<2, 2, 4> with the cut is red at 1024x640x2560, bytes differ from byte 2560.
  • nvfp4_expert_waves<1, 0, 1> with the cut is red at 1024x2560x640, bytes differ from byte 10240.

If a larger GPU ever holds the whole shape in one wave, these controls go green and the contract fails, instead of passing without testing the later waves.

1-warp instances. All seven kernels that TF_NVFP4_DECODE_WARPS=1 selects are now contract kinds: nvfp4_expert_kernel<2,2,1>, <1,0,1>, <1,3,1>, <1,1,1>, nvfp4_expert_waves<2,2,1>, <1,0,1> and nvfp4_expert_half<2,2,1>. Each is byte-compared against its 4-warp classic kernel. The fixture loads both sets explicitly (loadWarps(4) as the baseline, loadWarps(1) under test), whatever the environment says. The fixture now also covers bf16 down and relu^2 up. relu^2 declares a 50x fp16 margin, not 100x: the bf16 rounding before the square is part of its bound, and fp16 and bf16 sums miss by 64x. All seven have limit records at sm_80/86/89/120/121 (32 threads, 80 to 127 registers), and all 35 pass as current.

Results on GB10 (sm_121):

  • Kernel contract, the decode kinds: 10/10 pass gates 1-4. That is the three 4-warp waves/half kinds and the seven 1-warp kinds, the waves kinds at the two-wave shape included.
  • Both new controls are red as planted. The existing prompt-tile control is still red, and the fact kinds still pass.
  • kernel-contract-limits: every 1-warp record is current at all five SMs. The only failures are the tf_q35_attn_partial/partial8 records at 1,024 threads in cuda_lane_pending.zig. Those are unchanged here and identical on main.
  • zig build test rc 0; lean 0 new.

@ashhart

ashhart commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Thank you for the two-wave shape and the 1-warp kinds. Both are what we asked for, and the controls that go red only
when a later wave exists are a nice touch. I checked the half kernel against compute_stage term by term (words,
scale words, shifts, the fmaf order and the columns) and found it matches.

Before we can run it on device, the branch needs a rebase. Main's K32 prompt work (7f75a8908) changed
cuda_nvfp4_tile.run to run(k, pt, ...), and the contract runner to contract_nvfp4_tile.Group. The PR conflicts
in cuda_nvfp4_tile.zig, contract_tests.zig and contract_nvfp4_fact.zig. Main's cuda_nvfp4_tile_test.zig also
expects the classic kernel for a decode tile, and with this PR that plan picks nvfp4_expert_half. Please keep main's
run signature with experts_waves.run for pt == 16, and fold Sets/k1 into the tile group.

Two smaller points. First, nvfp4_expert_waves divides by waves, which is 0 when counts is 0; an early return
would avoid it. Second, your later comment says served decode is flat within noise, but the description still shows
the +1.6 % table. Please update it. On device we'll run Flash Next FP4 with MTP at --parallel 8 under both warp
settings, and Nemotron. To keep it on by default we need to see a served gain at 1 and 2 streams.

@jschmied

Copy link
Copy Markdown
Contributor Author

Thanks for checking the half kernel term by term. Rebased on main 59044397a and force-pushed as one commit, bb51eef6b.

  • Rebase. cuda_nvfp4_tile.run(k, pt, ...) keeps main's signature: prompt tiles go to the K32 tile or runFact as before, and pt == 16 goes to experts_waves.run. The contract runner uses contract_nvfp4_tile.Group. Sets/k1 moved into it: the group loads the 4-warp baseline and the 1-warp set itself, and holds every waves, half and 1-warp kind, then the K32 tile's three. contract_tests.zig calls Group.create(a, gpu, s, sms, router, &kernels).
  • cuda_nvfp4_tile_test.zig. For a decode tile the test now expects the kernel each plan size picks: half at 3 items, classic at 30 (one wave), waves at 60, each with the classic kernel's arguments, 128 threads and no dynamic shared memory.
  • counts == 0. nvfp4_expert_waves returns before it computes waves when there are no units. Only the four waves instances changed SASS (20 functions over 5 SMs). Every other function in nvfp4_experts is identical to the pre-rebase head. I re-derived the records: the 1-warp waves gate-up and down at sm_120/121 now take 124 and 92 registers on 13.0. 13.2 compiles the six 1-warp instances differently, so they get a 13.2 set (nvfp4_fact_records.cuda_132). Unrelated to this PR: main's tf_nvfp4_fact_down_f32 compiles to 142 registers on 13.2 at sm_120/121, against its record of 141, and has no 13.2 entry.
  • Description. Updated: the +1.6 % table is replaced by "flat within noise, no served gain claimed".

Checks: lean 0 new; zig build test passes; ATC shows no PR conflicts. I have not run the device contract on this head.

@ashhart

ashhart commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Thanks for the quick rebase, and for keeping main's run(k, pt, ...) with experts_waves.run at pt == 16. We checked the head: Sets/k1 now live in contract_nvfp4_tile.Group, nvfp4_expert_waves returns before the division when counts is 0 (the only kernel change since the previous head), and the description no longer claims a served gain. On a merge with current main, zig fmt, the lean check (no new findings), test-cuda-host and the Linux cross-compile all pass.

One ask before the device run:

  1. experts_waves.run passes the half kernel's resident warps (e.resident[first + 2]) to both of pick's tests (zig/src/cuda/experts_waves.zig:32). The one-wave test should use the kernel it replaces (e.resident[0] for gate-up, e.resident[1] for down). Their registers differ: on sm_120/121 the 1-warp half takes 95 against 124 for 1-warp waves and 127 for 1-warp classic gate-up, and 4-warp waves down takes 96 against half's 126 (nvfp4_fact_records.zig:8-16). So with TF_NVFP4_DECODE_WARPS=1 the four-row gate-up plan (780 units) is judged against about 1,000 half warps and keeps the classic kernel's tail wave, and at 4 warps one-row down (880 units) is judged against half's 768 warps, where down's waves kernel holds 960. Please add a pick test with unequal residencies that fails before the fix.

After that we'll run Flash Next FP4 with MTP at --parallel 8 under both warp settings, and Nemotron and GLM for unchanged tokens. To keep it on by default we still need a served gain at 1 and 2 streams.

@jschmied

Copy link
Copy Markdown
Contributor Author

Thanks, that was a real bug. Fixed in 2e9801c15, a fast-forward on bb51eef6b.

  • pick now takes two residencies: the half kernel's resident warps for the half test, and the replaced classic kernel's for the one-wave test. run passes e.resident[0] * warps for gate-up and e.resident[1] * warps for down as the second one.

  • Test. The new test "the one-wave test uses the replaced classic kernel's resident warps, not the half kernel's" uses unequal residencies and covers the two cases you named:

    • TF_NVFP4_DECODE_WARPS=1, four-row gate-up (39 items × 20 blocks = 780 units), half ~1,000 warps, classic 768: waves (entry 4), not classic.
    • 4 warps, one-row down (11 × 80 = 880 units), half 768, classic 960: classic stays (null), not waves.
    • The half test still reads only half's warps: 760 half units fit 768, 800 do not.

    With the old rule (_ = classic, one-wave test against half), the test fails on its first assertion: expected 4, found null. With the fix, both experts_waves tests pass.

Checks: zig fmt --check zig clean; lean 0 new (428 against 428 on the base); zig build test -Dnvcc=... RC 0; ATC overlap shows no conflicts with open PRs. I have not run the device contract or a served run on this head.

@ashhart

ashhart commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied, that's the fix we asked for. run now passes the replaced classic kernel's resident warps to the one-wave test: e.resident[0] for gate-up and e.resident[1] for fp32 down (zig/src/cuda/experts_waves.zig:32). The half test still reads only the half kernel's warps. We ran your unequal-residency test against the previous rule, and it fails at its first assertion (expected 4, found null). With the fix it passes.

On a merge with current main, these all pass: zig fmt, the lean check (no new findings), test-runtime-host, test-cuda-host and the Linux cross-compiles.

Next we'll run it on device:

  • the kernel contract for the decode kinds, including the two-wave shape and its two controls, and kernel-contract-limits at every SM;
  • Flash Next FP4 with MTP at --parallel 8, under TF_NVFP4_DECODE_WARPS=4 and =1, for tokens against main;
  • Nemotron and GLM, for unchanged tokens.

For the new dispatch to stay on by default, we still need a served gain at 1 and 2 streams, so we'll take speed rounds there against main.

@ashhart

ashhart commented Oct 28, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. First device results, from a GB10 with Qwen3.8-Flash-Next FP4-Local, your two commits on main a1e6d3424 against main:

  • TF_NVFP4_DECODE_WARPS=4 and =1: the same tokens as main in every prompt row (2k and 8k), and every stream in the 1 to 8 stream sweep equals its run alone. Prefill and decode are within 1.4% either way.
  • The default dispatch, alternating base, new, new, base: one stream +0.6%, two streams +0.2%.

That's a small gain, but in the right direction. Still to run on our side: the kernel contract for the decode kinds (the two-wave shape and its controls, and the limits at every SM), Flash Next with MTP drafts at --parallel 8 (this checkpoint has no MTP head), and Nemotron and GLM for unchanged tokens. Since it changes kernels, it will land just after the 1.1.8 release rather than in it.

…t for one row

The decode form's units go out in equal waves: past the resident grid, ceil(units / waves) warps run and
the rest leave, so no wave is a short tail (gate-up at four rows had 12 units left over a 768-warp grid).
When every half unit fits the grid (one row's eleven items), two warps share a unit's 32 columns. A plan
that fits one wave and no half fit (Flash Next rows 2 and 3) keeps the classic kernel, where waves cost
2 % and gain nothing. Each output keeps the same mma sequence over the same K order and epilogue: the bits
do not change. A plan with no items returns before it divides by its waves.

cuda_nvfp4_tile.run keeps main's run(k, pt, ...): prompt tiles take the K32 tile or runFact as before,
decode tiles (pt == 16) take experts_waves.run. Its dispatch test expects the half, classic and waves
kernels by plan size, with the classic kernel's arguments.

The contract's nvfp4 group (contract_nvfp4_tile.Group) loads the decode kernels at 4 warps (the baseline,
whatever the environment says) and at 1, and holds every kind: the waves, half and 1-warp instances
TF_NVFP4_DECODE_WARPS=1 selects, byte-compared with the 4-warp classic kernel, with a 1,024-row
two-wave shape and two controls cut to one wave that go red only there, then the K32 tile's kinds.

The classic kernel is untouched (same SASS); only the four waves instances change with the early return.
Launch records for nvcc 13.0, and a 13.2 set for the six 1-warp instances it compiles differently.
GB10 gate-up: -6 % at one row, -11 % at four, -2 % at five and eight; down -1.5 % at four and five.
…ident warps

pick judged both the half test and the one-wave test against the half kernel's resident warps. The one-wave test
decides whether the classic kernel holds every unit in one wave, so it now reads that kernel's residency (resident[0]
gate-up, resident[1] down). Their registers differ: with TF_NVFP4_DECODE_WARPS=1 a four-row gate-up plan fit half's
warps and kept classic's tail wave; at 4 warps one-row down was judged against half's 768 warps. A pick test with
unequal residencies covers both and fails on the previous pick.
@ashhart
ashhart merged commit 1b9a987 into ashhart:main Oct 28, 2026
9 checks passed
@ashhart

ashhart commented Oct 28, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied, this is merged. What we ran on our side:

  • Kernel contract on a GB10 (sm_121), against main: no new failures. The new FAIL lines are the two-wave shape's two controls, red as planted, and the launch count (1,314 registered launches, from 1,304). kernel-contract-limits at all five SMs has no new problems.
  • SASS: every kernel that nvfp4_experts.cu had on main compiles to identical SASS on sm_121 and sm_80, all 13 of them. The only change is the six new waves and half instances, so Nemotron and GLM, which use the classic kernel, are unchanged bit for bit.
  • Flash Next FP4-Local on a GB10: the same tokens as main in every row under TF_NVFP4_DECODE_WARPS=4 and =1, and the default dispatch is +0.6% at one stream and +0.2% at two.

Thanks for the careful kernel work, and for the unequal-residency test.

ashhart added a commit that referenced this pull request Oct 28, 2026
…decode waves (#1351), Qwen 27B whole-window prefill (#1458), the release path check (#1533) and the Qwen3.5-2B prompt cache on Metal (#505)
@ashhart

ashhart commented Oct 29, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied: the NVFP4 decode waves shipped in TensorFold 1.1.9.

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.

2 participants