Fix Qwen3 causal mask for batch size > 1#3610
Open
NahButch wants to merge 1 commit into
Open
Conversation
causal_mask filled tgt * (tgt + offset) elements but declared the shape (b, 1, tgt, tgt + offset), so only batch row 0 carried a valid mask and rows >= 1 attended over a corrupt, effectively non-causal pattern. Build the mask as (1, 1, tgt, tgt + offset) and expand it across the batch (a zero-copy view). Adds a unit test asserting every batch row gets the correct causal pattern; it fails on the previous behavior. Fixes huggingface#3582 Co-Authored-By: Claude Fable 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.
Model::causal_maskfilledtgt * (tgt + offset)elements but declared the shape(b, 1, tgt, tgt + offset), so only batch row 0 carried a valid mask and rows >= 1 attended over a corrupt, effectively non-causal pattern — two identical sequences in one batch produce different outputs (row 0 correct, row 1 garbage).Build the mask as
(1, 1, tgt, tgt + offset)andexpandit across the batch (a zero-copy view). Adds a unit test asserting every batch row gets the correct causal pattern; it fails on the previous behavior.Related to #3609 (Qwen2 mask fix); this one is the batch-dimension variant in Qwen3.
Fixes #3582
🤖 Generated with Claude Code