Repository navigation
Qwen2.5-VL / Qwen2-VL: windowed prefill + state-threaded warm continuation (wires #420) - #448
Conversation
…ation Wires the ml-explore#399 mechanism through Qwen2.5-VL and Qwen2-VL per the ml-explore#420 checklist: getRopeIndex gains an offset-aware positionOffset, prepare consumes the incoming LMOutput.State and delegates warm/oversized prompts to a chunked prepareContinuation anchored at the carried rope delta, and the returned resume state threads the following turn through the existing decode reconstruction. Adds random-weight continuation tests for both models mirroring Qwen35ContinuationTests.
|
One thing worth calling out from the diff: this removes the note in |
davidkoski
left a comment
There was a problem hiding this comment.
Change looks good to me, thank you!
|
Thanks @davidkoski! That helps a lot — together with #475 this closes #443 across the whole Qwen family. Happy to follow up on anything post-merge. |
Qwen2.5-VL / Qwen2-VL: windowed prefill + state-threaded warm continuation (wires #420)
Proposed changes
#399 added windowed prefill and state-threaded warm cross-turn continuation to
Qwen3.5/3.6, fixing multi-turn M-RoPE drift for that family. It also widened
LanguageModel.prepareto thread an optionalLMOutput.State, but theremaining Qwen VLMs listed in #420 were only updated to accept and ignore that
state (
state _:). This PR wires the same mechanism through Qwen2.5-VL andQwen2-VL, so a warm KV cache is continued from its anchor instead of
recomputing M-RoPE positions from zero.
Without this, a warm continuation (multi-turn chat, tool-call restart, restored
prompt cache) silently recomputes positions from scratch: the post-image text
tail is placed at
cacheOffset + arangeinstead ofcacheOffset + ropeDelta + arange, dropping the delta the image accumulated. On dense single-imagegrounding at temperature 0 this surfaces as degenerate output.
Mirroring #399 exactly (not a bespoke path):
getRopeIndexgains an offset-awarepositionOffset(default0), appliedto the text-only positions, the image
llmPositions, and the returned ropedelta — so a new image's diverging t/h/w indices are computed from the warm
cache's anchor rather than zero. Same shape as Qwen3-VL's
getRopeIndex.preparenow consumes the incomingstateand delegates to a newprepareContinuationwhen the cache is already warmed (offset > 0) or theprompt is longer than
windowSize(default512). The continuation runs thevision tower + image→token merge once over the remainder, computes the
remainder's M-RoPE positions once from the seeded position anchor (cache offset
plus the rope delta carried in
state), then drives the language model forwardin chunks of
windowSize— bounding the full-attention scratch to[heads, chunk, L]instead of[heads, L, L]. The windowed loop usesasyncEvalper chunk + a trailingeval, matching the Gemma3 chunked-prefillpattern.
getRopeIndex delta − cacheOffset, so acaller threading state end-to-end continues the following turn through the
existing decode reconstruction branch. Decode stays caller-owned.
preparepath now passes token ids (rather than apre-embedded tensor) on the text-only branch, so a 2-D
[1, seq]textcontinuation — what a warm text turn produces — is handled without the prior
[.newAxis, .ellipsis]rank inflation. Image single-shot behaviour isunchanged.
The vision-embedding preamble is factored into small shared helpers
(
gatherVisionInputs,mergedVisionEmbeds) used by bothprepareandprepareContinuation. Batched inputs keep the single-shot path.Qwen3-VL is intentionally left for a follow-up. Its
preparethreadsper-layer deepstack features and a visual mask into the language model; a
windowed continuation would have to slice those in lockstep with the embeddings
and positions, which is beyond the drop-in Qwen3.5/2.5/2 pattern. Wiring it would
be structural surgery, so it is out of scope here and its #420 box stays
unchecked.
Tests
New
Tests/MLXLMTests/Qwen25VLContinuationTests.swiftmirrorsQwen35ContinuationTests, run against tiny random-weight configs for bothwired models (no downloads, CI-safe). Per model:
prefilled on top; bounded against a token-by-token decode noise floor.
threaded into turn 2's prefill via
state.turn; the post-image resume state must position turn 3 correctly.
windowSize: 8vs one shot.boundary, the hard case for chunked slicing.
All 10 pass. The full
MLXLMTestssuite is green viaxcodebuild test -scheme mlx-swift-lm-Package -destination 'platform=macOS' -skipPackagePluginValidation,and
swift-formatis clean.#420 checklist
chunking; not a drop-in of the Qwen3.5/3.6: windowed prefill + state-threaded warm continuation (fixes multi-turn M-RoPE drift) #399 pattern)
Grounding validation (real weights)
Ran
mlx-community/Qwen2.5-VL-7B-Instruct-4bit(temp 0) on 10 GUI-grounding rows(3 canonical + 7 dense screenshots), each asked twice: COLD (fresh session, single
turn) and WARM (turn 2 of a live session whose turn 1 grounded a different image,
state threaded between turns).
main(343cae3)Warm coordinates track the cold single-shot within a few pixels on clean rows;
where they diverge the model is legitimately attending to turn-1 conversational
context — the controlled random-weight tests above are the positional-correctness
check (warm ≡ full prefill on logits), and this run is the end-to-end usability
check on real weights.