3636HEAD = common_types .HEAD
3737D_KV = common_types .D_KV
3838
39+
3940@flax .struct .dataclass
4041class Transformer2DModelOutput (BaseOutput ):
4142 """
@@ -111,8 +112,8 @@ class FluxSingleTransformerBlock(nn.Module):
111112 dtype : jnp .dtype = jnp .float32
112113 weights_dtype : jnp .dtype = jnp .float32
113114 precision : jax .lax .Precision = None
114- use_global_modulation : bool = False # Added flag!
115- use_swiglu : bool = False # Added flag!
115+ use_global_modulation : bool = False # Added flag!
116+ use_swiglu : bool = False # Added flag!
116117
117118 def setup (self ):
118119 self .mlp_hidden_dim = int (self .dim * self .mlp_ratio )
@@ -170,7 +171,7 @@ def __call__(self, hidden_states, temb=None, image_rotary_emb=None, temb_mod=Non
170171 shift_msa = jnp .expand_dims (shift_msa , axis = 1 )
171172 scale_msa = jnp .expand_dims (scale_msa , axis = 1 )
172173 gate = jnp .expand_dims (gate , axis = 1 )
173-
174+
174175 norm_hidden_states = self .norm (hidden_states )
175176 norm_hidden_states = (1 + scale_msa ) * norm_hidden_states + shift_msa
176177 else :
@@ -244,16 +245,24 @@ class FluxTransformerBlock(nn.Module):
244245 mlp_ratio : float = 4.0
245246 qkv_bias : bool = False
246247 attention_kernel : str = "dot_product"
247- use_global_modulation : bool = False # Added flag!
248- use_swiglu : bool = False # Added flag!
248+ use_global_modulation : bool = False # Added flag!
249+ use_swiglu : bool = False # Added flag!
249250
250251 def setup (self ):
251252 if self .use_global_modulation :
252- self .img_norm1 = nn .LayerNorm (use_bias = False , use_scale = False , epsilon = self .eps , dtype = self .dtype , param_dtype = self .weights_dtype )
253- self .txt_norm1 = nn .LayerNorm (use_bias = False , use_scale = False , epsilon = self .eps , dtype = self .dtype , param_dtype = self .weights_dtype )
253+ self .img_norm1 = nn .LayerNorm (
254+ use_bias = False , use_scale = False , epsilon = self .eps , dtype = self .dtype , param_dtype = self .weights_dtype
255+ )
256+ self .txt_norm1 = nn .LayerNorm (
257+ use_bias = False , use_scale = False , epsilon = self .eps , dtype = self .dtype , param_dtype = self .weights_dtype
258+ )
254259 else :
255- self .img_norm1 = AdaLayerNormZero (self .dim , dtype = self .dtype , weights_dtype = self .weights_dtype , precision = self .precision )
256- self .txt_norm1 = AdaLayerNormZero (self .dim , dtype = self .dtype , weights_dtype = self .weights_dtype , precision = self .precision )
260+ self .img_norm1 = AdaLayerNormZero (
261+ self .dim , dtype = self .dtype , weights_dtype = self .weights_dtype , precision = self .precision
262+ )
263+ self .txt_norm1 = AdaLayerNormZero (
264+ self .dim , dtype = self .dtype , weights_dtype = self .weights_dtype , precision = self .precision
265+ )
257266
258267 self .attn = FlaxFluxAttention (
259268 query_dim = self .dim ,
@@ -285,27 +294,30 @@ def setup(self):
285294 name = "img_mlp" ,
286295 )
287296 else :
288- self .img_mlp = nn .Sequential ([
289- nn .Dense (
290- int (self .dim * self .mlp_ratio ),
291- use_bias = True ,
292- kernel_init = nn .with_logical_partitioning (nn .initializers .lecun_normal (), ("embed" , "mlp" )),
293- bias_init = nn .with_logical_partitioning (nn .initializers .zeros , (None ,)),
294- dtype = self .dtype ,
295- param_dtype = self .weights_dtype ,
296- precision = self .precision ,
297- ),
298- nn .gelu ,
299- nn .Dense (
300- self .dim ,
301- use_bias = True ,
302- kernel_init = nn .with_logical_partitioning (nn .initializers .lecun_normal (), ("mlp" , "embed" )),
303- bias_init = nn .with_logical_partitioning (nn .initializers .zeros , (None ,)),
304- dtype = self .dtype ,
305- param_dtype = self .weights_dtype ,
306- precision = self .precision ,
307- ),
308- ], name = "img_mlp" )
297+ self .img_mlp = nn .Sequential (
298+ [
299+ nn .Dense (
300+ int (self .dim * self .mlp_ratio ),
301+ use_bias = True ,
302+ kernel_init = nn .with_logical_partitioning (nn .initializers .lecun_normal (), ("embed" , "mlp" )),
303+ bias_init = nn .with_logical_partitioning (nn .initializers .zeros , (None ,)),
304+ dtype = self .dtype ,
305+ param_dtype = self .weights_dtype ,
306+ precision = self .precision ,
307+ ),
308+ nn .gelu ,
309+ nn .Dense (
310+ self .dim ,
311+ use_bias = True ,
312+ kernel_init = nn .with_logical_partitioning (nn .initializers .lecun_normal (), ("mlp" , "embed" )),
313+ bias_init = nn .with_logical_partitioning (nn .initializers .zeros , (None ,)),
314+ dtype = self .dtype ,
315+ param_dtype = self .weights_dtype ,
316+ precision = self .precision ,
317+ ),
318+ ],
319+ name = "img_mlp" ,
320+ )
309321
310322 self .txt_norm2 = nn .LayerNorm (
311323 use_bias = False ,
@@ -325,56 +337,60 @@ def setup(self):
325337 name = "txt_mlp" ,
326338 )
327339 else :
328- self .txt_mlp = nn .Sequential ([
329- nn .Dense (
330- int (self .dim * self .mlp_ratio ),
331- use_bias = True ,
332- kernel_init = nn .with_logical_partitioning (nn .initializers .lecun_normal (), ("embed" , "mlp" )),
333- bias_init = nn .with_logical_partitioning (nn .initializers .zeros , (None ,)),
334- dtype = self .dtype ,
335- param_dtype = self .weights_dtype ,
336- precision = self .precision ,
337- ),
338- nn .gelu ,
339- nn .Dense (
340- self .dim ,
341- use_bias = True ,
342- kernel_init = nn .with_logical_partitioning (nn .initializers .lecun_normal (), ("mlp" , "embed" )),
343- bias_init = nn .with_logical_partitioning (nn .initializers .zeros , (None ,)),
344- dtype = self .dtype ,
345- param_dtype = self .weights_dtype ,
346- precision = self .precision ,
347- ),
348- ], name = "txt_mlp" )
340+ self .txt_mlp = nn .Sequential (
341+ [
342+ nn .Dense (
343+ int (self .dim * self .mlp_ratio ),
344+ use_bias = True ,
345+ kernel_init = nn .with_logical_partitioning (nn .initializers .lecun_normal (), ("embed" , "mlp" )),
346+ bias_init = nn .with_logical_partitioning (nn .initializers .zeros , (None ,)),
347+ dtype = self .dtype ,
348+ param_dtype = self .weights_dtype ,
349+ precision = self .precision ,
350+ ),
351+ nn .gelu ,
352+ nn .Dense (
353+ self .dim ,
354+ use_bias = True ,
355+ kernel_init = nn .with_logical_partitioning (nn .initializers .lecun_normal (), ("mlp" , "embed" )),
356+ bias_init = nn .with_logical_partitioning (nn .initializers .zeros , (None ,)),
357+ dtype = self .dtype ,
358+ param_dtype = self .weights_dtype ,
359+ precision = self .precision ,
360+ ),
361+ ],
362+ name = "txt_mlp" ,
363+ )
349364
350365 # let chunk size default to None
351366 self ._chunk_size = None
352367 self ._chunk_dim = 0
353368
354- def __call__ (self , hidden_states , encoder_hidden_states , temb = None , image_rotary_emb = None ,
355- temb_mod_img = None , temb_mod_txt = None ):
369+ def __call__ (
370+ self , hidden_states , encoder_hidden_states , temb = None , image_rotary_emb = None , temb_mod_img = None , temb_mod_txt = None
371+ ):
356372 if self .use_global_modulation :
357373 (shift_msa , scale_msa , gate_msa , shift_mlp , scale_mlp , gate_mlp ) = jnp .split (temb_mod_img , 6 , axis = - 1 )
358374 (c_shift_msa , c_scale_msa , c_gate_msa , c_shift_mlp , c_scale_mlp , c_gate_mlp ) = jnp .split (temb_mod_txt , 6 , axis = - 1 )
359-
375+
360376 # Unsqueeze sequence dimension for broadcasting when batch_size > 1
361377 shift_msa = jnp .expand_dims (shift_msa , axis = 1 )
362378 scale_msa = jnp .expand_dims (scale_msa , axis = 1 )
363379 gate_msa = jnp .expand_dims (gate_msa , axis = 1 )
364380 shift_mlp = jnp .expand_dims (shift_mlp , axis = 1 )
365381 scale_mlp = jnp .expand_dims (scale_mlp , axis = 1 )
366382 gate_mlp = jnp .expand_dims (gate_mlp , axis = 1 )
367-
383+
368384 c_shift_msa = jnp .expand_dims (c_shift_msa , axis = 1 )
369385 c_scale_msa = jnp .expand_dims (c_scale_msa , axis = 1 )
370386 c_gate_msa = jnp .expand_dims (c_gate_msa , axis = 1 )
371387 c_shift_mlp = jnp .expand_dims (c_shift_mlp , axis = 1 )
372388 c_scale_mlp = jnp .expand_dims (c_scale_mlp , axis = 1 )
373389 c_gate_mlp = jnp .expand_dims (c_gate_mlp , axis = 1 )
374-
390+
375391 norm_hidden_states = self .img_norm1 (hidden_states )
376392 norm_hidden_states = (1 + scale_msa ) * norm_hidden_states + shift_msa
377-
393+
378394 norm_encoder_hidden_states = self .txt_norm1 (encoder_hidden_states )
379395 norm_encoder_hidden_states = (1 + c_scale_msa ) * norm_encoder_hidden_states + c_shift_msa
380396 else :
@@ -465,8 +481,8 @@ class FluxTransformer2DModel(nn.Module, FlaxModelMixin, ConfigMixin):
465481 joint_attention_bias : bool = True
466482 x_embedder_bias : bool = True
467483 proj_out_bias : bool = True
468- use_global_modulation : bool = False # Added config flag!
469- use_swiglu : bool = False # Added config flag!
484+ use_global_modulation : bool = False # Added config flag!
485+ use_swiglu : bool = False # Added config flag!
470486
471487 def setup (self ):
472488 self .out_channels = self .in_channels
@@ -699,7 +715,7 @@ def __call__(
699715
700716 hidden_states = jnp .concatenate ([encoder_hidden_states , hidden_states ], axis = 1 )
701717 hidden_states = nn .with_logical_constraint (hidden_states , ("activation_batch" , "activation_length" , "activation_embed" ))
702-
718+
703719 for single_block in self .single_blocks :
704720 hidden_states = single_block (
705721 hidden_states = hidden_states ,
0 commit comments