Skip to content

Commit f0a7497

Browse files
author
yiyixuxu
committed
Revert "[WIP] Add Kandinsky decoder (#3330)"
This reverts commit e74c173.
1 parent e74c173 commit f0a7497

7 files changed

Lines changed: 35 additions & 539 deletions

File tree

scripts/convert_kandinsky_to_diffusers.py

Lines changed: 5 additions & 415 deletions
Large diffs are not rendered by default.

src/diffusers/models/attention.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -369,24 +369,3 @@ def forward(self, x, emb):
369369
x = F.group_norm(x, self.num_groups, eps=self.eps)
370370
x = x * (1 + scale) + shift
371371
return x
372-
373-
class SpatialNorm(nn.Module):
374-
"""
375-
Spatially conditioned normalization as defined in https://arxiv.org/abs/2209.09002
376-
"""
377-
def __init__(
378-
self,
379-
f_channels,
380-
zq_channels,
381-
):
382-
super().__init__()
383-
self.norm_layer = nn.GroupNorm(num_channels=f_channels,num_groups=32,eps=1e-6,affine=True)
384-
self.conv_y = nn.Conv2d(zq_channels, f_channels, kernel_size=1, stride=1, padding=0)
385-
self.conv_b = nn.Conv2d(zq_channels, f_channels, kernel_size=1, stride=1, padding=0)
386-
387-
def forward(self, f, zq):
388-
f_size = f.shape[-2:]
389-
zq = F.interpolate(zq, size=f_size, mode="nearest")
390-
norm_f = self.norm_layer(f)
391-
new_f = norm_f * self.conv_y(zq) + self.conv_b(zq)
392-
return new_f

src/diffusers/models/resnet.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import torch.nn as nn
2121
import torch.nn.functional as F
2222

23-
from .attention import AdaGroupNorm, SpatialNorm
23+
from .attention import AdaGroupNorm
2424

2525

2626
class Upsample1D(nn.Module):
@@ -460,7 +460,7 @@ def __init__(
460460
eps=1e-6,
461461
non_linearity="swish",
462462
skip_time_act=False,
463-
time_embedding_norm="default", # default, scale_shift, ada_group, spatial
463+
time_embedding_norm="default", # default, scale_shift, ada_group
464464
kernel=None,
465465
output_scale_factor=1.0,
466466
use_in_shortcut=None,
@@ -487,8 +487,6 @@ def __init__(
487487

488488
if self.time_embedding_norm == "ada_group":
489489
self.norm1 = AdaGroupNorm(temb_channels, in_channels, groups, eps=eps)
490-
elif self.time_embedding_norm == "spatial":
491-
self.norm1 = SpatialNorm(in_channels, temb_channels)
492490
else:
493491
self.norm1 = torch.nn.GroupNorm(num_groups=groups, num_channels=in_channels, eps=eps, affine=True)
494492

@@ -499,7 +497,7 @@ def __init__(
499497
self.time_emb_proj = torch.nn.Linear(temb_channels, out_channels)
500498
elif self.time_embedding_norm == "scale_shift":
501499
self.time_emb_proj = torch.nn.Linear(temb_channels, 2 * out_channels)
502-
elif self.time_embedding_norm == "ada_group" or self.time_embedding_norm == "spatial":
500+
elif self.time_embedding_norm == "ada_group":
503501
self.time_emb_proj = None
504502
else:
505503
raise ValueError(f"unknown time_embedding_norm : {self.time_embedding_norm} ")
@@ -508,8 +506,6 @@ def __init__(
508506

509507
if self.time_embedding_norm == "ada_group":
510508
self.norm2 = AdaGroupNorm(temb_channels, out_channels, groups_out, eps=eps)
511-
elif self.time_embedding_norm == "spatial":
512-
self.norm2 = SpatialNorm(out_channels, temb_channels)
513509
else:
514510
self.norm2 = torch.nn.GroupNorm(num_groups=groups_out, num_channels=out_channels, eps=eps, affine=True)
515511

@@ -555,7 +551,7 @@ def __init__(
555551
def forward(self, input_tensor, temb):
556552
hidden_states = input_tensor
557553

558-
if self.time_embedding_norm == "ada_group" or self.time_embedding_norm == "spatial":
554+
if self.time_embedding_norm == "ada_group":
559555
hidden_states = self.norm1(hidden_states, temb)
560556
else:
561557
hidden_states = self.norm1(hidden_states)
@@ -583,7 +579,7 @@ def forward(self, input_tensor, temb):
583579
if temb is not None and self.time_embedding_norm == "default":
584580
hidden_states = hidden_states + temb
585581

586-
if self.time_embedding_norm == "ada_group" or self.time_embedding_norm == "spatial":
582+
if self.time_embedding_norm == "ada_group":
587583
hidden_states = self.norm2(hidden_states, temb)
588584
else:
589585
hidden_states = self.norm2(hidden_states)

src/diffusers/models/unet_2d_blocks.py

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import torch.nn.functional as F
1919
from torch import nn
2020

21-
from .attention import AdaGroupNorm, AttentionBlock, SpatialNorm
21+
from .attention import AdaGroupNorm
2222
from .attention_processor import Attention, AttnAddedKVProcessor, AttnAddedKVProcessor2_0
2323
from .dual_transformer_2d import DualTransformer2DModel
2424
from .resnet import Downsample2D, FirDownsample2D, FirUpsample2D, KDownsample2D, KUpsample2D, ResnetBlock2D, Upsample2D
@@ -348,7 +348,6 @@ def get_up_block(
348348
resnet_act_fn=resnet_act_fn,
349349
resnet_groups=resnet_groups,
350350
resnet_time_scale_shift=resnet_time_scale_shift,
351-
temb_channels=temb_channels
352351
)
353352
elif up_block_type == "AttnUpDecoderBlock2D":
354353
return AttnUpDecoderBlock2D(
@@ -361,7 +360,6 @@ def get_up_block(
361360
resnet_groups=resnet_groups,
362361
attn_num_head_channels=attn_num_head_channels,
363362
resnet_time_scale_shift=resnet_time_scale_shift,
364-
temb_channels=temb_channels
365363
)
366364
elif up_block_type == "KUpBlock2D":
367365
return KUpBlock2D(
@@ -408,6 +406,7 @@ def __init__(
408406
super().__init__()
409407
resnet_groups = resnet_groups if resnet_groups is not None else min(in_channels // 4, 32)
410408
self.add_attention = add_attention
409+
411410
# there is always at least one resnet
412411
resnets = [
413412
ResnetBlock2D(
@@ -440,6 +439,7 @@ def __init__(
440439
upcast_softmax=True,
441440
_from_deprecated_attn_block=True,
442441
)
442+
)
443443
else:
444444
attentions.append(None)
445445

@@ -465,8 +465,7 @@ def forward(self, hidden_states, temb=None):
465465
hidden_states = self.resnets[0](hidden_states, temb)
466466
for attn, resnet in zip(self.attentions, self.resnets[1:]):
467467
if attn is not None:
468-
hidden_states = attn(hidden_states, temb)
469-
468+
hidden_states = attn(hidden_states)
470469
hidden_states = resnet(hidden_states, temb)
471470

472471
return hidden_states
@@ -1972,30 +1971,6 @@ def custom_forward(*inputs):
19721971
return hidden_states
19731972

19741973

1975-
class MOVQAttention(nn.Module):
1976-
def __init__(self, query_dim, temb_channels, attn_num_head_channels):
1977-
super().__init__()
1978-
1979-
self.norm = SpatialNorm(query_dim, temb_channels)
1980-
num_heads = query_dim // attn_num_head_channels if attn_num_head_channels is not None else 1
1981-
dim_head = attn_num_head_channels if attn_num_head_channels is not None else query_dim
1982-
self.attention = Attention(
1983-
query_dim=query_dim,
1984-
heads=num_heads,
1985-
dim_head=dim_head,
1986-
bias=True
1987-
)
1988-
1989-
def forward(self, hidden_states, temb):
1990-
residual = hidden_states
1991-
hidden_states = self.norm(hidden_states, temb).view(hidden_states.shape[0], hidden_states.shape[1], -1)
1992-
hidden_states = self.attention(hidden_states.transpose(1, 2), None, None).transpose(1, 2)
1993-
hidden_states = hidden_states.view(residual.shape)
1994-
hidden_states = hidden_states + residual
1995-
return hidden_states
1996-
1997-
1998-
19991974
class UpDecoderBlock2D(nn.Module):
20001975
def __init__(
20011976
self,
@@ -2010,7 +1985,6 @@ def __init__(
20101985
resnet_pre_norm: bool = True,
20111986
output_scale_factor=1.0,
20121987
add_upsample=True,
2013-
temb_channels=None
20141988
):
20151989
super().__init__()
20161990
resnets = []
@@ -2022,7 +1996,7 @@ def __init__(
20221996
ResnetBlock2D(
20231997
in_channels=input_channels,
20241998
out_channels=out_channels,
2025-
temb_channels=temb_channels,
1999+
temb_channels=None,
20262000
eps=resnet_eps,
20272001
groups=resnet_groups,
20282002
dropout=dropout,
@@ -2040,9 +2014,9 @@ def __init__(
20402014
else:
20412015
self.upsamplers = None
20422016

2043-
def forward(self, hidden_states, temb=None):
2017+
def forward(self, hidden_states):
20442018
for resnet in self.resnets:
2045-
hidden_states = resnet(hidden_states, temb=temb)
2019+
hidden_states = resnet(hidden_states, temb=None)
20462020

20472021
if self.upsamplers is not None:
20482022
for upsampler in self.upsamplers:
@@ -2066,7 +2040,6 @@ def __init__(
20662040
attn_num_head_channels=1,
20672041
output_scale_factor=1.0,
20682042
add_upsample=True,
2069-
temb_channels=None
20702043
):
20712044
super().__init__()
20722045
resnets = []
@@ -2079,7 +2052,7 @@ def __init__(
20792052
ResnetBlock2D(
20802053
in_channels=input_channels,
20812054
out_channels=out_channels,
2082-
temb_channels=temb_channels,
2055+
temb_channels=None,
20832056
eps=resnet_eps,
20842057
groups=resnet_groups,
20852058
dropout=dropout,
@@ -2102,6 +2075,7 @@ def __init__(
21022075
upcast_softmax=True,
21032076
_from_deprecated_attn_block=True,
21042077
)
2078+
)
21052079

21062080
self.attentions = nn.ModuleList(attentions)
21072081
self.resnets = nn.ModuleList(resnets)
@@ -2111,10 +2085,10 @@ def __init__(
21112085
else:
21122086
self.upsamplers = None
21132087

2114-
def forward(self, hidden_states, temb=None):
2088+
def forward(self, hidden_states):
21152089
for resnet, attn in zip(self.resnets, self.attentions):
2116-
hidden_states = resnet(hidden_states, temb=temb)
2117-
hidden_states = attn(hidden_states, temb)
2090+
hidden_states = resnet(hidden_states, temb=None)
2091+
hidden_states = attn(hidden_states)
21182092

21192093
if self.upsamplers is not None:
21202094
for upsampler in self.upsamplers:
@@ -2873,4 +2847,3 @@ def forward(
28732847
hidden_states = attn_output + hidden_states
28742848

28752849
return hidden_states
2876-

src/diffusers/models/vae.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from ..utils import BaseOutput, randn_tensor
2222
from .unet_2d_blocks import UNetMidBlock2D, get_down_block, get_up_block
23-
from .attention import SpatialNorm
23+
2424

2525
@dataclass
2626
class DecoderOutput(BaseOutput):
@@ -149,7 +149,6 @@ def __init__(
149149
layers_per_block=2,
150150
norm_num_groups=32,
151151
act_fn="silu",
152-
norm_type="default", # default, spatial
153152
):
154153
super().__init__()
155154
self.layers_per_block = layers_per_block
@@ -165,19 +164,16 @@ def __init__(
165164
self.mid_block = None
166165
self.up_blocks = nn.ModuleList([])
167166

168-
169-
temb_channels = in_channels if norm_type == "spatial" else None
170-
171167
# mid
172168
self.mid_block = UNetMidBlock2D(
173169
in_channels=block_out_channels[-1],
174170
resnet_eps=1e-6,
175171
resnet_act_fn=act_fn,
176172
output_scale_factor=1,
177-
resnet_time_scale_shift=norm_type,
173+
resnet_time_scale_shift="default",
178174
attn_num_head_channels=None,
179175
resnet_groups=norm_num_groups,
180-
temb_channels=temb_channels,
176+
temb_channels=None,
181177
)
182178

183179
# up
@@ -200,23 +196,19 @@ def __init__(
200196
resnet_act_fn=act_fn,
201197
resnet_groups=norm_num_groups,
202198
attn_num_head_channels=None,
203-
temb_channels=temb_channels,
204-
resnet_time_scale_shift=norm_type,
199+
temb_channels=None,
205200
)
206201
self.up_blocks.append(up_block)
207202
prev_output_channel = output_channel
208203

209204
# out
210-
if norm_type == "spatial":
211-
self.conv_norm_out = SpatialNorm(block_out_channels[0], temb_channels)
212-
else:
213-
self.conv_norm_out = nn.GroupNorm(num_channels=block_out_channels[0], num_groups=norm_num_groups, eps=1e-6)
205+
self.conv_norm_out = nn.GroupNorm(num_channels=block_out_channels[0], num_groups=norm_num_groups, eps=1e-6)
214206
self.conv_act = nn.SiLU()
215207
self.conv_out = nn.Conv2d(block_out_channels[0], out_channels, 3, padding=1)
216208

217209
self.gradient_checkpointing = False
218210

219-
def forward(self, z, zq=None):
211+
def forward(self, z):
220212
sample = z
221213
sample = self.conv_in(sample)
222214

@@ -238,18 +230,15 @@ def custom_forward(*inputs):
238230
sample = torch.utils.checkpoint.checkpoint(create_custom_forward(up_block), sample)
239231
else:
240232
# middle
241-
sample = self.mid_block(sample, zq)
233+
sample = self.mid_block(sample)
242234
sample = sample.to(upscale_dtype)
243235

244236
# up
245237
for up_block in self.up_blocks:
246-
sample = up_block(sample, zq)
238+
sample = up_block(sample)
247239

248240
# post-process
249-
if zq is None:
250-
sample = self.conv_norm_out(sample)
251-
else:
252-
sample = self.conv_norm_out(sample, zq)
241+
sample = self.conv_norm_out(sample)
253242
sample = self.conv_act(sample)
254243
sample = self.conv_out(sample)
255244

src/diffusers/models/vq_model.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,9 @@ def __init__(
8282
norm_num_groups: int = 32,
8383
vq_embed_dim: Optional[int] = None,
8484
scaling_factor: float = 0.18215,
85-
norm_type: str = "default"
8685
):
8786
super().__init__()
8887

89-
9088
# pass init params to Encoder
9189
self.encoder = Encoder(
9290
in_channels=in_channels,
@@ -114,7 +112,6 @@ def __init__(
114112
layers_per_block=layers_per_block,
115113
act_fn=act_fn,
116114
norm_num_groups=norm_num_groups,
117-
norm_type=norm_type,
118115
)
119116

120117
def encode(self, x: torch.FloatTensor, return_dict: bool = True) -> VQEncoderOutput:
@@ -134,8 +131,8 @@ def decode(
134131
quant, emb_loss, info = self.quantize(h)
135132
else:
136133
quant = h
137-
quant2 = self.post_quant_conv(quant)
138-
dec = self.decoder(quant2, quant if self.config.norm_type == "spatial" else None)
134+
quant = self.post_quant_conv(quant)
135+
dec = self.decoder(quant)
139136

140137
if not return_dict:
141138
return (dec,)

0 commit comments

Comments
 (0)