Skip to content

cuda: NVFP4 projections behind qlinear: the lane matmul for decode rows, the prompt GEMM for prompt rows - #595

Merged
ashhart merged 2 commits into
ashhart:mainfrom
jschmied:cuda-nvfp4-qlinear
Oct 10, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
jschmied:cuda-nvfp4-qlinear

Conversation

@jschmied

Copy link
Copy Markdown
Contributor

Second step of the quantized-weight interface (#548, after #587): NVFP4 projections behind qlinear.

The Python lane matmul (nvfp4/qmmf.cu) reads FP8G and FP4 from one kernel template, so the Zig side now does too. FP4 decode rows run on it. FP4 prompt rows run on nvfp4/prompt.cu's GEMM, as Fp4Linear.prefill does.

  • zig/kernels/cuda/fp8_lane.cu becomes qmmf.cu with the FP4 instances added beside the FP8G ones; prompt16.cu is prompt.cu's FP4 tile 4 (PROMPT_TILE).
  • cuda/qmmf.zig: Lane takes over from fp8.Lane. One Weight carries its mode (FP4 or FP8G) and its tensor scale; plan, tiles, slices and cluster choice are unchanged.
  • cuda/nvfp4.zig: packWords and packScales (qmm.pack's layout, ModelOpt tensors as stored: uint8 [N, K/2], e4m3 [N, K/16], the global scale), weight, and Prompt.
  • cuda/fp8.zig keeps the block-FP8 packing; fp8.weight returns the lane's view.
  • cuda/qlinear.zig: Weight gains nvfp4. Linear holds lane (fp8g rows of both kinds and nvfp4 decode rows) and nvfp4_prompt. A weight whose stored mode doesn't match its tag is refused before any launch, as is a format that isn't loaded.
  • tf-cuda-test nvfp4 <dir> with oracle/nvfp4.py. It uses cuda: one quantized-weight interface (qlinear.zig); Nemotron's 4-bit projections route through it #587's test pattern from d7469ca: a blocking stream, and the output filled before every call, both directly and via Linear. Here a prompt path that wrote nothing would otherwise pass on the decode call's bytes.
  • fixture.zig reads arrays up to 16 GiB: 288 experts' packed tables passed 2 GiB in @Bizuayeu's GLM run (Zig CUDA ports: shared layers below lanes.Backend, and one place to coordinate them #548).

Receipts on GB10 (DGX Spark, sm_121, CUDA 13.0). main (c37d0a4) and this branch were each built with nvcc for sm_121:

  • zig build test and zig fmt --check pass; lean_check is clean for the touched files.
  • tf-cuda-test nvfp4 against Fp4Linear (python-0.6 lineage): the repack, then the lane matmul at 1, 3, 17, 40 and 300 rows (tiles 16/32/64, cluster and fused paths) and the prompt GEMM at 17, 300 and 1,000 rows. Every case is byte-equal, directly and through Linear.decode / Linear.prompt, at [5120, 6144] and at [704, 2048] (rows not a multiple of 128, as GLM's TP=3 experts).
  • tf-cuda-test fp8-lane through the merged lane: 1 to 300 rows byte-equal, directly and via Linear.
  • Nemotron 3.5 Lightning MLX-4bit @ d9d758fb. tensorfold check-kernels passes 1,008 of 1,008 projection cases and 384 of 384 forked-MoE cases on both builds. tensorfold run on three prompts (11, 20, 1,090 tokens), greedy and sampled (0.8 / 20 / 0.95, seed 7): all six reply hashes equal main's.
  • The device code is the copy whose SASS we compared with the Python build's on 10-09 (qmmf FP8G + FP4 15/15 instances, prompt16 1/1); it's unchanged here.

@Bizuayeu ran this NVFP4 code on GLM-5.3-Flash's real tensors before the rebase (#548: dense lane and prompt rows 13/13 per tensor), and plans to rerun on this branch.

Next in #548's order: grouped NVFP4 / FP8 experts on #558's plan for decode rows, with @Bizuayeu's staged prompt-row kernel to follow.

@Bizuayeu

Copy link
Copy Markdown
Contributor

@jschmied I reran this branch at 64ef98d on a GB10 with GLM-5.3-Flash's real NVFP4 tensors, as promised in #548. It was built in our tf-dev (nvcc 13.3, sm_121). The Python bytes come from our fork's tree (release/2.8.0 lineage), and your oracle ran unmodified for the synthetic cases.

Fixture [N, K] Result
your oracle (synthetic) [5120, 6144] 9/9 PASS
your oracle (synthetic) [704, 2048] 9/9 PASS
GLM layer 0 gate_proj [12288, 4096] 17/17 PASS
GLM layer 0 down_proj [4096, 12288] 17/17 PASS
GLM layer 3 expert 0 down_proj [4096, 2048] 17/17 PASS
GLM layer 3 expert 0 gate_proj rows 0..704 (npad 768) [704, 4096] 17/17 PASS

Each GLM fixture checks the repack, then lane and prompt rows at 1, 7, 16, 17, 40, 256, 300 and 1,000, both direct and through Linear. Up to 256 rows, x comes from the goldens our own port is checked against, and the Python bytes equal those goldens. So this branch and our port give the same bytes on these tensors. The 300 and 1,000 rows use seeded inputs.

@ashhart

ashhart commented Oct 10, 2026

Copy link
Copy Markdown
Owner

Thank you, @jschmied. This is merged into main (8f757e5) with both your commits as they were, and it ships in 1.0.6. On a GB10 against main, with the full kernel set rebuilt (qmmf in place of fp8_lane, plus prompt16), Nemotron's paths are unchanged: tf-cuda-test train passes, check-kernels passes 1,008 of 1,008 projection cases and 384 of 384 forked-MoE cases, 29 of 29 served replies are equal, the four release prompts keep their SHAs, and decode and cold prompts are within noise.

One commit of ours on top, bcc1e43: like oracle/fp8_experts.py in #605, oracle/nvfp4.py imports the Python engine, so it doesn't land, and tf-cuda-test nvfp4 reads recorded fixtures. Neither of our GB10s has the Python engine installed, so the byte checks against it rest on your receipts for now. A float64 reference inside the test would let anyone run it.

NVFP4 is now a priority for us: next we serve NVIDIA's NVFP4 Nemotron checkpoint end to end on these pieces, then Flash Next and the 27B.

jschmied added a commit to jschmied/TensorFold that referenced this pull request Oct 11, 2026
jschmied added a commit to jschmied/TensorFold that referenced this pull request Oct 11, 2026
jschmied added a commit to jschmied/TensorFold that referenced this pull request Oct 11, 2026
@jschmied
jschmied deleted the cuda-nvfp4-qlinear branch October 11, 2026 17:36
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…s on the lane matmul, prompt rows on the prompt GEMM)

Thanks @jschmied.

# Conflicts:
#	zig/tests/cuda/main.zig
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