Skip to content

fix(inference): bound the Qwen 3.5 long-prompt prefill peak - #3054

Merged
inureyes merged 3 commits into
mainfrom
fix/issue-3045-prefill-peak
Nov 14, 2026
Merged

inureyes merged 3 commits into
mainfrom
fix/issue-3045-prefill-peak

Conversation

@inureyes

@inureyes inureyes commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

Summary

A 28K-token Qwen 3.5 prefill peaked far above weights plus KV on Metal, and the server peaked at the single-pass level although its default chunk is 2048 tokens. Two causes:

  • Qwen35Model, Qwen3NextModel and the Qwen35VLModel wrapper (the published checkpoints load as the VLM) did not override forward_last_logits*, so every prefill piece projected all of its positions through the LM head before the engine sliced the last row. They now slice the sampled hidden row before the final norm and LM head.
  • PrefillPlan ran each prompt-cache snapshot segment as one forward whatever the chunk size. The history boundary of a chat prompt sits a few tokens before its end, so the server prefilled almost the whole prompt in one pass. A segment is now cut into unpadded pieces of at most the chunk size that end on the boundary. The snapshot and the pieces after the boundary (what a prompt-cache hit replays) are unchanged. In a chunked plan every piece now counts as a chunk and emits prompt_progress, and the between-tick block reservation includes the padding of the piece that runs right after a boundary piece in the same tick (review follow-up).
  • MLXCEL_MEMORY_LIMIT stays soft by decision and is documented as such: past it MLX throttles evaluation and trims its cache but never refuses an allocation, so a hard cap would need a hook MLX does not have and would only fail the request.

Per-stage attribution (28000 tokens, MLX peak above the stage's start)

Stage 4B (GiB) 27B (GiB)
embedding 0.17 0.34
causal mask 3.65 3.65
one linear-attention layer, max 3.53 5.30
one full-attention layer, max 6.18 6.52
final norm 0.13 0.27
lm_head over every position 12.95 12.95
sampling 0.00 0.00
lm_head over the last position 0.00 0.00

Before and after (M3 Ultra, peak footprint, interleaved A/B/B/A in one GPU lock)

Path qwen3.5-4b-4bit qwen3.8-27b-4bit
CLI single pass 44.39 to 30.13 GB, 18.91 to 17.11 s 78.47 to 64.20 GB, 89.41 to 86.25 s
CLI 2048 chunks 11.00 to 8.97 GB, 15.51 to 13.89 s 27.83 to 25.80 GB, 77.94 to 74.88 s
server, --parallel 1 44.57 to 9.10 GB, TTFT 18.95 to 13.92 s 78.64 to 25.93 GB, TTFT 89.23 to 74.87 s

The server peak is now within 1.5% of the chunked CLI. Pairs agreed within 0.5 s; load averages are in the CSV. Greedy output is byte-identical to main in every cell, both repetitions. The rest of the single-pass peak is the causal array mask and one lazy graph's in-flight attention intermediates (#3051). Full data and rerun commands: docs/benchmark_results/qwen35-prefill-peak-m3ultra-2026-11-15.md.

Validation

  • New tests: qwen3_5_prefill_memory_tests (last-row logits equal the full forward's row, including a continued piece; peak stays under a quarter of the full logits at 1024 x 131072), plan tests for chunked segments and in-segment continuations, updated scheduler and MTP partition expectations.
  • prefill_plan, server::batch (608), prompt_cache, gemma4_mtp, qwen3_5_tests, qwen3_next pass; clippy (mlxcel, mlxcel-core lib and tests), fmt and the five contract checks pass.
  • Not covered on M5; no M5-specific code path is touched.

Closes #3045

A 28K-token Qwen 3.5 prefill peaked far above weights plus KV on Metal, and the server peaked at the single-pass level despite its 2048-token default chunk.

- Qwen35Model, Qwen3NextModel and the Qwen35VLModel wrapper now override forward_last_logits*, slicing the sampled hidden row before the final norm and LM head. The full-sequence logits (12.95 GiB at 28K positions, 248K vocab) were the largest single prefill buffer.
- PrefillPlan cuts each prompt-cache snapshot segment into unpadded pieces of at most the chunk size that end on the boundary. The history boundary sits a few tokens before a chat prompt's end, so the server used to prefill almost the whole prompt in one forward.
- MLXCEL_MEMORY_LIMIT stays a soft MLX limit and is documented as one.

M3 Ultra, 28K tokens, interleaved A/B: server peak 44.6 to 9.1 GB (4B) and 78.6 to 25.9 GB (27B), within 1.5% of the chunked CLI; CLI single pass 44.4 to 30.1 GB and 78.5 to 64.2 GB; output identical. Details in docs/benchmark_results/qwen35-prefill-peak-m3ultra-2026-11-15.md.

Closes #3045
@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:high High priority area:inference Generation, sampling, decoding (incl. speculative, DRY) platform:macos macOS (Apple Silicon) specific labels Nov 14, 2026
With snapshot segments chunked, the piece that ends on a boundary is a chunk like the others: it now counts toward mlxcel_batch_prefill_chunks_total and emits prompt_progress. A continuation can also land on that piece, and run_prefill_pieces runs the next piece in the same tick, so chunked_prefill_reserved_blocks adds that next piece's tile padding to the reservation held between ticks.

Refs #3045
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:inference Generation, sampling, decoding (incl. speculative, DRY) platform:macos macOS (Apple Silicon) specific priority:high High priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(inference): long-prompt prefill peaks far above weights plus KV on Metal

1 participant