Skip to content

qwen3 cuda: a 1-row call runs cuBLASLt's 1-row algorithm when 64 probe rows alone give the plan's bits (ASR decode 86.4 -> 141.3 tok/s) - #4734

Open
jschmied wants to merge 3 commits into
ashhart:mainfrom
jschmied:qwen3-one-row-gemm
Open

jschmied wants to merge 3 commits into
ashhart:mainfrom
jschmied:qwen3-one-row-gemm

Conversation

@jschmied

@jschmied jschmied commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Head: da5a6a91de on main a325f58258.

Review 7104961514: plan frees the checked-row map on error (errdefer right after the plan value is set, before the reference-row check and the 1-row pick). A new gemm_test.zig test fails each allocation of a fresh plan in turn: 3 leaked allocations without the errdefer, none with it. qwen3-cuda-gpu-test on bdd1c86956: 26 passed, 2 skipped. x86 full set on bdd1c86956: zig build -Dfixed-cpu test-host test test-golden test-kernel-contract test-server-security test-pull-security fuzz-cortheon check-release-paths 2417/2417 steps, 24354/24511 tests (157 skipped); check-native-cuda-host compile-cuda-tests 17/17; pr_check 0 errors; lean 165 vs 165, 0 new; zig fmt --check and git diff --check clean. The other rows below are on c2f53e0e6c; this change touches only the error path.

Review 7121441602: the 1-row algorithm is optional now. In plan, any failure of the 1-row path logs a warning, frees the probe's partial state and keeps 1-row calls on the plan's own algorithm. That covers no 1-row algorithm, a refused heuristic or setup, and a refused host or device probe buffer. Only a lost CUDA context still fails the load. The probe synchronizes the context before it frees its buffers on an error path. Regressions in gemm_test.zig: (a) a stubbed 1-row heuristic finds no algorithm for the 1-row plan, or refuses or returns nothing for the candidate list; (b) each host allocation of a plan fails in turn (FailingAllocator), and the probe's failures now fall back without a leak; (c) setAllocationLimit refuses each of the probe's four device buffers in turn. In each case the plan loads with no 1-row algorithm, device bytes return to their level before the plan, and 64 rows run one at a time give a batch's bits. Control: the same tests on bdd1c86956 fail 3 of 3 (25 passed, 2 skipped, 3 failed). qwen3-cuda-gpu-test on 0e2f6c8fd3 (same code as da5a6a91de): 28 passed, 2 skipped. x86 full set on 0e2f6c8fd3 (da5a6a91de differs from it by one test comment folded to one line): zig build -Dfixed-cpu test-host test test-golden test-kernel-contract test-server-security test-pull-security fuzz-cortheon check-release-paths 2417/2417 steps, 24272/24430 tests (158 skipped); check-native-cuda-host compile-cuda-tests 17/17; zig fmt --check and git diff --check clean. pr_check on da5a6a91de: 0 errors, lean 165 vs 165, 0 new. A stubbed heuristic that returns an error status for the 1-row plan's own pick is not tested: the shared Linear.init logs it as an error, and the test runner fails any test that logs an error. That path ends in the same fallback.

Qwen3-ASR-0.6B decode on the GB10 goes from 86.4 to 141.3 tok/s (+64 %). That's above the 129.7 tok/s the family's own LM pass had before #2239 moved it onto qwen3's CUDA pass. Every token and every bit is unchanged: a 1-row call now runs a second cuBLASLt algorithm only after that algorithm has given the plan's own bits on every probe row.

Root cause

qwen3/cuda/gemm.zig keeps one algorithm per (n, k), picked at 4096 rows, and runs it at every row count, so a batch's rows keep the bits they have alone. At 1 row that algorithm launches 4 to 16 CTAs on the GB10's 48 SMs and reads the weights at 60-160 GB/s. Qwen3-ASR's decode is the pass's only 1-row caller: the embedding and reranker engines run prompt passes.

nsys, clip 1221-135766-0001, 60 decode tokens, per token:

old ASR pass (66d573976 + bench tool) main this PR
wall per token 8.50 ms 12.26 ms 7.53 ms
GPU busy 94.7 % 96.6 % 94.3 %
7 projections per layer (x 28) 173 us 309 us ~165 us
tied head (151936 x 1024) 1.90 ms 2.10 ms 1.36 ms
  • The GPU is busy in all three, so host syncs, launches and the host argmax are not where the time goes.
  • On main, every projection is the 4096-row pick: Kernel2 and nvjet 128x176 tiles, 72 KB of shared memory, grids of 4 to 16 CTAs. The 28 layers' extra 136 us each make up the whole 3.8 ms gap.
  • With this PR, the same projections run at grids of 32 to 96 CTAs, and the head runs at 4752.

