Support context-parallel local_map in AutoParallel#512
Open
AlbedoWang wants to merge 1 commit into
Open
Conversation
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.
Adds Context Parallel attention support through
local_mapand teaches AutoParallel's local_map HOP path to honorin_grad_placements.Context Parallel needs asymmetric placements for attention inputs and their gradients. Q remains sequence-sharded on the CP axis in both forward and backward, while K/V are replicated for the forward attention kernel and produce partial gradients in backward:
Shard(sequence)on CPReplicate()on CPPartial()on CPThis matches Torchtitan full-DTensor CP semantics. The gap was specific to AutoParallel's tracing path: AutoParallel enables PyTorch's
local_map_hopwrapping so local_map regions remain explicit placement-aware graph boundaries. That HOP autograd path currently rejectsin_grad_placements, and its generated backward metadata defaults backward output placements to the forward input placements. That is correct for existing EP/MoE local_map regions within_grad_placements=None, but it is wrong for CP K/V gradients.This PR adds a scoped AutoParallel shim around local_map HOP autograd:
in_grad_placementsreject while AutoParallel is tracing or applying placementsout_placementsto the forwardin_grad_placementswhen providedThe placement optimizer no longer rejects
in_grad_placements; it continues to consume local_mapin_placementsandout_placements. For backward HOPs, the shim has already madeout_placementsreflect the gradient placements, so CP logic stays out of the generic placement-option path.Also adds
autoparallel.context_parallelhelpers for CP attention local_map wrapping, including an SDPA convenience wrapper. The helpers support named 2D, 3D, and 4D meshes such as:("dp_shard", "cp")("dp_shard", "tp")("dp_shard", "cp", "tp")("dp_replicate", "dp_shard", "cp", "tp")Docs included:
docs/context_parallel.md: user-facing usage and placement semanticsdocs/context_parallel_design.md: implementation rationale and unsupported path analysisReview order:
autoparallel/tracing.py— local_map HOP autograd shim and scoped enablementautoparallel/shardings/placement_options.py— local_map placement option support for in-grad metadataautoparallel/context_parallel.py— CP placement helper and SDPA wrappertests/test_context_parallel.pyandtests/test_correctness.py— end-to-end coverage and shared correctness helper extensiondocs/context_parallel.mdanddocs/context_parallel_design.mdTest plan:
pre-commit run --files autoparallel/api.py autoparallel/shardings/placement_options.py autoparallel/tracing.py autoparallel/context_parallel.py tests/test_correctness.py tests/test_context_parallel.py docs/README.md docs/context_parallel.md docs/context_parallel_design.mdgit diff --checkpython -m py_compile autoparallel/context_parallel.py autoparallel/tracing.py autoparallel/api.py autoparallel/shardings/placement_options.py tests/test_context_parallel.py tests/test_correctness.pypython -m pytest tests/test_context_parallel.py tests/test_correctness.py::test_correctness_attention tests/test_optimize_placement.py::test_local_map_placement_respected tests/test_activation_checkpointing.py::test_local_map_custom_metadata_propagation -q