Skip to content

[pipelining] Wire static eager pipeline execution - #4540

Merged
sanketpurandare merged 48 commits into
mainfrom
gh/sanketpurandare/16/head
Sep 26, 2026
Merged

sanketpurandare merged 48 commits into
mainfrom
gh/sanketpurandare/16/head

Conversation

@sanketpurandare

@sanketpurandare sanketpurandare commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Summary:
Give eager pipeline stages complete static decoder-boundary metadata when the
PP process group is fake. This avoids dynamic cross-rank metadata inference
while preserving PyTorch's existing PipelineStage ownership and supports
first, middle, and last decoder stages across context and tensor parallelism.

TorchTitan also enables PyTorch's schedule-derived per-edge P2P groups whenever
pipeline parallelism is active. Each directed physical edge receives a
deterministic communicator FIFO that PyTorch initializes before execution and
CUDA-graph capture, avoiding dependency on NCCL first-use ordering.

Receive buffers use PyTorch's landed just-in-time allocation. A buffer is
allocated immediately before RECV_F or RECV_B, transferred to the owning
forward or backward computation, and released according to tensor lifetime.
CUDA-graph capture records stable addresses, so replay does not repeat Python
allocation. TorchTitan adds no receive-buffer policy or stage subclass.

Test Plan:

  • python -m pytest -q tests/unit_tests/cpu/test_no_new_cli_options.py tests/unit_tests/cpu/test_pipeline_parallel.py tests/unit_tests/cpu/test_config_manager.py (79 passed)
  • Scoped pre-commit hooks passed for the changed files.
  • Targeted Pyrefly reported 0 errors for production files.
  • One-process pure-FakePG PP4 and four-process real-PP/fake-SPMD PP4 each completed 10 steps, captured at step 3, and replayed through step 10.

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
Comment thread torchtitan/config/configs.py Outdated
Comment on lines +251 to +252
pipeline_parallel_max_active_stages: Annotated[int, tyro.conf.Suppress] = 3
"""Maximum FSDP stages kept unsharded by a looped pipeline schedule."""

@tianyu-l tianyu-l Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this sounds legit, for others why would we make them configurable at all?

name is ambiguous, could be improved, e.g. pipeline_parallel_max_param_unsharded_stages

3 sounds too small as default? I feel default should be "everything being kept unsharded"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the number of stages on a rank are too much keeping them all unsharded throughout is very costly for memory, in that case we can reshard the earlier stages, it's a memory configuration knob that is good to have given that we are moving towards larger models and gpu poor regimes

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm saying:

  1. why we have other configs?
  2. I agree with having this knob, but default 3 sounds arbitrary -- I wonder if we should default to unshard everything, and if users need, they can tune it.
  3. rename this knob to pipeline_parallel_max_param_unsharded_stages

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the other public pipeline-runtime policy knobs and retained only pipeline_parallel_max_param_unsharded_stages, using the suggested name. Its default keeps every local stage eligible to remain unsharded; an explicit positive limit is the memory-control override.

Comment thread torchtitan/config/configs.py Outdated
"""Flight recorder trace files prefix"""

mode: Literal["default", "fake_backend"] = "default"
mode: Literal["default", "fake_backend", "real_pp_fake_spmd_backend"] = "default"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"fake_backend" should cover both

  • without PP it's purely fake
  • with PP it's real PP fake spmd

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also want fake_backend to have fake everything including PP, pp works with fake backend, these use case for real_pp_fake_spmd backed is for exercising pp comms locally.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer we simplify things right now to piggyback real_pp_fake_spmd_backend onto fake_backend, as there's no other option yet. We can worry about that later.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kept pure fake and real-PP/fake-SPMD as separate modes because they validate different contracts: pure fake models one rank of the complete logical topology without transport, while the hybrid mode runs real PP communication and fake SPMD collectives. The focused topology PR #4652 now returns one typed topology result with explicit validation rather than mutating a constructed mesh or falling back between modes. Its debugging guide documents the use cases, logical-rank formula, every launch variable, exact commands, represented memory, and limitations; the documented one-GPU pure-fake and two-GPU hybrid PP2/DP2 commands both completed one step.

Comment thread torchtitan/distributed/parallel_dims.py Outdated
parallelism_config: ParallelismConfig,
world_size: int,
*,
pp_mesh_override: DeviceMesh | None = None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks hacky, not clear why we need this to be passed in from outside

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for passing the real pp mesh when using real_pp_fake_spmd backend, agree that it is hacky will do it properly

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the external PP-mesh override and post-construction mesh mutation. Distributed initialization now returns a typed topology that contains the real PP and fake-SPMD meshes, and ParallelDims consumes it at construction.

