2727from ..modeling_utils import ModelMixin
2828from .attention import AttentionBlock
2929from .embeddings import GaussianFourierProjection , get_timestep_embedding
30- from .resnet import downsample_2d , upfirdn2d , upsample_2d
30+ from .resnet import downsample_2d , upfirdn2d , upsample_2d , Downsample , Upsample
3131from .resnet import ResnetBlock
3232
3333
@@ -185,37 +185,39 @@ def forward(self, x, y):
185185
186186
187187class FirUpsample (nn .Module ):
188- def __init__ (self , in_ch = None , out_ch = None , with_conv = False , fir_kernel = (1 , 3 , 3 , 1 )):
188+ def __init__ (self , channels = None , out_channels = None , use_conv = False , fir_kernel = (1 , 3 , 3 , 1 )):
189189 super ().__init__ ()
190- out_ch = out_ch if out_ch else in_ch
191- if with_conv :
192- self .Conv2d_0 = Conv2d (in_ch , out_ch , kernel_size = 3 , stride = 1 , padding = 1 )
193- self .with_conv = with_conv
190+ out_channels = out_channels if out_channels else channels
191+ if use_conv :
192+ self .Conv2d_0 = Conv2d (channels , out_channels , kernel_size = 3 , stride = 1 , padding = 1 )
193+ self .use_conv = use_conv
194194 self .fir_kernel = fir_kernel
195- self .out_ch = out_ch
195+ self .out_channels = out_channels
196196
197197 def forward (self , x ):
198- if self .with_conv :
198+ if self .use_conv :
199199 h = _upsample_conv_2d (x , self .Conv2d_0 .weight , k = self .fir_kernel )
200+ h = h + self .Conv2d_0 .bias .reshape (1 , - 1 , 1 , 1 )
200201 else :
201202 h = upsample_2d (x , self .fir_kernel , factor = 2 )
202203
203204 return h
204205
205206
206207class FirDownsample (nn .Module ):
207- def __init__ (self , in_ch = None , out_ch = None , with_conv = False , fir_kernel = (1 , 3 , 3 , 1 )):
208+ def __init__ (self , channels = None , out_channels = None , use_conv = False , fir_kernel = (1 , 3 , 3 , 1 )):
208209 super ().__init__ ()
209- out_ch = out_ch if out_ch else in_ch
210- if with_conv :
211- self .Conv2d_0 = self .Conv2d_0 = Conv2d (in_ch , out_ch , kernel_size = 3 , stride = 1 , padding = 1 )
210+ out_channels = out_channels if out_channels else channels
211+ if use_conv :
212+ self .Conv2d_0 = self .Conv2d_0 = Conv2d (channels , out_channels , kernel_size = 3 , stride = 1 , padding = 1 )
212213 self .fir_kernel = fir_kernel
213- self .with_conv = with_conv
214- self .out_ch = out_ch
214+ self .use_conv = use_conv
215+ self .out_channels = out_channels
215216
216217 def forward (self , x ):
217- if self .with_conv :
218+ if self .use_conv :
218219 x = _conv_downsample_2d (x , self .Conv2d_0 .weight , k = self .fir_kernel )
220+ x = x + self .Conv2d_0 .bias .reshape (1 , - 1 , 1 , 1 )
219221 else :
220222 x = downsample_2d (x , self .fir_kernel , factor = 2 )
221223
@@ -229,13 +231,14 @@ def __init__(
229231 self ,
230232 image_size = 1024 ,
231233 num_channels = 3 ,
234+ centered = False ,
232235 attn_resolutions = (16 ,),
233236 ch_mult = (1 , 2 , 4 , 8 , 16 , 32 , 32 , 32 ),
234237 conditional = True ,
235238 conv_size = 3 ,
236239 dropout = 0.0 ,
237240 embedding_type = "fourier" ,
238- fir = True , # TODO (patil-suraj) remove this option from here and pre-trained model configs
241+ fir = True ,
239242 fir_kernel = (1 , 3 , 3 , 1 ),
240243 fourier_scale = 16 ,
241244 init_scale = 0.0 ,
@@ -253,12 +256,14 @@ def __init__(
253256 self .register_to_config (
254257 image_size = image_size ,
255258 num_channels = num_channels ,
259+ centered = centered ,
256260 attn_resolutions = attn_resolutions ,
257261 ch_mult = ch_mult ,
258262 conditional = conditional ,
259263 conv_size = conv_size ,
260264 dropout = dropout ,
261265 embedding_type = embedding_type ,
266+ fir = fir ,
262267 fir_kernel = fir_kernel ,
263268 fourier_scale = fourier_scale ,
264269 init_scale = init_scale ,
@@ -308,21 +313,26 @@ def __init__(
308313 modules .append (Linear (nf * 4 , nf * 4 ))
309314
310315 AttnBlock = functools .partial (AttentionBlock , overwrite_linear = True , rescale_output_factor = math .sqrt (2.0 ))
311- Up_sample = functools .partial (FirUpsample , with_conv = resamp_with_conv , fir_kernel = fir_kernel )
316+
317+ if self .fir :
318+ Up_sample = functools .partial (FirUpsample , fir_kernel = fir_kernel , use_conv = resamp_with_conv )
319+ else :
320+ Up_sample = functools .partial (Upsample , name = "Conv2d_0" )
312321
313322 if progressive == "output_skip" :
314- self .pyramid_upsample = Up_sample (fir_kernel = fir_kernel , with_conv = False )
323+ self .pyramid_upsample = Up_sample (channels = None , use_conv = False )
315324 elif progressive == "residual" :
316- pyramid_upsample = functools .partial (Up_sample , fir_kernel = fir_kernel , with_conv = True )
325+ pyramid_upsample = functools .partial (Up_sample , use_conv = True )
317326
318- Down_sample = functools .partial (FirDownsample , with_conv = resamp_with_conv , fir_kernel = fir_kernel )
327+ if self .fir :
328+ Down_sample = functools .partial (FirDownsample , fir_kernel = fir_kernel , use_conv = resamp_with_conv )
329+ else :
330+ Down_sample = functools .partial (Downsample , padding = 0 , name = "Conv2d_0" )
319331
320332 if progressive_input == "input_skip" :
321- self .pyramid_downsample = Down_sample (fir_kernel = fir_kernel , with_conv = False )
333+ self .pyramid_downsample = Down_sample (channels = None , use_conv = False )
322334 elif progressive_input == "residual" :
323- pyramid_downsample = functools .partial (Down_sample , fir_kernel = fir_kernel , with_conv = True )
324-
325- # Downsampling block
335+ pyramid_downsample = functools .partial (Down_sample , use_conv = True )
326336
327337 channels = num_channels
328338 if progressive_input != "none" :
@@ -376,7 +386,7 @@ def __init__(
376386 in_ch *= 2
377387
378388 elif progressive_input == "residual" :
379- modules .append (pyramid_downsample (in_ch = input_pyramid_ch , out_ch = in_ch ))
389+ modules .append (pyramid_downsample (channels = input_pyramid_ch , out_channels = in_ch ))
380390 input_pyramid_ch = in_ch
381391
382392 hs_c .append (in_ch )
@@ -448,7 +458,7 @@ def __init__(
448458 )
449459 pyramid_ch = channels
450460 elif progressive == "residual" :
451- modules .append (pyramid_upsample (in_ch = pyramid_ch , out_ch = in_ch ))
461+ modules .append (pyramid_upsample (channels = pyramid_ch , out_channels = in_ch ))
452462 pyramid_ch = in_ch
453463 else :
454464 raise ValueError (f"{ progressive } is not a valid name" )
@@ -505,7 +515,8 @@ def forward(self, x, timesteps, sigmas=None):
505515 temb = None
506516
507517 # If input data is in [0, 1]
508- x = 2 * x - 1.0
518+ if not self .config .centered :
519+ x = 2 * x - 1.0
509520
510521 # Downsampling block
511522 input_pyramid = None
0 commit comments