Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 7.3k
[Feature] Support IP-Adapter Plus#5915
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
5e6e351d39d76307dbb439096c37989312721e627587aa9d5775487f69ea70559012a690096f2fe7d232bc9053d11cacbd8cb48a524ed74ae27b641390f3c0File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -20,6 +20,7 @@ | ||||||||||||||||||
| from ..utils import USE_PEFT_BACKEND | ||||||||||||||||||
| from .activations import get_activation | ||||||||||||||||||
| from .attention_processor import Attention | ||||||||||||||||||
| from .lora import LoRACompatibleLinear | ||||||||||||||||||
| @@ -790,3 +791,91 @@ def forward(self, caption, force_drop_ids=None): | ||||||||||||||||||
| hidden_states = self.act_1(hidden_states) | ||||||||||||||||||
| hidden_states = self.linear_2(hidden_states) | ||||||||||||||||||
| return hidden_states | ||||||||||||||||||
| class Resampler(nn.Module): | ||||||||||||||||||
| """Resampler of IP-Adapter Plus. | ||||||||||||||||||
| Args: | ||||||||||||||||||
| ---- | ||||||||||||||||||
| embed_dims (int): The feature dimension. Defaults to 768. | ||||||||||||||||||
| output_dims (int): The number of output channels, that is the same | ||||||||||||||||||
| number of the channels in the | ||||||||||||||||||
| `unet.config.cross_attention_dim`. Defaults to 1024. | ||||||||||||||||||
| hidden_dims (int): The number of hidden channels. Defaults to 1280. | ||||||||||||||||||
| depth (int): The number of blocks. Defaults to 8. | ||||||||||||||||||
| dim_head (int): The number of head channels. Defaults to 64. | ||||||||||||||||||
| heads (int): Parallel attention heads. Defaults to 16. | ||||||||||||||||||
| num_queries (int): The number of queries. Defaults to 8. | ||||||||||||||||||
| ffn_ratio (float): The expansion ratio of feedforward network hidden | ||||||||||||||||||
| layer channels. Defaults to 4. | ||||||||||||||||||
| """ | ||||||||||||||||||
| def __init__( | ||||||||||||||||||
| self, | ||||||||||||||||||
| embed_dims: int = 768, | ||||||||||||||||||
| output_dims: int = 1024, | ||||||||||||||||||
| hidden_dims: int = 1280, | ||||||||||||||||||
| depth: int = 4, | ||||||||||||||||||
| dim_head: int = 64, | ||||||||||||||||||
| heads: int = 16, | ||||||||||||||||||
| num_queries: int = 8, | ||||||||||||||||||
| ffn_ratio: float = 4, | ||||||||||||||||||
| ) -> None: | ||||||||||||||||||
| super().__init__() | ||||||||||||||||||
| from .attention import FeedForward # Lazy import to avoid circular import | ||||||||||||||||||
| self.latents = nn.Parameter(torch.randn(1, num_queries, hidden_dims) / hidden_dims**0.5) | ||||||||||||||||||
| self.proj_in = nn.Linear(embed_dims, hidden_dims) | ||||||||||||||||||
| self.proj_out = nn.Linear(hidden_dims, output_dims) | ||||||||||||||||||
| self.norm_out = nn.LayerNorm(output_dims) | ||||||||||||||||||
| self.layers = nn.ModuleList([]) | ||||||||||||||||||
| for _ in range(depth): | ||||||||||||||||||
| self.layers.append( | ||||||||||||||||||
| nn.ModuleList( | ||||||||||||||||||
| [ | ||||||||||||||||||
| nn.LayerNorm(hidden_dims), | ||||||||||||||||||
| nn.LayerNorm(hidden_dims), | ||||||||||||||||||
| Attention( | ||||||||||||||||||
yiyixuxu marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||||||||||||||||||
| query_dim=hidden_dims, | ||||||||||||||||||
| dim_head=dim_head, | ||||||||||||||||||
| heads=heads, | ||||||||||||||||||
| out_bias=False, | ||||||||||||||||||
| ), | ||||||||||||||||||
| nn.Sequential( | ||||||||||||||||||
| nn.LayerNorm(hidden_dims), | ||||||||||||||||||
| FeedForward(hidden_dims, hidden_dims, activation_fn="gelu", mult=ffn_ratio, bias=False), | ||||||||||||||||||
| ), | ||||||||||||||||||
Comment on lines
+848
to
+851
Contributor 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.
Suggested change
nice! | ||||||||||||||||||
| ] | ||||||||||||||||||
| ) | ||||||||||||||||||
| ) | ||||||||||||||||||
| def forward(self, x: torch.Tensor) -> torch.Tensor: | ||||||||||||||||||
| """Forward pass. | ||||||||||||||||||
| Args: | ||||||||||||||||||
| ---- | ||||||||||||||||||
| x (torch.Tensor): Input Tensor. | ||||||||||||||||||
| Returns: | ||||||||||||||||||
| ------- | ||||||||||||||||||
| torch.Tensor: Output Tensor. | ||||||||||||||||||
| """ | ||||||||||||||||||
| latents = self.latents.repeat(x.size(0), 1, 1) | ||||||||||||||||||
| x = self.proj_in(x) | ||||||||||||||||||
| for ln0, ln1, attn, ff in self.layers: | ||||||||||||||||||
| residual = latents | ||||||||||||||||||
| encoder_hidden_states = ln0(x) | ||||||||||||||||||
| latents = ln1(latents) | ||||||||||||||||||
| encoder_hidden_states = torch.cat([encoder_hidden_states, latents], dim=-2) | ||||||||||||||||||
| latents = attn(latents, encoder_hidden_states) + residual | ||||||||||||||||||
| latents = ff(latents) + latents | ||||||||||||||||||
| latents = self.proj_out(latents) | ||||||||||||||||||
| return self.norm_out(latents) | ||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
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.
Ok for now, but let's make sure to later factor this out with a
conversion_...function later