Repository navigation
Batch Qwen VL image rows with per-row decoder state - #101
Open
ronaldmannak wants to merge 2 commits into
Open
ronaldmannak wants to merge 2 commits into
ronaldmannak wants to merge 2 commits into
Conversation
BatchTokenIterator took token prompts only and refused a second row whenever a row's prefill returned model state, so Qwen VL image prompts, whose prefill leaves rope deltas, ran alone. Their decode positions also used the scalar cache offset, and Qwen 2 and 2.5 VL broadcast MROPE cos/sin as [1, batch, seq, dim], which fails for more than one row. - BatchTokenIterator.insert(inputs:) and insert(input:) queue full LMInputs, such as prompts with images. Each is prefilled whole through the model's prepare into a fresh cache. - BatchedGenerationStateProviding (MLXLMCommon): a model's decoder state merges in row order when rows join and is selected with the caches' indices when rows leave. The iterator evaluates it with the caches. Models that do not conform keep the single-row rule. - Qwen2VL, Qwen25VL, Qwen3VL, Qwen3VLMoE and Qwen35 (and Qwen35MoE) conform. Their state is each row's rope delta; a row without one, such as a text-only prefill, takes zero. Prefill-only keys (Qwen35's precomputed positions) are dropped on merge. - Decode positions use each row's own offset, from the batch cache's per-row rope offset (QwenVL.decodePositionIds), in all five models. - Qwen2VL and Qwen25VL broadcast MROPE cos/sin as [batch, 1, seq, dim]. QwenVLBatchedImageTests (six families, tiny random weights; Qwen 3.5 on the GPU for its Metal-only linear attention): - rowsMatchSingles: image and text rows of unequal lengths and grids, prefilled alone, decode as a batch with two late joins and a removal; each row's logits match the row continued alone (1e-4 on the CPU, 1e-2 on the GPU, where every family differs from single runs by 2e-3 to 7e-3). - iteratorRowsMatchSingles: through BatchTokenIterator, with two rows inserted while the first decode, greedy tokens match TokenIterator. Without the iterator change the image rows end in .failed; with the old Qwen 2 VL broadcast the batched forward fails to broadcast; with Qwen 3.5's old scalar offset its rows' logits differ. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
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.
Stacked on #91
No issue: requested by the maintainer. A leaf on #91, like #93; nothing stacks on it.
What changed
Full inputs:
BatchTokenIterator.insert(inputs:)andinsert(input:)queue fullLMInputs, such as prompts with images. Each is prefilled whole through the model'sprepare, into a fresh cache. Before, the iterator took token prompts only.BatchedGenerationStateProviding(new, MLXLMCommon) lets a model's decoder state follow rows through batched decoding:Models that do not conform keep the single-row rule (
modelStateRequiresSingleRow). Before, Qwen VL image prompts, whose prefill leaves rope deltas, could only run alone.Conformers: Qwen2-VL, Qwen2.5-VL, Qwen3-VL, Qwen3-VL-MoE and Qwen3.5 VL (and Qwen3.5-MoE, a subclass) conform.
Decode positions: each row now continues from its own offset, the batch cache's per-row RoPE offset, in all five models (
QwenVL.decodePositionIds). Before, they used the scalar cache offset, so ragged rows got wrong positions.MROPE broadcast: Qwen2-VL and Qwen2.5-VL now broadcast MROPE cos/sin as
[batch, 1, seq, dim]. Before it was[1, batch, seq, dim], which fails against[batch, heads, seq, dim]queries for more than one row.Why a new protocol: it has five conformers today. It sits at the boundary between the generic iterator and model-owned state, which the iterator cannot merge or filter without a model contract. It ports
BatchedGenerationStateProvidingfrom fork commit1bfd6f4(used by PicoCore ml-explore#201) onto the current batch API.Evidence
The new tests are in
RoPEApplicationTests.swift, suiteQwenVLBatchedImageTests. They use tiny random-weight models for six families. Qwen 3.5 runs on the GPU, because its linear attention is Metal-only; the others run on the CPU.rowsMatchSingles: image and text rows of unequal lengths and different image grids (4 and 2 merged tokens) are each prefilled alone, then decode as a batch, with two late joins and a removal. Each row's logits match the row continued alone on the same tokens.iteratorRowsMatchSingles: the same inputs throughBatchTokenIterator, with two rows inserted while the first two decode. The greedy tokens matchTokenIteratorfor every row.Each test fails without the change it guards:
.failed, so their tokens never match.[1, batch, seq, dim]broadcast, the batched forward fails:[broadcast_shapes] Shapes (2,4,1,16) and (1,2,1,16) cannot be broadcast.Focused suites at
b5fcb40(with #91's head merged in):The full
MLXLMTestsbundle at3e4bd4b, the change before #91's latest commits were merged in:These are the known failures on this machine: G7 left padding, LFM2-DSpark
promptLength: 3, and the two MiniCPM5 known issues. swift-format reports every changed file clean.Not done on purpose
insert(inputs:)takes cold requests only, with no supplied cache. Resuming a row on a cached prefix that carries rope deltas is a later follow-up.ResumablePrefillModelprefills the input's tokens chunk by chunk. None of the Qwen VL models is resumable, so their image rows always take the whole-promptpreparepath.1bfd6f4(generationStateBytesPerRow). Nothing here uses it.Test changes
None of the existing tests change. Added:
QwenVLBatchedImageTests(rowsMatchSingles,iteratorRowsMatchSingles, six families each) and theQwenVLImageFamilybuilders inRoPEApplicationTests.swift.🤖 Generated with Claude Code