-
Couldn't load subscription status.
- Fork 6.5k
Feature IP Adapter Xformers Attention Processor #9881
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
Changes from 2 commits
ac1c26d
a7af2b2
4475c0b
cd8702e
89f548c
37444bc
7741fb0
b01f302
4e9e4e0
3c66f70
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,8 @@ | |
| from huggingface_hub.utils import validate_hf_hub_args | ||
| from torch import nn | ||
|
|
||
| from diffusers.models.attention_processor import AttnProcessor, AttnProcessor2_0 | ||
|
|
||
| from ..models.embeddings import ( | ||
| ImageProjection, | ||
| IPAdapterFaceIDImageProjection, | ||
|
|
@@ -765,6 +767,7 @@ def _convert_ip_adapter_attn_to_diffusers(self, state_dicts, low_cpu_mem_usage=F | |
| from ..models.attention_processor import ( | ||
| IPAdapterAttnProcessor, | ||
| IPAdapterAttnProcessor2_0, | ||
| IPAdapterXFormersAttnProcessor | ||
| ) | ||
|
|
||
| if low_cpu_mem_usage: | ||
|
|
@@ -802,13 +805,20 @@ def _convert_ip_adapter_attn_to_diffusers(self, state_dicts, low_cpu_mem_usage=F | |
| hidden_size = self.config.block_out_channels[block_id] | ||
|
|
||
| if cross_attention_dim is None or "motion_modules" in name: | ||
| attn_processor_class = self.attn_processors[name].__class__ | ||
| attn_procs[name] = attn_processor_class() | ||
|
|
||
| else: | ||
| attn_processor_class = ( | ||
| IPAdapterAttnProcessor2_0 if hasattr(F, "scaled_dot_product_attention") else IPAdapterAttnProcessor | ||
| ) | ||
| if ('XFormers' not in str(self.attn_processors[name].__class__)): | ||
| attn_processor_class = ( | ||
| AttnProcessor2_0 if hasattr(F, "scaled_dot_product_attention") else AttnProcessor | ||
| ) | ||
| attn_procs[name] = attn_processor_class() | ||
| else: | ||
| attn_procs[name] = self.attn_processors[name] | ||
| else: | ||
|
||
| if ('XFormers' in str(self.attn_processors[name].__class__)): | ||
| attn_processor_class = (IPAdapterXFormersAttnProcessor) | ||
| else: | ||
| attn_processor_class = ( | ||
| IPAdapterAttnProcessor2_0 if hasattr(F, "scaled_dot_product_attention") else IPAdapterAttnProcessor | ||
| ) | ||
| num_image_text_embeds = [] | ||
| for state_dict in state_dicts: | ||
| if "proj.weight" in state_dict["image_proj"]: | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -368,8 +368,21 @@ def set_use_memory_efficient_attention_xformers( | |||
| "Memory efficient attention with `xformers` might currently not work correctly if an attention mask is required for the attention operation." | ||||
| ) | ||||
| processor = XFormersAttnAddedKVProcessor(attention_op=attention_op) | ||||
| else: | ||||
| processor = XFormersAttnProcessor(attention_op=attention_op) | ||||
| else: | ||||
| processor = self.processor | ||||
| if isinstance(self.processor, (IPAdapterAttnProcessor, IPAdapterAttnProcessor2_0)): | ||||
|
||||
| processor = IPAdapterXFormersAttnProcessor(hidden_size=self.processor.hidden_size, | ||||
| cross_attention_dim=self.processor.cross_attention_dim, | ||||
| scale=self.processor.scale, | ||||
| attention_op=attention_op) | ||||
| processor.load_state_dict(self.processor.state_dict()) | ||||
|
||||
| self.to_k_ip = nn.ModuleList( |
(i had forgotten about that sorry! lol)
yiyixuxu marked this conversation as resolved.
Show resolved
Hide resolved
Outdated
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.
this check can be removed because this class uses xformers.ops.memory_efficient_attention instead of torch.nn.functional.scaled_dot_product_attention


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.
can you explain this code change 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.
Hi, how are you? Help me understand if my reasoning is correct. In this condition it is true when "cross_attention_dim" is None or "motion_modules" is present. I just paid attention to the scenario where "cross_attention_dim" is None. Is there any specific attention class for models with "motion_modules" other than AttnProcessor and AttnProcessor2_0? Because if there was I would just leave "motion_modules" in an "elif". But this part of the code is part of a first solution that I had implemented some time ago when I had not yet implemented the replacement of the attention mechanism in the "set_use_memory_efficient_attention_xformers" method of the "Attention" class. So at the time when I was testing several adapters and combined adapters I was probably encountering a situation that made me force this xformers check in this part of the code. However, now that you mentioned it, I decided to comment out this part of the code and perform some more tests, and it seems that this modification is no longer necessary since "set_use_memory_efficient_attention_xformers" has been implemented. At least for now, I haven't run into any error situations when loading.
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.
If you agree i will commit updates without this verification to original code.
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.
for this, can you provide a code example that would fail without this change?
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.
yes see my code on PR if you check i have lines #pipe.enable_xformers_memory_efficient_attention() you can remove # to run before or after load PR i put the two lines before and after loading model.
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 will commit my lasted code with some fixes for quality check