2323from einops import repeat , rearrange
2424from ....configuration_utils import ConfigMixin , flax_register_to_config
2525from ...modeling_flax_utils import FlaxModelMixin
26- from ...normalization_flax import AdaLayerNormZeroSingle , AdaLayerNormContinuous , AdaLayerNormZero
26+ from ...normalization_flax import (
27+ AdaLayerNormZeroSingle ,
28+ AdaLayerNormContinuous ,
29+ AdaLayerNormZero ,
30+ NNXAdaLayerNormZeroSingle ,
31+ NNXAdaLayerNormContinuous ,
32+ NNXAdaLayerNormZero ,
33+ )
2734from ...attention_flax import FlaxFluxAttention as FluxAttention , FlaxFluxAttention , apply_rope
2835from flax import nnx
2936from ...embeddings_flax import (
3037 FluxPosEmbed ,
38+ NNXFluxPosEmbed ,
3139 CombinedTimestepGuidanceTextProjEmbeddings ,
3240 CombinedTimestepGuidanceTextProjEmbeddings as CombinedTimestepGuidanceTextEmbeddings ,
3341 CombinedTimestepTextProjEmbeddings ,
@@ -714,6 +722,7 @@ def init_weights(self, rngs, max_sequence_length, eval_only=True):
714722 )["params" ]
715723
716724
725+
717726class FlaxSwiGluFeedForward (nn .Module ):
718727 dim : int
719728 hidden_dim : int
@@ -1334,15 +1343,15 @@ def __init__(
13341343 )
13351344 self .query_norm = nnx .RMSNorm (
13361345 num_features = dim_head ,
1337- eps = 1e-6 ,
1346+ epsilon = 1e-6 ,
13381347 scale_init = nnx .with_partitioning (nnx .initializers .ones , ("heads" ,)),
13391348 dtype = dtype ,
13401349 param_dtype = weights_dtype ,
13411350 rngs = rngs ,
13421351 )
13431352 self .key_norm = nnx .RMSNorm (
13441353 num_features = dim_head ,
1345- eps = 1e-6 ,
1354+ epsilon = 1e-6 ,
13461355 scale_init = nnx .with_partitioning (nnx .initializers .ones , ("heads" ,)),
13471356 dtype = dtype ,
13481357 param_dtype = weights_dtype ,
@@ -1382,8 +1391,7 @@ def __call__(
13821391 v = jnp .concatenate ([v_txt , v_img ], axis = 1 )
13831392
13841393 if image_rotary_emb is not None :
1385- cos , sin = image_rotary_emb
1386- q , k = apply_rope (q , k , cos , sin )
1394+ q , k = apply_rope (q , k , image_rotary_emb )
13871395
13881396 scale = self .dim_head ** - 0.5
13891397 attn_weights = jnp .einsum ("b q h d, b k h d -> b h q k" , q , k , precision = None ) * scale
@@ -1437,15 +1445,15 @@ def __init__(
14371445 )
14381446 self .norm_q = nnx .RMSNorm (
14391447 num_features = attention_head_dim ,
1440- eps = 1e-6 ,
1448+ epsilon = 1e-6 ,
14411449 scale_init = nnx .with_partitioning (nnx .initializers .ones , ("heads" ,)),
14421450 dtype = dtype ,
14431451 param_dtype = weights_dtype ,
14441452 rngs = rngs ,
14451453 )
14461454 self .norm_k = nnx .RMSNorm (
14471455 num_features = attention_head_dim ,
1448- eps = 1e-6 ,
1456+ epsilon = 1e-6 ,
14491457 scale_init = nnx .with_partitioning (nnx .initializers .ones , ("heads" ,)),
14501458 dtype = dtype ,
14511459 param_dtype = weights_dtype ,
@@ -1472,8 +1480,7 @@ def __call__(
14721480 k = self .norm_k (k )
14731481
14741482 if image_rotary_emb is not None :
1475- cos , sin = image_rotary_emb
1476- q , k = apply_rope (q , k , cos , sin )
1483+ q , k = apply_rope (q , k , image_rotary_emb )
14771484
14781485 scale = self .dim_head ** - 0.5
14791486 attn_weights = jnp .einsum ("b q h d, b k h d -> b h q k" , q , k , precision = None ) * scale
@@ -1505,8 +1512,8 @@ def __init__(
15051512 self .head_dim = attention_head_dim
15061513 mlp_hidden_dim = int (dim * mlp_ratio )
15071514
1508- self .img_norm1 = AdaLayerNormZero (dim , dtype = dtype , weights_dtype = weights_dtype )
1509- self .txt_norm1 = AdaLayerNormZero (dim , dtype = dtype , weights_dtype = weights_dtype )
1515+ self .img_norm1 = NNXAdaLayerNormZero (dim , dtype = dtype , weights_dtype = weights_dtype )
1516+ self .txt_norm1 = NNXAdaLayerNormZero (dim , dtype = dtype , weights_dtype = weights_dtype )
15101517
15111518 self .attn = NNXFluxDoubleAttention (
15121519 rngs = rngs ,
@@ -1605,7 +1612,7 @@ def __init__(
16051612 weights_dtype : jnp .dtype = jnp .float32 ,
16061613 ):
16071614 self .dim = dim
1608- self .norm = AdaLayerNormZeroSingle (dim , dtype = dtype , weights_dtype = weights_dtype )
1615+ self .norm = NNXAdaLayerNormZeroSingle (dim , dtype = dtype , weights_dtype = weights_dtype )
16091616 self .attn = NNXFluxSingleAttention (
16101617 rngs = rngs ,
16111618 dim = dim ,
@@ -1660,7 +1667,7 @@ def __init__(
16601667 self .inner_dim = num_attention_heads * attention_head_dim
16611668 self .dtype = dtype
16621669
1663- self .pos_embed = FluxPosEmbed ( theta = theta , axes_dim = axes_dim , return_tuple = True )
1670+ self .pos_embed = NNXFluxPosEmbed ( axes_dim = axes_dim , theta = theta , return_tuple = True )
16641671 self .time_text_embed = NNXCombinedTimestepGuidanceTextProjEmbeddings (
16651672 rngs = rngs ,
16661673 embedding_dim = self .inner_dim ,
@@ -1710,7 +1717,7 @@ def __init__(
17101717 rngs = rngs ,
17111718 )
17121719
1713- self .double_blocks = [
1720+ self .double_blocks = nnx . List ( [
17141721 NNXFluxDoubleTransformerBlock (
17151722 rngs = rngs ,
17161723 dim = self .inner_dim ,
@@ -1720,9 +1727,9 @@ def __init__(
17201727 weights_dtype = weights_dtype ,
17211728 )
17221729 for _ in range (num_layers )
1723- ]
1730+ ])
17241731
1725- self .single_blocks = [
1732+ self .single_blocks = nnx . List ( [
17261733 NNXFluxSingleTransformerBlock (
17271734 rngs = rngs ,
17281735 dim = self .inner_dim ,
@@ -1732,11 +1739,11 @@ def __init__(
17321739 weights_dtype = weights_dtype ,
17331740 )
17341741 for _ in range (num_single_layers )
1735- ]
1742+ ])
17361743
1737- self .norm_out = AdaLayerNormContinuous (
1738- self . inner_dim ,
1739- elementwise_affine = False ,
1744+ self .norm_out = NNXAdaLayerNormContinuous (
1745+ rngs = rngs ,
1746+ embedding_dim = self . inner_dim ,
17401747 eps = 1e-6 ,
17411748 dtype = dtype ,
17421749 weights_dtype = weights_dtype ,
0 commit comments