Fix MPS SDPA output shape when value head dim differs from query head dim#45467
Closed
Jah-yee wants to merge 1 commit intohuggingface:mainfrom
Closed
Fix MPS SDPA output shape when value head dim differs from query head dim#45467Jah-yee wants to merge 1 commit intohuggingface:mainfrom
Jah-yee wants to merge 1 commit intohuggingface:mainfrom
Conversation
… dim Workaround for PyTorch < 2.12 bug (pytorch/pytorch#176767, pytorch/pytorch#176843) where scaled_dot_product_attention on MPS produces incorrect output when value head dim != query head dim. DeepSeek models (MQA) are affected as they have different qk and v head dims. The fix pads v to match q's head dim before SDPA, then truncates output back to the original v size. Fixes huggingface#44554
Member
|
@Jah-yee it's not actually helpful to just run your code agent on random issues! Hugging Face is a leading AI company, we're able to run agents ourselves if we need to 😅 |
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.
Good day,
Problem
On Apple Silicon (MPS backend),
torch.nn.functional.scaled_dot_product_attentionproduces incorrect output when the value tensor's head dimension differs from the query tensor's head dimension. This affects DeepSeek models (MQA) which have different qk and v head dims, and any model with multi-query attention where the value head dim != query head dim.Root cause: Upstream PyTorch bug (pytorch/pytorch#176767) that was fixed in PyTorch 2.12 (pytorch/pytorch#176843), but the fix is not yet widely available.
Solution
This adds a workaround in
sdpa_attention_forwardthat:This is the approach suggested and agreed upon by maintainers in the linked issue discussion.
Changes
src/transformers/integrations/sdpa_attention.py: Added MPS head-dim workaround insdpa_attention_forwardis_torch_mps_availablefor runtime MPS detectionTesting
The original reproducer from the issue:
Before:
y_mps.shape == (1, 1, 8, 4)with uninitialized memory in last 2 channelsAfter:
y_mps.shape == (1, 1, 8, 2)— correct output shape and valuesFixes #44554
Thank you for your attention. If there are any issues or suggestions, please leave a comment and I will address them promptly.
Warmly,
RoomWithOutRoof