Skip to content

CUDA Flash Next: read the n-gram table back after warm-up, and let an EXL3 table pin in row runs (new replies decode 11-14 % faster on EXL3 4.05, same tokens) - #254

Closed
grearjake-star wants to merge 1 commit into
ashhart:mainfrom
grearjake-star:upr-ngram-reread

Conversation

@grearjake-star

Copy link
Copy Markdown
Contributor

What goes wrong

At startup the n-gram table is read alongside the weights. The weights' own reads, and the warm-up's allocations after
them, then push much of the table back out of the page cache. On Flash Next EXL3 4.05 bpw (39 GB table) I measured
between 0 % and 90 % of the table's pages still resident after a load, depending on what the page cache held before
(mincore on the table's maps). The whole-table lock() does not apply there: the startup room is about 32 GB
against 39 GB.

From then on, each new reply's first n-gram lookups fault the missing pages from disk one at a time (MADV_RANDOM
maps). Rows are staged before every verify and MTP step, so the decode stalls on them. The cost shows up as a gap
between a reply decoded for the first time and the same reply decoded again:

  • 75 % resident: the first decode runs 10-17 % slower.
  • ~8 % resident: up to 35 % slower (fresh replies 62.7 / 36.5 / 63.0 tok/s against 95.7 / 52.6 / 87.7 repeated, on
    our 0.6.0-based tree).

Every request of a chat or agent session is a new reply, so a served model pays this until the session has touched
the pages itself.

What this changes

  1. Read the table again after warm-up. A table that is not locked is read once more with its own prefetch(),
    after graph capture and the prompt warm-up. That is the same read the load already does, at the point where
    nothing at startup evicts it any more. It costs 0.7-1.9 s when most pages survived and 5-9 s from a cold cache.
    An MADV_WILLNEED pass on the missing pages brought back almost nothing in my tests (35 GiB asked, ~3 % resident
    afterwards), so this is a plain synchronous read.
  2. An EXL3 table pins in row runs within the startup room. NgramTable.lock_runs(budget) mlocks whole runs of
    about 1 GiB of rows while the budget lasts, and the engine gives it the same room the whole-table lock uses
    (budget_bytes - total_bytes_estimate). A pack that stores its table as one tensor larger than the room (4.05 bpw:
    one 39 GB tensor) keeps most of it pinned (28 GiB on 4.05 at --context 131072), and the rest is the re-read part.
    HostTable and the other tables keep their all-or-nothing lock(). Windows returns 0, nothing pinned.
  3. The startup line says so: n-gram tables read alongside the weights (0.0s after them), read again after warm-up in 1.9s (28.0 GiB of it locked).

Lookups return the same bytes; this touches page residency only.

Scope relative to the n-gram read-ahead for prompt chunks (prefill hands later chunks' row ids to a background
MADV_WILLNEED): the two do not overlap. That change hides prompt-time faults one chunk ahead; this one makes the
table resident before the first request and keeps most of it pinned, which is what decode rounds (one or a few rows
a step, no look-ahead possible) need. With this change a prompt's read-ahead mostly finds its pages already resident.

Measured

One GB10 (DGX Spark), in-process, --context 131072, one load per arm. A reply's first drafted decode (192 tokens,
greedy, 3 fixed prompts) against the same reply decoded again right after, so both arms see the same tokens.
Table residency is from mincore just before the first reply.

