Repository navigation
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
Conversation
…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]>
|
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, |
|
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). |
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 GBagainst 39 GB.
From then on, each new reply's first n-gram lookups fault the missing pages from disk one at a time (
MADV_RANDOMmaps). 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:
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
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_WILLNEEDpass on the missing pages brought back almost nothing in my tests (35 GiB asked, ~3 % residentafterwards), so this is a plain synchronous read.
NgramTable.lock_runs(budget)mlocks whole runs ofabout 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.HostTableand the other tables keep their all-or-nothinglock(). Windows returns 0, nothing pinned.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 thetable 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.
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
--contextis 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
(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 atable stored as one tensor pins whole row runs, never past the budget, and gathers the same bytes afterwards.
tests/test_exl3_ngram_table.pyandtests/cuda/test_qwen4_exp_exl3.pywithTENSORFOLD_EXL3_FLASHNEXTset to the 4.05 pack: 29 passed, 1 skipped (exllamav3 not importable here). This includesthe cut model's windows == one-row steps and drafted == serial.
🤖 Generated with Claude Code