Skip to content

qwen3_5 cuda: a pool lane's state as pieces on any whole pages of its lease, no run (#2444 P3, Q3) - #2939

Draft
jschmied wants to merge 15 commits into
ashhart:mainfrom
jschmied:qwen-state-pages
Draft

jschmied wants to merge 15 commits into
ashhart:mainfrom
jschmied:qwen-state-pages

Conversation

@jschmied

@jschmied jschmied commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

The pages step of #2444 P3 for Qwen 3.5/3.6 on CUDA (Q3 in the P3 plan). Under TF_QWEN_KV_PAGES, a pool lane used
to take its state, log, rollback copy, taps and drafter ring as one run of consecutive pool pages, so a fragmented
pool refused a lane with LaneMemory. Here a lane's state lies as pieces on whole pages it takes from its lease,
any ids. #2620's state table already addresses the pieces, so no kernel changes.

Stacked on #2620 (6587e14, on main 328a566). The base is main because #2620's head is on the fork. Please
review only the two commits after it: 5451b4a (core) and 1350872 (Qwen). Head: 1350872.

  • Core, shared with F2 (flashnext: pool lanes keep their states and draft as pieces on whole pages, no run (#2444 P3, F2) #2898): kv_pieces.zig packs a family's pieces first-fit decreasing on whole pages,
    each piece inside one page and 256-byte aligned, and checks a map for overlap. Lease.state holds the state's pages
    beside the token table. Pool.takeState takes them from the promise, and finish gives them back. Layout.pieces
    counts them wherever the run counted (need, plan, holds, held). No claimRun and no consecutive run.
    takeState takes the highest free ids and keeps the free stack's order, so KV pages still fill from the bottom
    (see the c=4 cause below). flashnext: pool lanes keep their states and draft as pieces on whole pages, no run (#2444 P3, F2) #2898 carries the same core commit with the older takeState and will take this one.
  • Qwen: cuda_state_pieces.zig plans a lane's pieces: a recurrence and a conv tail per linear layer, their
    pre-round copies, and the 64 log rows. cuda_cache_lane.zig maps them at open (the state table written once and
    uploaded), zeroes them, rolls back and copies them piece by piece, and unmaps at close. A closed lane's table is
    refused (version 0, MissingQwenStates). The save/restore and reuse paths walk the pieces.
  • Outside the pool: the tap ring and a drafter's ring are wider than a page (27B DFlash2: about 300 MiB). They
    stay outside the pool as part of a lane's fixed part, are counted at admission (the extra lane the pool makes when
    every stream is on pages too: cuda_kv_pool.zig served), and the load line names them,
    as Roadmap: one paged KV pool and a radix prompt cache for every family #2444 option (a) settles.
  • Load line at 256 (27B): state, in the pool as pieces: 118 pieces, 411.38 MiB on 27 pages a lane, 20.63 MiB padding (4.8 %). qwen3_5 cuda: the GDN kernels read the state, its rollback copy and the log through a stream's state table (#2444 P3) #2620's run took 26 pages with 4.63 MiB padding.
  • Behaviour change for you to rule on: a page must hold the largest piece, which is a layer's recurrence or a
    log row. A smaller TF_QWEN_KV_PAGES is now refused at load by name (QwenPageBelowState), and the log names the
    smallest size that works: 67 rows on the 27B, 103 on the 35B-A3B. The run accepted any page size.

Rebase onto main 328a566 (head 1350872)

Rebased with #2620 (see its body for the GDN side: main's rollback-copy guard, Shape.key on u64 with table on
bit 33, SEG_STATES 445,472). On this PR's two commits:

  • Core commit: main added page retirement (56b998c) to kv_pool.zig. finish keeps main's skip of retired
    ids (retirement.none) for the token table and gives the state pages back as before; state pages are never in
    ids, so retirement does not see them. cuda_kv_pool.zig is flashnext: pool lanes keep their states and draft as pieces on whole pages, no run (#2444 P3, F2) #2898's current file as is (7ca3798, three blank
    lines out, 600 lines). kv_pool.zig takes main's kv_pool_trim_cost_test.zig line too, so to stay at 600 lines
    kv_pool_state_test.zig is referenced from kv_pieces.zig's test block instead of kv_pool.zig's; that is the
    one difference from flashnext: pool lanes keep their states and draft as pieces on whole pages, no run (#2444 P3, F2) #2898's core files, besides main's test line.
  • Qwen commit: cuda_reuse.zig grew on main (spares, 595 lines), so its image-rule test ("a state resumes only a
    request with the same images") moves unchanged to cuda_reuse_test.zig (596 lines). The cuda_pages.zig and
    cuda_reuse.zig imports keep both sides.
  • Main's spares test (cuda_spares_test.zig): its fixture starts from an undefined cache and sets main's
    fields; it now also sets pieces = null, so the save path walks the one block as on main. Without it the x86 host
    run crashed in 8 of those tests (cuda_cache_lane.zig part on garbage pieces).

Main against the pair (#2620 + this), on these heads

Main 328a566 (tensorfold-native md5 7f498d79), #2620 6587e14 (md5 6801b1a7) and the pair (md5 c34e9cf1;
1350872 and f4f591e74 differ only in a host test), each zig build native -Dsm=121, the md5 of each served binary
checked, one GB10 with the clock capped and the page cache dropped before each server, --parallel 8 --context 16384.
c=1 is 3 x 256 tokens (tok/s from the done lines, one decimal). c=4 is 3 rounds of 4 x 256. In every arm the greedy
shas (solo, c=4, c=8, decode and the c=4 rounds) are equal to main's.

Pages unset, main against the pair, A-B-B-A twice. The paired median is over 4 neighbouring pairs.

model c=1 main / pair c=1 paired c=4 main / pair c=4 paired tokens
Qwen3.8-27B NVFP4 + DFlash2 29.10 / 29.05 -0.17 % (2 of 4 slower) 71.51 / 71.66 +0.19 % (2 of 4 slower) equal
Qwen3.8-27B NVFP4, no drafter 11.60 / 11.60 +0.00 % (0 of 4 slower) 36.20 / 36.19 -0.04 % (2 of 4 slower) equal
Qwen3.6-35B-A3B + MTP 114.35 / 113.80 -0.44 % (4 of 4 slower) 185.60 / 185.15 -0.26 % (3 of 4 slower) equal

The 35B c=1 loss was run again (main, #2620, pair, A-B-C-C-B-A twice): the pair against main was -0.22 / +0.57 % at
c=1 and +0.07 / +0.06 % at c=4, #2620 alone -0.17 / +0.35 % and -0.37 / -0.17 %. With pages unset the pair runs main's
kernels (SASS above, through #2620).

TF_QWEN_KV_PAGES=256: main, #2620 alone and the pair, A-B-C-C-B-A twice. Each block's difference to main
cancels linear drift: (x_i + x_j) / 2 against (main_i + main_j) / 2.

model c=4 median main / #2620 / pair #2620 against main, block 1 / 2 pair against main, block 1 / 2 c=1 pair against main tokens
Qwen3.8-27B NVFP4 + DFlash2 71.27 / 70.90 / 71.32 -1.03 / +0.14 % +0.19 / +0.35 % +0.86 / -0.68 % equal
Qwen3.8-27B NVFP4, no drafter 36.09 / 36.06 / 36.07 -0.06 / -0.11 % +0.02 / -0.42 % +0.00 / -0.43 % equal
Qwen3.6-35B-A3B + MTP 184.27 / 184.72 / 184.06 +0.23 / +0.12 % -0.11 / -0.04 % +0.31 / +0.04 % equal

Paired losses, stated plainly: pages unset, the pair's 35B c=1 was slower in 4 of 4 pairs (-0.44 %) and its c=4
in 3 of 4 (-0.26 %); the rerun did not repeat the c=1 loss. At 256 the pair is slower than main in 5 of 12 blocks
(-0.04 to -0.68 %), and #2620 alone in 3 of 6 c=4 blocks. Under the no-slower-cell rule: don't land yet. These sit
inside the ±0.6 % run-to-run spread of one cell, and the unset path runs main's kernels, but I'm reporting them as
measured rather than calling them level.

The c=4 cause, found and fixed on this branch. An earlier head (4881be351) was slower than main at 256 and c=4
in 4 of 4 pairs on every configuration: -0.05 % (27B + DFlash2), -0.16 % (27B, no drafter) and -0.21 % (35B-A3B +
MTP). nsys over one c=4 window (27B, 256; never ncu) showed:

  • equal launch counts;
  • tf_q35_attn_partial 14-16 % slower by median (103 -> 118-120 us), only on this branch.

takeState had taken a lane's state pages from the low free ids, so the four streams' KV pages lay about 27 pages
apart instead of side by side. With the state on the highest ids (9a671bd), attention runs at 98.8-100.5 us
against main's 107.7 and #2620's 101.6. kv_pool_state_test.zig pins the order.

Checks on this head

On 1350872 (stacked on #2620 6587e14, merge base 328a566), nvcc 13.0.88, one GB10 (sm_121):

  • Build and test (aarch64, on 21d0a01ec; 1350872 adds qwen3_5 cuda: the GDN kernels read the state, its rollback copy and the log through a stream's state table (#2444 P3) #2620's qwen-table line and the spares fixture line): zig build -Dfixed-cpu test test-golden (781/781 steps), test-cuda-host (438/438 steps), the CI set (54/54 steps, 343/343), test-kv-pool
    (113/113), test-qwen35-stage-host (25/25), every qwen/recurrent -host step (331/331 steps),
    tools/zig/cuda_common_check.py, pr_check (0 errors), zig fmt --check, git diff --check, no changed file over
    600 lines, lean 0 new (168 against 168). On 1350872: test-qwen35-kept-spares-host and
    test-qwen35-modelopt-admission-host (220/222, 2 skipped).
  • x86-64 host, on 1350872: check-native-cuda-host compile-cuda-tests (17/17), the full host set with
    check-release-paths (2287/2287 steps, 22513/22669 tests, 156 skipped), pr_check (0 errors, the CHANGELOG
    warning), zig fmt --check. Run with umask 022: under the box's umask 0002 main fails its own cli_config_test
    mode check the same way.
  • GPU, tf-cuda-test outcome lines equal to main's: qwen-pending 49 lines plus the 49 _table cases, all PASS;
    qwen-pages 26 lines; kernel-contract qwen35 q35 125 lines plus 11 _table limit lines, all PASS; qwen3_5 cuda: the GDN kernels read the state, its rollback copy and the log through a stream's state table (#2444 P3) #2620's
    qwen-table on this head: 25 PASS lines, every planted control red.
  • SASS: this PR changes no kernel source (git diff 6587e1435 1350872c2 -- zig/kernels is empty); cuda_sass_compare.py 6587e1435 f4f591e74 --images qwen35,qwen35_keep,qwen35_kv8 refuses with
    "image scope is not reached by this change". The SASS against main is qwen3_5 cuda: the GDN kernels read the state, its rollback copy and the log through a stream's state table (#2444 P3) #2620's (qwen35 33/33, qwen35_keep 4/4,
    qwen35_kv8 PASS on all five SMs).
  • Conformance kit (main's tools/conformance.py, profile qwen35-cuda), main 328a566 and this head, three
    configurations (27B + DFlash2, 27B without a drafter, 35B-A3B + MTP), each with TF_QWEN_KV_PAGES unset, at 256 and
    at 256 with TF_QWEN_KV_SHARE=16:
    • in all 9 cells the reply keys are equal on main and this head (139 a cell with a drafter: 60 greedy, 61 seeded
      sampling, 4 refusals, 14 evict and overlap; 116 without one) and each gate's verdict is the same;
    • on this head, 256 and 256 + share against unset: 0 keys differ in all 6 pairs (greedy and seeded, resume,
      concurrency 1/2/4/8, drafting);
    • gates: 27B + DFlash2 7/7 (6/7 with share), 35B-A3B 6/7; evict fails on main too (its fill evicts nothing:
      cached 4096 -> 4096, 3/3 equal against cache-off); the 27B without a drafter uses the earlier copies profile, 4/7
      on main and here.
  • Planted controls (from the earlier rounds, code unchanged by the rebase), each red: the packer overlapping
    pieces (kv-pool and cuda-host tests); the rollback copy's pieces on log row 0's piece (cuda-host); a reopened lane
    keeping its last lease's table entries (18 of 76 concurrent == solo unequal, 0 illegal-address lines); the old init
    order (cuda_pages_init_test.zig). LEAK: a failed state allocation, a cancel after open and a failed table write
    leave the pool as at load.

Receipt

Proposed CHANGELOG line (for you to place):

  • Qwen 3.5/3.6 on CUDA: with TF_QWEN_KV_PAGES, a pool lane keeps its recurrent state, rollback copy and log as pieces on any whole pages of its lease instead of one run of consecutive pages, so a fragmented pool still admits a lane; a page size below the largest piece (67 rows on the 27B, 103 on the 35B-A3B) is refused at load by name (part of Roadmap: one paged KV pool and a radix prompt cache for every family #2444). Thanks to @jschmied.

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency.
  • Comments and docstrings are one line. No measurements or history in the source.

@ashhart

ashhart commented Nov 11, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. The piece packing, the page accounting through statePages, the closed-lane refusal and the save/restore walk all read correctly, and nothing changes with pages unset. A few things before this lands (after #2620):

  1. zig/src/families/qwen3_5/cuda_pages.zig:103-110: made += 1 now comes before the lane's outside block is allocated. If that allocation fails, the loop body's errdefer gpa.destroy(c) (line 106) frees the cache. The function's errdefer at line 103 then runs freeLane on the same lane: deinit on freed memory and a second destroy. Please count the lane only once its outside block exists (or give the block its own errdefer). Please also add an init test with outside > 0 that fails that allocation and checks that everything is released.
  2. CI is red. The zig job stops at the lean check, because cuda_native.zig is 600 lines on main and this adds 3 (603 merged), so none of the Zig tests ran on this head. Please rebase on main and keep the file at 600 lines or under, for example by moving the statePieces call into cuda_pages.zig.
  3. Under TF_QWEN_KV_SHARE the pool has one lane more than the plan counts: cuda_native.zig:127 makes streams + 1 lanes, while kv_pool.zig:144 charges lanes - 1 fixed parts, and the engine's own window is still allocated (cuda_reserve.zig:12). That lane's taps and ring used to come out of the pool's pages. Now cuda_pages.zig:110 allocates its outside block (about 300 MiB on the 27B with DFlash2) beyond the plan. Please count it when every stream is on pages, so "counted at admission" holds for every lane.
  4. Small: kv_pieces_test.zig:7 and cuda_state_pieces_test.zig:78 cite f2_pack.py, q3_pack.py and pieceSizes, which are not in this tree. Please say what the numbers check, or drop the references.

On the behaviour change you asked us to rule on: refusing a page smaller than the largest piece at load, by name and with the smallest size that works, is fine. It applies only under the opt-in, and 256 clears it on the 27B, the 35B-A3B and the 2B, with fp8 caches too (their minimums are higher there, 131 and 202 rows on the 27B and 35B-A3B, which your log line already computes).

#2898 carries the same core commit, so whichever of the two lands second takes the other's kv_pieces.zig, kv_pool.zig and kv_pool_state_test.zig.

  • Synthena (Ash's Agent)

@jschmied

Copy link
Copy Markdown
Contributor Author

@ashhart thanks.

  1. Double free at init (cuda_pages.zig:103-110). Fixed. The loop now counts a lane only once its cache and its outside block both exist. Until then the loop's own errdefers free the cache (deinit, then destroy), so freeLane never sees a half-made lane. The new test in cuda_pages_init_test.zig, "failed outside block (taps and ring) frees its lane once and everything before it", runs Pages.init with outside > 0 and fails lane 0's outside block, then lane 1's. Both times it checks that every device allocation and host allocation is released and that no fill was queued. With the old order it goes red (checked by putting the old order back).
  2. CI and lean, rebased. Both branches are rebased onto main f0d8c8d. qwen3_5 cuda: the GDN kernels read the state, its rollback copy and the log through a stream's state table (#2444 P3) #2620 is now 0a49c0a; its commits are unchanged, and the base moved because Q3 sits on it. This PR is now f4a0bdf. cuda_native.zig is 600 lines on this head: the statePieces call moved out of its own line, and statePieces returns no pieces when there is no pool.
    Gates on f4a0bdf: test/golden, cuda-host, the CI set, test-kv-pool, x86 with pr_check and lean (0 new, 386 against 386), all green. qwen-pending, qwen-pages and kernel-contract qwen35 q35 match main's outcome lines, and qwen-table gives 25 PASS lines with every control red. All host and stale-lane plants go red. qwen3_5 cuda: the GDN kernels read the state, its rollback copy and the log through a stream's state table (#2444 P3) #2620's gates are rerun on 0a49c0a too, SASS included: every main function is equal on all five SMs.
  3. The extra lane under TF_QWEN_KV_SHARE. It is counted now. When every stream is on pages (share, and also TF_QWEN_KV_PAGES_ALL, which makes the same extra lane), cuda_kv_pool.zig served takes that lane's block outside the pool off the room before the plan sizes the pool. So "counted at admission" holds for every lane. Families without an outside block, GLM among them, see no change. A test in cuda_kv_pool.zig checks the room with and without it.
    Conformance kit on f4a0bdf against main f0d8c8d (27B + DFlash2, 27B without a drafter, 35B-A3B + MTP): unset, 256 and 256 + TF_QWEN_KV_SHARE=16 all pass, with 0 of 114 keys differing in all 9 cells. On this head, 256 and 256 + share against unset differ in 0 of 114 keys in all 6 pairs.
  4. Tool references in the tests. Dropped. kv_pieces_test.zig now says its numbers are Flash Next's piece sizes (a recurrence, a conv tail, the MTP tail and projection, a page head), counted by hand. cuda_state_pieces_test.zig:78 compares against the run as runBytes took it.

Thanks for ruling on the page-size refusal. On #2898: agreed, whichever lands second takes the other's kv_pieces.zig, kv_pool.zig and kv_pool_state_test.zig. This PR's takeState takes the highest free ids (the c=4 fix in the body), and #2898 will take that.

The decode hot path did not change (load, init and the load-time plan only), so the speed table stands.

Main has moved again since the rebase: on 000b534 the pipeline-stage commits (4dd870b to 799ae94) touch cuda_engine.zig and cuda_shapes.zig, so both PRs show a conflict there. Say if you want another rebase; the gates on both heads take about 8 h.

@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. All four points are answered on f4a0bdf32:

  1. The init loop counts a lane only once its cache and its outside block both exist (zig/src/families/qwen3_5/cuda_pages.zig:103-113), and the new test (cuda_pages_init_test.zig:173) fails lane 0's and then lane 1's outside block and checks that everything is released.
  2. cuda_native.zig is at 600 lines.
  3. served takes the extra lane's outside block off the room when every stream is on pages (zig/src/native/cuda_kv_pool.zig:89-92), with a test for both cases.
  4. The test comments say what their numbers are.

Yes, please rebase #2620 and this onto main. We tried the merge here, and it needs more than the two textual conflicts:

  1. Shape.key (cuda_shapes.zig): main's stage commits gave bit 31 to part == .head, the same bit qwen3_5 cuda: the GDN kernels read the state, its rollback copy and the log through a stream's state table (#2444 P3) #2620 gives table. All 32 bits are in use. Keeping both on bit 31 passes every current test, because the Rollback conformance tests #2138 distinctness test never varies part, but a head piece and a table shape would then replay each other's graph. Please give table a bit of its own. Two ways that work: slots only ever take 1, 2, 4, 8 or 16, so narrowing the slots field to 5 bits (with an assert) frees bits 24-26, or the key can widen to u64. Please also extend the Rollback conformance tests #2138 test to vary part (whole and head) alongside table, so a shared bit fails it.
  2. forward in cuda_engine.zig: main dropped const prompt = !record;, but e.prompt_states = if (prompt and table) ... still reads it, so the merged file doesn't build until that reads !record. In the useMt2 call, take main's !record beside shape.table = table;. The other conflicts keep both sides: the engine's fields and their init (hop, boundary, held beside prompt_states, forward_from), and stateEntries beside launchShape.
  3. Five tests in cuda_pages_init_test.zig crash after the merge. Main's stage commits added first and whole to the geometry, and Geometry.deltas now reads g.first. The test helper engine() (cuda_pages_init_test.zig:87) starts from undefined and doesn't set them, so deltas indexes linear with a garbage offset (index out of bounds in cuda_geometry.zig:139, reached from pieces.plan and States.init). Setting e.g.first = 0 and e.g.whole = 0 in the helper fixes it.
  4. Small, while you're there: the TooMany message in cuda_kv_pool.zig:191 still says "state runs" (and its test looks for that). With pieces it's state pages.

With those three changes and nothing else (we tried table on bit 26, which stays clear while slots are 16 or under), the merged tree builds, the host tests pass (test-cuda-host, test-kv-pool, test-qwen35-stage-host, including the #2138 test extended over part and table), and the Linux cross-compile of check-native-cuda-host and compile-cuda-tests is clean.

For gates on the rebased pair, the full 8 h set isn't needed again, because the rebase changes only these lines and no kernel. Please run the host set and the x86 checks, qwen-pending, qwen-pages and qwen-table, and the conformance kit's unset, 256 and 256 + TF_QWEN_KV_SHARE=16 cells against main on the 27B with DFlash2 and the 35B-A3B with MTP. The speed table stands, since the decode path doesn't move.

  • Synthena (Ash's Agent)

@jschmied

Copy link
Copy Markdown
Contributor Author

@ashhart both PRs are rebased onto main 6c5cef8. #2620 is now at 74140af and this PR at 250994a, still stacked on #2620. The four points are folded into the commits that introduced each line:

  1. Shape.key: slots now has 5 bits (19-23), with an assert that it is 16 or under. table moves to bit 26, and the head piece keeps bit 31. The Rollback conformance tests #2138 distinctness test now varies part (whole, head) alongside table, hidden, paged and mt2. As a planted control, I put table back on bit 31, and that test went red (expected .whole, found .head).
  2. forward: the state-table line reads !record, the useMt2 call takes main's !record beside shape.table = table;, and both sides are kept for hop, boundary and held beside prompt_states and forward_from, and for stateEntries beside launchShape. cuda_engine.zig is at 591 lines: the "dense read cost" key test moved from it into cuda_shapes.zig, beside the other key tests. cuda_native.zig stays at 600.
  3. cuda_pages_init_test.zig: engine() sets e.g.first = 0 and e.g.whole = 0.
  4. TooMany: the message and its test now say "state pages".

Gates, the reduced set you named, on both heads against main 6c5cef8:

  • Host: test-cuda-host, test-kv-pool and test-qwen35-stage-host pass, along with the qwen35 host steps; zig fmt, no changed file over 600 lines, lean 0 new.
  • x86: check-native-cuda-host and compile-cuda-tests, the -Dfixed-cpu CI set and pr_check (0 errors) pass on both heads.
  • GPU, compared with main's outcome lines: qwen-pending 49 lines equal main's (plus 49 _table PASS), qwen-pages 26 equal, kernel-contract qwen35 q35 123 equal (plus 11 _table PASS), and qwen-table gives 25 PASS lines with every control red.
  • Conformance kit: with main's kit (tools/conformance.py, profile qwen35-cuda, 7 gates), 27B + DFlash2 and 35B-A3B + MTP, pages unset, 256, and 256 + TF_QWEN_KV_SHARE=16: 0 reply keys differ between main and the head in any cell: 137 keys a cell on 250994a in all 6 cells, and on 74140af the 35B share cell; qwen3_5 cuda: the GDN kernels read the state, its rollback copy and the log through a stream's state table (#2444 P3) #2620's other 5 cells ran on the kit's previous copy (5 gates, 114 keys) before that copy's worktree went away mid-run, all PASS. drafted == plain, repeat, concurrent == solo, resumed == fresh, refusals and cancel pass in every arm. evict gives the same verdict on main and on the head in every cell: it passes on 27B pages unset and 256. Elsewhere its 10 fill prompts evict nothing (cached 4096 -> 4096; 3/3 equal against cache-off), on main as well, so the gate's fill is too small for those caches rather than anything this PR changes.

No SASS run this time: no kernel changed. The speed table stands.

kv_pool.zig and kv_pool_state_test.zig are still byte-equal with #2898/#2992. This PR drops its copy once that pair lands. cuda_kv_pool.zig differs from #2898's only by point 4's two strings, and #2898 picks up the same change.

@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied for the rebase onto 6c5cef894 and for folding each point into the commit that brought its line. On 250994a85:

  • Shape.key gives slots bits 19-23 with an assert that it's 16 or under, and table bit 26 (zig/src/families/qwen3_5/cuda_shapes.zig:30-31). The Rollback conformance tests #2138 test varies part with table (:93-99), and your planted control on bit 31 turned it red.
  • forward reads !record, the test helper sets g.first and g.whole, and the TooMany message and its test say "state pages".
  • cuda_engine.zig is at 591 lines and cuda_native.zig at 600.

CI is green on this head and on #2620's 74140afe9. The GPU gates match main's lines, and the conformance kit shows 0 reply keys differing in all 6 cells. Nothing more is needed on the code.

Next we'll run the pair on our GB10, main 6c5cef894 against this head: the 27B with and without DFlash2, pages unset and at 256, one and four streams, and the 35B-A3B with MTP. If it's level, we'll land #2620 and this together in one train. Both can stay drafts until then.

  • Synthena (Ash's Agent)

…og through a stream's state table

The GDN kernels assumed one contiguous block a stream: a layer's recurrence at `sg.rec + layer * rec()`, its tail
at `sg.conv + layer * conv_rows()`, the rollback copy's tail past `nl` recurrences, a window row's record at
`(row * nl + layer) * log_bytes`. Seg.rec, .conv, .before and .log now point into a per-stream table of addresses
(cuda_cache.zig States: nl recurrences, nl tails, 2 nl copies, 64 log rows; 2 KiB on the 27B), so the pieces can
later live on separate pages without another kernel change. Seg keeps its layout, so kernels that do not touch the
state compile as before.

The table is filled from today's blocks, so every entry is the address the kernels computed before and the
placement is unchanged. Cache.seg rewrites it on the engine's stream only when the state, before or log block moved
(a pool lane's mapRun, the 27B's adaptive scratch leases); a captured forward reads it from device memory, so no
graph is recaptured. The prompt chunk's delta rule and the pending keep copy take their addresses from the host
copy. Its bytes count in streamBytes and a pool lane's fixed bytes.
kernel-contract-limits on 5-SM fatbins (sm_80, 86, 89, 120, 121) built with nvcc 13.0.88 printed these for the ten
launches that now read a stream's state table; every other launch's record is current. nvcc 13.2's and 13.3's
receipts for them are not measured here and stay as they are.
…compute

Each GDN launch read its Seg, then the table entry, then the state: one more dependent load than before the table.
The engine's tables buffer now holds a state table for each forward segment after its staged planes (cuda_round.zig
states_at), copied from the stream's own when the stream or its blocks change, and the forward kernels find segment
s's table at segs + SEG_STATES + s tables, so the entry load does not wait for the Seg. Keep launches, which take
their Seg by value, read the stream's own table as before.
…a_states.zig

A pure move: cuda_cache.zig keeps States as a name for it, so no caller changes.
…g receipts, not records

The seven rows predate the state table and nvcc 13.3 is not on the machine that changed the kernels, so they
move from nvcc_133_records.zig to measurements/pending_133.zig until 13.3 measures them.
…ve SMs

Measured with nvcc 13.2.86 on five-SM fatbins (kernel-contract-limits); the ten launches' rows in
qwen_lanes_132.zig take its numbers, and the slot scan's selection test follows them.
…te table's readers are _table copies

Main's gdn_pre, conv_commit, gdn_chain, gdn_slots_{1,2,4,8,16}, k_apply, k_apply_pend and k_conv keep main's
code and read Seg's blocks again. Each is built a second time as name_table, reading the stream's state table:
the kernel's source is included twice (qwen35_gdn.cuh, qwen35_slots.cuh, qwen35_keep_state.cuh) with
TF_Q35_TABLE 0 and 1, and #if TF_Q35_TABLE picks the table reads, so the TF_Q35_TABLE 0 pass is main's text
token for token and main's names compile to main's SASS on sm_80, 86, 89, 120 and 121 (a template parameter
did not: nvcc reordered the address math on sm_80-89).

Only a forward with a pool lane among its parts (TF_QWEN_KV_PAGES, Cache.on_table) runs the _table kernels
(Shape.table, its own captured graphs); every other forward builds main's Seg and launches main's kernels, and
its keep, pending rows and prompt delta read the stream's blocks as on main. The _table kernels' contract rows
carry the numbers the table readers had under main's names; main's rows are main's again.
…tate table

A cache holds its state table beside its blocks since the first commit (one more device allocation and its
bytes in streamBytes), and Cache.seg can fail; the nightly host steps test-qwen35-shared-host and
test-qwen35-recurrent's 27B checks now expect both.
…ive SMs

The other ten _table launches carry a 13.2 row in measurements/qwen_lanes_132.zig; apply_pend_table had none, so
kernel-contract-limits found no 13.2 record for it. Measured with nvcc 13.2.86 on this head's fatbins.
…_table on pieces against main's on blocks

qwen-table runs each _table kernel on a state split into one allocation a layer's region and a log row, two segments,
and compares its outputs and both streams' state with main's kernel on the blocks; a planted table entry (layer 1's
tail or recurrence naming layer 0's, or the kept log row naming another row) in segment 1 must change them.
… in cuda_round.zig (cuda_engine.zig 600 lines)
kv_pieces packs a family's state pieces first-fit decreasing on whole pages
(each piece inside one page, 256-byte aligned) and checks a map for overlap.
Lease.state holds those pages beside the token table; takeState takes them
from the promise and finish gives them back, so no consecutive run is needed.
Layout.pieces counts them wherever a run counted (need, plan, holds), and
Layout.outside names the buffers wider than a page a stream keeps in its fixed
part. The load report shows the pieces, their pages and pad, and that fixed part.
Under TF_QWEN_KV_PAGES a lane's recurrences and tails, its verify log rows and the rollback copy are pieces on
whole pages taken from its lease (cuda_state_pieces.zig over core kv_pieces): no consecutive run, so a pool
with no two free pages side by side still admits a lane. The copy lies in the bytes of log rows 1.., as the
Overlay does: a one-row window uses row 0 and the copy, a wider window the rows and no copy. The taps and a
drafter's ring, wider than a page, are a lane's own block outside the pool, counted in its fixed part.

The lane's state table is written once at open from the piece map; a closed lane's table is refused (version
0, and a lane without blocks or pieces has no entries to read), so no forward copies entries that point at the
pages it gave back. Kernels are ashhart#2620's: a forward with a lane on pieces runs the _table kernels.

A page must hold the largest piece (a log row or a layer's recurrence): a smaller TF_QWEN_KV_PAGES is refused
at load with the smallest size that works (27B: 67 rows; 35B-A3B: 103).
@jschmied

Copy link
Copy Markdown
Contributor Author

@ashhart this PR is rebased onto main 328a566, on #2620's new head 6587e14. The new head is 1350872: the core commit is 5451b4a and the Qwen commit is 1350872.

  • Core commit: finish keeps main's skip of retired ids for the token table and gives the state pages back. cuda_kv_pool.zig is flashnext: pool lanes keep their states and draft as pieces on whole pages, no run (#2444 P3, F2) #2898's current file. To keep kv_pool.zig at 600 lines with main's new test line, kv_pool_state_test.zig is referenced from kv_pieces.zig.
  • Qwen commit:
    • cuda_reuse.zig grew on main, so the image-rule test moves unchanged to cuda_reuse_test.zig.
    • Main's new cuda_spares_test.zig fixture starts from an undefined cache. It now sets pieces = null; without that, the x86 run crashed in 8 of its tests.

Gates:

The body names the new head.

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.

2 participants