What changes

  • zig/src/cuda/one_row.zig is new and shared, beside cublaslt.zig. It holds the pick, the probe and their host tests.
    • pick takes cuBLASLt's 1-row heuristic candidates in order. It passes over any that won't run in the workspace or that split K (refused before the probe runs), and any whose probe bits differ. It takes the first one left. With none left, nothing changes.

    • The probe has 64 input rows, 16 of each kind:

      • normals at 2^-4 to 2^3;
      • wide exponents from 2^-100 to 2^100;
      • half bf16 subnormals, half the smallest normals;
      • cancelling +v/-v pairs with zeros, plus an all-zero row and an all-one row.

      The weights are N(0, 0.03), with one value in 512 an outlier of 1 to 8, repeated over the plan's [n, k]. Every output bit of all 64 rows, each run alone, must equal the plan's algorithm over the 64 together.

  • In qwen3/cuda/gemm.zig, each plan probes once when it is built. A rows == 1 call runs the taken algorithm, and every other call is as on main. The load log names the pick for each (n, k).
  • The one shared-code edit outside the new file is one API symbol, cublasLtMatmulAlgoConfigSetAttribute, used by the planted control.

On the GB10, all six Qwen3-0.6B (n, k) pairs take a 1-row algorithm: the heuristic's candidate 1, 3, 3, 0, 4 and 1, the same picks an 8-row probe made. Loading the plans takes 0.17 s longer, the median over 40 loads.

Exactness

  • Real activations: tf-qwen3-asr --check-one-row runs each projection's rows on the other algorithm too and compares every bit. A 1-row call is checked against the plan's algorithm at 1 row, and each row of a batch against the 1-row algorithm alone. Over the 40 public clips (LibriSpeech test-clean 20, FLEURS cmn 20), every prompt pass and every decode step, all seven projections and the head: 1,423,589 rows compared, 0 differ, 40 of 40 clips PASS.
  • ASR transcripts: in the A-B-B-A below, none of the 40 clips' greedy ids differ between main and this PR, over all 8 passes. As on main, the two FLEURS near-ties from qwen3-asr: host slice, the shared tower and Qwen3 schedules over a backend binding (#2105) #2239 (cmn-10104380129516909856 token 27, cmn-15324743446256875074) differ from the package at the same token with the same margin.
  • GPU tests (qwen3-cuda-gpu-test, gemm_test.zig):
    • For each Qwen3-0.6B (n, k), 64 fresh rows on another seed, run 1 row at a time, give a batch of 64's bits.
    • Planted control (gemm_test.zig:90): the 1-row heuristic's first candidate with K split 4 ways. Past the K-order check, the probe alone sees other bits (red, gemm_test.zig:106). The candidate the plan took passes the same probe (green, :112). Offered alone, the planted candidate is refused and no 1-row algorithm is set (:115). The 1-row calls then run the plan's algorithm, with a batch's bits (:121).
  • Host tests (one_row.zig):
    • pick passes over an unrunnable, an over-budget, a split-K and an other-bits candidate, in order. The split-K one never reaches the probe, and with none left it returns null.
    • The probe rows cover their classes and are the same at every load (seeded).

Other callers: Qwen3-Embedding served, against main

Qwen3-Embedding-0.6B (the size on disk) through tensorfold-native serve. Main a325f58258 (md5 ceef9f7c) and this head (md5 47a3b403) took turns in the order main, head, head, main. The page cache was dropped before each start, and one server ran at a time. Each run made 20 requests: the card's 4 texts alone and batched, 10 one-token inputs alone and batched, and seeded ids of 2048, 8192, 16384 and 30000 tokens.

  • Bytes: every vector's SHA256 is equal across all four runs (20 of 20 requests). Repeat requests didn't drift.

  • The 1-row path: each one-token input runs alone through the new 1-row algorithm. Its vector equals the same input's row in one batch of 10, which runs the plan's algorithm, in every run on main and on head. The embedding server's load log takes the same 1-row picks as ASR (1, 3, 3, 0, 4).

  • Prompt passes, paired (head − main), median over 3 requests in each run:

    request main ms head ms paired median
    2048 ids 631.7, 630.3 632.6, 630.3 +0.9, −0.0 +0.07 %
    8192 ids 8669.5, 8664.4 8671.1, 8668.8 +1.7, +4.4 +0.03 %
    16384 ids 33721.0, 33704.2 33717.6, 33719.5 −3.4, +15.3 +0.02 %
    4 texts batched 17.3, 15.9 16.0, 15.9 −1.3, −0.0 −3.8 %

    4 of 8 pairs are slower on head, all within 0.07 %, and their spread includes zero. Runs of more than one row execute the same code as main. Server load is 2.0 s in all four runs.

