Skip to content
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

[PyTorch] Fix get_swa_mask() for padding masks #1281

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

cyanguwa
Copy link
Collaborator

Description

This PR fixes the mask generation for sliding window in UnfusedDotProductAttention. It fixes the logic for padding and arbitrary masks in get_swa_mask(), adds more docstring, refactors the call site, and adds more testing in the unit tests.

Fixes #1271

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refractor

Changes

Please list the changes introduced in this PR:

  • Improve the logic in get_swa_mask() and its call site

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@cyanguwa
Copy link
Collaborator Author

/te-ci pytorch

@Marks101
Copy link
Contributor

Hi @cyanguwa,
great, I like the idea to have all the masking logic at one place 👍
I just tested this and found a problem with cross attention:

        if "padding" in attn_mask_type:
            if max_seqlen_q == max_seqlen_kv:
                attention_mask = torch.logical_or(
>                   attention_mask.squeeze(1).unsqueeze(3), attention_mask
                )
E               AttributeError: 'tuple' object has no attribute 'squeeze'

The code in UnfusedDotProductAttention made these lines dependent on the attention_type.

@cyanguwa
Copy link
Collaborator Author

Hi @cyanguwa, great, I like the idea to have all the masking logic at one place 👍 I just tested this and found a problem with cross attention:

        if "padding" in attn_mask_type:
            if max_seqlen_q == max_seqlen_kv:
                attention_mask = torch.logical_or(
>                   attention_mask.squeeze(1).unsqueeze(3), attention_mask
                )
E               AttributeError: 'tuple' object has no attribute 'squeeze'

The code in UnfusedDotProductAttention made these lines dependent on the attention_type.

Yes, I think I should use if attention_type == "self" here because there could be cross-attention cases where max_seqlen_q == max_seqlen_kv and actual_seqlen_q != actual_seqlen_kv. I'll go through attention.py and see if there're other places I should use attention_type instead.

Let me know if you observe any other issues too! :) Thanks!

is applied, the bottom right corner comes from the [actual_seqlen_q[i], actual_seqlen_kv[i]] matrix,
for each batch i, not the [max_seqlen_q, max_seqlen_kv] matrix.::

attn_mask_type output shape diagonal alignment
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants