-
Couldn't load subscription status.
- Fork 6.5k
Fix Attention Mask Padding to Ensure Multiple of 8 Alignment #9678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! I left some comments, let me know if it makes sense.
| attention_mask_shape_before = attention_mask.shape[-1] | ||
| if attention_mask.dtype == torch.bfloat16 and attention_mask.shape[-1] % 8 != 0: | ||
| padded_length = math.ceil(attention_mask.shape[-1] / 8) * 8 | ||
| mask = torch.zeros( | ||
| (attention_mask.shape[0], attention_mask.shape[1], padded_length), | ||
| device=attention_mask.device, | ||
| dtype=attention_mask.dtype, | ||
| ) | ||
| mask[:, :, : attention_mask.shape[-1]] = attention_mask | ||
| attention_mask = mask | ||
|
|
||
| assert attention_mask.shape[-1] % 8 == 0, "Attention mask not padded to a multiple of 8" | ||
| assert attention_mask[:, :, :attention_mask_shape_before].equal( | ||
| attention_mask[:, :, :attention_mask_shape_before] | ||
| ), "Original values in attention mask are not preserved" | ||
|
|
||
| expanded_attention_mask = attention_mask.expand(-1, query_tokens, -1) | ||
|
|
||
| assert expanded_attention_mask.shape[1] == query_tokens, "Attention mask expansion for query tokens failed" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in this test, we want to have a check for functional correctness instead of applying the same logic that we're applying within the attention processor class.
So, this means we could first enable xformers attention on the UNet and then do a forward pass and then design our tests accordingly.
|
can you help to fix this error when i run the test script |
What does this PR do?
Fixes #9637 resolve Attention Mask Padding Issue for Compatibility with xFormers
Before submitting
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Anyone in the community is free to review the PR once the tests have passed.
@sayakpaul @yiyixuxu
Code from Issue
Output
torch.Size([2, 350, 256])Hardware Information