Repository navigation
qwen3_omni_moe: trim explicit attention masks to the cached key length - #2801
Merged
Merged
Conversation
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>
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.
Refs #2731 (the off-by-one; the bf16 part is out of scope, see the end)
Problem
Attention.__call__inmlx_vlm/models/qwen3_omni_moe/language.pycomputedkv_seq_len = L + cache.offset + 1on both branches and cut array masks to that many columns. Aftercache.update_and_fetchthe keys holdoffset + Lentries, so the slice kept one column too many:offset + L + 1wide and failed to broadcast in SDPA ([broadcast_shapes] Shapes (4,13) and (1,2,4,12) cannot be broadcast).position_ids=Noneand no cache,cache.offsetraisedAttributeError.BatchKVCache,max(offset)is below_idxwhen 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 + Lwide are unchanged by the slice. Those are the masks mlx-vlm builds itself throughcreate_attention_mask, so normal prefill, decode and speculative verify output does not change.Fix
Same as
qwen3_vlsince #2548: rope positions come fromcache.offset(0 without a cache), and the mask is trimmed afterupdate_and_fetchtokv_sequence_length(keys), the real key length, which covers plain, batched, rotating and quantized caches. Slicing tooffset + 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_keysinmlx_vlm/tests/test_models.pyuses the tiny random qwen3_omni_moe config frommodel_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_moeandqwen3_5carry 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