Skip to content

dsv41: prefill chunks before the kept rows stop at layer 20's cache side - #44

Merged
jjang-ai merged 1 commit into
mainfrom
perf/dsv41-prefill-kv-source-trim
Nov 1, 2026
Merged

jjang-ai merged 1 commit into
mainfrom
perf/dsv41-prefill-kv-source-trim

Conversation

@jjang-ai

Copy link
Copy Markdown
Owner

What

With prompt-suffix pruning (prompt_end), layers 21-39 run only the trailing 1 + (40 - i) * 127 rows a prompt's end
still 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 row
each.

Such a forward now runs only layer 20's cache side -- attn_norm(hc_pre(h)), the window kv, the compressor and the index
keys, 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=0 restores 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 control
where 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 %):

bundle 6.5k 8.3k 26.5k 32.8k 100k
JANGHT1.4 +3.2 % +3.1 % +5.6 % +6.0 % +9.2 %
JANG_2K +2.8 % +6.1 % +5.9 %

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 serve with paged + block-disk cache, alternating launches against origin/main: identical greedy and
decode text, identical multi-turn cached_tokens, partial prefix hit, disk-cache restore after a restart, and DSpark
acceptance / tok/s.

Tests

  • tests/dsv41_* / dsv4_* / test_dsv4_*: same pre-existing failures as origin/main (7, M5 Max decode-fusion), all
    else passes; 12 new tests pass.
  • vMLX DSV4 / DSV4.1 / JANGT / prefill test files (107 files) against this branch vs origin/main jang-tools: 1,454 passed,
    18 failed, 53 skipped on both, the same failure ids.
  • JANG_2K served with DSpark off (VMLX_DSV41_DSPARK=0, plain SingleBatchGenerator lane), branch vs origin/main:
    • greedy identity at 1,382 / 8,269 / 32,845 is equal;
    • every turn's text and cached_tokens are equal (8,307 / 10,544), including the partial-hit cold control;
    • AR decode 46.97 vs 47.08 tok/s.

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).
@jjang-ai

jjang-ai commented Nov 1, 2026

Copy link
Copy Markdown
Owner Author

In the live MLX Studio proof. jang_tools integration/mlxstudio-final-20261031 = 06f3f2ae (main + #44 + #47, both merged cleanly) ran under vMLX main + #356 #361 #362 #363 #364 in the Electron app, on the M5 Max and the M5 Ultra.

  • DSV4.1 prefix cache: partial hits (an edited earlier message) restored 12,130-12,156 of about 12.15k tokens. Tool-loop steps prefill 26-29 tokens from the generation-end entry. An SSD restore after an app restart restored 12,201-12,227 tokens.
  • DSV4.1 prefill vs main (12,034-token turn, ABBA): Studio JANG_2K +10.4 %, M5 Max JANGHT +15.5 %. Decode is equal.

Results with the numbers: jjang-ai/vmlx#362 and jjang-ai/vmlx#363.

@jjang-ai
jjang-ai merged commit a45e364 into main Nov 1, 2026
2 of 3 checks passed
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.

1 participant