#4141·omlx

Batched Lightning MTP on qwen4_exp at long context: the ragged rollback rolls every row's whole K/V bank each verify cycle (~110 ms at 8 × 100K)

Author: AureliusClawCreated Oct 1, 2026Updated Oct 1, 2026

Environment: M5 Ultra 256 GB, macOS 26, Qwen3.8-Flash-Next-oQ4e (MTP head), oMLX main 3a400dd2 plus #3963, #4030 and #4070 (batched QSA decode/verify), --max-concurrent-requests 10. Still applies to main 87460f4d (v0.7.0): the rollback path below is unchanged there (mlx_vlm/speculative/cache_state.py identical, qwen4_exp/cache.py untouched upstream), and the switch build re-measured on it gives 112 / 117 / 156 / 224 tok/s.

Symptom

At long context, Lightning MTP makes batched decode slower than plain decode as soon as two requests share a batch. Steady aggregate decode, 100K-token shared prompt + 2K unshared tokens per session, temperature 0.7, 768 tokens per session:

sessions 1 2 4 8
MTP on 115.5 107.1 123.2 150.9
MTP off 93.5 124.1 161.3 228.3

(With stock main, both rows are dominated by the batched QSA attention cost that #4070 removes; this issue is what is left.)

Cause 1: the vector rollback's finalize() is O(context) per cycle

mlx_vlm/speculative/cache_state.py _trim_append_caches rolls a batched verify back with trim(block_size - max_retained) followed by prepare(right_padding=[...]) + finalize() whenever rows accepted different draft counts — which, with several rows, is almost every cycle. BatchKVCache.finalize (qwen4_exp/cache.py, dynamic_roll) then rolls every row's entire K and V bank right by its rejected count, and BatchQSAKVCache.finalize rolls the raw indexer bank too, in all 12 QSA layers.

Measured in isolation (12 QSA layers at the served shapes, 4-bit projections, rows of 100K–114K tokens, 4-row verify windows, random ragged acceptance, every rolled array evaluated):

rows verify attention (with #4070) rollback
2 10.5 ms 21.5 ms
8 33.1 ms 110.3 ms

The roll moves ~all K/V bytes of the batch once per cycle; a plain decode step at 8 × 100K costs ~29 ms end to end.

Cause 2: calibration/park churn rebuilds the batch

The batch policy (omlx/patches/mlx_lm_mtp/batch_policy.py) does park MTP when it loses (Lightning MTP batch parked: rows=4 standard_ms=20.2), but on a 100K run the log shows 113 batch-standard-calibration, 61 batch-parked and repeated re-entry probes. Each switch reconciles the batch cache between MTP and standard state; at 8 × 100K that reconciliation is itself a multi-GB copy.

Possible direction

A per-row rotation offset in the batched QSA K/V (and indexer) banks would make the ragged commit O(1): finalize() after a rollback only advances each row's rotation and left padding, appends write at (logical − rotation) mod capacity, and the gathered batched arm (#4070) already addresses rows by explicit token indices. Paths that need a physically aligned bank (the dense masked fallback, filter/extend/extract/merge/state) would materialize it once with the existing roll. Happy to prototype this if the direction is acceptable.

What we run meanwhile

A one-line switch in _model_supports_batch_mtp (omlx/patches/mlx_lm_mtp/batch_generator.py): with OMLX_MTP_MULTI_REQUEST=0 it returns false, so batches of two or more requests decode ordinarily while a request running alone keeps singleton Lightning MTP (the multirow marker's singleton recovery resumes it once a batch drains). Same setup as above:

sessions 1 2 4 8
MTP on, OMLX_MTP_MULTI_REQUEST=0 109.5 122.7 155.5 223.6

It sidesteps both causes rather than fixing them, so it's not a PR here. If you'd take it, as an environment variable or as a per-model setting next to mtp_enabled, I'll open one: AureliusClaw/omlx mtp-multi-request-switch.

Reproduction

bench.py from our harness (--legs decode --decode-prefix-tokens 100000 --decode-stagger-tokens 2000 --concurrency 1,2,4,8 --decode-tokens 768) against a server with MTP on and off; microbenchmark script attached on request.