Flash Next EXL3 resident code prose reasoning
4.05 bpw v0.6.1, new reply 75 % 81.4 tok/s 48.1 80.9
v0.6.1, same reply again 98.5 54.6 90.3
this PR, new reply 100 % (28 GiB pinned) 93.1 (+14 %) 54.6 (+14 %) 90.1 (+11 %)
this PR, same reply again 98.6 54.8 90.3
3.05 bpw v0.6.1, new reply 100 % (whole table locked: the room holds it) 100.5 46.5 93.7
this PR, new reply 100 % (whole table locked; the PR's path is skipped) 100.7 46.8 94.1

On 3.05 bpw the startup room holds the whole table, so v0.6.1 already locks it and this PR changes nothing there:
the same path, the same numbers within noise. The PR matters for packs whose table exceeds the room. 4.05 bpw is one;
any pack at a longer --context is another.
The remaining 5 % first-reply gap on 4.05's code prompt is not the table: its staging is back to 0.45 ms a round.

Staging time a verify round (host, gathering the rows): v0.6.1 5.0-6.8 ms on a new reply, this PR 0.3-0.5 ms.
Startup: +1.9 s on 4.05 here; MemAvailable after start 18 GiB with 28 GiB pinned.

Exactness

  • The emitted tokens are identical between v0.6.1 and this PR on every measured reply: 12 token hashes on each pack
    (3 prompts, greedy and seeded 1234, first decode and repeat).
  • tests/test_exl3_ngram_table.py::test_a_consolidated_table_pins_whole_row_runs_within_a_budget (new) checks that a
    table stored as one tensor pins whole row runs, never past the budget, and gathers the same bytes afterwards.
  • Existing suites: tests/test_exl3_ngram_table.py and tests/cuda/test_qwen4_exp_exl3.py with
    TENSORFOLD_EXL3_FLASHNEXT set to the 4.05 pack: 29 passed, 1 skipped (exllamav3 not importable here). This includes
    the cut model's windows == one-row steps and drafted == serial.

🤖 Generated with Claude Code

…EXL3 table pins in row runs within the startup budget

The startup read of the n-gram table runs alongside the weights, whose own reads (and the
warm-up's allocations) then push much of it back out of the page cache: on Flash Next EXL3
4.05 bpw between 0 and 90% of the 39 GB stayed resident after a load. Each new reply's first
lookups then fault the missing pages from disk one at a time, and its decode runs slower than
the same reply repeated.

- After warm-up, a table that is not locked is read once more (its own prefetch: the same
  bytes, seconds at startup).
- NgramTable.lock_runs(budget) pins whole runs of about 1 GiB of rows while the budget lasts,
  so a pack that stores its table as one tensor larger than the startup room (4.05 bpw: 39 GB
  against ~32 GB) keeps most of it resident; the engine gives it the same room the whole-table
  lock uses.
- The startup line says so: "read again after warm-up in N s (X GiB of it locked)".

Lookups return the same bytes: page residency only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ashhart

ashhart commented Oct 3, 2026

Copy link
Copy Markdown
Owner

Thank you, @grearjake-star. Reading the n-gram table back after warm-up and pinning an EXL3 table in page runs is in TensorFold 0.6.4, under your name: https://github.com/ashhart/TensorFold/releases/tag/v0.6.4

We adapted it: the pinning now goes through a shared module, tensorfold.cuda.ngram_pages, which rounds runs to pages, stays within the remaining startup budget, tracks overlapping runs and keeps a run that fails to unlock, without changing the mapped bytes or the gathers. On a DGX Spark the combined Flash Next stack (#202, #201, #254, #248, #268) matched one-token decoding in 12 of 12 cells, with prompt speed at parity. Thank you for the careful work.

@grearjake-star

Copy link
Copy Markdown
Contributor Author

Thank you for reviewing this, @ashhart, and for saying where it would go on Zig. For GB10 owners on the CUDA engine, this change now lives in our DGX Spark fork, SparkFold (https://github.com/grearjake-star/TensorFold/tree/spark-exl3; 0.6.5 + our GB10 work, CUDA-only), so it can be used today. The n-gram table read-back after warm-up and the EXL3 row-run pin are in SparkFold; the pin re-pins when memory frees.

Receipts for the fork as a whole (exactness, decode c1-c8, prompt speed) are in #286. If the Zig engine wants any of it later, it is all Apache-2.0 and we are glad to help port it.

Written with AI assistance (Claude Code).

@grearjake-star
grearjake-star deleted the upr-ngram-reread branch October 24, 2026 01:23
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