Skip to content

qwen3_5 cuda: a dense MLP's NVFP4 prompt gate, up and act in one lean launch, the same bytes - #3252

Merged
ashhart merged 5 commits into
ashhart:mainfrom
jschmied:fusedact
Nov 18, 2026
Merged

ashhart merged 5 commits into
ashhart:mainfrom
jschmied:fusedact

Conversation

@jschmied

@jschmied jschmied commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Head 625fe3102f = 19cdfdfbd plus the serving re-pins of prompt16.cu and nvfp4.zig, and four single-use imports inlined in zig/tests/cuda/main.zig (600 lines once applied to main). No kernel, launcher or served code changes, so the rows below, measured on 19cdfdfbd, stand. Base main 223da7659b. GB10 only measured; exact, on by default for NVFP4 gate/up prompt rows past 16.

On main a Qwen3.8-27B dense layer's prompt MLP runs two prompt_lean products (gate, up: 17408 x 5120 each), writes
both as bf16, reads them back in tf_q35_mlp_act, and writes the act. bound1110 measured that act kernel at 3.1-3.4 %
of cold TTFT at the bandwidth floor. This PR computes gate and up of the same 128 features in one lean tile and takes
the act in the epilogue, so neither bf16 product is written or read again and the act kernel does not run.

GB10, Qwen3.8-27B NVFP4 (unsloth, nvfp4 head), DFlash2, cold prompt, A-B-B-A main this PR paired median slower
8K (8175-8194 tokens) x3 a arm 6.455-6.622 s 6.249-6.402 s -3.28 % 0 of 6
32K (32666-32689 tokens) x2 a arm 27.908-28.030 s 27.101-27.159 s -2.99 % 0 of 4
decode, 1 stream, 256 tokens 53.80 / 53.90 tok/s 54.08 / 54.12 tok/s +0.52 / +0.40 % 0 of 2

Fresh server per arm, page cache dropped before each start, 90 s between arms; SM clock per arm 2279 / 2278 / 2283 /
2284 MHz (the box's 2300 MHz cap; throttle flags are that cap's, same in every arm). Decode texts identical in all arms.
These rows are on this head's binary; an earlier run on 3b21a9a68e (same kernels, comments since) gave -3.26 % / -2.87 %.
Kernel level (tf-cuda-test prompt-swiglu-bench, same process, A-B-A): two lean products + act vs the pair launch at
2048 / 4096 / 8192 rows: 9.16 vs 7.96 ms (1.15x), 18.44 vs 15.44 ms (1.19x), 36.17 vs 30.81 ms (1.17x).

What changes

  • zig/kernels/cuda/prompt16_lean.cuh (new; prompt16.cu includes it, 397 lines): prompt_lean moved here and
    templated on an output kind (LEAN_BF16, LEAN_F32, LEAN_SWIGLU) with four trailing arguments (up's words,
    scales and scale, and the act kernel's device row count). For LEAN_SWIGLU a tile is 128 features: gate's two
    64-column blocks and up's two at the same features are staged as the usual 256-column tile (no repack, no second
    weight copy), and warp wn takes gate subtiles 2wn, 2wn+1 and up's 16+2wn, 17+2wn, so one lane holds a feature's
    gate and up sums. Each sum is its own column's mma chain over the same K order as a plain lean product. The epilogue
    rounds both to bf16 (acc * scale, as the bf16 lean epilogue) and then takes tf_q35_mlp_act's steps
    (g / (1 + expf(-g)) * u, rounded), skipping rows past P[2] as that kernel does.
  • zig/src/cuda/nvfp4.zig: Prompt.swiglu and Prompt.pairs (both FP4, same n/k/npad, whole 128-feature tiles);
    the lean launch passes the four trailing arguments (zeros for one product).
  • zig/src/families/qwen3_5/cuda_modelopt.zig swiglu + cuda_forward.zig: a dense layer whose gate and up are NVFP4
    takes the pair for prompt rows past 16 with no learning hook and no group sums; everything else (FP8/fp8g layers,
    EXL3, affine-4, BF16, decode/verify rows, --learn) runs today's three launches.
  • Registry: the renamed lean symbols and a record for the pair (255/255/255/247/247 registers, sm_80/86/89/120/121).

Shared code: the kernel is the CUDA core prompt tile (prompt16_lean.cuh), the launcher core (cuda/nvfp4.zig);
only the wiring is qwen3_5's. Any family with NVFP4 gate/up prompt rows and SiLU can call Prompt.swiglu. Metal and
HIP are untouched (HIP has its own epilogue form, #2344).

Same bits

  • Kernel contract (tf-cuda-test prompt-swiglu-contract, also run after the gates in kernel-contract; file
    zig/tests/cuda/contract_prompt_swiglu.zig): the pair against main's path (two prompt_lean launches, then
    tf_q35_mlp_act) byte for byte, SUMMARY prompt_swiglu: 3 pass, 0 fail; 2 controls red, 0 missed:
    • served shapes: 6 bit-equal (2048 and 4096 x 17408 x 5120, 129 rows, 1 x 128 x 64, 17 x 256 x 1088, 300 x 384 x 576),
      non-finite inputs and a NaN block scale included, five repeats byte-identical, guards intact;
    • the device row count (P[2] = rows - 37): 3 shapes bit-equal, rows past it left as the act kernel leaves them;
    • every bf16 gate value: 65536 rows whose gate is exactly row r's bit pattern (a 1.0 code at input 0) against 128
      up values each, through the real kernel: bit-equal, NaN, Inf and denormals included;
    • planted controls, red at shapes: gate and up swapped (contract_prompt_swiglu.zig .swapped), and the gate's
      scale one fp32 ulp up (.gate_ulp, a pre-rounding change the rule must see).
  • Off == main: the two existing lean instances keep their SASS: cuobjdump -sass of prompt16 from main and this
    PR, per function, sm_80/86/89/120/121 (nvcc 13.0): 7 of 7 functions identical, 1 added (the pair). The built
    zig build fatbins -Dsm=121 set: 113 of 114 images byte-identical (sha256); prompt16 differs only by the added
    function. Filtered kernel-contract prompt16: 7 kernels pass gates 1-4, live limits PASS (247 registers recorded).
  • Replies equal to main (27B, DFlash2 drafting and --no-drafts): 49 of 49 identical: greedy and seeded (temp
    0.8, top-p 0.95, seed 1234) one at a time, 4 and 8 at once, greedy over 2K and 8K prompts (2048/4096-row pair
    launches); drafted == plain greedy on both binaries.
  • Resumed == fresh: a 3-turn chat over an 8K document, greedy and seeded, with the prompt cache (5 turns
    resumed at row 8192, their 24-125 new rows on the pair launch) against the same turns on a cache-off server: equal on
    both binaries, and main == this PR.

Checks on 19cdfdfbd (kernels, launcher and served code identical at 625fe3102f; compile-cuda-tests and test-gradient-serving pass there)

  • x86 (-Dfixed-cpu test test-golden test-kernel-contract test-server-security test-pull-security fuzz-cortheon check-release-paths, tools/pr_check.py lean 341 vs 341 (0 new), zig fmt --check, git diff --check): pass.
  • GB10 (nvcc 13.0, sm_121): test-cuda-host test-qwen35-launch-args-host test-qwen35-modelopt-admission-host test-qwen35-stack-symbols-host test-qwen35-formats-host test-qwen35-prefill-host 400/400 steps, 6332 tests pass
    (30 skipped); prompt-swiglu-contract and kernel-contract prompt16 as above.
  • Not run: the full kernel-contract (40-60 min; its other kernels' images are byte-identical to main's).

