Skip to content

perf: Flash Next reads a prompt chunk's n-gram rows on 16 threads (same bytes) - #73

Closed
devopsfortunato wants to merge 1 commit into
ashhart:mainfrom
devopsfortunato:ngram-parallel-gather
Closed

devopsfortunato wants to merge 1 commit into
ashhart:mainfrom
devopsfortunato:ngram-parallel-gather

Conversation

@devopsfortunato

Copy link
Copy Markdown
Contributor

TL;DR: faster prompts with the default settings, no setting to change, same bits. Flash Next on CUDA now reads a prompt's n-gram rows on 16 threads instead of one. On a DGX Spark with a 131,072-token cache, short and mid-size prompts get up to ~25% faster when part of the n-gram tables is not in the page cache, and stay the same when all of it is; decode is unchanged. Independent of #40.

The problem

HostTable.gather copied a prompt's n-gram rows with one fancy index per file. Rows whose pages are not in the page cache are then read from disk one page fault at a time, while the prompt is staged. On a Spark with a 131,072-token cache the ~31 GB n-gram tables barely fit in the page cache, and how much of them stays cached after the start-up prefetch varies from start to start (29-32 GB here), so a prompt of new text often pays hundreds to thousands of page faults.

The change

A gather of 1,024 rows or more (a prompt chunk of 64 tokens or more) copies its rows on up to 16 threads, at least 512 rows each. numpy's fancy indexing releases the GIL, so the page faults overlap: staging costs about 0.07 ms per page fault instead of 0.25 ms. Decode steps (a few rows) keep the single-threaded copy. The copied bytes are the same.

Results

One DGX Spark, MLX 4-bit checkpoint, 131,072-token cache, default 2,048-row chunks. A fresh process each, with the n-gram files dropped from the page cache before it (the engine's start-up prefetch refills what fits), release and branch alternated four times, every prompt new text; prompt to first token:

Prompt Release (5 runs) This PR (4 runs)
606, first after start 0.39-0.55 s 0.40-0.42 s
606, later 0.37-0.44 s 0.36-0.39 s
1,118 0.56-0.73 s 0.55-0.59 s
4,186 1.95-2.31 s 1.92-1.97 s
8,192 3.65-4.29 s 3.60-3.70 s
32,768 15.1-16.5 s 15.06-15.21 s
120,000 65.4-65.7 s 64.7-65.3 s

The best cases are the same (a release start that happened to cache every table page); the slow starts go away.

Through the server, back to back in one session (median of 3):

Release This PR
TTFT, 560 tokens 0.44-0.45 s 0.39-0.41 s
TTFT, 1,070 tokens 0.62-0.64 s 0.56-0.59 s
TTFT, 4,140 tokens 2.03-2.10 s 1.98-2.00 s
decode, 512 tokens greedy (prose / code) 53.5 / 74.1 tok/s 53.5 / 73.9 tok/s

Checks

  • The threaded copy is byte-identical to the single-threaded one: new test test_a_prompt_chunks_gather_on_threads_gives_the_same_bytes (1, 2 and 16 threads), and 1,000 to 131,072 random rows of the real table.
  • Teacher-forced NLL through the prompt and the decode path identical to the release on all 119,980 per-token values (5 documents, 2,048- and 8,192-row chunks).
  • 55 replayed benchmark replies byte-identical to the release's; drafted vs serial 9/9; needles 9/9 at 6k/16k/60k.
  • tests/test_ple_ssd.py, tests/test_ngram_read_ahead.py: 54 passed (4 Metal-only skipped). CUDA Flash Next suite passed (test_glm_nonfit_refuses_before_weight_load fails the same way on the unmodified release).

…me bytes

HostTable.gather copied a prompt's n-gram rows with one fancy index per file, so rows whose pages were not in the
page cache were read from disk one page fault at a time. On a DGX Spark with a 131,072-token cache the ~31 GB tables
barely fit in the page cache, and how much of them the start-up prefetch leaves cached varies from start to start.

A gather of 1,024 rows or more (a prompt chunk of 64 tokens or more) now copies on up to 16 threads, 512 rows or
more each. numpy's fancy indexing releases the GIL, so the page faults overlap: staging costs about 0.07 ms a fault
instead of 0.25 ms. Decode steps keep the single-threaded copy. The bytes are the same.

One Spark, 131,072-token cache, default 2,048-row chunks, a fresh process each with the n-gram files dropped from
the page cache before it (the start-up prefetch refills what fits), release and branch alternated, every prompt new
text: 606 tokens 0.37-0.55 -> 0.36-0.42 s, 1,118 tokens 0.56-0.73 -> 0.55-0.59 s, 4,186 tokens 1.95-2.31 ->
1.92-1.97 s, 8,192 tokens 3.65-4.29 -> 3.60-3.70 s, 32,768 tokens 15.1-16.5 -> 15.1-15.2 s, 120,000 tokens
65.4-65.7 -> 64.7-65.3 s. Decode unchanged.

Checked: the threaded copy is byte-identical to the single-threaded one (new test, and 1,000 to 131,072 random rows
of the real table); teacher-forced NLL through the prompt and the decode path identical to the release on all
119,980 values; 55 replayed benchmark replies byte-identical; drafted vs serial 9/9; needles 9/9.
@ashhart

ashhart commented Sep 29, 2026

Copy link
Copy Markdown
Owner

Thank you, @MovieMaker93. The threaded n-gram reads are in 0.3.6.3 under your name (2c984cf1). We added tests for
eight gathers at once, a failed copy raising cleanly, and the pool's threads ending when a model unloads. Your
numbers on a cold page cache are in the release notes. Thanks again, and for #40 before it.

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