qwen3-cuda-gpu-test's embedding and reranker checks skip here. Their oracle is pinned to Qwen3-Embedding-8B, and a reranker checkpoint isn't on this box either. The served bytes above against main stand in for them on the 0.6B.

Seeded sampling, resume and concurrency don't apply here. Qwen3-ASR's CUDA path is greedy on one stream (tf-qwen3-asr), and the embedding and reranker engines keep no decode state across requests. The change sits below them all, in which algorithm a 1-row projection runs, and its bits are the plan's. The served bytes and the 1-token-alone-vs-batched rows above cover the serving side.

Speed: ASR decode, A-B-B-A, final head

tf-qwen3-asr, 40 clips (421.2 s of audio) per pass, passes ordered A B B A A B B A. A is main a325f58258 (md5 628c645a) and B is this head (md5 65acf5ef). The page cache was dropped first. The SM clock, sampled every second, read 2288 MHz in all 744 samples, with the same throttle word in both arms (the box's 2300 MHz cap) and no thermal or power throttle.

pass arm decode tok/s
1 A 86.4
2 B 141.1
3 B 141.3
4 A 86.3
5 A 86.3
6 B 141.5
7 B 141.3
8 A 86.4
  • main 86.3-86.4 tok/s, this PR 141.1-141.5 tok/s: +63.5 %. Before the rebase, the same change measured 142.1 against 86.3 on e6094de13.
  • Before qwen3-asr: host slice, the shared tower and Qwen3 schedules over a backend binding (#2105) #2239's final form (66d573976, the family's own LM pass), decode was 129.7 tok/s. This PR restores that speed and goes past it.
  • None of the 40 clips' greedy ids differ across any pass or arm. Prompt-pass, tower and mel times are untouched: the 1-row algorithm runs only on 1-row calls.

Checks on c2f53e0e6c

  • x86 (x86-steps.py --final) on c2f53e0e6c: zig build -Dfixed-cpu test-host test test-golden test-kernel-contract test-server-security test-pull-security fuzz-cortheon check-release-paths rc 0 (2,417 of 2,417 steps; 24,796 of 24,956 tests passed, 160 skipped, 0 failed); check-native-cuda-host compile-cuda-tests rc 0; tools/pr_check.py rc 0; zig fmt --check rc 0; git diff --check rc 0; lean: 0 new problems (165 on the base, 165 here).
  • GB10: zig build test test-golden test-cuda-host test-qwen3-cuda-host test-qwen3-checks-host test-qwen3-asr-host -Dnvcc rc 0 (829 of 829 steps; 12,208 of 12,300 tests passed, 92 skipped, 0 failed). qwen3-cuda-gpu-test rc 0 (25 passed, 2 skipped: the oracle-pinned embedding and reranker checks). tf-qwen3-asr --check-one-row on 40 clips: 40 of 40 PASS.
  • No kernels are added or changed, so the kernel contract has nothing new to record. The planted control is the split-K test above.

Proposed CHANGELOG line (not committed):

- qwen3 cuda: a 1-row projection runs cuBLASLt's 1-row algorithm when 64 probe rows alone give the 4096-row plan's bits; Qwen3-ASR-0.6B decode 86.3 -> 142.1 tok/s on the GB10, tokens unchanged.

…alone gives the plan's bits

Each (n, k) plan keeps its 4096-row algorithm for every row count. At 1 row that algorithm launches 4 to 16
CTAs on the GB10's 48 SMs and reads the weights at 60-160 GB/s, so Qwen3-ASR's decode, the pass's only 1-row
caller, ran at 86.3 tok/s. A plan now asks cuBLASLt's heuristic for 8 algorithms at 1 row and keeps the first
that runs in the workspace, splits no K and gives the plan's bits on 64 probe rows run alone: normals, wide
exponents, subnormals and cancelling pairs, over probe weights of the plan's (n, k). Without one, 1-row calls
run the plan's algorithm as before.

cuda/one_row.zig (shared, beside cublaslt.zig) holds the pick and the probe, with host tests. gemm_test.zig
(qwen3-cuda-gpu-test) checks 1 row against a batch of 64 for Qwen3-0.6B's pairs and plants a split-K candidate:
other bits, refused, fallback. tf-qwen3-asr --check-one-row runs every projection on both algorithms and fails
on a differing bit.
@ashhart

ashhart commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Thanks, @jschmied, for the carefully guarded one-row path and exact-bit controls.

I found one cleanup issue. Once the initial row check succeeds, the plan’s checked-row hash map owns storage. If creation of the one-row plan or its probe then fails, the existing error handlers remove the plan and destroy its linear descriptor, but never deinitialize that hash map. Outer teardown cannot recover the storage because the plan has already been removed.

Please add an error defer for the checked-row map immediately after initializing the plan value, before either fallible operation, so failed loads release it.

  • Synthena

…s checked-row map

plan() removed a failed plan and destroyed its linear descriptor but never deinitialized the checked-row map, which
owns storage once the first row check succeeds; a failing 1-row pick or probe leaked it. An errdefer now frees it
right after the plan value is set. gemm_test.zig fails every allocation of a fresh plan in turn: 3 leaks without
the errdefer, none with it.
@jschmied

Copy link
Copy Markdown
Contributor Author

Done in bdd1c86956: plan now has errdefer got.value_ptr.checked.deinit(g.gpa) right after the plan value is set, before the reference-row check and the 1-row pick, so a failed load frees the checked-row map.

How it was checked: a new test in gemm_test.zig fails each allocation of a fresh plan in turn (std.testing.FailingAllocator over the leak-checking test allocator) until the plan builds. Without the errdefer it reports 3 leaked 96-byte allocations (the checked-row map, from the failures inside the 1-row probe); with it, none. qwen3-cuda-gpu-test on the GB10: 26 passed, 2 skipped (the oracle-pinned embedding and reranker checks, as before). x86 full set on bdd1c86956: zig build -Dfixed-cpu test-host test test-golden test-kernel-contract test-server-security test-pull-security fuzz-cortheon check-release-paths 2417/2417 steps, 24354/24511 tests (157 skipped); check-native-cuda-host compile-cuda-tests 17/17; pr_check 0 errors; lean 165 vs 165, 0 new; zig fmt --check and git diff --check clean.

@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks, @jschmied. The checked-row map cleanup is now in the right place, and the allocation-failure test reaches the later probe allocations that exposed the leak. The cleanup order looks correct, with no double free.

Two fallback cases still need attention. If the optional one-row heuristic cannot provide an algorithm or its setup fails, please keep the valid existing batch plan instead of failing model loading. The optional probe also needs a second full weight copy and output buffers—about 350 MB of extra GPU memory for the tied head—so an allocation failure can stop a model that otherwise fits. Please release partial optional state and fall back to the existing algorithm for these cases, with regressions proving the fallback and leak-free cleanup.

The guarded dispatch itself still looks sound: batch calls stay on the original plan, and an admitted one-row candidate must match every probe row bit for bit.

  • Synthena

No 1-row algorithm, a refused heuristic or setup, and a refused host or device
probe buffer now log a warning, free the probe's partial state and leave 1-row
calls on the plan's own algorithm; only a lost CUDA context still fails the load.
@jschmied

Copy link
Copy Markdown
Contributor Author

Done in da5a6a91de. The 1-row algorithm is optional now: in plan, any failure of the 1-row path logs a warning, frees the probe's partial state and keeps 1-row calls on the plan's own algorithm. That covers no 1-row algorithm, a refused heuristic or setup, and a refused host or device probe buffer. Only a lost CUDA context still fails the load. On an error path, the probe now synchronizes before it frees its buffers.

Regressions in gemm_test.zig, each checking that the plan loads with no 1-row algorithm, that device bytes return to their level before the plan, and that 64 rows run one at a time give a batch's bits:

  • a stubbed 1-row heuristic finds no algorithm for the 1-row plan, or refuses or returns nothing for the candidate list;
  • each host allocation of a plan fails in turn (FailingAllocator): the probe's failures fall back with no leak, and only the map's two fail the plan;
  • setAllocationLimit refuses each of the probe's four device buffers in turn. The peak shows the earlier ones were held, and the refused one never was.

A stubbed error status for the 1-row plan's own pick is not tested. The shared Linear.init logs it as an error, and the test runner fails any test that logs one. That path ends in the same fallback.

How it was checked: the same tests on bdd1c86956 fail 3 of 3 (25 passed, 2 skipped, 3 failed). qwen3-cuda-gpu-test with the fix: 28 passed, 2 skipped. x86 full set on 0e2f6c8fd3 (da5a6a91de differs from it by one test comment folded to one line): zig build -Dfixed-cpu test-host test test-golden test-kernel-contract test-server-security test-pull-security fuzz-cortheon check-release-paths 2417/2417 steps, 24272/24430 tests (158 skipped); check-native-cuda-host compile-cuda-tests 17/17; zig fmt --check and git diff --check clean. pr_check on da5a6a91de: 0 errors, lean 165 vs 165, 0 new.

@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. Both fallback cases are handled on da5a6a91de:

  • No usable 1-row algorithm: a missing algorithm, a refused heuristic or setup, or a refused probe buffer now logs a warning, frees the probe's partial state and keeps 1-row calls on the plan's own algorithm. Only a lost context still fails the load.
  • Cleanup: on an error path the probe waits for its queued runs before it frees its buffers.
  • Tests: the stubbed heuristic, each host allocation and each of the four device buffers are covered, and each case checks that device memory returns to its earlier level and that rows run one at a time still give the batch's bits.

That closes our review of the code, and nothing more is needed from you. The landing step is on our side.

  • Synthena

@joshuaswarren

Copy link
Copy Markdown
Contributor

sm_120 contract run on RTX 5060 Ti (independent of GB10), head bdd1c86.

tf-cuda-test kernel-contract Qwen3-ASR (queued run): 2/2 kernels pass gates 1-4, 1 limit problem (tf_vit_conv_cols 20 vs 22 recorded regs).

tf-cuda-test kernel-contract qwen3 Qwen3 (full family filter, same head): 15 kernels pass gates 1-4, 0 failures, 4/4 controls red as planted, 6 known findings, 20 limit problems — the sm_120 records for qwen35_bf16_rows, attn_partial_paged/contiguous and tf_q35k_apply_pend read 0,0 placeholders; small reg drifts elsewhere (tf_q35d_gather_rows 18 vs 20, tf_q35_tap 25 vs 26, tf_q35d_norm_rope_batch 40 vs 36). nvcc 13.5 images against the 13.3 record set.

No gate failures on sm_120; the limit diffs look like record-staleness on this arch. The 1-row pick itself is host-side and untouched by the contract kernels; no checkpoint available on this box for the qwen3-cuda-gpu-test oracles.

@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thank you @jschmied. The checked-row cleanup and optional heuristic/allocation fallback are in place, with queued probe work waited for before its buffers are released. The batch dispatch is preserved.

The new sm_120 contract receipt is useful, but it uses the earlier head and does not exercise the one-row selection. Current-main exact projection and transcript checks, plus the skipped embedding and reranker checks, remain before landing. No further code change is requested from this review.

  • Synthena

@AIFlowML

Copy link
Copy Markdown
Contributor

The clean trial merge of da5a6a91de into 4c79cfe346 passed 22 Qwen3 CPU tests and five host oracle checks on M5 Max. The native adapter, the CUDA test binary, and tf-qwen3-asr all cross-compiled for ARM64 Linux.

zig build test-qwen3-cuda-host check-native-cuda-host -Dfixed-cpu -j2 --summary all
zig build test-qwen3-checks-host qwen3-cuda-gpu-build tf-qwen3-asr -Dtarget=aarch64-linux-gnu -Dfixed-cpu -j2 --summary all

For the remaining device receipt, the embedding test needs TF_QWEN3_EMBED_DIR, TF_QWEN3_EMBED_REF and TF_QWEN3_EMBED_BAR. The reranker test needs TF_QWEN3_RERANK_DIR and TF_QWEN3_RERANK_REF. For the ASR projection check, tf-qwen3-asr MODEL_DIR CLIP.wav --check-one-row reports rows compared only for plans that selected the alternate algorithm, so please retain the comparison count and note any fallback shapes alongside the new transcript result. I could compile those checks here. The cuBLASLt runs remain open.

Igor Lessio - AIFLOW LABS

This branch has not been deployed

No deployments
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