Notes

  • Overlaps qwen35 cuda: fuse the dense MLP's NVFP4 prompt gate and up projections into one launch #2410 (GustavBlack, gate+up in one launch, act kernel kept; level on GB10): both touch prompt16 and the
    qwen3_5 dense MLP site. This PR does the act in the epilogue, which is where the GB10 gain is. Either can land first;
    I'll rebase onto the other when you say.
  • No new refusal or refusal test: Prompt.swiglu returns FormatMismatch for a non-pair, but the forward asks
    Prompt.pairs() first and runs today's three launches otherwise, so nothing refuses on a served path.
  • Bandwidth-bound here; on a discrete card (sm_120 5090) the saved write/read and act kernel should matter at least
    as much. A tester run (Maxrio9 sm_120) of the 8K/32K cells would be welcome.
  • Proposed CHANGELOG line (Unreleased): "Qwen3.8 on CUDA runs a dense layer's NVFP4 prompt gate, up and SwiGLU in one
    launch; replies are byte-identical and cold prefill on GB10 is about 3 % quicker at 8K and 32K."

@ashhart

ashhart commented Nov 14, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. The host tests pass, and the contract compares the fused result with the replaced operations without loosening byte equality.

We will run the device check on our side for the new pair and the existing BF16 and FP32 lean launches, including partial tiles and shortened active row counts. Outputs must be bit identical, both planted controls must fail, and launch limits must pass for the compiled CUDA targets. We will also check served tokens and prompt and decode speed at one and four streams.

  • Synthena (Ash's Agent)

@Nipale-ai

Copy link
Copy Markdown
Contributor

Numbers for the cold 8K and 32K prefill cells on an RTX 5090 (sm_120), which the description asks for ("A tester run (Maxrio9 sm_120) of the 8K/32K cells would be welcome"). On this card the pair launch is quicker by about 0.6 % at 8K and 0.5 % at 32K, the greedy replies are byte-identical, and decode is within 0.05 % of main.

Setup

  • Builds: main 2ba55ec7d32d0bec446bb012678550be7ef52895 (the commit titled "Release 1.2.9: the version, install examples and changelog") against the same tree with the head of this pull request, 19cdfdfbd18ac8ca0bd01ff2ac15efdffa67cd46, merged (local merge commit a8ed0977a59c25bf8b5c43b1b5c893286329bf91, no conflicts, 10 files, +595/-213). Both servers were built here from source with the same command: zig build -Dgpu=sm_120 -Dnvcc=<nvcc 13.0.88>, Zig 0.17.0. tensorfold-native sha256: main d65b250cb92b420d1944c6c7e7071d69ea98a40e0756743547ea1aa631a24a6c, PR a2188e7d3d107765ce35fc35ff03a238bc9912f3ad82475cfe25502d21e3ef80 (build.zig.zon reads version 1.2.9 in both trees). Tests built from the merged tree: zig build compile-cuda-tests -Dsm=120 -Dnvcc=<nvcc 13.0.88> -Dcuobjdump=<cuobjdump of the same toolkit> -Doptimize=fast -j8.
  • Card: RTX 5090 32 GB, compute capability 12.0, driver 610.43.02, Debian 13, Linux 6.12.95, glibc 2.41. Each arm started only with less than 1024 MiB in use on the card. No clock, power-limit or other setting was changed; clocks and throttle reasons were not logged, so the clock rule in docs/optimizing.md ("Measuring honestly") was not applied.
  • Model: nvidia/Qwen3.8-27B-NVFP4 @482ca0f3832238542f8f5295dde86b5f22711d80, drafter z-lab/Qwen3.8-27B-DFlash2 @50307d4c4cde6860d4eee73e2547cd786fe8e8a4. The description names the unsloth checkpoint; this run uses the nvidia one, whose gate and up are NVFP4 in all 64 layers.
  • Server: tensorfold-native serve nvidia/Qwen3.8-27B-NVFP4 --drafter z-lab/Qwen3.8-27B-DFlash2 --context 40960 --prompt-cache-gib 0 --name bench --host 127.0.0.1 --port 8133 --no-update-check (startup line with DFlash2: model 20.78 GiB, 2 streams at a 40960-token window). A second set runs the same line with --no-drafts instead of --drafter.
  • Method: a fresh server per arm, 90 s between arms, A = main, B = this PR. DFlash2: 12 arms in the order ABBA, BAAB, ABBA (three blocks); --no-drafts: 8 arms in the order ABBA, BAAB. Each arm runs three 8192-token prompts, then two 32768-token prompts, then decode. The prompts are raw token IDs from the checkpoint's tokenizer (public text: the repository's docs/*.md behind a nonce line, cut to the exact length), 64 reply tokens, greedy, seed 1234, tools/bench_cache.py --require-cold --expected-prompt-tokens; the figure is ttft_s of the first request of each run. Decode is tools/bench_openai.py --tokens 256 --reps 2 --temperatures 0 (two of its prompts). Paired: inside a block, the mean of the two A arms against the mean of the two B arms for each prompt, as PR / main.
  • "Cold" here means a fresh process, --prompt-cache-gib 0 and prompts that process had not seen. The page cache was not dropped (no root), so the weights were most likely in the page cache in every arm. An extra first arm with the same procedure was run and discarded.

DFlash2 drafts, 12 arms

cell main this PR paired median (range) PR slower
cold 8192 tokens, TTFT 2.190-2.220 s 2.176-2.203 s -0.65 % (-1.07 to -0.45) 0 of 9
cold 32768 tokens, TTFT 9.841-9.889 s 9.785-9.825 s -0.54 % (-0.81 to -0.41) 0 of 6
decode, 1 stream, 256 tokens, code prompt 347.88-348.32 tok/s 347.96-348.37 tok/s +0.05 % (+0.01 to +0.05) 0 of 3
decode, 1 stream, 256 tokens, chat prompt 224.80-225.03 tok/s 224.80-225.15 tok/s +0.01 % (+0.00 to +0.04) 0 of 3

--no-drafts, 8 arms

cell main this PR paired median (range) PR slower
cold 8192 tokens, TTFT 2.155-2.192 s 2.142-2.176 s -0.57 % (-0.93 to -0.37) 0 of 6
cold 32768 tokens, TTFT 9.629-9.689 s 9.591-9.621 s -0.45 % (-0.58 to -0.39) 0 of 4
decode, 1 stream, 256 tokens, code prompt 70.36-70.39 tok/s 70.38-70.42 tok/s +0.02 % (+0.02 to +0.03) 0 of 2
decode, 1 stream, 256 tokens, chat prompt 70.49-70.52 tok/s 70.51-70.54 tok/s +0.02 % (+0.00 to +0.03) 0 of 2

Same bits

  • Every first-request token hash and output hash is identical across all 20 arms (main and PR, both server lines) for all five prompts; the repeated and the concurrent request of each run equal the first one, and each run reported cached_tokens 0 (cache_state cold). The 256-token decode replies of both prompts have one output hash across all arms, and the same hash with --drafter and with --no-drafts.
  • tf-cuda-test prompt-swiglu-contract on the merged tree: SUMMARY prompt_swiglu: 3 pass, 0 fail; 2 controls red, 0 missed
  • tf-cuda-test kernel-contract prompt16 (filtered): (filtered) SUMMARY 7 kernels: 7 pass gates 1-4, 0 fail, 0 known findings; 0 controls: 0 red as planted, 0 missed; 0 limit problems; gates 20269 ms, limits 0 ms; the live limits for prompt16 FP4 lean SwiGLU pair read 247 registers and 76848 shared bytes, as recorded.
  • Kernel level, tf-cuda-test prompt-swiglu-bench, three runs in a row (range of A1 / A2 = two lean products + act, B = pair launch, ratio separate / pair):
rows separate (A1, A2) pair (B) separate / pair
2048 3.539-3.576 ms 3.235-3.238 ms 1.0957x-1.1008x
4096 6.627-6.695 ms 6.476-6.514 ms 1.0239x-1.0279x
8192 13.354-14.006 ms 13.154-13.177 ms 1.0380x-1.0393x

Not run

Raw logs (server logs, every request report, hashes, build logs) are available if useful. Thanks for the pull request.

@jschmied

Copy link
Copy Markdown
Contributor Author

Thanks @Nipale-ai, that's the sm_120 run the description asked for. On a card that isn't ours, the pair launch is 0.65 % / 0.54 % faster at cold 8K / 32K with drafts and 0.57 % / 0.45 % without, PR slower in 0 of 25 TTFT pairs. Decode is level and every hash is equal across all 20 arms. The kernel ratio (1.10x at 2048 rows, 1.02–1.04x at 4096–8192) also matches the shape we saw on the GB10.

Two notes for the record. The nvidia checkpoint is a fair stand-in, because its gate and up are NVFP4 in all 64 layers, so the pair path runs in every layer. And the weights sat in the page cache in every arm, so it's equal for A and B. I don't need the raw logs unless @ashhart wants them.

@ashhart

ashhart commented Nov 16, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied and @Nipale-ai. The RTX 5090 results add useful evidence: greedy replies match, the shortened-row checks pass and both controls fail as they should. The small timing gain stays provisional, since clocks and throttle reasons weren't recorded.

Two things block it on Linux, both reproducible against main: the serving check rejects the changed prompt kernel and launcher because their pinned hashes weren't updated (please re-pin both, with the PR's note), and once applied to main the CUDA test dispatcher comes to 604 lines, past the 600-line cap. The zig and zig-windows jobs fail at this head; once the pins and the line count are fixed, please push and we'll see whether Windows has anything of its own.

After that our GB10 check covers the fused pair and the existing BF16 and FP32 lean launches, seeded replies, cache reuse and one- and four-stream serving, every output bit for bit.

  • Synthena (Ash's Agent)

@jschmied

Copy link
Copy Markdown
Contributor Author

@ashhart Both fixed in 625fe3102f:

  • Pins: zig/kernels/cuda/prompt16.cu and zig/src/cuda/nvfp4.zig are re-pinned in gradient-serving.tsv, each with a #3252: note. Every served-source row matches on this head, and on this head merged into main 6bc82acac4.
  • Line count: zig/tests/cuda/main.zig was already at 600 lines on main, so the two new commands made it 604. Four imports there are used only once (glm_mm_fixed, gb10_many_contract, qmm_prefill_tile22_contract, glue_contract). They are now @import at their use, as other commands in the file already do. That keeps the PR's net change to the file at zero, and applied to main it is 600 lines. Nothing else in the dispatcher changes.

On this head: compile-cuda-tests and test-gradient-serving pass. On the merged tree, lean_check reports nothing for the files this PR touches. The merge with main is clean.

@ashhart

ashhart commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied and @Nipale-ai. The GB10 check we promised is done, at 625fe3102f against its merge-base on Qwen3.8-27B NVFP4:

  • The lean SwiGLU pair's contract and the filtered kernel-contract prompt16 both pass.
  • Prompt passes are 3.9-7.2% faster from 2K to 32K tokens (for example 1,459.6 to 1,532.1 tok/s at 8K), in balanced runs with the PR faster in every pair.
  • Decode is level at 1, 2, 4 and 8 streams, greedy and sampled, chat and code.
  • Every compared reply is token-identical.

Nothing more is needed from you. It's in our landing queue.

  • Synthena

gilby and others added 5 commits November 18, 2026 15:37
…, not synthetic layer times

# Conflicts:
#	CHANGELOG.md
… with the thank-you

Drop the older synthetic M5 Max paragraph that the served-results line replaced, and end the kept line with "Thanks to @gilby." as main's lines for his work do.

# Conflicts:
#	CHANGELOG.md
… pair launch, the same bytes

prompt_lean gains a SwiGLU pair form: a 128 x 256 tile stages gate's two 64-column blocks and up's at the same
features, each warp holds a feature's gate and up sums in one lane, and the epilogue rounds both to bf16 and takes
tf_q35_mlp_act's steps. The two existing instances keep their SASS on sm_80/86/89/120/121 (renamed: int out kind,
four trailing arguments). Qwen3.5 dense layers with NVFP4 gate and up take it for prompt rows past 16.
…ir; inline four single-use imports in the CUDA test dispatcher

The dispatcher sat at 600 lines on main, so the two new commands pushed it to 604 once applied there.
@ashhart
ashhart merged commit 10ea963 into ashhart:main Nov 18, 2026
@ashhart

ashhart commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Thank you @jschmied. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

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.

4 participants