Skip to content

qwen3_5 cuda: the GDN kernels read the state, its rollback copy and the log through a stream's state table (#2444 P3) - #2620

Draft
jschmied wants to merge 23 commits into
ashhart:mainfrom
jschmied:qwen-state-table
Draft

jschmied wants to merge 23 commits into
ashhart:mainfrom
jschmied:qwen-state-table

Conversation

@jschmied

@jschmied jschmied commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

The first kernel step of #2444 P3 for Qwen 3.5/3.6 on CUDA (Q1 in the P3 plan). Main's GDN kernels assume one
contiguous block a stream and compute their addresses from it: a layer's recurrence at sg.rec + layer * rec(), its
conv tail at sg.conv + layer * conv_rows(), the one-row rollback copy's tail past nl recurrences, and a window
row's record at (row * nl + layer) * log_bytes. This branch adds a per-stream table of those addresses
(cuda_states.zig States: nl recurrences, nl tails, nl + nl copies, 64 log rows; 2 KiB on the 27B, 1.4 KiB on the
35B-A3B) and a second build of each of those kernels, name_table, that takes every address from the table. That is
what lets P3 put the state on separate pool pages (Q3) without touching a kernel again.

  • Pages off runs main's kernels. Main's gdn_pre, conv_commit, gdn_chain, gdn_slots_{1,2,4,8,16},
    k_apply, k_apply_pend and k_conv are main's code, compiled to main's SASS on all five SMs (table below). 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.
  • Shared source, not copies. Each kernel's source is included twice (qwen35_gdn.cuh, qwen35_slots.cuh,
    qwen35_keep_state.cuh), with TF_Q35_TABLE 0 and 1; #if TF_Q35_TABLE picks the table reads (1 to 4 lines a
    kernel) and TF_Q35_K(name) names the kernel. The TF_Q35_TABLE 0 pass is main's text token for token, so the main
    names compile to main's SASS. A template <bool TABLE> body was tried first and is not used: on sm_80, 86 and 89
    nvcc reordered the address math of main's instantiation (same instruction count and registers, different order) in
    gdn_chain, the slot scans, k_apply and k_apply_pend.
  • The table is rewritten on the engine's stream only when the state, the rollback copy or the log block moved (a pool
    lane's mapRun, the dense 27B's adaptive scratch leases). The forward kernels find segment s's table in the
    engine's tables buffer after its staged planes (cuda_round.zig states_at, qwen35.cuh forward_states), copied
    device to device only when the stream at that position or its blocks change, so a captured forward needs no
    recapture and the entry load issues beside the Seg load. Keep launches, which take their Seg by value, read the
    stream's own table.
  • Not in this step: the TF_QWEN_KEEP_LATE pending buffer (Seg.plog) stays a direct pointer (P3 Q5).

Cost: pages off (the default) is main's SASS, see the table. Under TF_QWEN_KV_PAGES the _table kernels run;
the speed table below names the cells that came out slower than main.

Proposed CHANGELOG line (for you to place):

Rebase onto main 6af9f19 (3899d00)

From 6587e14 (main 328a566), through main d6a5ae2, each conflict fitted into the commit that brought the line,
taking main's code where main moved:

  • PDL (qwen3_8 cuda: opt-in PDL on ModelOpt qmmf lanes and qwen35 glue #4706, 0ad3ede / 99296f9 / 5525486): gdn_pre's qwen_pdl::enter() sits in the shared source
    (qwen35_gdn.cuh), so main's kernel and its _table copy both take it; cuda_forward.zig launches gdn_pre(_table)
    and gdn_post through runPdl as main does, and tools/pdl_early_loads.py lists conv_commit_table and
    gdn_chain_table as plain launches beside main's two.
  • Engine fields: main turned Engine's doc comments into trailing ones to stay at 600 lines; prompt_states and
    forward_from follow that style. finish allocates the tables beside main's new tfq3 errdefer.
  • Main's pins (dense_serving_test.zig): qwen35.cu now pins this PR's bytes with main's all-rows BF16 attention
    argument (4f28cca); qwen35.cuh, qwen35_keep.cu and the launch sequence pin the same bytes as before.
  • No change to this PR's code beyond those lines; cuda_engine.zig stays at 600 lines or under.

Main has moved again: 33e7164 conflicts with this head in dense_serving_test.zig, qwen35.cu,
cuda_engine.zig, cuda_engine_tail.zig, cuda_forward.zig and tests/cuda/contract_after.zig. I haven't rebased onto it: the pair lands in one train,
so say when you want it and I'll fold it in.

Since 3899d00 (head bcc3bb7)

  • Slot-scan fix, folded into eed1422 (qwen35_slots.cuh): the _table scan loads the rollback copy's table
    entry beside the recurrence's, at the top of the kernel, instead of after the state's loads and the pending rows.
    Only the _table slot scans change; slots_1_table goes from 80 to 94 registers on sm_120/121.
  • The undo's guard (89eb3bb), following a review note: the rollback offset is formed only where the undo runs,
    and on the table path the guard also tests the entry. A plain part of a mixed forward can have no rollback copy,
    which the table holds as zero, and adding an offset to that null pointer was undefined even with the store skipped.
  • Limit rows (005b375): the _table rows in cuda_lane_pending.zig (13.0) and qwen_lanes_132.zig (13.2) are
    the new 5-SM fatbins' registers; nothing else in them changed.
  • forwardStates (bcc3bb7): compares the optional table addresses with std.meta.eql, the same decision.

Since bcc3bb7 (head 2f6d72d), new commits only

  • Row log entry with the row's inputs (ecdb2f1, qwen35_slots.cuh): the _table slot scan loads the next
    row's log entry together with that row's inputs, not right before each recorded store.
  • qwen-table covers the slot scans (6aa4689, 8ad9432): tf_q35_gdn_slots_{1,2}_table on pieces against
    main's slots_{1,2} on blocks, every recorded window whose tree fits the slots, outputs and both streams' state,
    log rows included. Its planted control moves the window's last log row entry. Separately, a plant of ecdb2f1's
    load (every row's log entry taken from row 0) turns it red at tf_q35_gdn_slots_1_table, a 4-row chain.
  • Main's register count for the one- and two-slot _table scans (e55ddd9, 422dbdb): __launch_bounds__(128, 7)
    on those two _table kernels only (72 registers instead of 92, 7 blocks an SM instead of 5, no spills); the 4-, 8-
    and 16-slot _table scans keep their SASS, and main's kernels keep __launch_bounds__(128). In a micro bench of
    the 27B's no-drafter c=4 shape (4 one-row recorded segments, 48 layers, 64 MiB written between launches so the
    table entries are cold, as they are in the model) slots_1_table was +4.7 to +5.7 us a launch over main's at 92
    registers and -0.9 to +1.3 us at 72. With L2 warm the two were equal either way.
  • Limit rows (04a18b5): the five _table slot-scan rows in cuda_lane_pending.zig (13.0) and
    qwen_lanes_132.zig (13.2), from the 5-SM builds of this head. Only registers changed.
  • Pin (2f6d72d): dense_serving_test.zig pins qwen35.cuh's new bytes (ecdb2f1's log_row_at). It's a
    host test only, so no kernel or binary changes; the GPU gates and the speed cell below ran on 04a18b5.

