@@ -1824,6 +1824,8 @@ class FlaxFluxAttention(nn.Module):
18241824 out_axis_names : AxisNames = (BATCH , LENGTH , EMBED )
18251825 precision : jax .lax .Precision = None
18261826 qkv_bias : bool = False
1827+ use_base2_exp : bool = False
1828+ use_experimental_scheduler : bool = False
18271829
18281830 def setup (self ):
18291831 if self .attention_kernel in {"flash" , "cudnn_flash_te" } and self .mesh is None :
@@ -1843,6 +1845,8 @@ def setup(self):
18431845 flash_block_sizes = self .flash_block_sizes ,
18441846 dtype = self .dtype ,
18451847 float32_qk_product = False ,
1848+ use_base2_exp = self .use_base2_exp ,
1849+ use_experimental_scheduler = self .use_experimental_scheduler ,
18461850 )
18471851
18481852 kernel_axes = ("embed" , "heads" )
@@ -1923,41 +1927,59 @@ def __call__(
19231927 attention_mask = None ,
19241928 image_rotary_emb = None ,
19251929 ):
1926- qkv_proj = self .qkv (hidden_states )
19271930 B , L = hidden_states .shape [:2 ]
1928- H , D , K = self .heads , qkv_proj .shape [- 1 ] // (self .heads * 3 ), 3
1929- qkv_proj = qkv_proj .reshape (B , L , K , H , D ).transpose (2 , 0 , 3 , 1 , 4 )
1930- query_proj , key_proj , value_proj = qkv_proj
1931+ # Deduce dimensions cleanly from class attributes
1932+ H , D = self .heads , self .dim_head
19311933
1932- query_proj = self .query_norm (query_proj )
1934+ qkv_proj = self .qkv (hidden_states )
1935+ qkv_proj = checkpoint_name (qkv_proj , "img_qkv_proj" )
1936+
1937+ qkv_proj = qkv_proj .reshape (B , L , 3 , H , D )
1938+ query_proj , key_proj , value_proj = jnp .split (qkv_proj , 3 , axis = 2 )
1939+ query_proj = query_proj .squeeze (2 )
1940+ key_proj = key_proj .squeeze (2 )
1941+ value_proj = value_proj .squeeze (2 )
19331942
1943+ query_proj = self .query_norm (query_proj )
19341944 key_proj = self .key_norm (key_proj )
19351945
19361946 if encoder_hidden_states is not None :
1947+ B_enc , L_txt = encoder_hidden_states .shape [:2 ]
19371948 encoder_qkv_proj = self .encoder_qkv (encoder_hidden_states )
1938- B , L = encoder_hidden_states .shape [:2 ]
1939- H , D , K = self .heads , encoder_qkv_proj .shape [- 1 ] // (self .heads * 3 ), 3
1940- encoder_qkv_proj = encoder_qkv_proj .reshape (B , L , K , H , D ).transpose (2 , 0 , 3 , 1 , 4 )
1941- encoder_query_proj , encoder_key_proj , encoder_value_proj = encoder_qkv_proj
1949+ encoder_qkv_proj = checkpoint_name (encoder_qkv_proj , "txt_qkv_proj" )
1950+ encoder_qkv_proj = encoder_qkv_proj .reshape (B_enc , L_txt , 3 , H , D )
1951+ enc_query_proj , enc_key_proj , enc_value_proj = jnp .split (encoder_qkv_proj , 3 , axis = 2 )
1952+ enc_query_proj = enc_query_proj .squeeze (2 )
1953+ enc_key_proj = enc_key_proj .squeeze (2 )
1954+ enc_value_proj = enc_value_proj .squeeze (2 )
19421955
1943- encoder_query_proj = self .encoder_query_norm (encoder_query_proj )
1956+ encoder_query_proj = self .encoder_query_norm (enc_query_proj )
1957+ encoder_key_proj = self .encoder_key_norm (enc_key_proj )
19441958
1945- encoder_key_proj = self .encoder_key_norm (encoder_key_proj )
1959+ query_proj = jnp .concatenate ((encoder_query_proj , query_proj ), axis = 1 )
1960+ key_proj = jnp .concatenate ((encoder_key_proj , key_proj ), axis = 1 )
1961+ value_proj = jnp .concatenate ((enc_value_proj , value_proj ), axis = 1 )
19461962
1947- query_proj = jnp .concatenate ((encoder_query_proj , query_proj ), axis = 2 )
1948- key_proj = jnp .concatenate ((encoder_key_proj , key_proj ), axis = 2 )
1949- value_proj = jnp .concatenate ((encoder_value_proj , value_proj ), axis = 2 )
1950-
1951- query_proj = nn .with_logical_constraint (query_proj , self .query_axis_names )
1952- key_proj = nn .with_logical_constraint (key_proj , self .key_axis_names )
1953- value_proj = nn .with_logical_constraint (value_proj , self .value_axis_names )
1963+ # query_proj = nn.with_logical_constraint(query_proj, self.query_axis_names)
1964+ # key_proj = nn.with_logical_constraint(key_proj, self.key_axis_names)
1965+ # value_proj = nn.with_logical_constraint(value_proj, self.value_axis_names)
19541966
19551967 image_rotary_emb = rearrange (image_rotary_emb , "n d (i j) -> n d i j" , i = 2 , j = 2 )
1968+
1969+ query_proj = query_proj .swapaxes (1 , 2 )
1970+ key_proj = key_proj .swapaxes (1 , 2 )
19561971 query_proj , key_proj = apply_rope (query_proj , key_proj , image_rotary_emb )
1972+ query_proj = query_proj .swapaxes (1 , 2 )
1973+ key_proj = key_proj .swapaxes (1 , 2 )
1974+
1975+ query_proj = query_proj .reshape (B , - 1 , H * D )
1976+ key_proj = key_proj .reshape (B , - 1 , H * D )
1977+ value_proj = value_proj .reshape (B , - 1 , H * D )
19571978
1958- query_proj = query_proj .transpose (0 , 2 , 1 , 3 ).reshape (query_proj .shape [0 ], query_proj .shape [2 ], - 1 )
1959- key_proj = key_proj .transpose (0 , 2 , 1 , 3 ).reshape (key_proj .shape [0 ], key_proj .shape [2 ], - 1 )
1960- value_proj = value_proj .transpose (0 , 2 , 1 , 3 ).reshape (value_proj .shape [0 ], value_proj .shape [2 ], - 1 )
1979+ if encoder_hidden_states is not None :
1980+ query_proj = nn .with_logical_constraint (query_proj , self .query_axis_names )
1981+ key_proj = nn .with_logical_constraint (key_proj , self .key_axis_names )
1982+ value_proj = nn .with_logical_constraint (value_proj , self .value_axis_names )
19611983
19621984 attn_output = self .attention_op .apply_attention (query_proj , key_proj , value_proj , attention_mask = attention_mask )
19631985 context_attn_output = None
0 commit comments