-
Notifications
You must be signed in to change notification settings - Fork 6.1k
[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
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. |
Additionally, since you can pre-QK norm (before reshaping to 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. |
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.
let's not add a method and just apply the qk_norm inside the attention processor
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.
Agree with @yiyixuxu regarding not having the method.
Should we try to have a test for this as well?
# Apply QK-norm if needed | ||
query, key = attn.maybe_apply_qk_norm(query, key) |
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.
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.
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 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
Will let @yiyixuxu to give the final approval. |
* apply qk norm in attention processors * revert attention processor * qk-norm in only attention proc 2.0 and fused variant
What does this PR do?
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