Repository navigation
perf: Flash Next prefill path on the grid, 1.4-2.5x faster prompts, still exact - #29
Closed
quigles1977 wants to merge 1 commit into
Closed
quigles1977 wants to merge 1 commit into
quigles1977 wants to merge 1 commit into
Conversation
…till exact 0.3.4.1 prefills Flash Next prompts through MLX's forward on the 2,048-token grid. This adds a prefill path for prompt chunks of more than 16 rows on the same grid (TF_FLASH_PREFILL=0: MLX's forward): - Sparse attention through the decode's block selection and a grouped-query attention kernel (prefill.attention_rows_gqa) that reads only each row's selected blocks and tail; MLX's forward attends over every key, so its cost grows with the context. - Hyper-connections through the fused decode's kernels (prefill_hc.py), with MLX's matmuls between them; the n-gram rows through the decode's lookup tables. - MLX's own 4-bit kernels with prefill tiles (prefill_mm.py): the same bits, and they step aside where MLX splits K, on GPUs with the M5's tensor units, and if a once-a-process check against MLX fails. DeltaNet's in-projections as one stacked matmul; the MoE sums its expert-sorted outputs without unsorting them. - Slices of 2 layers queued ahead of the GPU, so peak memory stays near the weights and caches (113-115 GiB). - Prefix-snapshot keys name the forward that prefilled them (FlashNext.prefill_key). M3 Ultra, server at --context 153600, cold time to first token at 30k / 100k / 145k 43.9 / 215.1 / 386.7 s -> 31.7 / 105.8 / 157.3 s; follow-up turns 3.08 / 7.97 / 10.39 s -> 1.95 / 3.17 / 3.32 s. Resumed conversations stay byte-identical to the same conversation sent fresh (3 of 3 at 9k, 30k and 61k tokens). Next-1,024-token NLL and top-1 stay within MLX's forward's rounding noise at 8k, 32k and 100k; decode is unchanged and drafted replies stay byte-identical to serial. Docs: the recipe's Prefill section, and the recipe book's stale "A known limit" rewritten as "Resumed prompts". Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Contributor
|
It works great! I test it combined with PR #30 |
Owner
|
Thanks @quigles1977 for the Flash Next prefill path. It's in final testing for 0.3.5, which ships tonight, as a commit authored by you, combined with 0.3.5's sparse prompt attention. This PR closes when the release is published. |
ashhart
pushed a commit
that referenced
this pull request
Sep 27, 2026
0.3.4.1 prefills Flash Next prompts through MLX's forward on the 2,048-token grid. This adds a prefill path for prompt chunks of more than 16 rows on the same grid (TF_FLASH_PREFILL=0: MLX's forward): - Sparse attention through the decode's block selection and a grouped-query attention kernel (prefill.attention_rows_gqa) that reads only each row's selected blocks and tail. - Hyper-connections through the fused decode's kernels (prefill_hc.py), with MLX's matmuls between them; the n-gram rows through the decode's lookup tables. - MLX's own 4-bit kernels with prefill tiles (prefill_mm.py): the same bits, and they step aside where MLX splits K, on GPUs with the M5's tensor units, and if a once-a-process check against MLX fails. DeltaNet's in-projections as one stacked matmul; the MoE sums its expert-sorted outputs without unsorting them. - Slices of 2 layers queued ahead of the GPU, so peak memory stays near the weights and caches. - Prefix-snapshot keys name the forward that prefilled them (FlashNext.prefill_key). M3 Ultra, server at --context 153600, cold time to first token at 30k / 100k / 145k 43.9 / 215.1 / 386.7 s -> 31.7 / 105.8 / 157.3 s; follow-up turns 3.08 / 7.97 / 10.39 s -> 1.95 / 3.17 / 3.32 s. Resumed conversations stay byte-identical to the same conversation sent fresh (3 of 3 at 9k, 30k and 61k tokens). From quigles1977's pull request #29, written against 0.3.4.1 and carried onto release-0.3.5 as submitted. 0.3.5 split the Flash Next kernels into modules, so the PR's calls into kernels.py now name base, hc, attention and embed. Where 0.3.5 changed the same lines, both sides stay: the PR's prompt attention runs before 0.3.5's selection-kernel path, and the n-gram lookup tries the decode's tables before 0.3.5's memory-mapped host tables. The README keeps 0.3.5's wording. The commits after this one reconcile the two prompt attentions and carry the maintainers' corrections.
ashhart
added a commit
that referenced
this pull request
Sep 27, 2026
…1 before M5 - #29's prefill path combined with the sparse prompt attention: cold prefill faster than 0.3.4.1 from 1k to 128k, and the pause before the first decoded token after a long prompt is gone. - Before M5, Flash Next's projections and hyper-connections use 0.3.4.1's per-row kernels at every row count, so single-stream decode matches 0.3.4.1 and concurrent streams still equal their solo runs. Fixes #27
Owner
|
Thanks @quigles1977! This landed in 0.3.5 as 97a7414, authored by you, and ships in https://github.com/ashhart/TensorFold/releases/tag/v0.3.5. |
This was referenced Oct 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