SASS against main (pages off runs main's kernels)

tools/zig/cuda_sass_compare.py 6af9f191e bcc3bb761 (nvcc 13.0.88, every emitted function, sm_80/86/89/120/121).
On 04a18b5 (2f6d72d changes no kernel): qwen35_kv8 60 functions equal under the same tool; qwen35 and qwen35_keep compiled from both trees with
the build's flags and compared function by function: main's 33 and 4 functions equal on all five SMs, added only the
same 8 and 3 _table kernels.
The change reaches three images: qwen35, qwen35_keep and qwen35_kv8.

image sm_80 sm_86 sm_89 sm_120 sm_121 added
qwen35 33 / 33 equal 33 / 33 equal 33 / 33 equal 33 / 33 equal 33 / 33 equal 8 _table kernels a SM (40)
qwen35_keep 4 / 4 equal 4 / 4 equal 4 / 4 equal 4 / 4 equal 4 / 4 equal 3 _table kernels a SM (15)
qwen35_kv8 equal equal equal equal equal none (60 functions, SUMMARY PASS)
  • Every main function is equal (SASS and encoding sha256); nothing changed, removed or unstable. The tool counts an
    added function as a difference, so qwen35 and qwen35_keep were compared in runs of their own and the verdict is
    "main's functions equal, only the expected _table kernels added"; qwen35_kv8 runs to the tool's own SUMMARY PASS.
  • Receipts: compared-functions.tsv and the per-run logs, kept with the run.

Checks on this head

On 04a18b5 (main 6af9f19; 2f6d72d adds only the host pin), clean builds (tensorfold-native md5 2c2c965c, tf-cuda-test d9a1df43):

  • GPU: qwen-pending 49 lines equal main's plus 49 _table PASS; qwen-pages 26 equal; kernel-contract qwen35 q35
    140 equal plus 11 _table limit lines, all PASS; qwen-table 30 PASS lines, every planted control red.
  • kernel-contract-limits (nvcc 13.0.88 and 13.2.86, 5-SM fatbins): no _table row stale. The 13.0 stale lines
    are main's own set plus tf_moe_router_tiles on sm_80 (199 registers against 182 recorded), from moe.cu, which
    this branch doesn't touch.
  • x86-64, full set (--final): on 04a18b5 everything passed except dense_serving_test's qwen35.cuh pin (stale since ecdb2f1); on 2f6d72d the rerun of that step is 2714/2714 steps, 29195/29375 tests (180 skipped). pr_check 0 errors, zig fmt, git diff --check, lean 123 vs 123 (0 new), check-native-cuda-host compile-cuda-tests 21/21.

Unchanged from bcc3bb7, on bcc3bb7 (main 6af9f19), nvcc 13.0.88 (13.2.86 for the 13.2 records), one GB10 (sm_121):

  • aarch64 host: every qwen/recurrent/kv/gradient -host step with test-cuda-host: 814/814 steps,
    11656/11702 tests (46 skipped); install compile-cuda-tests check-native-cuda-host 254/254.
  • x86-64, full set (--final, umask 022): pr_check 0 errors (the CHANGELOG warning; lean 123 against 123, 0 new),
    zig fmt --check, git diff --check; -Dfixed-cpu test-host test test-golden test-kernel-contract test-server-security test-pull-security fuzz-cortheon check-release-paths 2714/2714 steps, 28545/28723 tests
    (178 skipped); check-native-cuda-host compile-cuda-tests 21/21.
  • Planted control on Shape.key: table on join's bit 32 turns the Rollback conformance tests #2138 key test red by name.
  • GPU, main's and the head's tf-cuda-test outcome lines: qwen-pending 49 lines equal main's, plus the 49
    _table cases, all PASS; qwen-pages 26 equal; kernel-contract qwen35 q35 140 equal plus 11 _table limit lines,
    all PASS; qwen-table 25 PASS lines, every planted control red.
  • kernel-contract-limits on 5-SM fatbins (nvcc 13.0.88 and 13.2.86): no _table row stale or failing. The other
    stale rows (193 lines under 13.0, 1815 under 13.2) are exactly the lines main's own tf-cuda-test gives on the same
    fatbins: pre-existing on main, unchanged.

Speed on 04a18b5 (= 2f6d72d's binaries): the 27B without a drafter, 256, c=4

Main 6af9f19 (md5 988048f6) against #2620 04a18b5 (md5 2c2c965c), abba5: 3 A-vs-A rounds for the floor, then
5 rounds of A,B,B,A. Tokens equal in every run.

VERDICT c4_tok_s: WITHIN NOISE (median -0.104%, floor 0.093%, B worse 4 of 5), with c=1 at 0.0 %.

  • Main 43.50 / 43.27 / 43.50 / 43.51 / 43.51 tok/s, the head 43.46 / 43.48 / 43.43 / 43.47 / 43.44. The one round
    the head won is main's dip in round 2. In the other four the head is 0.08 to 0.16 % behind.
  • So this counts as within noise only by abba5's every-round rule. The median is where it was on bcc3bb7 (-0.102 %),
    and the micro bench gain from the register bound did not show up served. I report it as within noise and don't
    claim the cell is fixed.

Speed: main 6af9f19 against the pair (#2620 bcc3bb7 + #2939 6517021)

tensorfold-native of each, zig build native -Dsm=121; one GB10, GPU clock capped, page cache dropped before each
server, --parallel 8 --context 16384 --prompt-cache-gib 0. c=1 is the median of 3 x 256 tokens (tok/s from the done
lines), c=4 the median of 3 rounds of 4 x 256 with a distinct prompt per stream. Gap: + = the pair faster. The greedy
token shas are equal to main's in every run of every cell.

model pages c=1 main / pair c=1 gap, floor, rounds worse c=4 main / pair c=4 gap, floor, rounds worse
Qwen3.8-27B NVFP4 + DFlash2 unset 29.40 / 29.25 -0.341 %, 0.514 %, 4 of 5: within noise 94.75 / 94.54 -0.052 %, 1.028 %, 3 of 5: within noise
Qwen3.8-27B NVFP4 + DFlash2 256 29.40 / 29.40 +0.000 %, 0.000 %, 1 of 3: within noise 94.78 / 95.02 +0.238 %, 0.125 %, 0 of 3: faster
Qwen3.8-27B NVFP4, no drafter unset 11.70 / 11.70 +0.000 %, 0.427 %, 0 of 5: within noise 43.00 / 43.02 +0.002 %, 0.087 %, 2 of 5: within noise
Qwen3.8-27B NVFP4, no drafter 256 11.60 / 11.60 +0.000 %, 0.429 %, 0 of 3: within noise 42.85 / 42.78 -0.204 %, 0.089 %, 2 of 3: slower
Qwen3.6-35B-A3B + MTP unset 113.70 / 113.80 +0.176 %, 0.788 %, 0 of 5: within noise 243.05 / 243.62 +0.188 %, 1.380 %, 2 of 5: within noise
Qwen3.6-35B-A3B + MTP 256 113.70 / 112.80 -0.880 %, 0.175 %, 2 of 3: beyond the floor 243.13 / 239.64 -1.482 %, 1.140 %, 2 of 3: beyond the floor
  • The 27B without a drafter at 256, c=4 is still slower: -0.204 % against a 0.089 % floor, 2 of 3 rounds worse.
    Don't land yet. On the previous heads it was -0.113 % (floor 0.077 %, 5 of 5 worse). With the slot-scan fix
    alone on those heads it was -0.07 % against a 0.137 % floor (5 of 5 worse), so the fix did not clear it.
  • The 35B at 256 is beyond its floor at both c=1 and c=4, 2 of 3 rounds worse each. On the previous heads, with
    5 rounds, the same cells were 0.0 % (floor 1.20 %) and +0.554 % (faster). abba5 calls them within noise because
    not every round is worse; I count them with the slower cell until a full run says otherwise.
  • Next I'd try: the full 3 + 5 run of the three 256 cells, then nsys on the 27B c=4 cell (and the 35B if it holds)
    to see whether slots_1_table at 94 registers still costs the +2 us a layer.
  • The 27B cells serve the checkpoint through its own no-MTP index (model.safetensors.index.local-no-mtp-20261021.json):
    the local copy's model_mtp.safetensors link is missing, and none of these cells uses the MTP head (copies only, or
    DFlash2). Both arms read the same view.

Receipt

  • Conformance kit (main's tools/conformance.py, profile qwen35-cuda), main 6af9f19 and qwen3_5 cuda: the GDN kernels read the state, its rollback copy and the log through a stream's state table (#2444 P3) #2620 bcc3bb7, both
    run in this pass: the 27B + DFlash2, the 27B without a drafter and the 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 each gate's verdict is the same as main's, and 0 reply keys differ (139 a cell with a drafter: greedy,
      seeded sampling, refusals, evict and overlap; 116 for the 27B without one);
    • on the head, 256 and 256 + share against unset: 0 keys differ in all 6 pairs (greedy and seeded, resume,
      concurrency 1/2/4/8, drafting);
    • the gates that fail here fail on main the same way: evict (its fill evicts nothing in those caches) and, for the
      27B without a drafter, the older copies profile's missing blocks.
  • Environment: TensorFold bcc3bb7 (qwen3_5 cuda: a pool lane's state as pieces on any whole pages of its lease, no run (#2444 P3, Q3) #2939 6517021) on main 6af9f19; Zig 0.17.0; nvcc 13.0.88 (13.2.86 for the
    13.2 rows), sm_121; one NVIDIA GB10 (128 GB unified); checkpoints Qwen3.8-27B NVFP4 (Unsloth FP8 head) with the
    Qwen3.8-27B DFlash2 drafter, and Qwen3.6-35B-A3B MLX 4-bit with its MTP head.
  • Paths and platforms: CUDA sm_121 run; kernel limits on sm_80, 86, 89, 120 and 121; x86-64 host set. Metal, HIP
    and XPU are not reached.

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.

jschmied added a commit to jschmied/TensorFold that referenced this pull request Nov 8, 2026
jschmied added a commit to jschmied/TensorFold that referenced this pull request Nov 8, 2026
@ashhart

ashhart commented Nov 9, 2026

Copy link
Copy Markdown
Owner

@jschmied, thanks. This is a clean first step for P3: every entry is the address the kernels computed before, so the arithmetic and its order are unchanged, and the host tests pin that. A few things before it lands:

  1. Records past 13.0. Only the nvcc 13.0 rows changed (cuda_lane_pending.zig). The ten changed launches' 13.2 rows in measurements/qwen_lanes_132.zig are untouched, though the description says 13.2 was updated; please post the 13.2 limits output, and update those rows or show they're unchanged. Their 13.3 rows in nvcc_133_records.zig:125-131 still carry the old numbers; please measure them, or move them to measurements/pending_133.zig so they're pending rather than stale.
  2. File size. cuda_cache.zig goes from 417 to 542 lines; could States and its two tests move to their own file?
  3. Speed. 4-stream decode is 0.2-0.6% slower in every round of your table. Please push the follow-up that loads the entry with the Seg, and post the same main-against-branch table for it: both models, pages off and at 256, one and four streams.

@ashhart

ashhart commented Nov 9, 2026

Copy link
Copy Markdown
Owner

@jschmied, thanks for the follow-up and the full table. Finding each segment's table without waiting for the Seg is a neat fix, and the host tests check every address against the old formulas. Two earlier asks are still open, and there's one speed question:

  1. Records past 13.0. The 13.3 rows in nvcc_133_records.zig:125-131 still carry the old numbers. Please move them to measurements/pending_133.zig, which needs no 13.3 install. For the 13.2 rows in measurements/qwen_lanes_132.zig, either update them or post their limits output. The description's Environment line still says "13.0 and 13.2, records updated", which doesn't match.
  2. File size. States and its two tests would be better in their own file, since cuda_cache.zig grows from 417 to 551 lines.
  3. Speed. The 27B with DFlash2 is still 0.2 to 0.4% slower at four streams in every round. The plain 27B's second round at 256 pages shows -0.68% decode, where the summary says -0.08 to -0.16%. Could you correct the summary and say whether you see anything left to recover? We hold slower cells back, so a step that is level at four streams is what lands.

@jschmied

Copy link
Copy Markdown
Contributor Author

@ashhart thanks. The head is 3f044af90: three plain commits on 6ea5ca844, no rebase. The kernels are unchanged from 6ea5ca844.

  • Records past 13.0
    • nvcc 13.3 (3a526ac25): the seven rows from nvcc_133_records.zig:125-131 (conv_commit, gdn_pre, slots_1/2/4/8/16) are now in measurements/pending_133.zig, unchanged. tf_q35k_apply_pend's 13.3 row was already there and also predates the table.
    • nvcc 13.2 (3f044af90): I measured the ten rows in measurements/qwen_lanes_132.zig with kernel-contract-limits on five-SM fatbins (zig build fatbins kernel-contract-fatbins -Dnvcc=/usr/local/cuda-13.2/bin/nvcc -Dsm=80,86,89,120,121), the same way as the 13.0 rows. Nine change and tf_q35k_apply is unchanged; the slot scan's selection test in pending_132_test.zig follows them. Their limits output is below.
    • One caveat: this machine's nvcc is 13.2.86. Against the repo's 13.2 set it marks 319 launches stale, including launches this PR doesn't touch (tf_q35_tap, tf_q35_rope, tf_q35k_compact: 32/34/34/23/23 here, 18/17/17/34/34 recorded). So the repo's 13.2 set came from another 13.2 build. Every row I changed is current against 13.2.86. If you'd rather keep the 13.2 numbers from the compiler the set came from, say so and I'll move the ten rows to pending instead.
    • The description's Environment line now says nvcc 13.0.88 and 13.2.86 measured, 13.3 pending.
  • File size (5dae99674, pure move): States and its two tests are in families/qwen3_5/cuda_states.zig (116 lines). cuda_cache.zig keeps States as a name for it, so no caller changes, and goes from 551 to 446 lines (417 on main). cuda.zig's test block imports the new file, so both tests still run.
  • Speed
    • The summary was wrong for the 27B without a drafter. With the -0.68 % round it reads -0.08 to -0.68 %; that round's arms also show prompt TTFT +0.76 %. The description now says so.
    • What is left to recover: I looked at every changed launch in SASS on sm_121 (nvcc 13.0, main e5889236b against this head):
launch registers main / head the table load depends on
gdn_pre 38 / 38 conv tail entry, one LDG.E.64.CONSTANT rowseg only (as sg.conv did)
conv_commit 24 / 30 tail and copy entries blockIdx only
gdn_chain 56 / 56 recurrence entry; with a recording, one per row blockIdx only
slots_1 / 2 / 4 / 8 / 16 72 / 78, 72 / 78, 84 / 88, 90 / 90, 94 / 92 recurrence, copy, and one log entry per recorded row blockIdx (row: the step's row)
k_apply, k_apply_pend, k_conv 40 / 40, 39 / 39, 24 / 18 the stream's own table: one extra hop (Seg by value) the kernel argument
  • The table address is already uniform. In the slot scan, segs + SEG_STATES + s * len is computed with UIMAD.WIDE from SR_CTAID.Z and the argument, and the entry's LDG.E.64.CONSTANT is the kernel's first global load, issued before the Seg fields' loads. So there is nothing left to hoist there.
  • The constant bank would mean putting each stream's entries in the launch arguments. Each block move would then change a captured graph's arguments, which is what Q3's moving pages must avoid, and the 64 log rows don't fit a launch argument.
  • The data arithmetic is identical (FFMA/FMUL/FADD/MUFU/F2FP per function); only HFMA2 immediate moves and integer index conversions differ.
  • Three things tried, none of them better:
    1. Launch bounds bringing the slot scans back to main's registers (posted before).
    2. Loading each recorded row's log entry one row early, with its inputs (43308eaca). The SASS confirms the load left the store path (slots_1: from 0x3200, in front of the stores, to 0x2e20 beside the next row's loads). It is not in the branch, because it recovered nothing; table below.
    3. Before that, the first commit's Seg hop, which the forward tables removed (the 35B-A3B went from -0.4…-0.6 % to level).
  • So I can't make the 27B with DFlash2 level at four streams. I'd put the remaining 0.1 to 0.3 % on the two places that still add a load or registers: the slot scans' registers on sm_121 (one block fewer an SM for slots_1/2) and the keep launches' extra hop. The launch bound put the registers back without moving the number; I haven't isolated the keep hop.
  • Re-measured: c=4, main e5889236b (tensorfold-native md5 0fce9528) against the prefetch variant 43308eaca (md5 09b186f9; the same tables and loads as this head plus the early log load). One server an arm, --parallel 8 --context 16384 --prompt-cache-gib 0, the page cache dropped before each start, A-B-B-A twice, three 4 x 256-token rounds an arm, on the dispatcher's timing lane:
27B NVFP4, 4 streams TF_QWEN_KV_PAGES round 1 main / head round 2 main / head
+ DFlash2 off 70.44 / 70.28 (-0.22 %) 70.42 / 70.21 (-0.30 %)
+ DFlash2 256 70.42 / 69.89 (-0.75 %) 69.99 / 69.91 (-0.11 %)
no drafter off 34.93 / 34.90 (-0.08 %) 34.93 / 34.89 (-0.13 %)
no drafter 256 34.84 / 34.78 (-0.17 %) 34.84 / 34.81 (-0.07 %)
  • Greedy reply sha256s alone and four at once are equal to main in all 32 arms. The earlier rounds on 6ea5ca844: DFlash2 -0.36 / -0.24 / -0.33 / -0.17 %.
  • So this step is 0.1 to 0.3 % slower at four streams with DFlash2, and up to 0.17 % without a drafter. If that holds it back, I'd suggest keeping it open and landing it together with Q3, which needs the table anyway. Or I can close it until then, if you prefer.

Checks on 3f044af90 (nvcc 13.0.88, sm_121):

  • zig build -Dfixed-cpu test test-golden (7588/7654, 66 skipped), test-cuda-host (2650/2658, 8 skipped), test-kernel-contract test-server-security test-pull-security fuzz-cortheon.
  • x86: check-native-cuda-host compile-cuda-tests, the host set and check-release-paths on an x86-64 host.
  • cuda_common_check.py, pr_check (0 errors), zig fmt --check, git diff --check, lean 398 against 398 on the base (0 new).
  • GPU: qwen-pending 49 pass, qwen-pages 26 pass, and kernel-contract qwen35 q35 (13 kernels pass gates 1-4, 6 controls red as planted, 0 limit problems). Outcome lines equal main's.
  • kernel-contract-limits 13.0: 0 stale, 0 failing. 13.2 for the ten launches:
PASS limits sm_80 qwen35 tf_q35_gdn_slots_1: 72 registers, 4096 static shared bytes, bound 128 (record current)
PASS limits sm_86 qwen35 tf_q35_gdn_slots_1: 78 registers, 4096 static shared bytes, bound 128 (record current)
PASS limits sm_89 qwen35 tf_q35_gdn_slots_1: 78 registers, 4096 static shared bytes, bound 128 (record current)
PASS limits sm_120 qwen35 tf_q35_gdn_slots_1: 88 registers, 4096 static shared bytes, bound 128 (record current)
PASS limits sm_121 qwen35 tf_q35_gdn_slots_1: 88 registers, 4096 static shared bytes, bound 128 (record current)
PASS limits sm_80 qwen35 tf_q35_gdn_slots_2: 72 registers, 8192 static shared bytes, bound 128 (record current)
PASS limits sm_86 qwen35 tf_q35_gdn_slots_2: 78 registers, 8192 static shared bytes, bound 128 (record current)
PASS limits sm_89 qwen35 tf_q35_gdn_slots_2: 78 registers, 8192 static shared bytes, bound 128 (record current)
PASS limits sm_120 qwen35 tf_q35_gdn_slots_2: 88 registers, 8192 static shared bytes, bound 128 (record current)
PASS limits sm_121 qwen35 tf_q35_gdn_slots_2: 88 registers, 8192 static shared bytes, bound 128 (record current)
PASS limits sm_80 qwen35 tf_q35_gdn_slots_4: 72 registers, 16384 static shared bytes, bound 128 (record current)
PASS limits sm_86 qwen35 tf_q35_gdn_slots_4: 80 registers, 16384 static shared bytes, bound 128 (record current)
PASS limits sm_89 qwen35 tf_q35_gdn_slots_4: 80 registers, 16384 static shared bytes, bound 128 (record current)
PASS limits sm_120 qwen35 tf_q35_gdn_slots_4: 90 registers, 16384 static shared bytes, bound 128 (record current)
PASS limits sm_121 qwen35 tf_q35_gdn_slots_4: 90 registers, 16384 static shared bytes, bound 128 (record current)
PASS limits sm_80 qwen35 tf_q35_gdn_slots_8: 64 registers, 16384 static shared bytes, bound 128 (record current)
PASS limits sm_86 qwen35 tf_q35_gdn_slots_8: 68 registers, 16384 static shared bytes, bound 128 (record current)
PASS limits sm_89 qwen35 tf_q35_gdn_slots_8: 68 registers, 16384 static shared bytes, bound 128 (record current)
PASS limits sm_120 qwen35 tf_q35_gdn_slots_8: 88 registers, 16384 static shared bytes, bound 128 (record current)
PASS limits sm_121 qwen35 tf_q35_gdn_slots_8: 88 registers, 16384 static shared bytes, bound 128 (record current)
PASS limits sm_80 qwen35 tf_q35_gdn_slots_16: 107 registers, 32768 static shared bytes, bound 128 (record current)
PASS limits sm_86 qwen35 tf_q35_gdn_slots_16: 109 registers, 32768 static shared bytes, bound 128 (record current)
PASS limits sm_89 qwen35 tf_q35_gdn_slots_16: 109 registers, 32768 static shared bytes, bound 128 (record current)
PASS limits sm_120 qwen35 tf_q35_gdn_slots_16: 92 registers, 32768 static shared bytes, bound 128 (record current)
PASS limits sm_121 qwen35 tf_q35_gdn_slots_16: 92 registers, 32768 static shared bytes, bound 128 (record current)
PASS limits sm_80 qwen35 tf_q35_gdn_chain: 64 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_86 qwen35 tf_q35_gdn_chain: 64 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_89 qwen35 tf_q35_gdn_chain: 64 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_120 qwen35 tf_q35_gdn_chain: 56 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_121 qwen35 tf_q35_gdn_chain: 56 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_80 qwen35_keep tf_q35k_apply: 32 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_86 qwen35_keep tf_q35k_apply: 40 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_89 qwen35_keep tf_q35k_apply: 40 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_120 qwen35_keep tf_q35k_apply: 40 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_121 qwen35_keep tf_q35k_apply: 40 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_80 qwen35 tf_q35_gdn_pre: 40 registers, 0 static shared bytes, bound 32 (record current)
PASS limits sm_86 qwen35 tf_q35_gdn_pre: 42 registers, 0 static shared bytes, bound 32 (record current)
PASS limits sm_89 qwen35 tf_q35_gdn_pre: 42 registers, 0 static shared bytes, bound 32 (record current)
PASS limits sm_120 qwen35 tf_q35_gdn_pre: 38 registers, 0 static shared bytes, bound 32 (record current)
PASS limits sm_121 qwen35 tf_q35_gdn_pre: 38 registers, 0 static shared bytes, bound 32 (record current)
PASS limits sm_80 qwen35 tf_q35_conv_commit: 28 registers, 0 static shared bytes, bound 0 (record current)
PASS limits sm_86 qwen35 tf_q35_conv_commit: 30 registers, 0 static shared bytes, bound 0 (record current)
PASS limits sm_89 qwen35 tf_q35_conv_commit: 30 registers, 0 static shared bytes, bound 0 (record current)
PASS limits sm_120 qwen35 tf_q35_conv_commit: 30 registers, 0 static shared bytes, bound 0 (record current)
PASS limits sm_121 qwen35 tf_q35_conv_commit: 30 registers, 0 static shared bytes, bound 0 (record current)
PASS limits sm_80 qwen35_keep tf_q35k_conv: 20 registers, 0 static shared bytes, bound 0 (record current)
PASS limits sm_86 qwen35_keep tf_q35k_conv: 20 registers, 0 static shared bytes, bound 0 (record current)
PASS limits sm_89 qwen35_keep tf_q35k_conv: 20 registers, 0 static shared bytes, bound 0 (record current)
PASS limits sm_120 qwen35_keep tf_q35k_conv: 18 registers, 0 static shared bytes, bound 0 (record current)
PASS limits sm_121 qwen35_keep tf_q35k_conv: 18 registers, 0 static shared bytes, bound 0 (record current)

@ashhart

ashhart commented Nov 10, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied, this is a careful answer.

  1. Records. The 13.3 rows are pending now, unchanged (measurements/pending_133.zig), and the 13.2 rows are measured
    on this source with their limits output. Please keep your 13.2.86 numbers; that's what we asked for. The wider drift
    you found in the 13.2 set (319 launches stale on 13.2.86) is ours to sort out, not this PR's.
  2. File size. Done, and a clean move: cuda_states.zig is 116 lines and cuda_cache.zig is back to 446.
  3. Speed. The SASS reading is convincing. The table entry is already the slot scan's first global load, the data
    arithmetic is unchanged, and your three tries rule out the easy fixes. That still leaves 0.1 to 0.3% at four streams
    with DFlash2, and we hold back a step that makes a served cell slower. So let's take your first suggestion. Could you
    convert this to a draft and build Q3 on it? Then post one main-against-pair table: the 27B with and without DFlash2,
    pages off and at 256, one and four streams, and the 35B-A3B with MTP. Please don't close it.

One small thing for the next push: the comment at qwen3_5/cuda_pages_init_test.zig:133 runs to 127 columns; it fits on
the line above.


@jschmied
jschmied marked this pull request as draft November 10, 2026 09:49
@jschmied

Copy link
Copy Markdown
Contributor Author

@ashhart, converted to draft; Q3 is being built on it. Two questions before the table, each with what I'll do unless you say otherwise:

  1. Flag. Q3 replaces the consecutive run inside the existing TF_QWEN_KV_PAGES path (still opt-in; with pages unset a server runs main's code), rather than adding a TF_QWEN_STATE_PAGES flag under it. A new flag would add a third arm to every 256 row of the table.
  2. "main" in the table. Current main, since the pair lands together. That means rebasing qwen3_5 cuda: the GDN kernels read the state, its rollback copy and the log through a stream's state table (#2444 P3) #2620 onto main once before the table; the alternative 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 base e5889236b, which every earlier table used.

Streams without pages will keep main's kernels exactly: the table-reading kernels become a second instantiation used only by paged streams, so the pages-off cells run main's SASS and should be level by construction.

@ashhart

ashhart commented Nov 10, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. On your two questions:

  1. Building Q3 inside TF_QWEN_KV_PAGES is fine: it stays opt-in and needs no new setting.
  2. Please use current main. This head no longer applies cleanly there: the last commit's hunk in measurements/qwen_lanes_132.zig meets main's changed qwen35_draft rows. Your nine rows match main's old values, so the rebase should be simple.

For the pages-off cells, please include the SASS (or fatbin) comparison against main that shows they run main's kernels, alongside the table. And on your next push, the comment at qwen3_5/cuda_pages_init_test.zig:133 (127 columns) can go on the line above.

  • Synthena (Ash's Agent)

@jschmied

Copy link
Copy Markdown
Contributor Author

@ashhart thanks.

  1. Rebase. Rebased onto current main (cc0bb1f); the measurements/qwen_lanes_132.zig hunk now sits on main's qwen35_draft rows and only adds the _table rows. Head ce657e4.
  2. Pages off runs main's kernels. Changed rather than only documented: main's ten state kernels (gdn_pre, conv_commit, gdn_chain, gdn_slots_{1,2,4,8,16}, k_apply, k_apply_pend, k_conv) are main's code again, and the table readers are _table builds of the same source (each kernel's source included twice, #if TF_Q35_TABLE picks the 1 to 4 table lines), launched only by a forward holding a TF_QWEN_KV_PAGES lane. cuda_sass_compare.py cc0bb1f67 ce657e4db on sm_80/86/89/120/121: every main function equal (qwen35 32/32, qwen35_keep 4/4, qwen35_kv8 11/11 with the tool's SUMMARY PASS), only the 11 _table kernels added, and those equal the earlier head's table readers (b6ff841) on all five SMs. The table is in the PR body. A template parameter was tried first and dropped: nvcc reordered main's address math on sm_80-89.
  3. cuda_pages_init_test.zig:133. The comment is on its own line now (b6ff841).

Gates on ce657e4: test/golden, cuda-host, ci set, x86 cross, pr_check (0 errors), fmt, lean 0 new; tf-cuda-test qwen-pending, qwen-pages and kernel-contract equal main plus all _table cases PASS; limits 13.0 0 stale, 13.2 _table rows current; Qwen conformance 18/18 arms, reply keys equal main. Three nightly host tests that counted the cache's allocations without the state table are fixed in 75b440f.

@ashhart

ashhart commented Nov 11, 2026

Copy link
Copy Markdown
Owner

@jschmied, thanks; this answers everything we asked:

  1. Rebase: the contract changes only add rows now: 11 _table rows in zig/src/core/contract/measurements/qwen_lanes_132.zig, 11 in cuda_lane_pending.zig:31-41 and 8 pending in measurements/pending_133.zig:49-58, with main's rows, qwen35_draft included, untouched.
  2. Pages off: with TF_Q35_TABLE 0, qwen35_gdn.cuh, qwen35_keep_state.cuh and qwen35_slots.cuh come out as main's text for all ten kernels, the include guard and the #undef aside, and the host picks _table only through on_table (zig/src/families/qwen3_5/cuda_cache.zig:128, set only in mapRun at :219). Sharing the source between the two builds was a good call.
  3. The comment is on its own line at cuda_pages_init_test.zig:133.

Since TF_QWEN_KV_PAGES already reaches users, the opt-in path needs two more things:

  1. Page-on against page-off: from your conformance runs on ce657e4db, could you compare the reply keys of the 256 and 256 + TF_QWEN_KV_SHARE=16 cells against the unset cells, greedy and seeded, with resume and concurrency, and say whether seeded replies are among the 114 keys?
  2. Controls for four _table kernels: contract_qwen_pending.zig:284-359 covers the slot scans and apply_pend_table with planted controls, but we couldn't find device checks with controls for gdn_pre_table, conv_commit_table, gdn_chain_table or tf_q35k_conv_table. If they're there, a file:line would settle it; if not, a control for each would help.

Otherwise the plan stands: keep this as a draft, build Q3 on it, and post the main-against-pair table with the SASS comparison for the pages-off cells beside it.

  • Synthena (Ash's Agent)

@jschmied

Copy link
Copy Markdown
Contributor Author

@ashhart thanks.

  1. Pages on against pages off. From the ce657e4 runs, each of the 256 and 256 + TF_QWEN_KV_SHARE=16 cells against the unset cell, key by key: 0 of 114 differ in all 12 pairs (27B + DFlash2, 27B without a drafter, 35B-A3B + MTP; main's binary and the head's). Seeded replies are among the 114: 55 greedy, 55 seeded sampling (seed 7), 4 refusals, across solo, drafted, repeat, concurrent at 1/2/4/8, mixed, and fresh against resumed.
  2. Controls for the four _table kernels. There were none, so I added tf-cuda-test qwen-table (zig/tests/cuda/contract_qwen_table.zig, head c4ba435, test only). gdn_pre_table, conv_commit_table, gdn_chain_table and tf_q35k_conv_table run on a state split into one allocation per layer region and per log row (two segments, layer 1). Their outputs and both streams' state must equal main's kernels' bytes on the blocks, for a decode row, a 4-row chain and an 8-row drafted tree, recorded and not, 3 repeats, with guards. Each case plants a fault in segment 1's table and must go red: layer 1's tail entry naming layer 0's (gdn_pre, conv_commit), its recurrence entry (gdn_chain), and the kept log row naming another row (tf_q35k_conv). The plant is at :126, the check at :288. Result on sm_121: 24 cases PASS, every control red. The one exception is a recorded window of 2+ rows, where conv_commit returns early; that case is checked as unchanged. The x86 compile, fmt, pr_check and lean checks are green (0 new). It also runs in a full kernel-contract (contract_after.zig).

Still a draft. The body has both.

@ashhart

ashhart commented Nov 11, 2026

Copy link
Copy Markdown
Owner

@jschmied thanks, both are answered. Zero of 114 keys differing in all 12 page-on/page-off pairs, with the seeded replies in the set and drafting, resume and concurrency covered, is the comparison we were after. contract_qwen_table.zig gives each of the four _table kernels a planted fault in segment 1's table (:125-130) that must turn the output red (:284-292), with the recorded-window exception for conv_commit explained beside it. So every _table kernel now has a control. Nothing more is needed on P3 from our side; as planned, it stays a draft for Q3, and the main-against-pair table with the SASS comparison goes beside Q3 when it's up.

  • Synthena (Ash's Agent)

@jschmied

Copy link
Copy Markdown
Contributor Author

@ashhart Q3 is up as #2939, stacked on this PR (c4ba435). Its body has the main-against-pair table, with the SASS comparison for the pages-off cells beside it, as planned. This PR stays a draft.

jschmied added a commit to jschmied/TensorFold that referenced this pull request Nov 12, 2026
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 f0d8c8d, because #2939 asked for a rebase and is stacked on it. The new head is 0a49c0a; the commits and messages are unchanged.

Gates rerun on 0a49c0a:

  • Host and x86: test/golden, cuda-host, the CI set, x86 with pr_check, and lean (0 new).
  • Limits: kernel-contract-limits with 13.0 is clean.
  • GPU, against main's outcome lines: qwen-pending 49 + 49 _table, qwen-pages 26 and kernel-contract qwen35 q35 123 + 11 _table all match main; qwen-table gives 25 PASS lines with every control red.
  • SASS against main on sm_80/86/89/120/121: every main function is equal (qwen35 32/32, qwen35_keep 4/4, qwen35_kv8 11/11 PASS), and only the _table kernels are added.
  • Conformance kit: all 18 arms pass, with 0 of 114 keys differing between main and this head; pages on against off differs in 0 of 114 keys in all 12 pairs.

The body names the new head.

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 for rebasing the pair and rerunning every gate on 0a49c0a, including the SASS comparison and the conformance kit. We compared it with c4ba435, and all twelve commits carry the same changes, so our earlier review stands and the code needs nothing new.

On another rebase: yes, one more is needed before the pair can land, because both heads now conflict with main's pipeline-stage commits in qwen3_5/cuda_engine.zig and qwen3_5/cuda_shapes.zig. To save you two 8-hour gate runs, please rebase both PRs together once we've answered your latest update on #2939. Two points need care beyond the text:

  1. Shape.key in cuda_shapes.zig has run out of bits. This PR puts table at bit 31, and main's 4dd870b already puts part == .head there, with a test pinning the head piece to bit 31 alone. If both share the bit, a _table shape and a head-piece shape can share a captured graph. Please give table a bit of its own. slots only takes 1, 2, 4, 8 or 16, so five bits would free three; widening the key to u64 also works. Please also extend the key test so that table and part each flip a bit of their own.
  2. cuda_engine.zig comes out at about 602 lines with both sides kept (599 here, 585 on main), so a few lines have to move out to stay at 600 or under, as on cuda_native.zig in qwen3_5 cuda: a pool lane's state as pieces on any whole pages of its lease, no run (#2444 P3, Q3) #2939.

GitHub ran no CI on 0a49c0a, because it doesn't start pull request jobs while a branch conflicts with main. The rebased head will get its first run. This stays a draft and lands with #2939 in one train.

  • Synthena (Ash's Agent)

jschmied added a commit to jschmied/TensorFold that referenced this pull request Nov 12, 2026
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).
@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied for rebasing the pair onto 6c5cef894 and rerunning the gates. On this PR's head, 74140afe9, the three merge points read right:

  • Shape.key (zig/src/families/qwen3_5/cuda_shapes.zig:30-31): slots has bits 19-23, with an assert that it's 16 or under. table is on bit 26, and the head piece keeps bit 31. The Rollback conformance tests #2138 test now varies part and table together (:93-99), and your planted control turned it red.
  • forward reads !record for the state tables and in the useMt2 call (cuda_engine.zig:469, :506). The file is at 591 lines.
  • The test helper sets e.g.first and e.g.whole (cuda_pages_init_test.zig:92-93).

CI is green on 74140afe9 and on #2939's 250994a85. Nothing more is needed on the code.

Next is a run on our GB10, main 6c5cef894 against the pair (#2939's 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 #2939 together in one train. This can stay a draft until then.

  • Synthena (Ash's Agent)

…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.
…a_round.zig (cuda_engine.zig 600 lines)

main moved deltaScan to cuda_forward.zig itself; the table switch joins it there. stateEntries moves beside the state table's offsets, and admit (main's, unchanged) beside the round's limits it checks, so cuda_engine.zig stays under 600 lines.
A plain part of a mixed forward can have no rollback copy: its table entry is zero. Forming the offset
before the undo check was a pointer offset on null. The entry is loaded early, the offset is formed
only where the undo runs, and the undo tests the entry as well.
jschmied added a commit to jschmied/TensorFold that referenced this pull request Nov 29, 2026
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).

TF_QWEN_KV_HOST (main's host capacity mode, one stream) keeps the KV pages in mapped host memory and the state
on the device: its pieces lie on a device block of a lane's state pages (Pages.run_dev, ids 0..), mapped by the
same piece map, and the pool holds KV pages only, as on main.
jschmied added a commit to jschmied/TensorFold that referenced this pull request Nov 29, 2026
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).

TF_QWEN_KV_HOST (main's host capacity mode, one stream) keeps the KV pages in mapped host memory and the state
on the device: its pieces lie on a device block of a lane's state pages (Pages.run_dev, ids 0..), mapped by the
same piece map, and the pool holds KV pages only, as on main.
jschmied added a commit to jschmied/TensorFold that referenced this pull request Nov 29, 2026
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).

TF_QWEN_KV_HOST (main's host capacity mode, one stream) keeps the KV pages in mapped host memory and the state
on the device: its pieces lie on a device block of a lane's state pages (Pages.run_dev, ids 0..), mapped by the
same piece map, and the pool holds KV pages only, as on main.
jschmied added a commit to jschmied/TensorFold that referenced this pull request Nov 30, 2026
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).

TF_QWEN_KV_HOST (main's host capacity mode, one stream) keeps the KV pages in mapped host memory and the state
on the device: its pieces lie on a device block of a lane's state pages (Pages.run_dev, ids 0..), mapped by the
same piece map, and the pool holds KV pages only, as on main.
@jschmied

Copy link
Copy Markdown
Contributor Author

@ashhart this PR is now at bcc3bb7, still on main 6af9f19. Since 3899d00:

  • Slot-scan fix, folded into eed1422: the _table scan loads the rollback copy's table entry beside the
    recurrence's.
  • 89eb3bb, following a review note: the rollback offset is formed only under the undo's guard. A plain part of a
    mixed forward has a zero entry there.
  • 005b375 updates the _table limit rows, and bcc3bb7 simplifies forwardStates' compare.

Gates on bcc3bb7:

  • Host: 814/814.
  • x86: the full set, 2714/2714, with pr_check and lean 0 new.
  • GPU contract: outcome lines equal main's, and qwen-table gives 25 PASS with every control red.
  • SASS: main's functions equal on all five SMs; only the _table kernels are added.
  • Limits: no _table row stale; the other lines equal main's own on the same fatbins.
  • Conformance kit: 9 cells, verdicts equal main's, 0 keys differ.

Speed (in the body, shared with #2939): the fix did not clear the slower cell. These are 3 rounds, not 5:

  • 27B without a drafter at 256, c=4: -0.204 % against a 0.089 % floor, 2 of 3 rounds worse.
  • 35B-A3B at 256: beyond its floor at c=1 (-0.88 %) and c=4 (-1.48 %).

Next I'd try the full 3 + 5 run of the three 256 cells, then nsys on that c=4 cell. Don't land yet.

The body names the new head.

@ashhart

ashhart commented Dec 2, 2026

Copy link
Copy Markdown
Owner

Thanks for the slot-scan fix and for keeping the SASS-equality discipline (the TF_Q35_TABLE 0 pass is main's text token for token, verified equal on all five SMs).

The state: the source is done and the open item is one speed cell that the fix did not clear. On bcc3bb761 (restacked, still on main 6af9f191e), the gates are complete: host 814/814, the full x86 set 2714/2714 with pr_check and lean 0 new, the GPU contract with outcome lines equal to main's and qwen-table 25 PASS with every control red, SASS equal on sm_80/86/89/120/121 with only the _table kernels added, no stale limit rows, the Shape.key planted control red by name, and the conformance kit's 9 cells with verdicts equal to main's and 0 keys different (pages on against unset: 0 keys different in all 6 pairs).

The speed picture (3 rounds, not 5): 11 of the 12 cells within noise or faster, the 35B-A3B at 256 and c=4 +0.55% faster, and one cell slower - the 27B without a drafter at TF_QWEN_KV_PAGES=256, c=4, -0.204% against a 0.089% floor, 2 of 3 rounds worse. The earlier nsys cause (tf_q35_gdn_slots_1_table loading the rollback copy's table entry late, +2.1 us over main's scan on each of the 48 linear layers) was fixed by loading that entry beside the recurrence's, and the cell is still slower - so that was not the whole cause. Your proposed next step is the right one: the full 3 + 5 run of the three 256 cells (27B + DFlash2, 27B without a drafter, 35B-A3B + MTP), then nsys on the c=4 cell. The 35B cells (-0.88% at c=1, -1.48% at c=4) are shared with #2939 and need the same treatment.

One design point the review confirms as sound: pages off (the default) runs main's kernels and main's SASS, and only a forward with a pool lane among its parts runs the _table builds; the table is rewritten only when the state, rollback copy or log block moves, so a captured forward needs no recapture. That is the correct shape for a pages-only cost.

Main-state check: main's GDN kernels assume one contiguous block a stream and compute addresses from it; the per-stream state table (cuda_states.zig's States, 2 KiB on the 27B, 1.4 KiB on the 35B) and the _table kernel builds are not on main.

Status: waiting on the 3+5 rerun and the nsys cause for the 27B c=4 cell (and the 35B cells shared with #2939), then re-review. "Don't land yet" is the right verdict and the review holds it. The conflicts with main are ours; do not rebase.

  • Synthena

@jschmied

jschmied commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Status on the 27B-without-drafter cell at TF_QWEN_KV_PAGES=256, c=4, head bcc3bb761e unchanged:

  • 3 A-vs-A + 5 A-B-B-A against main, tokens equal: -0.102 % slower (floor 0.035 %, 5 of 5 rounds worse). The 35B cells on this PR are within noise.
  • nsys: the whole loss is the _table kernels' own cost: tf_q35_gdn_slots_1_table is about +1.2 us a launch over main's slots_1 (median 72.1 vs 70.9 us, 48 layers) plus about +0.4 us on gdn_pre_table. Launch counts are equal.
  • A local fix (each row's log entry loaded together with that row's inputs) brings it to -0.061 % (floor 0.04 %), still slower, so I haven't pushed it. Its planted control also didn't go red in qwen-table, so the test needs a case for that path first.

So this stays at "don't land yet". I'll come back with the kernel fix, a control that covers it, and the rerun.

@ashhart

ashhart commented Dec 3, 2026

Copy link
Copy Markdown
Owner

Thank you — the rerun and the nsys trace name the cause: the table builds' own launch cost, about 1.2 µs on the slot scan and 0.4 µs on the pre kernel, over every layer. The row-log load you tried brings the cell to 0.061% but it is still slower and its control does not fire yet, so that fix is not ready to take.

The one item left is yours: land the kernel fix with a control that covers that path, and we will rerun the cell. The code review is otherwise complete — the table design, the SASS equality with pages off and the conformance results all stand.

  • Synthena

@jschmied

jschmied commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

This PR is now at 2f6d72dec (measured against main 6af9f19; the branch now conflicts with main aafb329, and I will restack and rerun when you ask). Every change since bcc3bb761 is a new commit; nothing was amended or rebased.

The control:

  • qwen-table now covers the slot scans: slots_{1,2}_table on pieces against main's slots_{1,2} on blocks, recorded windows, outputs and both streams' state with the log rows (6aa4689, 8ad9432). It has 30 PASS lines, and every planted control is red.
  • The plant of the row-log load (every row's log entry taken from row 0) now goes red at tf_q35_gdn_slots_1_table, a 4-row chain. It also went red on 04a18b579.

The kernel fix:

  • A micro bench of the cell's shape reproduces the loss once L2 is cold between launches, as it is in the model: slots_1_table at 92 registers (5 blocks an SM) was +4.7 to +5.7 us a launch over main's slots_1.
  • __launch_bounds__(128, 7) on the one- and two-slot _table scans brings them to main's 72 registers with no spills, and the bench gap to -0.9 to +1.3 us (e55ddd9, 422dbdb). The 4/8/16-slot _table scans and all of main's kernels are unchanged.
  • The _table limit rows (13.0 and 13.2, five SMs) are updated in 04a18b5, and 2f6d72d updates dense_serving_test's pin of qwen35.cuh, which ecdb2f1 had left stale (a host test only).

Gates (GPU on 04a18b579, whose kernels and binaries 2f6d72dec doesn't change):

  • SASS: main's functions in qwen35 (33), qwen35_keep (4) and qwen35_kv8 (60) are equal on all five SMs.
  • Contract: qwen-pending, qwen-pages and kernel-contract qwen35 q35 lines equal main's, and every _table line passes.
  • Limits: no _table row is stale.
  • x86: full set on 04a18b5, then the failed step again on 2f6d72d: 2714/2714 steps, 29195/29375 tests, pr_check 0 errors, lean 0 new.

The cell (27B, no drafter, 256, c=4; 3 A-vs-A + 5 A-B-B-A; tokens equal):
VERDICT c4_tok_s: WITHIN NOISE (median -0.104%, floor 0.093%, B worse 4 of 5)
That verdict rests on one round where main dipped. In the other four the head is 0.08 to 0.16 % behind, about where bcc3bb761 was (-0.102 %). So the bench gain didn't carry over to the served cell. I'm reporting it as within noise by the rule, not as fixed.

The body names the new head and has the details.

@ashhart

ashhart commented Dec 5, 2026

Copy link
Copy Markdown
Owner

Thanks, @jschmied. The fix and the control landed the way the review asked: the register-pressure fix on the one- and two-slot table scans with the bench gap closed, the control extended to the slot scans so the row-log plant goes red, the SASS and contract gates equal to main, and the limits current. The cell reading within noise by the rule, with the per-round residual disclosed rather than smoothed over, is the honest report — the bench gain did not fully carry into the served cell and you say so.

The pair rerun with #2939 is the confirmation run; we will review both together when it lands. The code review on this PR is complete.

  • Synthena

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