Skip to content

fix(qwen3vl): add the M-RoPE delta per row in a batched decode step - #269

Merged
Gajesh2007 merged 4 commits into
mainfrom
fix/qwen3vl-batched-decode
Oct 4, 2026
Merged

Gajesh2007 merged 4 commits into
mainfrom
fix/qwen3vl-batched-decode

Conversation

@davidtai

@davidtai davidtai commented Oct 1, 2026 •

Copy link
Copy Markdown

Implementation

Fixes #266. Preparation only; no merge, deployment or downstream pin update.

Cached Qwen3-VL steps must add [batch] M-RoPE deltas along the batch axis, not along the last position axis. Reshape the offset-plus-delta to [-1, 1] before addition. The original code could both trap on a nonsquare chunk and silently corrupt a square chunk when unequal row deltas were treated as column deltas.

Merged main 3799b1150699e5595ae4c100a36b1a07cef3c44f without rebasing and preserved both fork-map topics. The actual stored-delta branch now uses a single internal pure Qwen3VLLanguage.cachedPositionIds helper, also used by permanent exact-position regressions. No public API, model-state test hook or reflection was added. The old kernel recording's batch-one comment is removed and its cache/full comparison now uses both token rows. All existing shape, dtype, finite, causality, batch-invariance and cache assertions remain.

Before

flowchart LR
  C["LanguageModel cached step"] --> D["offset + stored deltas: batch vector"]
  D --> A["base + delta on last axis"]
  A --> E["Nonsquare: broadcast trap"]
  A --> S["Square: wrong positions, silent logits corruption"]
  classDef bad fill:#742b35,color:#fff,stroke:#d67;
  class A,E,S bad;
Loading

After

flowchart LR
  C["LanguageModel cached step"] --> H["internal cachedPositionIds"]
  H --> R["reshape delta to batch x 1"]
  R --> P["3 x batch x length: correct per-row positions"]
  P --> M["Tiny text model cached/full logits agree"]
  H --> T["Exact unequal image/video delta and square regressions"]
  classDef good fill:#215545,color:#fff,stroke:#5b9;
  class H,R,P,M,T good;
Loading

Fresh Validation (2026-10-03)

  • Own-clone swift build --build-tests -j 6: passes; all test targets compiled, not a full-suite execution.
  • Under the shared GPU mutex: scripts/run-nested-suite.sh 'Qwen3VLBatchedDecodeTests|Qwen3VLForwardPassTests' --no-parallel: 23 tests pass, no skips, after the final candidate rebuild.
  • Permanent position test cells: batch 1/2/3, image/video deltas independently produced by supported single-row positionResult prompts. Expected deltas are [-2,-3,-6] for images and [-2,-3,-9] for videos. Exact Int32 checks cover all three rotary axes at widths batch, 1 and 4, plus singleton-delta repetition. Text cached/full logits retain tolerance 1e-4 with chunks 5/3/1/1/1 at batch 1/2/3. Actual B=1 image/video prefill and cached/full-logit tests also ran.
  • Fresh differential run: temporarily restored only base + delta in the production-used helper, rebuilt, and ran the permanent test. Batch-2 square positions were [22,22,22,22] instead of [22,23,21,22] on each axis; the following width-1 case then exited with signal 5 on the broadcast. Restored the fix, rebuilt, and reran all 23 tests successfully. The baseline variant was never committed or pushed.
  • git diff --check and python3 scripts/check_forkdiff.py: pass. Changed Swift files are formatted; the stricter local swift format lint --strict additionally reports an unchanged pre-existing OnlyOneTrailingClosureArgument diagnostic at Qwen3VL.swift:188. Latest-head hosted Lint, Fork Diff Check and CodeQL have succeeded.
  • Used only an immutable metallib in this clone's own build after exact mlx-swift/Cmlx/core/generated tuple verification (0f4fe403 / 3fa8f25e / 02cf6f4d / 17c9c070).

Scope And Gates

Multimodal prefill at B>1 remains explicitly unsupported by the baseline position builder. The unequal media-delta checks compose separately supported B=1 builder results to validate the cached arithmetic; they do not qualify a batched vision prefill path. CBv2 positioned-forward serving supplies its own position IDs and bypasses this legacy stored-delta calculation. No hosted checkpoint/provider qualification, production test or memory-policy change is claimed.

Base remains main; no dependency retarget is required. Related PR #267 has its own independent media/fork-map topic. Signed head 4870ac69a532bf9852bac261a4df7b86f104b0ef is GitHub Verified. Latest-head Fork Tests are still running; that required CI gate must settle before readiness is claimed, and old red runs are not evidence about this head. No review findings have been posted at the latest refresh.

Latest refresh at 2026-10-03 08:58 UTC: all PR-only commits are GitHub Verified (valid), base main, MERGEABLE / UNSTABLE. Lint, Fork Diff Check and CodeQL succeeded; Fork Tests 37110757267 is still in progress. No reviews, comments or inline findings are posted. Both diagrams were verified as rendered GitHub Mermaid SVGs, without syntax errors. This is prepared but not yet CI-cleared for merge.

Combined Integration Validation

On 2026-10-03, the current prepared heads of the twelve held PRs were composed
on a local-only validation branch based on main 3799b115, including the
signed-padding compatibility correction 96d5f976. No PR was merged.

  • Fresh swift build --build-tests -j 4 completed successfully.
  • Affected suites: 261 Swift Testing cases and 33 executed XCTest cases passed;
    five optional fixture-dependent XCTest cases skipped. Advanced Arrays/Mamba
    and nested signed-padding round trips passed in this composed build.
  • Bounded serving integration: 107 Swift Testing cases and 105 XCTest cases
    passed, with no failures or skips, covering paged backend, scheduler,
    checkpoint engine, SSM bounds, Nemotron storage/priming, OpenAI service and
    tool parser paths.
  • Both executions used swift test --skip-build --filter ... --no-parallel
    under the shared GPU mutex. This is focused integration validation, not a
    full-package, real-weight or provider qualification. Exact compatible MLX
    dependency tuple and immutable Metal artifact were verified before staging.
  • Combined git diff --check and python3 scripts/check_forkdiff.py passed.
    Existing assertions remain intact; this evidence does not waive required
    latest-head CI, review, or parent-first retarget/revalidation gates.

A cached decode step with batch size 2 or more added the delta of shape
[batch] to positions of shape [batch, seqLength] on the last axis, and
the broadcast failed. The delta now goes along the batch axis, as
mlx-vlm qwen3_vl does with delta.reshape(-1, 1).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Gajesh2007

Copy link
Copy Markdown
Member

Merge-time synchronization: current main was merged normally into this branch at c0ee8acb998db96909dd061bb2f0d478c21c307b. The only manual conflict resolution was in fork.yaml, preserving both metadata topics; the runtime files remain byte-identical to the validated combined candidate. The new commit is GitHub Verified, local prospective-tree fork-map and diff checks pass, and the PR is conflict-free against main. Fresh current-head full CI is running and remains a merge gate; the prior head green run is not substituted for it.

@Gajesh2007
Gajesh2007 merged commit 8211000 into main Oct 4, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Qwen3VL: a cached decode step with batch size 2 or more fails to broadcast the M-RoPE delta

2 participants