Skip to content

perf: opt-in wider Flash Next prompt chunks (TENSORFOLD_PREFILL_ROWS), default unchanged - #40

Closed
devopsfortunato wants to merge 2 commits into
ashhart:mainfrom
devopsfortunato:prefill-speedup-pr
Closed

devopsfortunato wants to merge 2 commits into
ashhart:mainfrom
devopsfortunato:prefill-speedup-pr

Conversation

@devopsfortunato

@devopsfortunato devopsfortunato commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

TL;DR: nothing changes by default. TENSORFOLD_PREFILL_ROWS=8192 is an opt-in that makes prompts from 4k tokens prefill 8-18% faster when the n-gram tables fit in RAM (e.g. a Spark with a 32k context), with short prompts unchanged and the same bits. At a 131k context it makes short prompts slower, so keep the default there.

Rebased on v0.3.6.1.

What this changes

  • TENSORFOLD_PREFILL_ROWS sets how many prompt rows Flash Next prefills per chunk on MLX checkpoints (256 to 16,384).
  • The default stays 2,048: identical to the release (the same startup estimate, 85.78 GiB at a 131,072-token cache, and the same per-token NLL on all 119,980 values checked).
  • EXL3 packs always keep 2,048 (their n-gram staging is sized for it).
  • The engine passes the chosen rows to the startup estimate and to its prompt buffers (single stream and --parallel), so the two cannot disagree.

A row's bits never depend on its chunk, so the setting changes speed and memory only.

Why opt-in and not a new default

Wider chunks run the same kernels on more rows at once, but 8,192-row buffers take 2.4 GiB more. On a Spark that memory comes out of the page cache holding Flash Next's ~31 GB n-gram tables. When the tables still fit, it is a pure win; when they do not, prompts of new text read n-gram rows from disk while they are staged, and short prompts get slower.

Results

One DGX Spark, MLX 4-bit checkpoint. A fresh process each, release and branch alternated twice, every prompt new text; prompt to first token, in one process.

32,768-token cache (the tables stay cached): recommended

Prompt 2,048 rows (default) 8,192 rows
606 0.37-0.41 s 0.37-0.42 s same
1,118 0.55-0.59 s 0.56-0.61 s same
4,186 1.97-1.98 s 1.72-1.81 s -8 to -13%
8,192 3.67 s 3.13-3.15 s -14%
24,000 11.10-11.11 s 9.09-9.26 s -17 to -18%

131,072-token cache (the tables no longer fit): keep the default

Prompt 2,048 rows (default) 8,192 rows
606 0.39-0.44 s 0.48-0.71 s slower
1,118 0.56-0.64 s 0.70-1.02 s slower
4,186 2.02-2.06 s 2.31-2.66 s slower
8,192 3.74-3.81 s 4.11-4.32 s slower
32,768 15.4-15.8 s 14.2-15.0 s -5 to -8%

Major page faults for a 606-token prompt at 131k: 48-161 (default) vs 557-1,566 (8,192 rows). When the prompt's n-gram rows are already cached (repeated text), 8,192 rows are 14-26% faster at 131k too (8k 2,236 -> 2,552 tok/s, 120k 1,939 -> 2,299).

Memory: 8,192 rows add 2.8 GiB to the startup estimate and 2.4 GiB to the peak allocation.

Checks

  • Default: startup estimate and teacher-forced NLL (prompt and decode paths, 5 documents) identical to the release.
  • 8,192 rows against 2,048: the whole state after 6k-, 24k- and 120k-token prompts and 48 drafted tokens (greedy and sampled) identical; NLL identical on every token; 55 replayed benchmark replies byte-identical; drafted vs serial 9/9; needles 9/9.
  • CUDA Flash Next, EXL3, build and planner tests pass (test_glm_nonfit_refuses_before_weight_load fails the same way on the unmodified release). New test: test_indexed_prefill_rows_default_override_and_budget.
  • Tried and slower (same bits): 16,384-row chunks, 128-pair expert tiles, dense prompt matmuls with 2 or 4 groups per pipeline stage.

Usage

TENSORFOLD_PREFILL_ROWS=8192 tensorfold serve Vontra/Qwen3.8-Flash-Next-MLX-4bit-MTP --context 32768

@ashhart

ashhart commented Sep 27, 2026

Copy link
Copy Markdown
Owner

Thanks @MovieMaker93, great find: most of that prefill time really was spent on work whose results were never used. 0.3.5's CUDA engine reworks the same Flash Next prefill path, about 9x faster than 0.3.4.1 on one Spark, so we're comparing your six changes against it now. Anything that adds speed on top lands in 0.3.5.1 as a commit authored by you, and we'll post the numbers here.

@devopsfortunato

Copy link
Copy Markdown
Contributor Author

@ashhart Pushed one more commit (7c80822): launch settings only, for the long-prompt path and the prefill experts, bit-identical (checked kernel by kernel and on the model; new test included). Prefill is a bit faster on long prompts, one Spark, 131,072-token capacity:

Prompt Before (this PR's first commit) After
32k 910 tok/s 1,004 tok/s
65k 921 tok/s 993 tok/s
120k 910 tok/s 972 tok/s

Through the server, time to first token against the release: 7.7k tokens 53.6 s → 7.65 s, 32.8k 232 s → 32.8 s, 120k 977 s → 124 s.

…rom 8k tokens, same bits

Prefill rows never depend on their chunk, so a wider chunk changes speed and memory only. MLX checkpoints now
prefill in 8,192-row chunks; EXL3 packs keep 2,048 (their n-gram staging is sized for it). The engine passes the
chosen rows to the startup estimate (gdn_geometry / indexed_stream_geometry) and to its prompt buffers.

One Spark, 131,072-token cache, a fresh process each, 8,192-row chunks against 2,048: 8k tokens 2,236 -> 2,552
tok/s, 32k 2,010 -> 2,532, 65k 2,084 -> 2,495, 120k 1,939 -> 2,299; about unchanged up to 4k. Peak allocation
grows about 2.4 GiB.

Checked on the model: the whole state after 6k-, 24k- and 120k-token prompts and 48 drafted tokens after the 24k
prompt (greedy and sampled) equal the 2,048-row path's, and teacher-forced NLL through both the prefill and the
decode path is identical on every token of five 6,000-token documents. 16,384-row chunks were no faster; 128-pair
expert tiles and multi-group dense matmul stages (same bits) were slower.
@devopsfortunato devopsfortunato changed the title perf: Flash Next prefill on CUDA about 7x faster, byte-exact perf: Flash Next prompts prefill in 8,192-row chunks, 14-26% faster from 8k tokens, same bits Sep 28, 2026
@devopsfortunato

devopsfortunato commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Rebased on v0.3.6.1. Wider prefill chunks are now opt-in (TENSORFOLD_PREFILL_ROWS); the default stays 2,048, identical to the release. With 8,192 rows, prompts from 4k tokens prefill 8-18% faster when the n-gram tables fit in RAM (e.g. a Spark with a 32k context), with short prompts unchanged; at a 131k context short prompts get slower, so keep the default there. Tables in the description.

…; default back to 2,048

Measured again with prompts of new text, a fresh process each, release and branch runs alternated. At a
131,072-token cache on a Spark the 8,192-row buffers' 2.4 GiB comes out of the page cache that holds the n-gram
tables, and new-text prompts up to about 16k tokens got slower (606 tokens 0.39-0.44 -> 0.48-0.71 s, 8,192 tokens
3.74-3.81 -> 4.11-4.32 s, about 10x the major page faults); the previous commit's numbers reused cached text. With a
32,768-token cache the tables stay cached: 4k- to 24k-token prompts 8-18% faster, short prompts unchanged.

So the default is the release's 2,048 again (the same startup estimate and per-token NLL as the release), and
TENSORFOLD_PREFILL_ROWS (256 to 16,384; MLX checkpoints, EXL3 packs keep 2,048) opts in to wider chunks. The
startup estimate and the prompt buffers read the same value (geometry.indexed_prefill_rows).
@devopsfortunato devopsfortunato changed the title perf: Flash Next prompts prefill in 8,192-row chunks, 14-26% faster from 8k tokens, same bits perf: opt-in wider Flash Next prompt chunks (TENSORFOLD_PREFILL_ROWS), default unchanged Sep 28, 2026
ashhart pushed a commit that referenced this pull request Sep 28, 2026
From #40, which made Flash Next prefill on CUDA about 7x faster than v0.3.4.1, byte-exact. There, prompts
prefilled through the decode kernels in 64-row chunks at 128-145 tok/s on one DGX Spark, and most of that
time was work whose results were never used:

- expert kernels: a member tile with no rows returns before reading its expert, and member tiles are the
  fastest grid axis, so one expert's tiles share its weights in L2
- attention: partials and grid sized for the 2,051 keys a QSA row can read instead of the capacity;
  programs past a row's keys or complete blocks return
- prefill computes the head only on the prompt's last chunk
- 1,024-row prefill chunks; above 64 rows the distinct experts are grouped by a stable sort
- member-list widths above 16 rows rounded up to a power of two, and the prefill kernels compiled at start

Against v0.3.4.1 on one Spark at a 131,072-token capacity: 8k prompts 142 -> 1,008 tok/s, 32k 132 -> 993,
120k ~123 -> 914; through the server, a 7.7k-token prompt's first token after 7.9 s instead of 53.6 s.

0.3.5 reworked this path: 2,048-row prompt chunks, shared CUDA expert and matmul prefill kernels that
launch only populated member tiles, attention partials capped at the keys a row reads, and expert grouping
on the device. This commit keeps the change that still lands on it: the head runs on the final chunk
alone. Its test checks that any chunking of a prompt past the sparse attention budget gives the one-chunk
prefill's first token and state bit for bit. The start-up compile is ported separately for the current
prompt kernels.
@ashhart

ashhart commented Sep 28, 2026

Copy link
Copy Markdown
Owner

Thank you, @MovieMaker93. Running the head on the final chunk only shipped in 0.3.6.2 under your name (96c0b0e4). We also
built a startup warm-up from your idea, so the first 2k prompt now takes 1.26 s instead of 1.79 s. Replies are
byte-identical to 0.3.5 at 2k, 8k and 32k (9/9). We left out the attention launch settings: on our Sparks they
measured about 1% slower at 32k and 64k (2,449 against 2,475 tok/s at 32k). Thanks for digging into Flash Next's
prompt path!

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