Skip to content

[CP] Fix SDPA context-parallel shard placement - #3681

Merged
aditvenk merged 1 commit into
mainfrom
aditvenk/cp-fix
Jun 16, 2026
Merged

aditvenk merged 1 commit into
mainfrom
aditvenk/cp-fix

Conversation

@aditvenk

Copy link
Copy Markdown
Contributor

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".

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.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jun 16, 2026
@aditvenk
aditvenk marked this pull request as ready for review June 16, 2026 05:41
@aditvenk aditvenk added the ciflow/h100.8 Trigger H100.8 CI label Jun 16, 2026
@anshul-si

Copy link
Copy Markdown
Contributor

#3678

@fegin

fegin commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

The fix works, but the CI is still failing. The root cause is different now -- the huggingface coco text download is timeout.
cc., @jiannanWang

This PR is safe to land.

@aditvenk

Copy link
Copy Markdown
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?

@aditvenk

Copy link
Copy Markdown
Contributor Author

The fix works, but the CI is still failing. The root cause is different now -- the huggingface coco text download is timeout. cc., @jiannanWang

This PR is safe to land.

Looks like our messages raced eeach other :D merging.

@aditvenk
aditvenk merged commit a695565 into main Jun 16, 2026
46 of 54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/h100.8 Trigger H100.8 CI ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants