-
Notifications
You must be signed in to change notification settings - Fork 6k
add only cross attention to simple attention blocks #3011
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
williamberman
merged 3 commits into
huggingface:main
from
williamberman:only_cross_attention_simple_attention_blocks
Apr 11, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import unittest | ||
|
||
import torch | ||
|
||
from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor | ||
|
||
|
||
class AttnAddedKVProcessorTests(unittest.TestCase): | ||
def get_constructor_arguments(self, only_cross_attention: bool = False): | ||
query_dim = 10 | ||
|
||
if only_cross_attention: | ||
cross_attention_dim = 12 | ||
else: | ||
# when only cross attention is not set, the cross attention dim must be the same as the query dim | ||
cross_attention_dim = query_dim | ||
|
||
return { | ||
"query_dim": query_dim, | ||
"cross_attention_dim": cross_attention_dim, | ||
"heads": 2, | ||
"dim_head": 4, | ||
"added_kv_proj_dim": 6, | ||
"norm_num_groups": 1, | ||
"only_cross_attention": only_cross_attention, | ||
"processor": AttnAddedKVProcessor(), | ||
} | ||
|
||
def get_forward_arguments(self, query_dim, added_kv_proj_dim): | ||
batch_size = 2 | ||
|
||
hidden_states = torch.rand(batch_size, query_dim, 3, 2) | ||
encoder_hidden_states = torch.rand(batch_size, 4, added_kv_proj_dim) | ||
attention_mask = None | ||
|
||
return { | ||
"hidden_states": hidden_states, | ||
"encoder_hidden_states": encoder_hidden_states, | ||
"attention_mask": attention_mask, | ||
} | ||
|
||
def test_only_cross_attention(self): | ||
# self and cross attention | ||
|
||
torch.manual_seed(0) | ||
|
||
constructor_args = self.get_constructor_arguments(only_cross_attention=False) | ||
attn = Attention(**constructor_args) | ||
|
||
self.assertTrue(attn.to_k is not None) | ||
self.assertTrue(attn.to_v is not None) | ||
|
||
forward_args = self.get_forward_arguments( | ||
query_dim=constructor_args["query_dim"], added_kv_proj_dim=constructor_args["added_kv_proj_dim"] | ||
) | ||
|
||
self_and_cross_attn_out = attn(**forward_args) | ||
|
||
# only self attention | ||
|
||
torch.manual_seed(0) | ||
|
||
constructor_args = self.get_constructor_arguments(only_cross_attention=True) | ||
attn = Attention(**constructor_args) | ||
|
||
self.assertTrue(attn.to_k is None) | ||
self.assertTrue(attn.to_v is None) | ||
|
||
forward_args = self.get_forward_arguments( | ||
query_dim=constructor_args["query_dim"], added_kv_proj_dim=constructor_args["added_kv_proj_dim"] | ||
) | ||
|
||
only_cross_attn_out = attn(**forward_args) | ||
|
||
self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sufficient for merging IMO. |
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.
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.
@patrickvonplaten you had used
only_cross_attention[-1]
for this argument but I think it makes sense to use a separate config for the mid block. The other arguments for the mid block that re-use the config for the last encoder block make sense because they are dimensionality based and they have to match. But this constraint doesn't hold necessarily for theonly_cross_attention
flagThere 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.
Actually with this commit, 7329ead , we can do a better default to the value of
only_cross_attention
when it's given as a single booleanThere 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.
Ok for me