@@ -42,7 +42,7 @@ def __init__(
4242 self .value = nn .Linear (channels , channels )
4343
4444 self .rescale_output_factor = rescale_output_factor
45- self .proj_attn = zero_module ( nn .Linear (channels , channels , 1 ) )
45+ self .proj_attn = nn .Linear (channels , channels , 1 )
4646
4747 def transpose_for_scores (self , projection : torch .Tensor ) -> torch .Tensor :
4848 new_projection_shape = projection .size ()[:- 1 ] + (self .num_heads , - 1 )
@@ -147,6 +147,8 @@ class SpatialTransformer(nn.Module):
147147
148148 def __init__ (self , in_channels , n_heads , d_head , depth = 1 , dropout = 0.0 , context_dim = None ):
149149 super ().__init__ ()
150+ self .n_heads = n_heads
151+ self .d_head = d_head
150152 self .in_channels = in_channels
151153 inner_dim = n_heads * d_head
152154 self .norm = torch .nn .GroupNorm (num_groups = 32 , num_channels = in_channels , eps = 1e-6 , affine = True )
@@ -160,7 +162,7 @@ def __init__(self, in_channels, n_heads, d_head, depth=1, dropout=0.0, context_d
160162 ]
161163 )
162164
163- self .proj_out = zero_module ( nn .Conv2d (inner_dim , in_channels , kernel_size = 1 , stride = 1 , padding = 0 ) )
165+ self .proj_out = nn .Conv2d (inner_dim , in_channels , kernel_size = 1 , stride = 1 , padding = 0 )
164166
165167 def forward (self , x , context = None ):
166168 # note: if no context is given, cross-attention defaults to self-attention
@@ -175,6 +177,12 @@ def forward(self, x, context=None):
175177 x = self .proj_out (x )
176178 return x + x_in
177179
180+ def set_weight (self , layer ):
181+ self .norm = layer .norm
182+ self .proj_in = layer .proj_in
183+ self .transformer_blocks = layer .transformer_blocks
184+ self .proj_out = layer .proj_out
185+
178186
179187class BasicTransformerBlock (nn .Module ):
180188 def __init__ (self , dim , n_heads , d_head , dropout = 0.0 , context_dim = None , gated_ff = True , checkpoint = True ):
@@ -270,14 +278,15 @@ def forward(self, x):
270278 return self .net (x )
271279
272280
273- # TODO(Patrick) - this can and should be removed
274- def zero_module (module ):
275- """
276- Zero out the parameters of a module and return it.
277- """
278- for p in module .parameters ():
279- p .detach ().zero_ ()
280- return module
281+ # feedforward
282+ class GEGLU (nn .Module ):
283+ def __init__ (self , dim_in , dim_out ):
284+ super ().__init__ ()
285+ self .proj = nn .Linear (dim_in , dim_out * 2 )
286+
287+ def forward (self , x ):
288+ x , gate = self .proj (x ).chunk (2 , dim = - 1 )
289+ return x * F .gelu (gate )
281290
282291
283292# TODO(Patrick) - remove once all weights have been converted -> not needed anymore then
@@ -298,17 +307,6 @@ def default(val, d):
298307 return d () if isfunction (d ) else d
299308
300309
301- # feedforward
302- class GEGLU (nn .Module ):
303- def __init__ (self , dim_in , dim_out ):
304- super ().__init__ ()
305- self .proj = nn .Linear (dim_in , dim_out * 2 )
306-
307- def forward (self , x ):
308- x , gate = self .proj (x ).chunk (2 , dim = - 1 )
309- return x * F .gelu (gate )
310-
311-
312310# the main attention block that is used for all models
313311class AttentionBlock (nn .Module ):
314312 """
@@ -348,7 +346,7 @@ def __init__(
348346 if encoder_channels is not None :
349347 self .encoder_kv = nn .Conv1d (encoder_channels , channels * 2 , 1 )
350348
351- self .proj = zero_module ( nn .Conv1d (channels , channels , 1 ) )
349+ self .proj = nn .Conv1d (channels , channels , 1 )
352350
353351 self .overwrite_qkv = overwrite_qkv
354352 self .overwrite_linear = overwrite_linear
@@ -370,7 +368,7 @@ def __init__(
370368
371369 self .GroupNorm_0 = nn .GroupNorm (num_groups = num_groups , num_channels = channels , eps = 1e-6 )
372370 else :
373- self .proj_out = zero_module ( nn .Conv1d (channels , channels , 1 ) )
371+ self .proj_out = nn .Conv1d (channels , channels , 1 )
374372 self .set_weights (self )
375373
376374 self .is_overwritten = False
@@ -385,7 +383,7 @@ def set_weights(self, module):
385383 self .qkv .weight .data = qkv_weight
386384 self .qkv .bias .data = qkv_bias
387385
388- proj_out = zero_module ( nn .Conv1d (self .channels , self .channels , 1 ) )
386+ proj_out = nn .Conv1d (self .channels , self .channels , 1 )
389387 proj_out .weight .data = module .proj_out .weight .data [:, :, :, 0 ]
390388 proj_out .bias .data = module .proj_out .bias .data
391389
0 commit comments