fix(qwen3_vl): build causal mask for prefill, not single-token decode (#3505)#3518
Open
0xSoftBoi wants to merge 1 commit into
Open
fix(qwen3_vl): build causal mask for prefill, not single-token decode (#3505)#35180xSoftBoi wants to merge 1 commit into
0xSoftBoi wants to merge 1 commit into
Conversation
…huggingface#3505) The condition gating attention-mask construction in Qwen3VLModel::forward was inverted: `if seqlen <= 1 { Some(mask) } else { None }`. This built the causal mask only on single-token decode steps (where the KV cache already constrains attention) and skipped it on multi-token prefill (where the model needs the mask to attend correctly). The result was wrong forward-pass behavior whenever a prompt of length > 1 was processed. All other candle text models (mistral, starcoder2, helium, olmo2, phi3, deepseek2, etc.) follow the canonical `if seq_len <= 1 { None }` else `Some(mask)` shape. This patch aligns Qwen3 VL with that pattern. Fixes huggingface#3505
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.
Summary
In
Qwen3VLModel::forward, the gate that decides whether to build a causal attention mask is inverted:The decoder causal mask is only useful when more than one token is being processed at once (prefill / multi-token prompt). For single-token autoregressive decode steps the KV cache already constrains attention and the mask can be skipped. The current code does the opposite.
Every other candle text model (mistral, starcoder2, helium, olmo2, phi3, deepseek2, csm, gemma4, …) follows the canonical pattern, e.g. mistral.rs:
This PR aligns Qwen3 VL with that shape and adds a comment explaining the invariant so the inversion isn't reintroduced.
Test
cargo build -p candle-transformers— cleancargo clippy -p candle-transformers --lib -- -D warnings— cleancargo fmt --all -- --check— cleanFixes #3505