Skip to content

qwen3_omni_moe: trim explicit attention masks to the cached key length - #2801

Merged
Blaizzy merged 2 commits into
Neywa-Labs:mainfrom
JarJarFran:fix-2731-omni-kv-seq-len
Nov 29, 2026
Merged

Blaizzy merged 2 commits into
Neywa-Labs:mainfrom
JarJarFran:fix-2731-omni-kv-seq-len

Conversation

@JarJarFran

Copy link
Copy Markdown
Contributor

Refs #2731 (the off-by-one; the bf16 part is out of scope, see the end)

Problem

Attention.__call__ in mlx_vlm/models/qwen3_omni_moe/language.py computed kv_seq_len = L + cache.offset + 1 on both branches and cut array masks to that many columns. After cache.update_and_fetch the keys hold offset + L entries, so the slice kept one column too many:

  • An explicit mask wider than the cached keys, for example a causal mask built over a longer key range for a speculative verify window, came out offset + L + 1 wide and failed to broadcast in SDPA ([broadcast_shapes] Shapes (4,13) and (1,2,4,12) cannot be broadcast).
  • With position_ids=None and no cache, cache.offset raised AttributeError.
  • With a BatchKVCache, max(offset) is below _idx when every row is left-padded, so the slice could come out too narrow instead (left padding >= 2 on every row fails to broadcast).

Masks exactly offset + L wide are unchanged by the slice. Those are the masks mlx-vlm builds itself through create_attention_mask, so normal prefill, decode and speculative verify output does not change.

Fix

Same as qwen3_vl since #2548: rope positions come from cache.offset (0 without a cache), and the mask is trimmed after update_and_fetch to kv_sequence_length(keys), the real key length, which covers plain, batched, rotating and quantized caches. Slicing to offset + L, as suggested in the issue, would still cut the mask short for a batched cache whose rows are all left-padded. The batched path also no longer calls .max().item() in every layer.

Test

test_qwen3_omni_explicit_mask_is_trimmed_to_cached_keys in mlx_vlm/tests/test_models.py uses the tiny random qwen3_omni_moe config from model_cases.json. It prefills 8 tokens, then verifies 4 tokens in one forward with explicit causal masks 12 (offset + L) and 16 columns wide; both must match token-by-token decoding. It also checks that a forward without cache or position ids matches one with a fresh cache. Before the fix it fails with the broadcast error; after the fix it passes.

pytest mlx_vlm/tests/test_models.py mlx_vlm/tests/test_model_ops.py mlx_vlm/tests/test_cache.py mlx_vlm/tests/test_audio_models.py: 2156 passed, 3 skipped (M1 Max, MLX 0.32.4).

The second part of the issue, multi-token forwards that differ from sequential decoding at near-ties in bf16, is kernel-path numerics, not something this change touches.

qwen3_vl_moe and qwen3_5 carry the same + 1. I left them out to keep this change to the model in the issue, and can send the same fix for them if you want it.

🤖 Generated with Claude Code

Attention cut array masks to `cache.offset + L + 1` columns, one more
than the keys returned by `update_and_fetch`, so an explicit mask wider
than the cache (e.g. for a speculative verify window) failed to
broadcast, and a forward without cache or position ids raised
AttributeError. Trim the mask after the cache update to
`kv_sequence_length(keys)`, as qwen3_vl does since Neywa-Labs#2548.

Fixes Neywa-Labs#2731

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

@Blaizzy Blaizzy left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@Blaizzy
Blaizzy merged commit 0352e23 into Neywa-Labs:main Nov 29, 2026
11 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.

2 participants