@@ -1033,6 +1033,7 @@ def apply_attention(
10331033 decoder_segment_ids ,
10341034 self .attn_logits_soft_cap ,
10351035 sinks ,
1036+ indexer_mask ,
10361037 record_max_logits = record_max_logits ,
10371038 )
10381039 if max_logits is not None :
@@ -1297,8 +1298,18 @@ def create_sa_config(config, query, key, attn_logits_soft_cap):
12971298 return sa_config
12981299
12991300 sa_config = create_sa_config (self .config , query , key , attn_logits_soft_cap )
1300- mask_shape = (query .shape [2 ], key .shape [2 ]) # (q_seq_len, kv_seq_len)
1301+ block_q = sa_config .block_q
1302+ block_kv = sa_config .block_kv
1303+ if self .attention_type == AttentionType .COMPRESSED and (
1304+ (query .shape [2 ] % block_q != 0 ) or (key .shape [2 ] % block_kv != 0 )
1305+ ):
1306+ padded_q_len = ((query .shape [2 ] + block_q - 1 ) // block_q ) * block_q
1307+ padded_kv_len = ((key .shape [2 ] + block_kv - 1 ) // block_kv ) * block_kv
1308+ mask_shape = (padded_q_len , padded_kv_len )
1309+ else :
1310+ mask_shape = (query .shape [2 ], key .shape [2 ]) # (q_seq_len, kv_seq_len)
13011311 mask_module = tokamax_splash_mask if self .config .use_tokamax_splash else splash_attention_mask
1312+
13021313 if self .attention_type == AttentionType .FULL :
13031314 mask = mask_module .FullMask (mask_shape )
13041315 else :
@@ -1315,14 +1326,14 @@ def create_sa_config(config, query, key, attn_logits_soft_cap):
13151326 local_window_size = (self .sliding_window_size - 1 , self .sliding_window_size )
13161327 if use_load_balanced_cp :
13171328 mask &= LoadBalancedLocalMask (
1318- shape = ( query . shape [ 2 ], key . shape [ 2 ]) ,
1329+ shape = mask_shape ,
13191330 window_size = local_window_size ,
13201331 offset = 0 ,
13211332 cp_size = cp_size ,
13221333 )
13231334 else :
13241335 mask &= mask_module .LocalMask (
1325- shape = ( query . shape [ 2 ], key . shape [ 2 ]) ,
1336+ shape = mask_shape ,
13261337 window_size = local_window_size ,
13271338 offset = 0 ,
13281339 )
@@ -1332,16 +1343,15 @@ def create_sa_config(config, query, key, attn_logits_soft_cap):
13321343
13331344 if use_load_balanced_cp :
13341345 mask &= LoadBalancedChunkedCausalMask (
1335- shape = ( query . shape [ 2 ], key . shape [ 2 ]) ,
1346+ shape = mask_shape ,
13361347 chunk_size = self .chunk_attn_window_size ,
13371348 cp_size = cp_size ,
13381349 )
13391350 else :
13401351 mask &= ChunkedCausalMask (
1341- shape = ( query . shape [ 2 ], key . shape [ 2 ]) ,
1352+ shape = mask_shape ,
13421353 chunk_size = self .chunk_attn_window_size ,
13431354 )
1344-
13451355 max_logit_value = None
13461356 if self .config .use_tokamax_splash :
13471357 # Create mask
@@ -1364,9 +1374,12 @@ def wrap_splash_kernel(single_head_mask):
13641374 )
13651375 return splash_kernel
13661376
1367- splash_kernel = wrap_splash_kernel (single_head_mask )
13681377 segment_axis_names_splash_kernel = self ._logical_to_mesh_axes ((Q_LENGTH ,))
1369- splash_kernel = self ._maybe_shard_with_pspec (splash_kernel , segment_axis_names_splash_kernel )
1378+ if indexer_mask is None :
1379+ splash_kernel = wrap_splash_kernel (single_head_mask )
1380+ splash_kernel = self ._maybe_shard_with_pspec (splash_kernel , segment_axis_names_splash_kernel )
1381+ else :
1382+ splash_kernel = None
13701383 elif self .config .use_jax_splash :
13711384 if self .config .use_max_logit_estimate > 0 :
13721385 sa_config = dataclasses .replace (sa_config , max_logit_const = self .config .use_max_logit_estimate )
@@ -1489,7 +1502,7 @@ def wrap_flash_attention(
14891502 decoder_segment_ids_tuple = None
14901503
14911504 if self .config .use_tokamax_splash :
1492- if self . config . use_indexer and indexer_mask is not None :
1505+ if indexer_mask is not None :
14931506 # Construct the splash kernel call with dynamic mask
14941507 def dynamic_mask_splash_kernel (q , k , v , segment , sinks , indexer_mask ):
14951508 splash_kernel = tokamax_splash_kernel .make_dynamic_splash_mha (
@@ -1533,6 +1546,7 @@ def kernel_fn(q, k, v, d, s):
15331546 query , key , value , decoder_segment_ids_tuple , sinks
15341547 )
15351548 return attention_output , None
1549+
15361550 elif self .config .use_jax_splash :
15371551 materialized_mask = jnp .asarray (mask [:, :])
15381552 attention_output = jax_flash_attention .flash_attention_block_masked (
0 commit comments