Repository navigation
[CP] Fix SDPA context-parallel shard placement - #3681
Merged
Merged
Conversation
Graph Trainer CI test "aot_fx_trace llama3 FSDP+TP+CP" failed in torch/distributed/tensor/experimental/_context_parallel/_attention.py with AssertionError: "inputs need to be redistributed". ScaledDotProductAttention.forward receives q/k/v in model layout (B, L, H, D), then transposes them to SDPA layout (B, H, L, D). The CP wrapper annotates q/k/v before that transpose, so the shard dimension must be expressed in the model layout. CP splits the sequence axis, which is dim 1 in (B, L, H, D). The old code used Shard(2), annotating the head axis instead. After the transpose, PyTorch saw Shard(1) at the SDPA op, i.e. head sharding in (B, H, L, D), instead of the required Shard(2) sequence sharding. This was latent because PyTorch's old CP SDPA sharding rule appended the CP sequence strategy to the normal SDPA base strategies, so the wrong head-sharded metadata could match a non-CP/base head-sharded strategy and pass propagation. pytorch/pytorch#186667 migrated these rules to single-dim strategies and the CP override now only accepts the CP sequence strategy. That correctly exposes the bad TorchTitan annotation as needs_redistribute=True, which triggers the CP SDPA handler's assertion. Use Shard(1) when wrapping the pre-transpose q/k/v tensors, so the transpose presents Shard(2) at the actual SDPA op.
aditvenk
marked this pull request as ready for review
June 16, 2026 05:41
aditvenk
requested review from
fegin,
tianyu-l,
wconstab and
wwwjn
as code owners
June 16, 2026 05:41
Contributor
fegin
approved these changes
Jun 16, 2026
Contributor
|
The fix works, but the CI is still failing. The root cause is different now -- the huggingface coco text download is timeout. This PR is safe to land. |
Contributor
Author
|
@fegin , @tianyu-l -- the Flux Integration test is timing out on CI due to downloading from HF taking too long. Same test is passing locally for me with this PR. On main, the same test hits the CP redistribute error that this PR is fixing. Shall I merge this PR with this failure and we treat the HF download timeout as a separate issue? |
Contributor
Author
Looks like our messages raced eeach other :D merging. |
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.
ScaledDotProductAttention.forward receives q/k/v in model layout (B, L, H, D), then transposes them to SDPA layout (B, H, L, D). The CP wrapper annotates q/k/v before that transpose, so the shard dimension must be expressed in the model layout. CP splits the sequence axis, which is dim 1 in (B, L, H, D). The old code used Shard(2), annotating the head axis instead. After the transpose, PyTorch saw Shard(1) at the SDPA op, i.e. head sharding in (B, H, L, D), instead of the required Shard(2) sequence sharding.
This was latent because PyTorch's old CP SDPA sharding rule appended the CP sequence strategy to the normal SDPA base strategies, so the wrong head-sharded metadata could match a non-CP/base head-sharded strategy and pass propagation. pytorch/pytorch#186667 migrated these rules to single-dim strategies and the CP override now only accepts the CP sequence strategy. That correctly exposes the bad TorchTitan annotation as needs_redistribute=True, which triggers the CP SDPA handler's assertion.
Use Shard(1) when wrapping the pre-transpose q/k/v tensors, so the transpose presents Shard(2) at the actual SDPA op.
Error seen in Graph Trainer CI test "aot_fx_trace llama3 FSDP+TP+CP" failed in torch/distributed/tensor/experimental/_context_parallel/_attention.py with AssertionError: "inputs need to be redistributed".