@@ -2283,6 +2283,8 @@ class FlaxFluxAttention(nn.Module):
22832283 out_axis_names : AxisNames = (BATCH , LENGTH , EMBED )
22842284 precision : jax .lax .Precision = None
22852285 qkv_bias : bool = False
2286+ use_base2_exp : bool = False
2287+ use_experimental_scheduler : bool = False
22862288
22872289 def setup (self ):
22882290 if self .attention_kernel in {"flash" , "cudnn_flash_te" } and self .mesh is None :
@@ -2302,6 +2304,8 @@ def setup(self):
23022304 flash_block_sizes = self .flash_block_sizes ,
23032305 dtype = self .dtype ,
23042306 float32_qk_product = False ,
2307+ use_base2_exp = self .use_base2_exp ,
2308+ use_experimental_scheduler = self .use_experimental_scheduler ,
23052309 )
23062310
23072311 kernel_axes = ("embed" , "heads" )
@@ -2382,41 +2386,59 @@ def __call__(
23822386 attention_mask = None ,
23832387 image_rotary_emb = None ,
23842388 ):
2385- qkv_proj = self .qkv (hidden_states )
23862389 B , L = hidden_states .shape [:2 ]
2387- H , D , K = self .heads , qkv_proj .shape [- 1 ] // (self .heads * 3 ), 3
2388- qkv_proj = qkv_proj .reshape (B , L , K , H , D ).transpose (2 , 0 , 3 , 1 , 4 )
2389- query_proj , key_proj , value_proj = qkv_proj
2390+ # Deduce dimensions cleanly from class attributes
2391+ H , D = self .heads , self .dim_head
23902392
2391- query_proj = self .query_norm (query_proj )
2393+ qkv_proj = self .qkv (hidden_states )
2394+ qkv_proj = checkpoint_name (qkv_proj , "img_qkv_proj" )
2395+
2396+ qkv_proj = qkv_proj .reshape (B , L , 3 , H , D )
2397+ query_proj , key_proj , value_proj = jnp .split (qkv_proj , 3 , axis = 2 )
2398+ query_proj = query_proj .squeeze (2 )
2399+ key_proj = key_proj .squeeze (2 )
2400+ value_proj = value_proj .squeeze (2 )
23922401
2402+ query_proj = self .query_norm (query_proj )
23932403 key_proj = self .key_norm (key_proj )
23942404
23952405 if encoder_hidden_states is not None :
2406+ B_enc , L_txt = encoder_hidden_states .shape [:2 ]
23962407 encoder_qkv_proj = self .encoder_qkv (encoder_hidden_states )
2397- B , L = encoder_hidden_states .shape [:2 ]
2398- H , D , K = self .heads , encoder_qkv_proj .shape [- 1 ] // (self .heads * 3 ), 3
2399- encoder_qkv_proj = encoder_qkv_proj .reshape (B , L , K , H , D ).transpose (2 , 0 , 3 , 1 , 4 )
2400- encoder_query_proj , encoder_key_proj , encoder_value_proj = encoder_qkv_proj
2408+ encoder_qkv_proj = checkpoint_name (encoder_qkv_proj , "txt_qkv_proj" )
2409+ encoder_qkv_proj = encoder_qkv_proj .reshape (B_enc , L_txt , 3 , H , D )
2410+ enc_query_proj , enc_key_proj , enc_value_proj = jnp .split (encoder_qkv_proj , 3 , axis = 2 )
2411+ enc_query_proj = enc_query_proj .squeeze (2 )
2412+ enc_key_proj = enc_key_proj .squeeze (2 )
2413+ enc_value_proj = enc_value_proj .squeeze (2 )
24012414
2402- encoder_query_proj = self .encoder_query_norm (encoder_query_proj )
2415+ encoder_query_proj = self .encoder_query_norm (enc_query_proj )
2416+ encoder_key_proj = self .encoder_key_norm (enc_key_proj )
24032417
2404- encoder_key_proj = self .encoder_key_norm (encoder_key_proj )
2418+ query_proj = jnp .concatenate ((encoder_query_proj , query_proj ), axis = 1 )
2419+ key_proj = jnp .concatenate ((encoder_key_proj , key_proj ), axis = 1 )
2420+ value_proj = jnp .concatenate ((enc_value_proj , value_proj ), axis = 1 )
24052421
2406- query_proj = jnp .concatenate ((encoder_query_proj , query_proj ), axis = 2 )
2407- key_proj = jnp .concatenate ((encoder_key_proj , key_proj ), axis = 2 )
2408- value_proj = jnp .concatenate ((encoder_value_proj , value_proj ), axis = 2 )
2409-
2410- query_proj = nn .with_logical_constraint (query_proj , self .query_axis_names )
2411- key_proj = nn .with_logical_constraint (key_proj , self .key_axis_names )
2412- value_proj = nn .with_logical_constraint (value_proj , self .value_axis_names )
2422+ # query_proj = nn.with_logical_constraint(query_proj, self.query_axis_names)
2423+ # key_proj = nn.with_logical_constraint(key_proj, self.key_axis_names)
2424+ # value_proj = nn.with_logical_constraint(value_proj, self.value_axis_names)
24132425
24142426 image_rotary_emb = rearrange (image_rotary_emb , "n d (i j) -> n d i j" , i = 2 , j = 2 )
2427+
2428+ query_proj = query_proj .swapaxes (1 , 2 )
2429+ key_proj = key_proj .swapaxes (1 , 2 )
24152430 query_proj , key_proj = apply_rope (query_proj , key_proj , image_rotary_emb )
2431+ query_proj = query_proj .swapaxes (1 , 2 )
2432+ key_proj = key_proj .swapaxes (1 , 2 )
2433+
2434+ query_proj = query_proj .reshape (B , - 1 , H * D )
2435+ key_proj = key_proj .reshape (B , - 1 , H * D )
2436+ value_proj = value_proj .reshape (B , - 1 , H * D )
24162437
2417- query_proj = query_proj .transpose (0 , 2 , 1 , 3 ).reshape (query_proj .shape [0 ], query_proj .shape [2 ], - 1 )
2418- key_proj = key_proj .transpose (0 , 2 , 1 , 3 ).reshape (key_proj .shape [0 ], key_proj .shape [2 ], - 1 )
2419- value_proj = value_proj .transpose (0 , 2 , 1 , 3 ).reshape (value_proj .shape [0 ], value_proj .shape [2 ], - 1 )
2438+ if encoder_hidden_states is not None :
2439+ query_proj = nn .with_logical_constraint (query_proj , self .query_axis_names )
2440+ key_proj = nn .with_logical_constraint (key_proj , self .key_axis_names )
2441+ value_proj = nn .with_logical_constraint (value_proj , self .value_axis_names )
24202442
24212443 attn_output = self .attention_op .apply_attention (query_proj , key_proj , value_proj , attention_mask = attention_mask )
24222444 context_attn_output = None
0 commit comments