Skip to content

[refactor] apply qk norm in attention processors #9071

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

Merged
merged 5 commits into from
Aug 4, 2024
Merged

Conversation

a-r-r-o-w
Copy link
Member

What does this PR do?

  • Applies QK-norm in some of the attention processors that currently support it, and newly in the default AttentionProcessor2_0 and FusedAttentionProcessor2_0 (for you know what 👀).
  • I think we should also refactor some of the attention processors and determine if we need these many.
  • We should also probably re-order these in alphabetical order to make it easier to read, and group the normal and fused variants together.

Happy to take it up in this PR if we're okay with it.

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@sayakpaul @yiyixuxu

@a-r-r-o-w a-r-r-o-w requested review from sayakpaul and yiyixuxu August 3, 2024 20:42
@HuggingFaceDocBuilderDev

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.

@a-r-r-o-w
Copy link
Member Author

Additionally, since you can pre-QK norm (before reshaping to batch_size, num_heads, seq_length, head_dim) or post-QK norm (after reshape), I propose we add a parameter like pre_qk_norm=False to Attention to determine if the intent is pre/post-norm and encapsulate the reshape + norm logic into a single function:

def prepare_attention_qkv(self, query, key, value):
    batch_size, seq_length, embedding_dim = query.shape
    head_dim = embedding_dim // self.heads
    
    query = self.norm_q(query) if self.norm_q and self.pre_qk_norm else query
    key = self.norm_k(key) if self.norm_k and self.pre_qk_norm else key

    query = query.reshape(batch_size, -1, self.heads, head_dim).transpose(1, 2)
    key = key.reshape(batch_size, -1, self.heads, head_dim).transpose(1, 2)
    value = value.reshape(batch_size, -1, self.heads, head_dim).transpose(1, 2)

    query = self.norm_q(query) if self.norm_q and not self.pre_qk_norm else query
    key = self.norm_k(key) if self.norm_k and not self.pre_qk_norm else key

# Or a more readable implementation of above

I think this should cover a few cases of QK norm attention processors, and possibly help us remove a few processors (Hunyuan, for example) - if we also handle rotary embeds well in a clean way. But if we introduce too many code branches, it could get difficult to understand code flow so let me know what you think is best.

Copy link
Collaborator

@yiyixuxu yiyixuxu left a comment

Choose a reason for hiding this comment

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

let's not add a method and just apply the qk_norm inside the attention processor

Copy link
Member

@sayakpaul sayakpaul left a comment

Choose a reason for hiding this comment

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

Agree with @yiyixuxu regarding not having the method.

Should we try to have a test for this as well?

Comment on lines 1229 to 1230
# Apply QK-norm if needed
query, key = attn.maybe_apply_qk_norm(query, key)
Copy link
Member

Choose a reason for hiding this comment

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

It's just four lines of code that we're wrapping as a method. It's not doing anything complex, either. So, agree with @yiyixuxu that we should just keep it here.

Copy link
Member Author

Choose a reason for hiding this comment

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

I see, I was hoping to get in a refactor based on my comment above to have some attention processors removed/reused. But we can come back to this later. Should be good to merge now? Let's do this as soon as possible to perform rebase

@sayakpaul
Copy link
Member

Will let @yiyixuxu to give the final approval.

@yiyixuxu yiyixuxu merged commit 2b76099 into main Aug 4, 2024
18 checks passed
@a-r-r-o-w a-r-r-o-w deleted the attention-qk-norm branch August 4, 2024 15:43
sayakpaul pushed a commit that referenced this pull request Dec 23, 2024
* apply qk norm in attention processors

* revert attention processor

* qk-norm in only attention proc 2.0 and fused variant
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.

4 participants