[LTX-2] Fix flash attention shard_map for sequence lengths not divisible by context mesh axis#363
Open
[LTX-2] Fix flash attention shard_map for sequence lengths not divisible by context mesh axis#363
Conversation
e22ec2c to
b8296b2
Compare
entrpn
reviewed
Mar 24, 2026
b8296b2 to
9697900
Compare
Collaborator
Author
|
@entrpn PTAL |
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:
When the sequence length (e.g., audio tokens) is not evenly divisible by the context mesh axis size, shard_map in _tpu_flash_attention raises a ValueError because it cannot partition the array evenly across devices.
For example, LTX-2 with 121 frames at 24 fps produces 126 audio latent tokens. On an 8-device context axis, 126 is not divisible by 8, causing the failure.
The existing _pad_data_for_flash already pads sequences for flash block-size alignment inside shard_map, but the shard_map itself requires even partitioning before entry.
This fix pads query/key/value sequence dimensions to the nearest multiple of the context mesh axis size before shard_map, and trims the output back to the original length afterward. Segment-ID masking inside wrap_flash_attention ensures padded positions do not affect attention results.