fix: compute local block padding for dynamic splash indexer mask in CP - #4950
Open
RexBearIU wants to merge 1 commit into
Open
fix: compute local block padding for dynamic splash indexer mask in CP#4950RexBearIU wants to merge 1 commit into
RexBearIU wants to merge 1 commit into
Conversation
Inside wrap_flash_attention under jax.shard_map, indexer_mask is sharded along the query sequence dimension (shape [batch, q_local_len, kv_full_len]). Previously, pad_q was computed using mask_shape[0] (the global un-sharded sequence length), which over-padded the local query sequence dimension of indexer_mask up to the global length. On TPU v7x and other architectures with dynamic splash attention schedulers, this caused a grid iteration and block dimension mismatch, leading to assertion failures in test_tpu_flash_attention_context_parallel_with_indexer (both no_lb_cp2 and lb_cp4_smallk). This fix computes padded_q_len and padded_kv_len based directly on the local indexer_mask dimensions and the hardware block sizes (sa_config.block_q / sa_config.block_kv), ensuring correct shape compatibility across all hardware platforms (v4, v5e, v5p, v6e, v7x, CPU, GPU) and arbitrary CP degrees.
RexBearIU
requested review from
NuojCheng,
RissyRan,
aireenmei,
bvandermoon,
gagika,
gobbleturk,
huytransformer,
igorts-git,
jiangjy1982,
parambole,
richjames0,
shralex,
shuningjin and
xibinliu
as code owners
August 20, 2026 03:36
There was a problem hiding this comment.
Code Review
This pull request updates the padding calculation for the Tokamax splash kernel in attention_op.py by aligning the query and key-value lengths to their respective block sizes (block_q and block_kv) before computing the padding widths. I have no feedback to provide as there are no review comments.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes an
AssertionErrorinMLATest.test_tpu_flash_attention_context_parallel_with_indexer(bothno_lb_cp2andlb_cp4_smallk) by properly computing local block-aligned padding for dynamic splash attention masks under context parallelism.Root Cause
Inside
wrap_flash_attentionunderjax.shard_map,indexer_maskis sharded across context devices along the query sequence dimensionQ_LENGTHinto shape(batch, q_local_len, kv_full_len)(e.g.[1, 128, 512]under CP=4 or[1, 256, 512]under CP=2).Previously,
pad_qwas computed usingmask_shape[0](the outer, global un-sharded sequence length 512):This over-padded the local query dimension of$512 \times 512$ mask for a 128 (or 256) token local shard.
indexer_maskwith hundreds ofFalserows back up to 512, creating aMachine-Level Compatibility
dq_reduction_steps=3withq.shape[1], causing trailing mask rows to be benignly ignored in some configurations but prone to subtle stride/allocation issues.padded_q_lenandpadded_kv_lenusingsa_config.block_qandsa_config.block_kvmultiples of the localindexer_maskshape (indexer_mask.shape[-2]andindexer_mask.shape[-1]):Tests
--xla_force_host_platform_device_count=8):MLATest.test_tpu_flash_attention_context_parallel_with_indexer_no_lb_cp2→PASSMLATest.test_tpu_flash_attention_context_parallel_with_indexer_lb_cp4_smallk→PASSMLATest.test_tpu_dot_product_context_parallel_with_indexer→PASSMLATest.test_tpu_flash_attention_ring_context_parallel_with_indexer→PASScodespell,pylint,pyink) passed cleanly.Checklist
gemini-reviewlabel.