device,
group=pp_mesh.get_group("pp"),
get_mesh=get_mesh,
pass_pipeline_metadata=pass_pipeline_metadata,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why introducing this flag as configurable? Shouldn't it be hardcoded to be True?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This metadata is stage and microbatch id metadata that is only useful for Dist-MoE

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then let's introduce it in the DistMoE integration PR, which we need to discuss about

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the public metadata flag. Pipeline stage and microbatch metadata is now capability-driven: setup marks only participating stages, the schedule supplies canonical kwargs, and a stage-root pre-hook consumes them before ordinary model forward.

sanketpurandare added a commit that referenced this pull request Sep 12, 2026
…ter"

Summary:
Add a thin TorchTitan adapter for the standalone Dist-MoE package. TorchTitan
owns backend selection, W13/W2 model parameters, FSDP/optimizer integration,
checkpoint key conversion, process groups, and context lifetime. The package
continues to own dispatch, expert compute, combine, memory planning, activation
storage, VMM, and kernels.

MXFP8 expert weights specialize TorchTitan's existing `_ShardedFSDPTensor`
contract. They share BF16 all-gather, operand preparation/refill/release,
SimpleFSDP gradient edges, RAF semantics, and checkpoint handling with
`MXFP8Linear`; only grouped Dist-MoE operand construction differs.

The package selects one of two entries to the same autograd implementation:
ordinary eager calls it directly, while Dynamo, FakeTensor, make_fx, and
activation-checkpoint tracing use the registered ordered-effect operation. The
adapter adds no third execution path or mode-specific kernel behavior.

Non-goals:
- Do not copy or modify kernels, routing policy, the memory planner, pipeline
  schedules, or the generic prepared-weight lifecycle.
- Do not silently support FSDP sharding outside logical dimension 0.

Dependencies and risks:
- Requires TorchTitan #4203/#4371, #4539, the PyTorch #194114/#196460 FSDP
  lifecycle, and a released exact `dist-moe` version. Pipeline use additionally
  requires #4540 and PyTorch #196462-#196465. Effectful AC requires #4559.
- Tests cover W13 reshape, W2 identity, shared qdata, stable four-buffer refill,
  logical gradient shape, RAF true/false, no-FSDP preparation, uneven shards,
  checkpoint round trip, and explicit cleanup.

Test Plan:
- `python -m pytest -q tests/unit_tests/cpu/test_dist_moe.py`
- Two-rank BF16 and MXFP8 FSDP+EP full-CUDA-graph training on Blackwell.
- Final-source MXFP8 device, ordinary/forced-spill VMM, and GraphTrainer rows
  completed with exact numerics, full graph replay, checked VMM teardown where
  applicable, and zero restarts.

[ghstack-poisoned]
sanketpurandare added a commit that referenced this pull request Sep 12, 2026
Summary:
Add a thin TorchTitan adapter for the standalone Dist-MoE package. TorchTitan
owns backend selection, W13/W2 model parameters, FSDP/optimizer integration,
checkpoint key conversion, process groups, and context lifetime. The package
continues to own dispatch, expert compute, combine, memory planning, activation
storage, VMM, and kernels.

MXFP8 expert weights specialize TorchTitan's existing `_ShardedFSDPTensor`
contract. They share BF16 all-gather, operand preparation/refill/release,
SimpleFSDP gradient edges, RAF semantics, and checkpoint handling with
`MXFP8Linear`; only grouped Dist-MoE operand construction differs.

The package selects one of two entries to the same autograd implementation:
ordinary eager calls it directly, while Dynamo, FakeTensor, make_fx, and
activation-checkpoint tracing use the registered ordered-effect operation. The
adapter adds no third execution path or mode-specific kernel behavior.

Non-goals:
- Do not copy or modify kernels, routing policy, the memory planner, pipeline
  schedules, or the generic prepared-weight lifecycle.
- Do not silently support FSDP sharding outside logical dimension 0.

Dependencies and risks:
- Requires TorchTitan #4203/#4371, #4539, the PyTorch #194114/#196460 FSDP
  lifecycle, and a released exact `dist-moe` version. Pipeline use additionally
  requires #4540 and PyTorch #196462-#196465. Effectful AC requires #4559.
- Tests cover W13 reshape, W2 identity, shared qdata, stable four-buffer refill,
  logical gradient shape, RAF true/false, no-FSDP preparation, uneven shards,
  checkpoint round trip, and explicit cleanup.

Test Plan:
- `python -m pytest -q tests/unit_tests/cpu/test_dist_moe.py`
- Two-rank BF16 and MXFP8 FSDP+EP full-CUDA-graph training on Blackwell.
- Final-source MXFP8 device, ordinary/forced-spill VMM, and GraphTrainer rows
  completed with exact numerics, full graph replay, checked VMM teardown where
  applicable, and zero restarts.

[ghstack-poisoned]
[ghstack-poisoned]
TorchTitan's eager pipeline runtime needs deterministic, precomputed contracts
before warmup: stage-boundary metadata, receive-buffer ownership, P2P topology,
and the maximum number of simultaneously unsharded local stages. Dynamic
metadata exchange or runtime buffer allocation prevents FakePG execution and
makes whole-step CUDA-graph capture difficult to reason about.

Wire the existing PyTorch pipelining facilities into TorchTitan:

- enable schedule-derived directed-edge P2P groups whenever pipeline
  parallelism is active;
- enable deterministic receive-buffer recycling for looped schedules;
- construct complete static decoder stage I/O metadata when the PP group is
  fake, failing clearly when a custom split cannot be represented;
- pass the configurable local-stage parameter-residency bound to the schedule;
- carry a fixed upper bound for varlen document metadata.

Directed P2P groups, receive-buffer reuse, and metadata selection are runtime
invariants, not user policy knobs. Their groups and assignments are finalized
before warmup; execution does not poll, recolor storage, or mutate the
schedule. The only public memory-policy knob added here is
`pipeline_parallel_max_param_unsharded_stages`.

This PR consumes a fake PP group when distributed initialization supplies one;
it does not define the fake or real-PP/fake-SPMD launch modes. Those topology
and launch contracts are introduced independently in #4652. Looped-schedule
CUDA-graph enablement is isolated in #4591.

Test Plan:
- `python -m pytest -q tests/unit_tests/cpu/test_no_new_cli_options.py tests/unit_tests/cpu/test_pipeline_parallel.py tests/unit_tests/cpu/test_config_manager.py tests/unit_tests/cpu/test_varlen_attention.py`
- Stack-level one-GPU pure-FakePG PP and four-GPU real-PP/fake-SPMD execution
  with repeated full CUDA-graph replay.

[ghstack-poisoned]
…ecution"

TorchTitan's eager pipeline runtime needs deterministic, precomputed contracts
before warmup: stage-boundary metadata, receive-buffer ownership, P2P topology,
and the maximum number of simultaneously unsharded local stages. Dynamic
metadata exchange or runtime buffer allocation prevents FakePG execution and
makes whole-step CUDA-graph capture difficult to reason about.

Wire the existing PyTorch pipelining facilities into TorchTitan:

- enable schedule-derived directed-edge P2P groups whenever pipeline
  parallelism is active;
- enable deterministic receive-buffer recycling for looped schedules;
- construct complete static decoder stage I/O metadata when the PP group is
  fake, failing clearly when a custom split cannot be represented;
- pass the configurable local-stage parameter-residency bound to the schedule;
- carry a fixed upper bound for varlen document metadata.

Directed P2P groups, receive-buffer reuse, and metadata selection are runtime
invariants, not user policy knobs. Their groups and assignments are finalized
before warmup; execution does not poll, recolor storage, or mutate the
schedule. The only public memory-policy knob added here is
`pipeline_parallel_max_param_unsharded_stages`.

This PR consumes a fake PP group when distributed initialization supplies one;
it does not define the fake or real-PP/fake-SPMD launch modes. Those topology
and launch contracts are introduced independently in #4652. Looped-schedule
CUDA-graph enablement is isolated in #4591.

Test Plan:
- `python -m pytest -q tests/unit_tests/cpu/test_no_new_cli_options.py tests/unit_tests/cpu/test_pipeline_parallel.py tests/unit_tests/cpu/test_config_manager.py tests/unit_tests/cpu/test_varlen_attention.py`
- Stack-level one-GPU pure-FakePG PP and four-GPU real-PP/fake-SPMD execution
  with repeated full CUDA-graph replay.

[ghstack-poisoned]
sanketpurandare added a commit that referenced this pull request Sep 24, 2026
…les"

TorchTitan captures the complete eager pipeline step, but previously rejected
every multi-stage looped schedule. A shared P2P communicator can serialize
logical pipeline edges in incompatible orders during CUDA-graph replay, so
removing that validation without first fixing communicator ownership would make
the expanded support unsafe.

Enable looped-schedule capture after the lower runtime layers establish the
required invariant:

- PyTorch derives one process group for each directed physical-rank edge from
  the finalized schedule topology, initializes and warms those groups before
  capture, and shares one Store connection across split children;
- TorchTitan enables that directed-edge topology automatically whenever PP is
  active; it is not a user-selectable policy;
- existing supported PP feature recipes now exercise full-step capture rather
  than disabling CUDA graphs solely because their schedule is looped.

This change only removes the obsolete blanket validation. It does not rewrite
schedule actions, add polling, require deferred receives, or expose another
pipeline configuration flag. Cases with independent blockers remain explicitly
disabled, including validation that reinitializes the shared schedule and
split-backward schedules with values that are not CUDA-graph inputs.

This supersedes the closed prototype in #4485.

Dependencies:
- pytorch/pytorch#196665
- pytorch/pytorch#196666
- #4540

Test Plan:
- `python -m pytest -q tests/unit_tests/cpu/test_config_manager.py -k cuda_graphs`
- `pre-commit run --files tests/integration_tests/features.py tests/unit_tests/cpu/test_config_manager.py torchtitan/config/configs.py torchtitan/trainer.py torchtitan_recipes/tests/features.py`
- Four-GPU `pp_looped_1f1b_cudagraph` integration test with repeated full-step
  capture and replay.

[ghstack-poisoned]
sanketpurandare added a commit that referenced this pull request Sep 24, 2026
TorchTitan captures the complete eager pipeline step, but previously rejected
every multi-stage looped schedule. A shared P2P communicator can serialize
logical pipeline edges in incompatible orders during CUDA-graph replay, so
removing that validation without first fixing communicator ownership would make
the expanded support unsafe.

Enable looped-schedule capture after the lower runtime layers establish the
required invariant:

- PyTorch derives one process group for each directed physical-rank edge from
  the finalized schedule topology, initializes and warms those groups before
  capture, and shares one Store connection across split children;
- TorchTitan enables that directed-edge topology automatically whenever PP is
  active; it is not a user-selectable policy;
- existing supported PP feature recipes now exercise full-step capture rather
  than disabling CUDA graphs solely because their schedule is looped.

This change only removes the obsolete blanket validation. It does not rewrite
schedule actions, add polling, require deferred receives, or expose another
pipeline configuration flag. Cases with independent blockers remain explicitly
disabled, including validation that reinitializes the shared schedule and
split-backward schedules with values that are not CUDA-graph inputs.

This supersedes the closed prototype in #4485.

Dependencies:
- pytorch/pytorch#196665
- pytorch/pytorch#196666
- #4540

Test Plan:
- `python -m pytest -q tests/unit_tests/cpu/test_config_manager.py -k cuda_graphs`
- `pre-commit run --files tests/integration_tests/features.py tests/unit_tests/cpu/test_config_manager.py torchtitan/config/configs.py torchtitan/trainer.py torchtitan_recipes/tests/features.py`
- Four-GPU `pp_looped_1f1b_cudagraph` integration test with repeated full-step
  capture and replay.

[ghstack-poisoned]
TorchTitan's eager pipeline runtime needs deterministic, precomputed contracts
before warmup: stage-boundary metadata, receive-buffer ownership, P2P topology,
and the maximum number of simultaneously unsharded local stages. Dynamic
metadata exchange or runtime buffer allocation prevents FakePG execution and
makes whole-step CUDA-graph capture difficult to reason about.

Wire the existing PyTorch pipelining facilities into TorchTitan:

- enable schedule-derived directed-edge P2P groups whenever pipeline
  parallelism is active;
- enable deterministic receive-buffer recycling for looped schedules;
- construct complete static decoder stage I/O metadata when the PP group is
  fake, failing clearly when a custom split cannot be represented;
- pass the configurable local-stage parameter-residency bound to the schedule;
- carry a fixed upper bound for varlen document metadata.

Directed P2P groups, receive-buffer reuse, and metadata selection are runtime
invariants, not user policy knobs. Their groups and assignments are finalized
before warmup; execution does not poll, recolor storage, or mutate the
schedule. The only public memory-policy knob added here is
`pipeline_parallel_max_param_unsharded_stages`.

This PR consumes a fake PP group when distributed initialization supplies one;
it does not define the fake or real-PP/fake-SPMD launch modes. Those topology
and launch contracts are introduced independently in #4652. Looped-schedule
CUDA-graph enablement is isolated in #4591.

Test Plan:
- `python -m pytest -q tests/unit_tests/cpu/test_no_new_cli_options.py tests/unit_tests/cpu/test_pipeline_parallel.py tests/unit_tests/cpu/test_config_manager.py tests/unit_tests/cpu/test_varlen_attention.py`
- Stack-level one-GPU pure-FakePG PP and four-GPU real-PP/fake-SPMD execution
  with repeated full CUDA-graph replay.

[ghstack-poisoned]
…ecution"

TorchTitan's eager pipeline runtime needs deterministic, precomputed contracts
before warmup: stage-boundary metadata, receive-buffer ownership, P2P topology,
and the maximum number of simultaneously unsharded local stages. Dynamic
metadata exchange or runtime buffer allocation prevents FakePG execution and
makes whole-step CUDA-graph capture difficult to reason about.

Wire the existing PyTorch pipelining facilities into TorchTitan:

- enable schedule-derived directed-edge P2P groups whenever pipeline
  parallelism is active;
- enable deterministic receive-buffer recycling for looped schedules;
- construct complete static decoder stage I/O metadata when the PP group is
  fake, failing clearly when a custom split cannot be represented;
- pass the configurable local-stage parameter-residency bound to the schedule;
- carry a fixed upper bound for varlen document metadata.

Directed P2P groups, receive-buffer reuse, and metadata selection are runtime
invariants, not user policy knobs. Their groups and assignments are finalized
before warmup; execution does not poll, recolor storage, or mutate the
schedule. The only public memory-policy knob added here is
`pipeline_parallel_max_param_unsharded_stages`.

This PR consumes a fake PP group when distributed initialization supplies one;
it does not define the fake or real-PP/fake-SPMD launch modes. Those topology
and launch contracts are introduced independently in #4652. Looped-schedule
CUDA-graph enablement is isolated in #4591.

Test Plan:
- `python -m pytest -q tests/unit_tests/cpu/test_no_new_cli_options.py tests/unit_tests/cpu/test_pipeline_parallel.py tests/unit_tests/cpu/test_config_manager.py tests/unit_tests/cpu/test_varlen_attention.py`
- Stack-level one-GPU pure-FakePG PP and four-GPU real-PP/fake-SPMD execution
  with repeated full CUDA-graph replay.

[ghstack-poisoned]
sanketpurandare added a commit that referenced this pull request Sep 25, 2026
…les"

TorchTitan captures the complete eager pipeline step, but previously rejected
every multi-stage looped schedule. A shared P2P communicator can serialize
logical pipeline edges in incompatible orders during CUDA-graph replay, so
removing that validation without first fixing communicator ownership would make
the expanded support unsafe.

Enable looped-schedule capture after the lower runtime layers establish the
required invariant:

- PyTorch derives one process group for each directed physical-rank edge from
  the finalized schedule topology, initializes and warms those groups before
  capture, and shares one Store connection across split children;
- TorchTitan enables that directed-edge topology automatically whenever PP is
  active; it is not a user-selectable policy;
- existing supported PP feature recipes now exercise full-step capture rather
  than disabling CUDA graphs solely because their schedule is looped.

This change only removes the obsolete blanket validation. It does not rewrite
schedule actions, add polling, require deferred receives, or expose another
pipeline configuration flag. Cases with independent blockers remain explicitly
disabled, including validation that reinitializes the shared schedule and
split-backward schedules with values that are not CUDA-graph inputs.

This supersedes the closed prototype in #4485.

Dependencies:
- pytorch/pytorch#196665
- pytorch/pytorch#196666
- #4540

Test Plan:
- `python -m pytest -q tests/unit_tests/cpu/test_config_manager.py -k cuda_graphs`
- `pre-commit run --files tests/integration_tests/features.py tests/unit_tests/cpu/test_config_manager.py torchtitan/config/configs.py torchtitan/trainer.py torchtitan_recipes/tests/features.py`
- Four-GPU `pp_looped_1f1b_cudagraph` integration test with repeated full-step
  capture and replay.

[ghstack-poisoned]
sanketpurandare added a commit that referenced this pull request Sep 25, 2026
TorchTitan captures the complete eager pipeline step, but previously rejected
every multi-stage looped schedule. A shared P2P communicator can serialize
logical pipeline edges in incompatible orders during CUDA-graph replay, so
removing that validation without first fixing communicator ownership would make
the expanded support unsafe.

Enable looped-schedule capture after the lower runtime layers establish the
required invariant:

- PyTorch derives one process group for each directed physical-rank edge from
  the finalized schedule topology, initializes and warms those groups before
  capture, and shares one Store connection across split children;
- TorchTitan enables that directed-edge topology automatically whenever PP is
  active; it is not a user-selectable policy;
- existing supported PP feature recipes now exercise full-step capture rather
  than disabling CUDA graphs solely because their schedule is looped.

This change only removes the obsolete blanket validation. It does not rewrite
schedule actions, add polling, require deferred receives, or expose another
pipeline configuration flag. Cases with independent blockers remain explicitly
disabled, including validation that reinitializes the shared schedule and
split-backward schedules with values that are not CUDA-graph inputs.

This supersedes the closed prototype in #4485.

Dependencies:
- pytorch/pytorch#196665
- pytorch/pytorch#196666
- #4540

Test Plan:
- `python -m pytest -q tests/unit_tests/cpu/test_config_manager.py -k cuda_graphs`
- `pre-commit run --files tests/integration_tests/features.py tests/unit_tests/cpu/test_config_manager.py torchtitan/config/configs.py torchtitan/trainer.py torchtitan_recipes/tests/features.py`
- Four-GPU `pp_looped_1f1b_cudagraph` integration test with repeated full-step
  capture and replay.

[ghstack-poisoned]
TorchTitan's eager pipeline runtime needs deterministic, precomputed contracts
before warmup: stage-boundary metadata, receive-buffer ownership, P2P topology,
and the maximum number of simultaneously unsharded local stages. Dynamic
metadata exchange or runtime buffer allocation prevents FakePG execution and
makes whole-step CUDA-graph capture difficult to reason about.

Wire the existing PyTorch pipelining facilities into TorchTitan:

- enable schedule-derived directed-edge P2P groups whenever pipeline
  parallelism is active;
- enable deterministic receive-buffer recycling for looped schedules;
- construct complete static decoder stage I/O metadata when the PP group is
  fake, failing clearly when a custom split cannot be represented;
- pass the configurable local-stage parameter-residency bound to the schedule;
- carry a fixed upper bound for varlen document metadata.

Directed P2P groups, receive-buffer reuse, and metadata selection are runtime
invariants, not user policy knobs. Their groups and assignments are finalized
before warmup; execution does not poll, recolor storage, or mutate the
schedule. The only public memory-policy knob added here is
`pipeline_parallel_max_param_unsharded_stages`.

This PR consumes a fake PP group when distributed initialization supplies one;
it does not define the fake or real-PP/fake-SPMD launch modes. Those topology
and launch contracts are introduced independently in #4652. Looped-schedule
CUDA-graph enablement is isolated in #4591.

Test Plan:
- `python -m pytest -q tests/unit_tests/cpu/test_no_new_cli_options.py tests/unit_tests/cpu/test_pipeline_parallel.py tests/unit_tests/cpu/test_config_manager.py tests/unit_tests/cpu/test_varlen_attention.py`
- Stack-level one-GPU pure-FakePG PP and four-GPU real-PP/fake-SPMD execution
  with repeated full CUDA-graph replay.

[ghstack-poisoned]
…ecution"

TorchTitan's eager pipeline runtime needs deterministic, precomputed contracts
before warmup: stage-boundary metadata, receive-buffer ownership, P2P topology,
and the maximum number of simultaneously unsharded local stages. Dynamic
metadata exchange or runtime buffer allocation prevents FakePG execution and
makes whole-step CUDA-graph capture difficult to reason about.

Wire the existing PyTorch pipelining facilities into TorchTitan:

- enable schedule-derived directed-edge P2P groups whenever pipeline
  parallelism is active;
- enable deterministic receive-buffer recycling for looped schedules;
- construct complete static decoder stage I/O metadata when the PP group is
  fake, failing clearly when a custom split cannot be represented;
- pass the configurable local-stage parameter-residency bound to the schedule;
- carry a fixed upper bound for varlen document metadata.

Directed P2P groups, receive-buffer reuse, and metadata selection are runtime
invariants, not user policy knobs. Their groups and assignments are finalized
before warmup; execution does not poll, recolor storage, or mutate the
schedule. The only public memory-policy knob added here is
`pipeline_parallel_max_param_unsharded_stages`.

This PR consumes a fake PP group when distributed initialization supplies one;
it does not define the fake or real-PP/fake-SPMD launch modes. Those topology
and launch contracts are introduced independently in #4652. Looped-schedule
CUDA-graph enablement is isolated in #4591.

Test Plan:
- `python -m pytest -q tests/unit_tests/cpu/test_no_new_cli_options.py tests/unit_tests/cpu/test_pipeline_parallel.py tests/unit_tests/cpu/test_config_manager.py tests/unit_tests/cpu/test_varlen_attention.py`
- Stack-level one-GPU pure-FakePG PP and four-GPU real-PP/fake-SPMD execution
  with repeated full CUDA-graph replay.

[ghstack-poisoned]
sanketpurandare added a commit that referenced this pull request Sep 25, 2026
…les"

TorchTitan captures the complete eager pipeline step, but previously rejected
every multi-stage looped schedule. A shared P2P communicator can serialize
logical pipeline edges in incompatible orders during CUDA-graph replay, so
removing that validation without first fixing communicator ownership would make
the expanded support unsafe.

Enable looped-schedule capture after the lower runtime layers establish the
required invariant:

- PyTorch derives one process group for each directed physical-rank edge from
  the finalized schedule topology, initializes and warms those groups before
  capture, and shares one Store connection across split children;
- TorchTitan enables that directed-edge topology automatically whenever PP is
  active; it is not a user-selectable policy;
- existing supported PP feature recipes now exercise full-step capture rather
  than disabling CUDA graphs solely because their schedule is looped.

This change only removes the obsolete blanket validation. It does not rewrite
schedule actions, add polling, require deferred receives, or expose another
pipeline configuration flag. Cases with independent blockers remain explicitly
disabled, including validation that reinitializes the shared schedule and
split-backward schedules with values that are not CUDA-graph inputs.

This supersedes the closed prototype in #4485.

Dependencies:
- pytorch/pytorch#196665
- pytorch/pytorch#196666
- #4540

Test Plan:
- `python -m pytest -q tests/unit_tests/cpu/test_config_manager.py -k cuda_graphs`
- `pre-commit run --files tests/integration_tests/features.py tests/unit_tests/cpu/test_config_manager.py torchtitan/config/configs.py torchtitan/trainer.py torchtitan_recipes/tests/features.py`
- Four-GPU `pp_looped_1f1b_cudagraph` integration test with repeated full-step
  capture and replay.

[ghstack-poisoned]
sanketpurandare added a commit that referenced this pull request Sep 25, 2026
TorchTitan captures the complete eager pipeline step, but previously rejected
every multi-stage looped schedule. A shared P2P communicator can serialize
logical pipeline edges in incompatible orders during CUDA-graph replay, so
removing that validation without first fixing communicator ownership would make
the expanded support unsafe.

Enable looped-schedule capture after the lower runtime layers establish the
required invariant:

- PyTorch derives one process group for each directed physical-rank edge from
  the finalized schedule topology, initializes and warms those groups before
  capture, and shares one Store connection across split children;
- TorchTitan enables that directed-edge topology automatically whenever PP is
  active; it is not a user-selectable policy;
- existing supported PP feature recipes now exercise full-step capture rather
  than disabling CUDA graphs solely because their schedule is looped.

This change only removes the obsolete blanket validation. It does not rewrite
schedule actions, add polling, require deferred receives, or expose another
pipeline configuration flag. Cases with independent blockers remain explicitly
disabled, including validation that reinitializes the shared schedule and
split-backward schedules with values that are not CUDA-graph inputs.

This supersedes the closed prototype in #4485.

Dependencies:
- pytorch/pytorch#196665
- pytorch/pytorch#196666
- #4540

Test Plan:
- `python -m pytest -q tests/unit_tests/cpu/test_config_manager.py -k cuda_graphs`
- `pre-commit run --files tests/integration_tests/features.py tests/unit_tests/cpu/test_config_manager.py torchtitan/config/configs.py torchtitan/trainer.py torchtitan_recipes/tests/features.py`
- Four-GPU `pp_looped_1f1b_cudagraph` integration test with repeated full-step
  capture and replay.

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@sanketpurandare sanketpurandare added the ciflow/real-pg Run the full 8-GPU Real PG integration suite label Sep 25, 2026
Comment thread torchtitan/training_engine.py Outdated
# communicator. This keeps NCCL operation ordering deterministic across
# ranks during eager execution and whole-step CUDA graph replay.
dist_config.pipeline_per_edge_p2p = (
config.parallelism.pipeline_parallel_degree > 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder what happens if pp degree is 2 -- in that case (where PP group communicator is a P2P communicator?), do we need this special treatment?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even with PP degree 2, the parent PP group is one two-rank communicator, while the schedule uses both directed edges. With pipeline_per_edge_p2p, PyTorch creates and preconnects separate child groups for (0, 1) and (1, 0), so forward and backward traffic have independent deterministic FIFO order instead of depending on first-use ordering in the shared communicator. I kept the policy enabled for every pp_degree > 1; PyTorch's focused two-rank topology test covers this case.

submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Sep 26, 2026
Summary:
Give eager pipeline stages complete static decoder-boundary metadata when the
PP process group is fake. This avoids dynamic cross-rank metadata inference
while preserving PyTorch's existing `PipelineStage` ownership and supports
first, middle, and last decoder stages across context and tensor parallelism.

TorchTitan also enables PyTorch's schedule-derived per-edge P2P groups whenever
pipeline parallelism is active. Each directed physical edge receives a
deterministic communicator FIFO that PyTorch initializes before execution and
CUDA-graph capture, avoiding dependency on NCCL first-use ordering.

Receive buffers use PyTorch's landed just-in-time allocation. A buffer is
allocated immediately before `RECV_F` or `RECV_B`, transferred to the owning
forward or backward computation, and released according to tensor lifetime.
CUDA-graph capture records stable addresses, so replay does not repeat Python
allocation. TorchTitan adds no receive-buffer policy or stage subclass.

Test Plan:
- `python -m pytest -q tests/unit_tests/cpu/test_no_new_cli_options.py tests/unit_tests/cpu/test_pipeline_parallel.py tests/unit_tests/cpu/test_config_manager.py` (79 passed)
- Scoped pre-commit hooks passed for the changed files.
- Targeted Pyrefly reported 0 errors for production files.
- One-process pure-FakePG PP4 and four-process real-PP/fake-SPMD PP4 each completed 10 steps, captured at step 3, and replayed through step 10.

ghstack-source-id: 77e9061

Pull Request resolved: pytorch/torchtitan#4540
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@sanketpurandare
sanketpurandare changed the base branch from gh/sanketpurandare/16/base to main September 26, 2026 18:17
@sanketpurandare
sanketpurandare merged commit 8a0d567 into main Sep 26, 2026
27 of 28 checks passed
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Sep 26, 2026
Summary:
Give eager pipeline stages complete static decoder-boundary metadata when the
PP process group is fake. This avoids dynamic cross-rank metadata inference
while preserving PyTorch's existing `PipelineStage` ownership and supports
first, middle, and last decoder stages across context and tensor parallelism.

TorchTitan also enables PyTorch's schedule-derived per-edge P2P groups whenever
pipeline parallelism is active. Each directed physical edge receives a
deterministic communicator FIFO that PyTorch initializes before execution and
CUDA-graph capture, avoiding dependency on NCCL first-use ordering.

Receive buffers use PyTorch's landed just-in-time allocation. A buffer is
allocated immediately before `RECV_F` or `RECV_B`, transferred to the owning
forward or backward computation, and released according to tensor lifetime.
CUDA-graph capture records stable addresses, so replay does not repeat Python
allocation. TorchTitan adds no receive-buffer policy or stage subclass.

Test Plan:
- `python -m pytest -q tests/unit_tests/cpu/test_no_new_cli_options.py tests/unit_tests/cpu/test_pipeline_parallel.py tests/unit_tests/cpu/test_config_manager.py` (79 passed)
- Scoped pre-commit hooks passed for the changed files.
- Targeted Pyrefly reported 0 errors for production files.
- One-process pure-FakePG PP4 and four-process real-PP/fake-SPMD PP4 each completed 10 steps, captured at step 3, and replayed through step 10.

ghstack-source-id: 106f48a

Pull Request resolved: pytorch/torchtitan#4540
@tianyu-l
tianyu-l deleted the gh/sanketpurandare/16/head branch September 26, 2026 23:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/real-pg Run the full 8-GPU Real PG integration suite ciflow/rl 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.

2 participants