Repository navigation
fix(inference): bound the Qwen 3.5 long-prompt prefill peak - #3054
Merged
Merged
Conversation
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
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
inureyes
marked this pull request as ready for review
November 14, 2026 20:55
This was referenced Nov 15, 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.
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,Qwen3NextModeland theQwen35VLModelwrapper (the published checkpoints load as the VLM) did not overrideforward_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.PrefillPlanran 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 emitsprompt_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_LIMITstays 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)
Before and after (M3 Ultra, peak footprint, interleaved A/B/B/A in one GPU lock)
--parallel 1The 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
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_nextpass; clippy (mlxcel,mlxcel-corelib and tests), fmt and the five contract checks pass.Closes #3045