Skip to content

cuda: one quantized-weight interface (qlinear.zig); Nemotron's 4-bit projections route through it - #587

Merged
ashhart merged 1 commit into
ashhart:mainfrom
jschmied:cuda-qlinear
Oct 10, 2026
Merged

ashhart merged 1 commit into
ashhart:mainfrom
jschmied:cuda-qlinear

Conversation

@jschmied

@jschmied jschmied commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

First step of the quantized-weight interface claimed in #548. The 4-bit projections Nemotron launches move out of families/nemotron into zig/src/cuda/qlinear.zig, behind one weight view with a decode-row and a prompt-row call. Nemotron routes through it. Behaviour doesn't change: same kernels, same grids, same arguments.

  • cuda/qlinear.zig:
    • Affine4 is the MLX 4-bit tiled weight (Nemotron's QLinear).
    • Affine4Kernels holds what moves: resolve from the loaded qmm_group, qmm_prefill and lane_gemv modules; decode = qmm.matmul's choice between split-K lane_gemv, lane_gemv and qmm_group's cluster tile; prompt = qmm.prefill_matmul.
    • Weight is a tagged view (affine4, fp8g from cuda: block-FP8 projections on the Python lane matmul's FP8G kernels #482). Linear.decode and Linear.prompt dispatch on it, and a format that isn't loaded is refused before any launch.
  • families/nemotron/cuda_kernels.zig:
    • QLinear, Split and splitK become aliases.
    • Kernels holds one Affine4Kernels instead of four functions and the split scratch, and Ops.dense, gemv, gemvSplit, cluster and prefillDense delegate.
    • No caller changes except check-kernels, which reads the split scratch at its new place. The family is 111 lines shorter.
  • tf-cuda-test fp8-lane also runs each row count through Linear.decode and Linear.prompt and checks the bytes against the Python lane matmul.

Receipts on GB10 (DGX Spark, sm_121, CUDA 13.0). main (4ebf52a) and this branch were built with one fatbin set (-Dsm=121) and run on Nemotron 3.5 Lightning (TensorFold/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-MLX-4bit @ d9d758fb):

  • zig build test passes; lean_check is clean for the touched files.

  • tf-cuda-test fp8-lane: 1, 3, 17, 40 and 300 rows of a [2560, 6144] block-FP8 weight equal Python's bytes, directly and through both qlinear calls. That covers tiles 16, 32 and 64, the cluster path and the fused path.

  • tensorfold check-kernels: 1,008 of 1,008 projection cases byte-equal (976 split-K), and 384 of 384 forked-MoE cases, on both builds.

  • tensorfold run: three prompts (11, 20 and 1,090 tokens), greedy and sampled (temperature 0.8, top_k 20, top_p 0.95, seed 7). All six reply hashes are equal:

    prompt greedy sampled
    11 tokens 5923b40d570b both d38725819372 both
    20 tokens ead827be94f5 both bb8855c2f084 both
    1,090 tokens 068276d3ec75 both a6961a8c7589 both

Next, as listed in #548: NVFP4 behind the same view (lane matmul for decode rows, a prompt GEMM for prompt rows, each with an oracle fixture), then the grouped experts per format behind #558's plan.

@ashhart

ashhart commented Oct 10, 2026

Copy link
Copy Markdown
Owner

Thank you, @jschmied. This is merged into main (6c3ce72) with your commit as it was, and it ships in the next release. Putting the symbols, shared-memory sizes, occupancy and split scratch in one struct that resolves itself from the loaded modules is the single owner #548 asked for, and your receipts made it quick to check.

We gated it on a GB10. tensorfold check-kernels gives 1,008 of 1,008 projection cases (976 split-K) and 384 of 384 forked-MoE cases on main and on the merge, as in your receipt, and fp8-lane passes on both of Kolibri-1's shapes, directly and through Linear.decode and Linear.prompt. Nemotron didn't move: 29 of 29 served replies (greedy and seeded sampled, drafted and "draft": false, three conversations at once, turns resumed from the prompt cache, and the same workload with the cache off) and the four release prompts give the same tokens as 1.0.4 and main. Decode is within 0.4% of main, cold prompts from 2k to 32k tokens within 0.7%, and device memory is the same to the byte.

We also checked the decode contract for FP8 directly. Every row count from 1 to 300 through Linear.decode gives the same bytes as the first rows of the 300-row call, so the fused path agrees with the cluster tiles, and prompt equals decode every time.

One small commit of ours follows yours. d7469ca fixes the FP8 test: it uploaded with fromHost, a synchronous copy on the legacy stream that can return before a pageable copy lands, and launched on a non-blocking stream that doesn't wait for it. With other work on the GPU, the 3-row case could read its input mid-copy, and four copies of the test at once failed in 4 of 40 runs. The stream is now blocking, and viaLinear fills its output before each call, so a prompt path that writes nothing can't pass on the decode call's bytes. That will matter for NVFP4, where the two calls differ.

Two notes for the next step. Nemotron calls Affine4Kernels directly, so Linear gets its first model with NVFP4 or a ported family. And the affine-4 packing still lives in Nemotron's weight loader, if you'd like it next to the format as #548 describes.

GarupanOjisan pushed a commit to GarupanOjisan/TensorFold that referenced this pull request Oct 10, 2026
ThinkOffApp pushed a commit to ThinkOffApp/TensorFold that referenced this pull request Oct 10, 2026
A pageable cuMemcpyHtoD can return before the copy lands, and kernels on a
non-blocking stream don't wait for the legacy stream, so a test that uploaded
with fromHost and launched at once could read its input mid-copy when other
work shared the GPU (ashhart#587 found it in fp8-lane: 8 failures in 80 runs with four
copies at once). Uploads now wait for the legacy stream; the engine's own
copies are stream-ordered and unchanged.
@jschmied
jschmied deleted the cuda-qlinear branch October 11, 2026 17:36
satindergrewal added a commit to satindergrewal/TensorFold that referenced this pull request Oct 12, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
… Nemotron's 4-bit projections route through it
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
A pageable cuMemcpyHtoD can return before the copy lands, and kernels on a
non-blocking stream don't wait for the legacy stream, so a test that uploaded
with fromHost and launched at once could read its input mid-copy when other
work shared the GPU (ashhart#587 found it in fp8-lane: 8 failures in 80 runs with four
copies at once). Uploads now wait for the legacy stream; the engine's own
copies are stream-ordered and unchanged.
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