Skip to content

flashnext: the MTP head's streams by row through the lease's pages, no per-lane block outside the pool (#2444 P3, F2 part c) - #2992

Open
jschmied wants to merge 24 commits into
ashhart:mainfrom
jschmied:flashnext-head-paged
Open

jschmied wants to merge 24 commits into
ashhart:mainfrom
jschmied:flashnext-head-paged

Conversation

@jschmied

@jschmied jschmied commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Head 42f3199 (11-28 review)

Ashhart 7238451798: the pieced open's look-ahead now posts its scattered state pages (same fix as #2898).
Ashhart 7238452186: the lone verify is drawn right after its own verify (defensive, no extra copy or launch). The
old order was not reproduced here: a regression test passes on the old code too. Details in the reply; device inputs asked.
Host set on this head: 496/496 steps (x86g). pr_check 0 errors, lean 0 new.

What this changes

Stacked on #2898 (F2, head d73c249). The two land together, as ashhart asked in #2444 (comments 6796185011 and
6824699204). This PR's first five commits are #2898's; the six after them are (c).

On #2898 with TF_FLASHNEXT_STATE_PAGES=1, two of the MTP head's buffers are each one matrix wider than a page:
streams (31.5 MB at 1536-row chunks) and the window's logits (15.9 MB). They stayed outside the pool as a lane's own
block (option (a)), and at a 77 GB limit that block cost a concurrent stream (fit table on #2444, comment 6796148352).
This PR removes the block:

  • streams lives in the lease as 48 pieces of 32 rows (20,480 B a row), and the head reads it by row through the page
    table. Block 0 holds rows 0..31, so every window (at most 32 rows) reads and writes there exactly as before. A prompt
    pass writes a chunk's rows block by block. A head prefill piece whose rows cross a block is first gathered into the
    pass's s.h rows. The piece doesn't touch those rows until after it has read its input.
  • logits: a paged draft keeps no scores of its own. Its window's scores stay where the verify pass already writes them,
    the lanes' shared 64-row buffer (verifyMany's out, which main copies each lane's rows from). The draws read them
    there with the same launches as main, before the next verify reuses the buffer. A lone window (a round with one
    drafting stream) scores into the same buffer through Ops.verifyIn. The plain single-stream verify refuses a paged
    draft by name (FlashNextPagedDraftVerify), which keeps the rank path and the tests honest.

An idle pool lane now holds nothing outside the pool, and the layout's fixed part is main's again ("0 MiB fixed each" in
the load line, the same as main).

Saving 3 pages a stream isn't possible: main's 48 is the floor, and pieces reach it. With the head and pending rows
(the default), a lane's state plus head is 374,645,504 B, which is 47.54 pages of 7,880,704 B (256-row pages with the
head). Main's run is 48 pages. Pieces pack to 48 as well: 271 pieces, 342.22 MiB on 48 pages, 18.53 MiB of padding. That
holds even though the 15.9 MB of scores no longer live in the lane, because the 3 MiB DeltaNet states leave gaps the
rest can't fill, so first-fit-decreasing still needs 48 pages. With TF_FLASHNEXT_KEEP_LATE=0 it is 234 pieces on 41, the same as the run's 41. So F2 + (c) ties main on
bytes. What pieces add is the thing #2898 is for: a lane opens on any free pages, with no consecutive run.

No kernel changed. No file under zig/kernels, zig/build or build.zig differs from main. The 114 fatbins
embedded in the served binary are byte-identical to main 3885dce's, in the same order (md5 of each blob). The kernel
contract rows are unchanged, so no new contract-runner rows or limits apply.

Unset (TF_FLASHNEXT_STATE_PAGES off), every new branch is skipped (no blocks, logit_paged false), and windows, draws
and prompt passes run main's code. The kit and A-B-B-A rows below show the bits and the speed both ways.

Core vs family:

part where
Rows: a matrix as row blocks (sizes, a run that never crosses a block, a row's address); Placed: those blocks placed, walked from any row core: core/kv_pieces.zig
host tests: block sizes and runs, addresses through any pages; a walk from any row, with planted swapped blocks and a lost offset that must fail core: core/kv_pieces_test.zig
Hooks.stream_rows (a Placed); per-block stream copies at the pass's write-back family: cuda_batch_state.zig, cuda_batch_head.zig, cuda_prompt.zig, cuda_prompt_windows.zig
Draft.pieceSizes adds the blocks, initPieces takes their addresses, streamAt, window_scores; the lane's wide block is gone family: cuda_mtp_draft.zig, cuda_kv_lanes.zig
prefill gather, prompt_last, verifyIn; draws read window_scores; plain verify on a paged draft refused by name family: cuda_mtp_ops.zig, cuda_mtp_draw.zig, cuda_lanes_verify.zig

Code sharing, checked first: no Qwen, GLM or core path reads a buffer wider than a page through pages. Their pool lanes
hold only whole-layer state. So Rows/Placed go in core next to the packer, and Q3 (or a Qwen head, if one comes) can
use them as is. Metal has no Flash Next pool.

Checks on this head

Served gates (GB10, Qwen3.8-Flash-Next-FP4, --context 16384 --parallel 4)

Conformance kit (tools/conformance.py, flashnext-cuda profile with the cancel/evict blocks), page cache dropped before
every server, run after the rebase: main 3885dce (md5 82a75428) and the rebased head e89fb3c1e (a4381ec5), in the
same job. Cells: pages off; TF_FLASHNEXT_KV_PAGES=256; and 256 + TF_FLASHNEXT_STATE_PAGES=1 (the branch only). All 7 gates pass in every
cell, main's included (35 of 35): drafted == plain (448 of 724 drafts accepted), repeat == first, concurrent == solo at
1/2/4/8 (drafted and plain, greedy and seeded), resumed == fresh, refusals, cancel (census back to idle), evict. Every kit
row's sha equals main's in the same paging: 149 rows, 137 keys, 0 differ, for off, 256, and 256 with pieces.
The final head 25108af (b9788991) reran the three branch cells: 21/21 gates, 0 of 137 keys differ from main's rows.

Load line, pieces cell: "state, in the pool as pieces: 271 pieces, 342.22 MiB on 48 pages a lane, 18.53 MiB padding
(5.1 %)" and "0 MiB fixed each". Main's is "state run, in the pool: 357.39 MiB on 48 pages", also with "0 MiB fixed
each".

Fit (ashhart's landing cell): admitted streams by memory limit

TENSORFOLD_CUDA_MEMORY_LIMIT_GB=L, --parallel auto (8 asked), --context 16384, 256-row pages, --prompt-cache-gib 0.
Main 33ada77 against this PR's pre-rebase head adae273 (md5 376329c7) with TF_FLASHNEXT_STATE_PAGES=1. It stands for
this head: the rebase changed no layout, packer or ledger code, and the load line above is the same (271 pieces on 48
pages, 0 MiB fixed each). A 16k-window stream needs 65 pages plus its state (48
both ways).

 L GB | lanes main/pieces | pool pages main/pieces | 16k streams main/pieces | 4k (17 pages) streams main/pieces
   76 | 7/7 | 337/337 | 3/3 | 6/6
 76.5 | 8/8 | 405/405 | 4/4 | 7/7
   77 | 8/8 | 473/473 | 5/5 | 8/8
 77.5 | 8/8 | 541/541 | 5/5 | 8/8
   78 | 8/8 | 610/610 | 6/6 | 8/8
   79 | 8/8 | 746/746 | 7/7 | 8/8

Main and pieces admit the same streams at every limit, and the pool has the same pages. #2898 alone lost a stream at 77
GB (16k streams 5 vs 4) and one at 76/76.5/77 GB with 4k prompts. That loss is gone.

Speed: A-B-B-A, page cache dropped before every server

Rerun after the rebase: main 3885dce (82a75428) against the rebased head e89fb3c1e (a4381ec5). c1 = 3 x 256 tokens
solo, c4 = 3 rounds of 4 x 256, TTFT = two 8.8k-token prompts. Two jobs: one ran A-B-B-A x2 at both pagings, the second
another A-B-B-A x2 at pages 256. Pooled: 4 rounds at 256, 2 off. Token shas are identical across every arm of each
paging, and solo == c4 in every arm.

== pages 256: main vs pieces; 4 rounds; token shas identical across 16 arms: True; solo==c4 every arm: True
  c1 tok/s         main [41.233, 41.4, 41.467, 41.3, 41.5, 41.467, 41.433, 41.333]  pieces [41.1, 41.4, 41.4, 41.533, 41.367, 41.467, 41.333, 41.567]  mean +0.01%  rounds -0.16% / +0.20% / -0.16% / +0.16%  (2 of 4 rounds worse; spread main 41.233-41.500, pieces 41.100-41.567)
  c4 tok/s         main [81.445, 81.788, 81.558, 81.788, 81.852, 81.425, 81.691, 80.818]  pieces [80.601, 81.74, 81.639, 81.499, 81.527, 81.51, 81.602, 81.899]  mean -0.05%  rounds -0.55% / -0.13% / -0.15% / +0.61%  (3 of 4 rounds worse; spread main 80.818-81.852, pieces 80.601-81.899)
  ttft 8.8k s (1)  main [4.564, 4.554, 4.552, 4.551, 4.616, 4.575, 4.541, 4.563]  pieces [4.54, 4.544, 4.551, 4.59, 4.545, 4.555, 4.532, 4.576]  mean -0.23%  rounds -0.39% / +0.41% / -0.99% / +0.04%  (2 of 4 rounds worse; spread main 4.541-4.616, pieces 4.532-4.590)
  ttft 8.8k s (2)  main [4.553, 4.563, 4.531, 4.54, 4.527, 4.557, 4.528, 4.712]  pieces [4.568, 4.514, 4.559, 4.549, 4.527, 4.55, 4.518, 4.556]  mean -0.47%  rounds -0.38% / +0.41% / -0.08% / -1.80%  (1 of 4 rounds worse; spread main 4.527-4.712, pieces 4.514-4.568)
== pages off: main vs branch; 2 rounds; token shas identical across 8 arms: True; solo==c4 every arm: True
  c1 tok/s         main [41.4, 41.667, 41.3, 41.5]  branch [41.367, 41.633, 41.567, 41.467]  mean +0.10%  rounds -0.08% / +0.28%  (1 of 2 rounds worse; spread main 41.300-41.667, branch 41.367-41.633)
  c4 tok/s         main [81.764, 81.722, 81.611, 81.777]  branch [81.653, 81.712, 81.858, 81.782]  mean +0.04%  rounds -0.07% / +0.15%  (1 of 2 rounds worse; spread main 81.611-81.777, branch 81.653-81.858)
  ttft 8.8k s (1)  main [4.576, 4.573, 4.563, 4.574]  branch [4.563, 4.566, 4.559, 4.557]  mean -0.23%  rounds -0.23% / -0.23%  (0 of 2 rounds worse; spread main 4.563-4.576, branch 4.557-4.566)
  ttft 8.8k s (2)  main [4.554, 4.563, 4.557, 4.545]  branch [4.544, 4.55, 4.548, 4.529]  mean -0.26%  rounds -0.25% / -0.27%  (0 of 2 rounds worse; spread main 4.545-4.563, branch 4.529-4.550)

Pooled at pages 256: c1 +0.01 %, c4 -0.05 %, TTFT -0.23 % / -0.47 %. Off: c1 +0.10 %, c4 +0.04 %, TTFT -0.23 % / -0.26 %.
No cell at 256 keeps one sign across the rounds. The c4 rounds go -0.55 / -0.13 / -0.15 / +0.61 %; the low arm is 80.6 in
one pieces arm and 80.8 in one main arm, the same dip that hit either arm before the rebase (adae273: pooled c1 -0.05 %,
c4 -0.07 %).

An earlier head that copied each score row to its own piece lost 0.69 % at c=4 in both rounds; this head does not copy.

Proposed CHANGELOG line (for ashhart to place): "Flash Next: with TF_FLASHNEXT_STATE_PAGES, the MTP head's streams live in
the stream's pages too and its window scores stay in the shared verify buffer, so a pool lane holds nothing outside the
pool (#2444)."

  • Pin rows (cdd666c, ashhart 6900336847): zig/tests/fixtures/gradient-serving.tsv rows for cuda_batch_state.zig, cuda_lanes_load.zig, cuda_mtp_draft.zig, cuda_mtp_ops.zig, cuda_prompt.zig now carry the files' new sha256, each with a note in the neighbouring rows' style. A sha256 check of every raw row on this head leaves one mismatch, zig/src/cuda/experts.zig, which this PR doesn't touch: its row is stale at our base 6c5cef8 and main has since updated both the file and the row, so test-gradient-serving passes once merged with main. The served code is unchanged; only the fixture changed.

@BlivionIaG

Copy link
Copy Markdown
Contributor

Coordination before publishing the EXL3 stack update (#1441–#1446, #1522): its new, unpublished capture-admission correction touches cuda_prompt_windows.zig and cuda_window_graph.zig, also touched by this stack. A real pooled-expert run failed with CUDA 900/901 after host planning / synchronization entered whole-pass capture. My guard excludes that case before capture; resident experts and covered active device plans retain eligibility.

This is separate from your paged head streams and shared verify scores; I am not implementing or replacing those. The current textual conflict is in cuda_prompt_windows.zig. Both your row/page handling and the host-planning admission check need to survive integration. Flagging this before push to agree on the shared call-site shape/order. The corrected EXL3 device validation is still pending, not claimed complete.

@jschmied

Copy link
Copy Markdown
Contributor Author

@BlivionIaG thanks for flagging it first. This stack's edits in those two files are one line each:

  • cuda_prompt_windows.zig:14, forwardGraph: one more refusal before anything is staged or captured. A pass with paged rows (hooks.stream_rows.bases.len > 0) returns false and runs uncaptured.
  • cuda_window_graph.zig:189, RollbackKey.init: two more hash inputs, d.saved_tail and d.saved_nrow, so a paged rollback can't reuse a graph keyed to other rows.

So both are pure admission and key changes, and they compose with your guard. Your host-planning check is one more early return false before window_graph.stageWindow, and either order works, because every check returns before staging or capture. Whichever stack lands second keeps the other's condition, in the same line or as an if right after it. If your correction also changes RollbackKey, please keep the two fields. I'll do the same on our side if we land second.

@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied, this is the cell we asked for on #2444: main and F2 + (c) admit the same streams at every limit from 76 to 79 GB, the kit rows equal main's in all three cells, and at 256 pages the pooled A-B-B-A means sit within 0.25% of main, with no cell keeping one sign across rounds. Thanks as well for the correction on the 3 pages. The pieces pack to 48 pages, the same as main's run, so nobody who turns this on loses a stream, and a lane can now open on any free pages instead of a run. That's enough for us. As we said on #2444, 8-head groups only come in if a measured cell shows the whole-layer pad (18.53 MiB a lane) costing a stream.

I read the head through, and it holds up:

  • Placed.next never crosses a block.
  • copyStreams and the prompt chunks carry from.
  • streamAt/prompt_last match the old offsets in block 0.
  • The window graph refuses a paged pass before staging.
  • The lone branch in cuda_lanes_verify.zig (n == 1 or cap <= 1) always holds a group of one.
  • Each group's draws run before the next group's verify reuses the lanes' buffer, so window_scores can't be overwritten while it's read.
  • Outside verify, verifyMany and draws, nothing on the head or on main touches a draft's logits.

@BlivionIaG's guard and these two lines compose as you described.

What's left before this and #2898 go in together:

  1. Please rebase flashnext: pool lanes keep their states and draft as pieces on whole pages, no run (#2444 P3, F2) #2898 and this PR onto today's main and push both. The conflict is in zig/src/families/flashnext/cuda_mtp_draw.zig's draws (main's starts at line 113). Main now waits once and checks every verify row's best values, and it refuses a non-finite selection by its row (c6a3377, 80e0fd7). A paged draft needs your scores in main's h.best, candidates and row download, with checkBest/badVerify kept as main has them. Your local copy on 93a2e93 should have most of this. Main has moved since, and dfd9ac5 touches the same file and the environment table, so it needs one more rebase.
  2. That resolution changes the draws a paged draft takes. Please rerun the kit's three cells (pages off, 256, and 256 with TF_FLASHNEXT_STATE_PAGES=1) on the rebased head, and post the row-sha comparison with main as before. The fit table and A-B-B-A rows can stand unless the resolution changes more than the score pointer.
  3. Please take flashnext: pool lanes keep their states and draft as pieces on whole pages, no run (#2444 P3, F2) #2898 out of draft once it's rebased, so CI runs on both heads. We'll review flashnext: pool lanes keep their states and draft as pieces on whole pages, no run (#2444 P3, F2) #2898 on its own thread then, as we said we would.
  4. Optional, your call: TF_FLASHNEXT_STATE_PAGES is a .cuda row (zig/src/platform/environment.zig:50 on flashnext: pool lanes keep their states and draft as pieces on whole pages, no run (#2444 P3, F2) #2898), so a malformed value stops every CUDA start, not only Flash Next's. Main now has a flashnext scope that both Flash Next load paths check first. Moving the row there would keep it with the other 29. The count test in environment_flashnext_test.zig and the "29 settings" line in docs/environment.md would move to 30.

On overlap: kv_pool.zig and kv_pool_state_test.zig are byte-equal to #2939's, so whichever of the two lands second just drops those hunks.

  • Synthena (Ash's Agent)

@jschmied
jschmied force-pushed the flashnext-head-paged branch from adae273 to 25108af Compare November 12, 2026 17:14
@jschmied

jschmied commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor Author

@ashhart thanks for the read-through. All four are done; heads are #2898 d432157 and this PR 25108af, both on main 3885dce.

  1. Rebased. In cuda_mtp_draw.zig draws, main's code stays as it is: one wait, checkBest over every verify row, badVerify on a non-finite row, pending, and dfd9ac5's on(). A paged draft changes the score pointer only: scores (window_scores on pieces, logits otherwise) goes into main's h.best, candidates and row download. git diff origin/main -- cuda_mtp_draw.zig is those three lines plus the const scores line. Every other file has the same added and removed lines as adae273.
  2. Kit rerun after the rebase. The three cells (pages off, 256, and 256 with TF_FLASHNEXT_STATE_PAGES=1) and main's two ran in one job against main 3885dce. All 7 gates pass in every cell (35/35). Row shas against main: 149 rows, 137 keys, 0 differ in each of the three cells. The final head 25108af reran the three cells: 21/21, 0 of 137 keys differ. Because the resolution only moved the pointer, the fit table stands; nothing in the layout, packer or ledger moved, and the load line is the same (271 pieces on 48 pages, 0 MiB fixed). I reran the A-B-B-A anyway: pooled over 4 rounds at 256, c1 +0.01 %, c4 -0.05 %, TTFT -0.23 % / -0.47 %; off, c1 +0.10 %, c4 +0.04 %. No cell at 256 holds one sign, and tokens are identical in every arm.
  3. flashnext: pool lanes keep their states and draft as pieces on whole pages, no run (#2444 P3, F2) #2898 is out of draft now.
  4. Taken: TF_FLASHNEXT_STATE_PAGES is now in the flashnext scope (flashnext: pool lanes keep their states and draft as pieces on whole pages, no run (#2444 P3, F2) #2898's d432157). The count test reads 30 settings (21 switches) and docs/environment.md says 30. The refusal test now checks it fires under .flashnext and not under .cuda.

Host test-kv-pool test-cuda-host 392/392 on each head, x86 720/720 + 16/16, lean 0 new on both. The 114 fatbins are byte-identical to main's. The PR body has the rows.

@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. f0fe4b7 is 25108af restacked on main 6c5cef8. The only difference is #2939's TooMany text in zig/src/native/cuda_kv_pool.zig ("state pages") and its test. In cuda_mtp_draw.zig, draws keeps main's one wait, checkBest, badVerify and pending, and changes only where the scores are read (:116-117, :127, :137). CI is green on this head and on #2898's 59ba8b4, and the kit, fit and A-B-B-A rows carry over.

Every ask is met, so we'll land #2898 and this PR together, #2898 first. #2939 carries the same core commit, so whichever of them lands second drops it. These are the changelog lines we'll use:

  • Flash Next on CUDA: TF_FLASHNEXT_STATE_PAGES=1 (off by default; needs TF_FLASHNEXT_KV_PAGES) keeps a pool lane's states and MTP buffers as pieces on any whole pages of its lease, so a lane opens on any free pages instead of a consecutive run; output tokens are unchanged (#2898, for #2444). Thanks to @jschmied.
  • Flash Next on CUDA: with TF_FLASHNEXT_STATE_PAGES=1, the MTP head's streams also sit in the lane's pages and its window scores stay in the shared verify buffer, so a pool lane holds nothing outside the pool, at 48 pages a stream as before (#2992, for #2444). Thanks to @jschmied.

Thanks also to @BlivionIaG for flagging the cuda_prompt_windows.zig overlap early. Your host-planning guard and this stack's one-line checks in cuda_prompt_windows.zig and cuda_window_graph.zig compose as @jschmied described, so whichever lands second keeps both.

  • Synthena (Ash's Agent)

@tacosta

tacosta commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

I’m preparing the dense MTP work for #2485 as an unpublished local branch. ATC reports a merge conflict in zig/src/platform/environment_test.zig with the FlashNext MTP head-stream changes. This appears to be shared environment-test wiring rather than a change to FlashNext behavior. Could we coordinate the landing order or a combined test edit? No #2485 code has been pushed; its 2K TTFT gate remains unresolved.

@jschmied

Copy link
Copy Markdown
Contributor Author

@tacosta Thanks for asking first. On our side the overlap in zig/src/platform/environment_test.zig is additive only: two valid rows for TF_FLASHNEXT_STATE_PAGES (after the TENSORFOLD_PROMPT_CACHE_KEEP_OTHERS rows) and one new test block after the keep-others test. Nothing existing is edited.

So no particular landing order is needed. Whichever branch lands second keeps both sets of rows and both tests; there's no shared logic to merge. If #2485 lands first, I'll take your rows as they are when ashhart asks me to rebase #2992 (head f0fe4b77c).

@tacosta

tacosta commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Thanks for confirming. We’ll preserve both TF_FLASHNEXT_STATE_PAGES valid rows and both test blocks; no landing order is needed. The #2485 branches remain local until the other ATC overlaps are resolved.

@ashhart

ashhart commented Nov 15, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied, and thanks for answering @tacosta so quickly. I checked your note against the head: in zig/src/platform/environment_test.zig this PR only adds the two valid rows for TF_FLASHNEXT_STATE_PAGES (:70-71, after the keep-others rows) and the test block at :248. Nothing existing is edited, so the two branches can land in either order, as you said.

Our earlier asks still hold on f0fe4b7. draws keeps main's checks and only changes where the scores are read. TF_FLASHNEXT_STATE_PAGES is in the flashnext scope (zig/src/platform/environment.zig:191), and the count and docs read 30. On a closer pass over the served files, though, I found one thing we missed last time. I'm sorry it comes after we said this was ready.

  1. Five pinned serving files change, but their rows in zig/tests/fixtures/gradient-serving.tsv don't. Those are cuda_batch_state.zig (:119), cuda_lanes_load.zig (:134), cuda_mtp_draft.zig (:146), cuda_mtp_ops.zig (:148) and cuda_prompt.zig (:156), all under zig/src/families/flashnext/. zig build -Dfixed-cpu test-gradient-serving fails on this PR with serving source changed: zig/src/families/flashnext/cuda_batch_state.zig, while all five pins match main's files. Please update each row to the new sha256, with a note in the same style as the rows around it, for example: #2898/#2992: TF_FLASHNEXT_STATE_PAGES=1 places the state and MTP head on lease pages; off, every new branch is skipped and replies are unchanged. Updating the rows on flashnext: pool lanes keep their states and draft as pieces on whole pages, no run (#2444 P3, F2) #2898 for the files it changes and here for the rest is fine. Whichever way you split them, both heads should pass the step.

We'll handle two things on our side when we settle the stack against today's main, so there's nothing for you to do on these. Main has since added a failure-only diagnostic, badCandidates in cuda_mtp_draw.zig, which cuda_draw_batch.zig also calls. It reads d.logits, and a paged draft no longer has that, so we'll give it the same scores pointer the draws use. Main also grew zig/src/core/kv_pool.zig by a line, which puts the merged file at 601 lines, and we'll trim that as well.

Once the pin rows are in, please push both heads and we'll take another look.

  • Synthena (Ash's Agent)

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

Copy link
Copy Markdown
Contributor Author

@ashhart Thanks for the closer pass. The pin rows are in on both heads, each with a note in the neighbouring rows' style:

A sha256 check of every raw row on each head now leaves one mismatch, zig/src/cuda/experts.zig. Neither PR touches it: its row is stale at our base 6c5cef8, and main has since updated both the file and the row. So on the branches alone test-gradient-serving still stops at that row, and it passes on the merge with main. Only the fixture changed; the served code is the same as before. Bodies updated to the new heads.

@ashhart

ashhart commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied for updating the serving pins on both heads and explaining the inherited row. Nothing further is requested from you. We will resolve the current score-pointer diagnostic and pool limits, reconcile the pins and changelog, and run the merged pages-off/pages-on checks on a GB10 before #2898 and #2992 land in order.

  • Synthena

jschmied and others added 20 commits December 2, 2026 23:34
…ith the other Flash Next settings

As a .cuda row a malformed value stopped every CUDA start, not only Flash Next's. In main's flashnext scope it is
checked where the other 29 are, before either Flash Next load path does device or checkpoint work; the count test
and docs/environment.md move to 30 settings, 21 switches.
Main holds the file at 600; this PR adds a line, so one blank line
between declarations goes.
Adapt the Draft fixture to the saved_tail and saved_nrow addresses hashed by RollbackKey on main 1eed88b, and keep the existing every-buffer test sensitive to each address.
… windows, kv_pool.zig within 600 lines

Main sizes the draft's best values with bestBytes(rows) where the branch had best_bytes, so the pieced draft's
piece list and its init take bestBytes(l.rows) from the same layout as main's run path. Main's MTP windows are
48 rows (mtp.max_rows = batch.late_rows since main 375b08b), not 32, so the pieces test's state bytes grow by 16 rows of
saved projections a DeltaNet layer and its outside block by 16 score rows; piece and page counts are unchanged.
Pool.takeState moves to kv_pool_take_state.zig (kv_pool.zig 614 -> 596; Pool.ownership becomes pub for it).
TF_FLASHNEXT_STATE_PAGES gets its generated row, and cuda_mtp_draft.zig's pin follows the file.
…indows, two files within 600 lines

Main's TF_FLASHNEXT_GRAPH_TAIL takes a lone verify's row best inside the window's graph from d.logits; a paged draft
scores into the lanes' buffer instead, so verifyTail now reads the hooks' logits_out (d.logits when unset, as on main).
The absorb's stream gather moves to Draft.gatherStreams (cuda_mtp_ops.zig 611 -> 600), and two one-line field docs in
cuda_batch_state.zig fold onto their fields (602 -> 600). Pins follow the files.
Main's MTP windows are 48 rows, not 32 (main 375b08b): the pieces tests count 32 stream blocks of 1536 rows,
16 pieces fewer, and 16 more saved projection rows a DeltaNet layer; with pending rows on, the pieces take 49
pages where the run takes 48, so that case's equality becomes the run's pages plus one.
@ashhart
ashhart force-pushed the flashnext-head-paged branch from 42f3199 to 02b64ab Compare December 3, 2026 00:01
@ashhart ashhart added kind:perf Classified by Synthena as performance improvement synthena:needs-receipt Speed PR awaiting verified perf_receipt.py hash run labels Dec 3, 2026
@ashhart

ashhart commented Dec 3, 2026

Copy link
Copy Markdown
Owner

Synthena Initial Review — Performance receipt required (kind:perf)

Moves the MTP head's streams and window scores into pool lease pages (TF_FLASHNEXT_STATE_PAGES) so an idle lane holds nothing outside the pool, removing a per-lane block that cost a concurrent stream at a 77 GB limit.

  • Performance claim detected without a verified tf-receipt-v1 receipt.

Performance PRs require a verifiable benchmark receipt before running project CI hardware. See CONTRIBUTING.md (Performance PRs: verifiable benchmark receipt) and attach the output of:

python3 tools/perf_receipt.py run --base-bin /path/to/base-tf --head-bin /path/to/head-tf --model /path/to/model.tfold --out receipt.json

Findings:

  • [BLOCKER] zig/src/core/kv_pieces.zig: nthHighest is O(n*ids.len) and its scan is wrong when ids contains duplicates or when n exceeds the count of distinct values: with duplicate ids the second iteration can re-select the same id (id < cut fails only against the cut, not against already-picked ids), so takeState can take fewer than n distinct pages while still decrementing l.promised by n. Suggested fix: Select strictly-decreasing distinct values (track the previous pick and require id < previous) and assert n <= ids.len, or replace with a partial sort/quickselect over a copy of the ids.
  • [WARNING] zig/src/families/flashnext/cuda_lanes_verify.zig: The lone-window path now interleaves verify and draws per window so each draw reads its scores before the next verify reuses the shared 64-row buffer; any caller that previously relied on all verifies completing before draws (e.g. graph-captured tails queuing best_ahead) may read scores overwritten by a later window's verify. Suggested fix: Confirm the graph-tail path (d.best_queued) cannot be combined with logit_paged, or gate the interleaved order on !d.best_queued as well.
  • [WARNING] zig/src/families/flashnext/cuda_prompt.zig: When stream_rows.bases.len > 0 the code keeps hooks.streams unchanged but still advances stream_rows.from by at; if hooks.streams is also nonzero (both a linear pointer and a placed map set), the linear pointer is silently ignored while the map is used, which may be an unintended combination rather than an enforced invariant. Suggested fix: Assert or document that streams and stream_rows.bases are mutually exclusive, and refuse the combination at open time instead of silently preferring the map.
  • Synthena

@jschmied

jschmied commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Head is unchanged: 02b64ab4b0 (your restack). I checked all three findings against the code and none is reachable, so there's no new commit.

BLOCKER, kv_pieces.zig:139-147 nthHighest.

  • The scan can't pick the same id twice. Line 144 only accepts id < cut, and cut is the previous pick, so every id at or above the previous pick is excluded. If there were a duplicate, takeState would take more pages than n (kv_pool_take_state.zig:15 keeps everything >= cut), not fewer.
  • The free stack has no duplicates. An id is pushed only when its refs reaches 0 (kv_pool_retire.zig:81-88, :104-106). release refuses a page with refs <= leased (kv_pool.zig:362), so a free page can't be released a second time. Pops (kv_pool.zig:384, :425), claimRun (:537) and takeState take ids off the stack before they get any holder.
  • n is bounded in takeState. n <= l.promised (:11), l.promised <= p.promised, and p.promised <= nfree (promise, kv_pool.zig:368-374, which tests available() = nfree - promised at :286). So n <= ids.len holds there.
  • One caller can pass n past the free count: mapAhead's look-ahead (cuda_kv_lanes.zig:407) on a nearly full pool. In that case nthHighest returns 0, stateIds marks every free id as held, and nextPages posts nothing. That is a look-ahead that maps nothing, and the open itself is then refused by promise. waits guards with fits(n + k) first (:277).
  • Cost: n is statePages(), a handful of pages.

WARNING, lone-window order versus the graph tail (cuda_lanes_verify.zig:28-37). The queued best is computed inside its own verify's forward. verifyTail (cuda_mtp_ops.zig:98-108) points tail.scores at the scores that verify writes (logits_out, the lanes' buffer) and writes the result to d.best, which each draft has to itself. The next window's verify is enqueued after this window's draw on the same stream, so it can't overwrite scores that a queued best has yet to read. draws reads d.best and not the shared rows when best_queued is set (cuda_mtp_draw.zig:122-125). Nothing outside windows() depends on the order: the lone path's draws happen in this same loop. Test: cuda_lanes_verify_test.zig:143 (two and three one-row paged drafts, each draw reads its own stream's scores).

WARNING, streams and stream_rows both set (cuda_prompt.zig:429-430). This combination is intended, and only one destination is used. hooks.streams != 0 is the pass's switch for writing streams at all (cuda_batch_head.zig:18, :49, :65; cuda_batch.zig:503, :573). stream_rows, when set, says where they go row by row (copyStreams, cuda_batch_head.zig:78-86). The only caller that sets both is prefillChunk (cuda_mtp_ops.zig:140-141). Its streams is the paged draft's block 0 (cuda_mtp_draft.zig:349), and the head reads rows past that block back by row (cuda_mtp_ops.zig:200-201). The window graph refuses stream_rows (cuda_prompt_windows.zig:14). If we refused the combination at open, the switch would turn off the streams.

Receipt. No speedup is claimed. The claim is memory placement: an idle lane holds nothing outside the pool (load lines in the body, 342.22 MiB as pieces against main's 357.39 MiB run, 0 MiB fixed each). The A-B-B-A rows in the body are parity (c1 +0.01 %, c4 -0.05 %).

@ashhart ashhart added synthena:needs-device Initial diff review passed; waiting for device and slowdown check before READY and removed synthena:needs-receipt Speed PR awaiting verified perf_receipt.py hash run labels Dec 5, 2026
@ashhart

ashhart commented Dec 5, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. The three later warnings are closed: page selection walks distinct descending IDs, queued best values belong to each draft, and the stream pointer and row map are intended to work together. The diagnostic score pointer and regression coverage are in place too.

Ready for our device A/B, with #2898 first and this next. We'll compare the current main and integrated stack at pages off, 256, and 256 with state pages enabled, requiring identical token hashes and checking admission and timing. The earlier parity receipts stand at their recorded revisions. Nothing more is needed from you.

  • 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

kind:perf Classified by Synthena as performance improvement synthena:needs-device Initial diff review passed; waiting for device and slowdown check before READY

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants