Skip to content

flashnext cuda: size the rows-store staging permit from the served window - #5501

Merged
ashhart merged 5 commits into
ashhart:mainfrom
shantanugoel:flashnext-cuda-disk-permit
Nov 28, 2026
Merged

ashhart merged 5 commits into
ashhart:mainfrom
shantanugoel:flashnext-cuda-disk-permit

Conversation

@shantanugoel

@shantanugoel shantanugoel commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Flash Next on CUDA opens its rows store with no served window, so the staging permit stays at 1 GiB: a kept state with more unwritten rows than that (about 31K tokens with the MTP head's rows, 35K without) is skipped with StagingFull, and after a restart the prompt prefills again. Its kept-state view now declares context, as Dory, Gemma 4 and Spark2.5 do, so core sizes the permit to one state at the served window, within the RAM store's budget. This is the CUDA counterpart of #5400.

The same declaration bounds the identity probe to half the window, so --learn-conversations now loads at small contexts, where main stops with PromptTooLong. Below 16 tokens the window leaves no mark to keep, so the shared probe is skipped (probe seed as its hash, no measured rate).

Checks on this head

  • Head commit: 6d7dd9899. It adds the guard in native/cuda_kept.zig and its test to 3d1a70d9c. On this head: test-host 2,809/2,815 steps, 14,480 passed, 85 skipped, 1 failed (the same one; 390 of 1,143 test steps reused from cache); test-golden fuzz-cortheon 8/8 steps; lean 93 and 93; test-cuda-kept-host 23/23, where the new boundary test panics (integer overflow) without the guard. The receipt below was not re-measured: at 262,144 tokens the guard is not taken.
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 93 and 93, 0 new. tools/pr_check.py --base origin/main --pr 5501: 0 errors; warnings are three inherited import cycles and no Unreleased bullet in CHANGELOG.md (the line is in lines/5501.txt).
  • Tests run, with the command and pass, fail and skip counts (Linux aarch64, Zig 0.17.0):
    • New test CUDA kept: Flash Next's permit takes a state at its served window whole, within the RAM store's budget: fails on main (expected 262144, found 0), passes with the change (checked on 89375de45; zig build test-cuda-kept-host: 22/22).
    • On b7b66bcba, zig build -Dfixed-cpu test-host --test-timeout 20m: 2,809/2,815 steps; 32,658 passed, 204 skipped, 1 failed (6 of 1,173 test steps were reused from cache and aren't in these counts). The one failure, Flash Next memory: pack phases … allocation peak (697,244 bytes against the recorded 601,023), also fails on unmodified main at 89375de45, and its file is unchanged since.
    • On b7b66bcba, zig build test-golden fuzz-cortheon: 33/33.
    • zig fmt --check build.zig zig, git diff --check: clean.
  • CI: pending.
  • Not run, and why: macOS, Metal, HIP, XPU (no such hardware); dev-tools (macOS). No kernel changes.

Receipt

  • Main against branch (--parallel 2 --prompt-cache-gib 10 --learn-conversations, greedy, 64 tokens):
Prompt main this branch
65,536 tokens: save skipped (StagingFull) 2,034 MiB written in 3.3 s
after restart: cached / TTFT 0 / 33.80 s 65,535 / 0.74 s
163,840 tokens: save skipped (StagingFull) 4,920 MiB written in 7.9 s
after restart: cached / TTFT 0 / 87.50 s 163,839 / 1.64 s

Cost: large saves now happen, and each pauses replies once. The state is gathered to host memory on the engine thread. Longest gap between chunks of a reply decoding at that moment:

Saved state, tokens main (skips) this branch
65,535 69 ms 1,125 ms
163,839 110 ms 2,787 ms
249,999 134 ms 4,325 ms

The staged bytes (at most one state at the window, capped by the RAM store's budget; 7,450 MiB at 249,999 tokens) are pageable memory outside the CUDA budget, the question raised on #2337; on a GB10 it is the same memory. Lowest host MemAvailable over a run saving 163,839- and 249,999-token states, 13 GiB reserve: 23.6 GiB on main, 16.3 GiB here.

  • Boxes it reaches: Flash Next on CUDA with --learn-conversations or --spill-gib.
  • New switch? None.
  • Environment: receipt base 89375de45, head bd1aa092c; Zig 0.17.0, nvcc 13.0.88, -Dgpu=spark; DGX Spark (GB10, sm_121, 128 GB unified), Ubuntu 24.04.5, driver 580.178.04; nvidia/Qwen3.8-Flash-Next-FP4-Local@b9ce18a6, its own MTP head. --context 262144 --max-tokens 32768 --thinking --vision --vision-max-images 50 --learn-gib 200 --learn-daily-gib 150 --learn-min-free-gib 8, TENSORFOLD_MEMORY_RESERVE_GIB=13, TENSORFOLD_RESTORE_PREFETCH_GIB=4.
  • Paths and platforms the change reaches, which I ran and which I could not: ran --learn-conversations on one GB10, --context 4096 (loads and writes states; no restart at that size) and, on 6d7dd9899, contexts 1, 8, 14, 15 and 16 (all load; a completion answers at 16; main 89375de45 stops at 8 and 16 with PromptTooLong). Not run: --spill-gib (an eviction can't wait for the permit, so one arriving during another large write is still skipped), other CUDA GPUs. --tp refuses --learn-conversations for this family.
  • Exactness: bench_concurrent.py --levels 1,2 --alone --serial --temperatures 0 --tokens 256 --reps 3: 0 unequal, 0 failed (concurrent against solo, drafted against "draft": false); all 90 token hashes equal main's. The replies resumed from disk after restart have the token_sha of main's cold replies (e5a60f8ef459, d5567ce88931).
  • Main against this branch, same machine and checkpoint, posted before asking for a run on our machines: above. Raw results: https://gist.github.com/shantanugoel/c71b851182405c970aef5472de8c05bf
  • Decode speed before and after (bench_openai.py, 256 tokens, medians of 3): code 83.95 → 84.12 tok/s, chat 50.53 → 50.47. Two streams, aggregate: code 145.8 → 145.7, chat 92.3 → 92.2. These don't include the one-off pause above.
  • Prompt speed before and after: n/a for the change, which is reached only with a disk tier. Cold runs with the disk off, to rule out a build difference (bench_cache.py; every row uncached, hashes equal; seconds to first token, main → branch):
Prompt tokens first request repeat
2,048 1.04 → 1.53 (first after boot on both) 0.93 → 0.93
8,192 4.13 → 4.27 3.79 → 3.79
32,768 16.60 → 16.68 15.51 → 15.51
65,536 32.98 → 33.05 31.79 → 31.76
  • Licence of any new weights or drafter: n/a.

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.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.
  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.
  • No precision traded for speed. If the bits change, the description says which sums change and why.
  • Decode and prompt processing are no slower than main at one stream and the widest admitted; with --drafter, no slower than without it, greedy and sampled (steady decode is level; a large save now pauses replies once, above; greedy only, no --drafter).

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

ashhart commented Nov 28, 2026

Copy link
Copy Markdown
Owner

Thanks for fixing the large kept-state staging and small-window loading, @shantanugoel.

The new served-window method also enables context-sized cache probes. With conversation learning enabled and an accepted context of 1–13 tokens, the probe takes fewer than seven input tokens and then subtracts seven to form its cache mark, causing an underflow before prefill. At 14 or 15 tokens the mark is zero, which Flash Next cannot keep, so initialization still fails instead of honoring the small context.

Please keep the staging size based on the served window, but handle contexts below 16 with a valid probe or by skipping that probe. Boundary coverage for contexts 1, 8, 14, 15 and 16 would protect the fix.

  • Synthena

@shantanugoel

Copy link
Copy Markdown
Contributor Author

Thanks, confirmed. Below 16 the probe either underflows (1–13) or marks row 0 (14, 15), and declaring context is what exposes Flash Next to it.

Fixed in 6d7dd9899: Kept.probe runs no pass when the window leaves no mark, returning the probe seed as its hash and no rate (the store learns that from served prompts). The staging permit is still sized from the served window. Such a window's store key then has no probe bits; it still binds the binary, checkpoint, kernels, device and driver. Dory, Gemma 4 and Spark2.5 share this probe, so the guard covers them too (not run on those models).

  • New test CUDA kept: a window under 16 tokens skips the identity probe, and 16 probes with a mark past row 0: contexts 1, 8, 14 and 15 run no pass; 16 runs one and keeps at row 1. Without the fix it panics (integer overflow). zig build test-cuda-kept-host: 23/23.
  • GB10, FP4 Flash Next, --learn-conversations: contexts 1, 8, 14, 15 and 16 load and open the store, and a completion answers at 16. main (89375de45) stops at 8 and 16 with PromptTooLong.
  • On 6d7dd9899, zig build -Dfixed-cpu test-host: 2,809/2,815 steps; 14,480 passed, 85 skipped, 1 failed, the same allocation-peak test that fails on main (390 of 1,143 test steps reused from cache, not in these counts). test-golden fuzz-cortheon: 8/8 steps. Lean 93 and 93, 0 new; pr_check --pr 5501 0 errors; fmt and diff check clean.

@itsnimbus

Copy link
Copy Markdown

A CUDA data point for this change, in case it helps review.

tensorfold-native 1.3.5 on CUDA sm_120 (one RTX PRO 6000 Blackwell), a Flash-Next family TFQ3 checkpoint with vision, --context 262144 --parallel 2 --prompt-cache-gib 16 --spill-gib 12.

Synthetic repro: a 39,143-token prompt (system message of filler words, one user turn), thinking off, max_tokens 8.

  • Cold: 35.8 s to first token. Same prompt again: 39,136 cached, 0.46 s.
  • Planned stop (SIGTERM): every kept state of about 37K-39K tokens logs prompt cache: imprint: saving 39148 tokens skipped (StagingFull). Only tiny states (2-21 tokens, about 110 MiB each) are written.
  • After the restart, the same prompt resumes at 0: 33.0 s, 0 cached.

Our long chat sessions run 80K-100K tokens, so on CUDA spill currently never covers them. Sizing the permit from the served window, as this PR does, is exactly what we need. Happy to run a build of this branch against the same repro and report the before and after.

@ashhart
ashhart force-pushed the flashnext-cuda-disk-permit branch from 6d7dd98 to e9d46b8 Compare November 28, 2026 23:38
@ashhart
ashhart merged commit c87a954 into ashhart:main Nov 28, 2026
2 of 12 checks passed
ashhart pushed a commit that referenced this pull request Nov 28, 2026
@ashhart

ashhart commented Nov 28, 2026

Copy link
Copy Markdown
Owner

Thank you @shantanugoel. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

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.

3 participants