Fix indexer mask padding in Tokamax Flash Attention with Context Parallelism - #4947
Draft
SurbhiJainUSC wants to merge 1 commit into
Draft
Fix indexer mask padding in Tokamax Flash Attention with Context Parallelism#4947SurbhiJainUSC wants to merge 1 commit into
SurbhiJainUSC wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the padding calculation for the indexer_mask in wrap_flash_attention within src/maxtext/layers/attention_op.py. Specifically, it replaces the use of mask_shape with dynamically calculated padded_q_len and padded_kv_len based on the query/key shapes and block configurations. There are no review comments, and I have no additional feedback to provide.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
SurbhiJainUSC
force-pushed
the
ci_tests
branch
3 times, most recently
from
August 19, 2026 20:57
1831e58 to
1cd8f64
Compare
SurbhiJainUSC
force-pushed
the
ci_tests
branch
2 times, most recently
from
August 19, 2026 22:58
b2a6f1a to
8125c70
Compare
SurbhiJainUSC
force-pushed
the
ci_tests
branch
from
August 19, 2026 22:58
8125c70 to
69b6b20
Compare
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
This PR fixes TPU Pre-training tests on v7x: https://github.com/AI-Hypercomputer/maxtext/actions/runs/31905605774/job/95079478073
This PR fixes a bug in
attention_op.pywheredynamic indexer_maskpadding was incorrectly computed when using Context Parallelism (cp_size > 1) with Tokamax Flash Attention.Root Cause
Inside
attention_op.py:2016-2166(which runs inside jax.shard_map), tensors are sharded across context parallel devices, so each device receives a local slice of queries (query.shape[2] = global_seq_len // cp_size) and indexer mask (indexer_mask.shape[-2] = global_seq_len // cp_size). However, the pad_q calculation was referencing mask_shape[0] (the outer un-sharded global sequence length). For cp_size > 1, pad_q was computed as global_seq_len - (global_seq_len // cp_size) > 0, erroneously padding indexer_mask with trailing False rows up to the full global sequence length. When passed into tokamax_splash_kernel.make_dynamic_splash_mha, the kernel generated grid dimensions and active block mappings for the full sequence length rather than the local query shard, leading to logits divergence and assertion failures.Changes
Updated pad_q and pad_kv calculation in attention_op.py:2110-2125 to compute block-boundary padding based on local query.shape[2] and key.shape[2] dimensions using sa_config.block_q and sa_config.block_kv.
Tests
CI tests
Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.