0.3.4.1 prefills Flash Next prompts through MLX's forward on the 2,048-token grid. It's exact across resumes, but
MLX's forward attends over every key, so prompts slow down as the context grows, and so do follow-up turns. A
follow-up turn prefills again from the last grid point. This PR adds a prefill path for prompt chunks of more than
16 rows. It runs on the same grid with the same exactness, and
TF_FLASH_PREFILL=0goes back to MLX's forward.Measured on an M3 Ultra (256 GB, MLX 0.32.2) with the 4-bit MTP conversion, through the server at
--context 153600 --prompt-cache-gib 24. The follow-up is a turn of about 440 new tokens on the cached prefix,which prefills 1.8k–2.2k tokens again from the grid point.
Peak memory stays near the weights and caches, as 0.3.4.1 intends. In process at 32k and 100k, the prefill path
peaks at 113 / 115 GiB, against 110 / 113 GiB for MLX's forward.
What changed
Sparse attention in prompt chunks (
SparseAttention._sparse_rows). A chunk past the indexer's budget usesthe decode's own block selection (
index_select: fp32 scores, radix top 512, ties to the lower block). A newgrouped-query kernel,
prefill.attention_rows_gqa, reads only each row's selected blocks and tail:Hyper-connections through the fused decode's kernels (
prefill_hc.py), with MLX's matmuls between them. Then-gram rows come from the decode's lookup tables, bit-identical.
MLX's own 4-bit kernels with prefill tiles (
prefill_mm.py, compiled at runtime from the headers MLX ships),bit-identical to MLX where they run:
qmm_t_implwith 64×64 or 64×32 tiles;affine_gather_qmm_rhswith its alignment as template arguments (2–7% faster).They step aside, leaving the matmul to MLX:
DeltaNet's four in-projections go through the fused decode's stacked matrix as one matmul.
The MoE sorts its rows by expert and sums the sorted outputs without unsorting them.
Slices of 2 layers are queued ahead of the GPU (
prefill_hc.QUEUE_LAYERS). This mirrors 0.3.4.1's queuefor MLX's forward, and the bits are the same however the graph is sliced (checked on the real model: hidden
states and all 121 cache arrays).
Prefix-snapshot keys name the forward that prefilled them (
FlashNext.prefill_key, via aprefill_keyhook infamilies.kernel_version), so a snapshot from the other forward is never resumed.Docs:
Exactness
Resumed equals fresh. I sent three two-turn conversations: 9k, 30k and 61k tokens of code, thinking on,
seeded sampling, 900-token replies. Each turn 2 resumed from turn 1's cache was compared with the same turn 2 sent
to a restarted server with nothing cached, including no saved session snapshots.
Quality. The prefill path rounds differently from MLX's forward. Attention sums keys in another order, and
the hyper-connections use the fused decode's arithmetic. So the check is quality against MLX's own rounding
noise (its 1,024-token chunks instead of 2,048). The measure is teacher-forced NLL and top-1 accuracy on the next
1,024 tokens of real text:
Decoding is unchanged. Drafted replies were byte-identical to
"draft": falseat 40k, both greedy and seeded.Without drafts, prose and code decode at 89.1 / 88.8 tok/s, against 89.0 / 88.7 on 0.3.4.1.
Validation
tests/test_qwen4_exp_prefill.pyhas 19 GPU tests:forward. It is mutation-checked: dropping the PLE term, or offsetting the indexer's RoPE, fails it.
edit_filetool call on a 25k-token prompt;Not measured, and possible next steps
kernels still apply; it's worth a run on the M5 Max.
1,040–1,073 tok/s cold, and follow-ups at 100k–145k took 2.4–2.6 s. The 2,048 grid is kept here to match the
engine.
🤖 Generated with Claude Code