1818import torch .nn .functional as F
1919from torch import nn
2020
21- from .attention import AdaGroupNorm
21+ from .attention import AdaGroupNorm , AttentionBlock , SpatialNorm
2222from .attention_processor import Attention , AttnAddedKVProcessor , AttnAddedKVProcessor2_0
2323from .dual_transformer_2d import DualTransformer2DModel
2424from .resnet import Downsample2D , FirDownsample2D , FirUpsample2D , KDownsample2D , KUpsample2D , ResnetBlock2D , Upsample2D
@@ -348,6 +348,7 @@ 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
351352 )
352353 elif up_block_type == "AttnUpDecoderBlock2D" :
353354 return AttnUpDecoderBlock2D (
@@ -360,6 +361,7 @@ def get_up_block(
360361 resnet_groups = resnet_groups ,
361362 attn_num_head_channels = attn_num_head_channels ,
362363 resnet_time_scale_shift = resnet_time_scale_shift ,
364+ temb_channels = temb_channels
363365 )
364366 elif up_block_type == "KUpBlock2D" :
365367 return KUpBlock2D (
@@ -406,7 +408,6 @@ def __init__(
406408 super ().__init__ ()
407409 resnet_groups = resnet_groups if resnet_groups is not None else min (in_channels // 4 , 32 )
408410 self .add_attention = add_attention
409-
410411 # there is always at least one resnet
411412 resnets = [
412413 ResnetBlock2D (
@@ -439,7 +440,6 @@ def __init__(
439440 upcast_softmax = True ,
440441 _from_deprecated_attn_block = True ,
441442 )
442- )
443443 else :
444444 attentions .append (None )
445445
@@ -465,7 +465,8 @@ 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 )
468+ hidden_states = attn (hidden_states , temb )
469+
469470 hidden_states = resnet (hidden_states , temb )
470471
471472 return hidden_states
@@ -1971,6 +1972,30 @@ def custom_forward(*inputs):
19711972 return hidden_states
19721973
19731974
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+
19741999class UpDecoderBlock2D (nn .Module ):
19752000 def __init__ (
19762001 self ,
@@ -1985,6 +2010,7 @@ def __init__(
19852010 resnet_pre_norm : bool = True ,
19862011 output_scale_factor = 1.0 ,
19872012 add_upsample = True ,
2013+ temb_channels = None
19882014 ):
19892015 super ().__init__ ()
19902016 resnets = []
@@ -1996,7 +2022,7 @@ def __init__(
19962022 ResnetBlock2D (
19972023 in_channels = input_channels ,
19982024 out_channels = out_channels ,
1999- temb_channels = None ,
2025+ temb_channels = temb_channels ,
20002026 eps = resnet_eps ,
20012027 groups = resnet_groups ,
20022028 dropout = dropout ,
@@ -2014,9 +2040,9 @@ def __init__(
20142040 else :
20152041 self .upsamplers = None
20162042
2017- def forward (self , hidden_states ):
2043+ def forward (self , hidden_states , temb = None ):
20182044 for resnet in self .resnets :
2019- hidden_states = resnet (hidden_states , temb = None )
2045+ hidden_states = resnet (hidden_states , temb = temb )
20202046
20212047 if self .upsamplers is not None :
20222048 for upsampler in self .upsamplers :
@@ -2040,6 +2066,7 @@ def __init__(
20402066 attn_num_head_channels = 1 ,
20412067 output_scale_factor = 1.0 ,
20422068 add_upsample = True ,
2069+ temb_channels = None
20432070 ):
20442071 super ().__init__ ()
20452072 resnets = []
@@ -2052,7 +2079,7 @@ def __init__(
20522079 ResnetBlock2D (
20532080 in_channels = input_channels ,
20542081 out_channels = out_channels ,
2055- temb_channels = None ,
2082+ temb_channels = temb_channels ,
20562083 eps = resnet_eps ,
20572084 groups = resnet_groups ,
20582085 dropout = dropout ,
@@ -2075,7 +2102,6 @@ def __init__(
20752102 upcast_softmax = True ,
20762103 _from_deprecated_attn_block = True ,
20772104 )
2078- )
20792105
20802106 self .attentions = nn .ModuleList (attentions )
20812107 self .resnets = nn .ModuleList (resnets )
@@ -2085,10 +2111,10 @@ def __init__(
20852111 else :
20862112 self .upsamplers = None
20872113
2088- def forward (self , hidden_states ):
2114+ def forward (self , hidden_states , temb = None ):
20892115 for resnet , attn in zip (self .resnets , self .attentions ):
2090- hidden_states = resnet (hidden_states , temb = None )
2091- hidden_states = attn (hidden_states )
2116+ hidden_states = resnet (hidden_states , temb = temb )
2117+ hidden_states = attn (hidden_states , temb )
20922118
20932119 if self .upsamplers is not None :
20942120 for upsampler in self .upsamplers :
@@ -2847,3 +2873,4 @@ def forward(
28472873 hidden_states = attn_output + hidden_states
28482874
28492875 return hidden_states
2876+
0 commit comments