-
Notifications
You must be signed in to change notification settings - Fork 6.7k
[WIP] Add Kandinsky decoder #3330
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1a16918
Add movq
e905cb9
Merge branch 'kandinsky' into kandinsky_decoder
e0582c1
Merge decoder conversion script with others
86f2145
Refactoring
d4859aa
Use new attention processor
b6e9861
Fix minor bug in Attention Block
f2300d8
Merge branch 'kandinsky' into kandinsky_decoder
7de1237
Reposition Spatial Norm
cf3cbcb
Add decoder to text2img pipeline
9b55765
Register decoder
366c1e1
Merge branch 'kandinsky' into kandinsky_decoder
yiyixuxu 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
|---|---|---|
|
|
@@ -18,7 +18,7 @@ | |
| import torch.nn.functional as F | ||
| from torch import nn | ||
|
|
||
| from .attention import AdaGroupNorm | ||
| from .attention import AdaGroupNorm, AttentionBlock, SpatialNorm | ||
| from .attention_processor import Attention, AttnAddedKVProcessor, AttnAddedKVProcessor2_0 | ||
| from .dual_transformer_2d import DualTransformer2DModel | ||
| from .resnet import Downsample2D, FirDownsample2D, FirUpsample2D, KDownsample2D, KUpsample2D, ResnetBlock2D, Upsample2D | ||
|
|
@@ -348,6 +348,7 @@ def get_up_block( | |
| resnet_act_fn=resnet_act_fn, | ||
| resnet_groups=resnet_groups, | ||
| resnet_time_scale_shift=resnet_time_scale_shift, | ||
| temb_channels=temb_channels | ||
| ) | ||
| elif up_block_type == "AttnUpDecoderBlock2D": | ||
| return AttnUpDecoderBlock2D( | ||
|
|
@@ -360,6 +361,7 @@ def get_up_block( | |
| resnet_groups=resnet_groups, | ||
| attn_num_head_channels=attn_num_head_channels, | ||
| resnet_time_scale_shift=resnet_time_scale_shift, | ||
| temb_channels=temb_channels | ||
| ) | ||
| elif up_block_type == "KUpBlock2D": | ||
| return KUpBlock2D( | ||
|
|
@@ -406,7 +408,6 @@ def __init__( | |
| super().__init__() | ||
| resnet_groups = resnet_groups if resnet_groups is not None else min(in_channels // 4, 32) | ||
| self.add_attention = add_attention | ||
|
|
||
| # there is always at least one resnet | ||
| resnets = [ | ||
| ResnetBlock2D( | ||
|
|
@@ -439,7 +440,6 @@ def __init__( | |
| upcast_softmax=True, | ||
| _from_deprecated_attn_block=True, | ||
| ) | ||
| ) | ||
| else: | ||
| attentions.append(None) | ||
|
|
||
|
|
@@ -465,7 +465,8 @@ def forward(self, hidden_states, temb=None): | |
| hidden_states = self.resnets[0](hidden_states, temb) | ||
| for attn, resnet in zip(self.attentions, self.resnets[1:]): | ||
| if attn is not None: | ||
| hidden_states = attn(hidden_states) | ||
| hidden_states = attn(hidden_states, temb) | ||
|
|
||
| hidden_states = resnet(hidden_states, temb) | ||
|
|
||
| return hidden_states | ||
|
|
@@ -1971,6 +1972,30 @@ def custom_forward(*inputs): | |
| return hidden_states | ||
|
|
||
|
|
||
| class MOVQAttention(nn.Module): | ||
|
Collaborator
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. I think we don't need to create a new class anymore now this PR is merged #3387 |
||
| def __init__(self, query_dim, temb_channels, attn_num_head_channels): | ||
| super().__init__() | ||
|
|
||
| self.norm = SpatialNorm(query_dim, temb_channels) | ||
| num_heads = query_dim // attn_num_head_channels if attn_num_head_channels is not None else 1 | ||
| dim_head = attn_num_head_channels if attn_num_head_channels is not None else query_dim | ||
| self.attention = Attention( | ||
| query_dim=query_dim, | ||
| heads=num_heads, | ||
| dim_head=dim_head, | ||
| bias=True | ||
| ) | ||
|
|
||
| def forward(self, hidden_states, temb): | ||
| residual = hidden_states | ||
| hidden_states = self.norm(hidden_states, temb).view(hidden_states.shape[0], hidden_states.shape[1], -1) | ||
| hidden_states = self.attention(hidden_states.transpose(1, 2), None, None).transpose(1, 2) | ||
| hidden_states = hidden_states.view(residual.shape) | ||
| hidden_states = hidden_states + residual | ||
| return hidden_states | ||
|
|
||
|
|
||
|
|
||
| class UpDecoderBlock2D(nn.Module): | ||
| def __init__( | ||
| self, | ||
|
|
@@ -1985,6 +2010,7 @@ def __init__( | |
| resnet_pre_norm: bool = True, | ||
| output_scale_factor=1.0, | ||
| add_upsample=True, | ||
| temb_channels=None | ||
| ): | ||
| super().__init__() | ||
| resnets = [] | ||
|
|
@@ -1996,7 +2022,7 @@ def __init__( | |
| ResnetBlock2D( | ||
| in_channels=input_channels, | ||
| out_channels=out_channels, | ||
| temb_channels=None, | ||
| temb_channels=temb_channels, | ||
| eps=resnet_eps, | ||
| groups=resnet_groups, | ||
| dropout=dropout, | ||
|
|
@@ -2014,9 +2040,9 @@ def __init__( | |
| else: | ||
| self.upsamplers = None | ||
|
|
||
| def forward(self, hidden_states): | ||
| def forward(self, hidden_states, temb=None): | ||
| for resnet in self.resnets: | ||
| hidden_states = resnet(hidden_states, temb=None) | ||
| hidden_states = resnet(hidden_states, temb=temb) | ||
|
|
||
| if self.upsamplers is not None: | ||
| for upsampler in self.upsamplers: | ||
|
|
@@ -2040,6 +2066,7 @@ def __init__( | |
| attn_num_head_channels=1, | ||
| output_scale_factor=1.0, | ||
| add_upsample=True, | ||
| temb_channels=None | ||
| ): | ||
| super().__init__() | ||
| resnets = [] | ||
|
|
@@ -2052,7 +2079,7 @@ def __init__( | |
| ResnetBlock2D( | ||
| in_channels=input_channels, | ||
| out_channels=out_channels, | ||
| temb_channels=None, | ||
| temb_channels=temb_channels, | ||
| eps=resnet_eps, | ||
| groups=resnet_groups, | ||
| dropout=dropout, | ||
|
|
@@ -2075,7 +2102,6 @@ def __init__( | |
| upcast_softmax=True, | ||
| _from_deprecated_attn_block=True, | ||
| ) | ||
| ) | ||
|
|
||
| self.attentions = nn.ModuleList(attentions) | ||
| self.resnets = nn.ModuleList(resnets) | ||
|
|
@@ -2085,10 +2111,10 @@ def __init__( | |
| else: | ||
| self.upsamplers = None | ||
|
|
||
| def forward(self, hidden_states): | ||
| def forward(self, hidden_states, temb=None): | ||
| for resnet, attn in zip(self.resnets, self.attentions): | ||
| hidden_states = resnet(hidden_states, temb=None) | ||
| hidden_states = attn(hidden_states) | ||
| hidden_states = resnet(hidden_states, temb=temb) | ||
| hidden_states = attn(hidden_states, temb) | ||
|
|
||
| if self.upsamplers is not None: | ||
| for upsampler in self.upsamplers: | ||
|
|
@@ -2847,3 +2873,4 @@ def forward( | |
| hidden_states = attn_output + hidden_states | ||
|
|
||
| return hidden_states | ||
|
|
||
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -82,9 +82,11 @@ def __init__( | |||||
| norm_num_groups: int = 32, | ||||||
| vq_embed_dim: Optional[int] = None, | ||||||
| scaling_factor: float = 0.18215, | ||||||
| norm_type: str = "default" | ||||||
|
Collaborator
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
|
||||||
| ): | ||||||
| super().__init__() | ||||||
|
|
||||||
|
|
||||||
| # pass init params to Encoder | ||||||
| self.encoder = Encoder( | ||||||
| in_channels=in_channels, | ||||||
|
|
@@ -112,6 +114,7 @@ def __init__( | |||||
| layers_per_block=layers_per_block, | ||||||
| act_fn=act_fn, | ||||||
| norm_num_groups=norm_num_groups, | ||||||
| norm_type=norm_type, | ||||||
| ) | ||||||
|
|
||||||
| def encode(self, x: torch.FloatTensor, return_dict: bool = True) -> VQEncoderOutput: | ||||||
|
|
@@ -131,8 +134,8 @@ def decode( | |||||
| quant, emb_loss, info = self.quantize(h) | ||||||
| else: | ||||||
| quant = h | ||||||
| quant = self.post_quant_conv(quant) | ||||||
| dec = self.decoder(quant) | ||||||
| quant2 = self.post_quant_conv(quant) | ||||||
| dec = self.decoder(quant2, quant if self.config.norm_type == "spatial" else None) | ||||||
|
|
||||||
| if not return_dict: | ||||||
| return (dec,) | ||||||
|
|
||||||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.