Repository navigation
dsv41: prefill chunks before the kept rows stop at layer 20's cache side - #44
Merged
Merged
Conversation
With prompt-suffix pruning, a prefill forward that ends before the first row layer 21 keeps (every chunk but those holding the last ~2.4k prompt rows) feeds nothing exact from layer 20's output on: layers 21-39 ran one row each there, and layer 20 ran its query side, index scoring, attention, wo and MoE on every row. Such a forward now runs layer 20's cache side only -- attn_norm(hc_pre(h)), the window kv, the compressor and the index keys, in the same N-row calls as the full layer -- and stops; the windows of layers 21-39 restart, as pruning restarts them. A forward that reaches the kept rows runs the whole layer, so every row the prompt end reads is computed by the same calls as before: the logits, return_hidden rows and every lane at the prompt end are bit-identical. VMLX_DSV41_KV_SOURCE_TRIM=0 restores the old path. Tests: tests/dsv41_kv_source_trim_test.py (tiny model, served-lane chunking at non-aligned lengths, a partial prefix hit, image rows; controls: a one-token change is seen and a cut that reaches the kept rows is caught).
Owner
Author
|
In the live MLX Studio proof. jang_tools
Results with the numbers: jjang-ai/vmlx#362 and jjang-ai/vmlx#363. |
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.
What
With prompt-suffix pruning (
prompt_end), layers 21-39 run only the trailing1 + (40 - i) * 127rows a prompt's endstill reads. A prefill forward that ends before the first row layer 21 keeps -- every chunk except those holding the last
~2.4k prompt rows -- therefore reads nothing exact from layer 20's output on. Until now such a forward still ran layer 20
in full (query side, index scoring, attention,
wo, MoE on every row) and then layers 21-39 and the head on one roweach.
Such a forward now runs only layer 20's cache side --
attn_norm(hc_pre(h)), the window kv, the compressor and the indexkeys, in the same N-row calls as the full layer -- restarts the windows of layers 21-39 (as pruning restarts them) and
stops. A forward that reaches the kept rows runs the whole layer as before.
Why not trim the rows of a chunk that holds some kept rows: MLX's projections round differently at other row counts, so
running the query side / MoE on fewer rows would change the kept rows in the last bits. Cutting only whole forwards keeps
every row the prompt end reads on the exact same calls.
VMLX_DSV41_KV_SOURCE_TRIM=0restores the old path.Exactness
Bit-identical at the prompt end: logits, every lane of every layer (window, compressed pool, index keys, pending tails),
the DSpark draft windows and the greedy continuation, on the plain lane, a partial prefix hit and the DSpark lane, at
1,382 / 8,269 / 32,845 / 100,190 tokens, against origin/main on the real JANGHT1.4 bundle (and JANG_2K on the Ultra).
A one-token prompt change is detected. Unit tests:
tests/dsv41_kv_source_trim_test.py(tiny model; includes a controlwhere a cut that reaches the kept rows is caught).
Speed
Cold prefill through the served DSpark lane (draft resident, 1024-row chunks), trim on vs off interleaved in one process,
a warm probe per length discarded, 3 rounds, medians, M5 Ultra at 1.61 GHz (spread +-0.5 %):
M5 Max (per GHz, closed lid): +3.0-3.8 % at 6.5-8.3k, +6.0-9.3 % at 26.5-32.8k, +7.7-8.4 % at 100k. Prompts of up to
~2.4k tokens are unchanged (no forward qualifies).
Served path
vmlx_engine.cli servewith paged + block-disk cache, alternating launches against origin/main: identical greedy anddecode text, identical multi-turn
cached_tokens, partial prefix hit, disk-cache restore after a restart, and DSparkacceptance / tok/s.
Tests
tests/dsv41_*/dsv4_*/test_dsv4_*: same pre-existing failures as origin/main (7, M5 Max decode-fusion), allelse passes; 12 new tests pass.
18 failed, 53 skipped on both, the same failure ids.
VMLX_DSV41_DSPARK=0, plainSingleBatchGeneratorlane), branch vs origin/main:cached_tokensare equal (8,307 / 10,544), including the partial-hit cold control;