Skip to content

[do-not-review] [cudagraph] Support looped pipeline schedules - #4485

Closed
xmfan wants to merge 1 commit into
xmfan/stack/52from
xmfan/stack/54
Closed

xmfan wants to merge 1 commit into
xmfan/stack/52from
xmfan/stack/54

Conversation

@xmfan

@xmfan xmfan commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Stacked PRs:


[do-not-review] [cudagraph] Support looped pipeline schedules

Why

Looped pipeline schedules map multiple virtual stages onto each physical rank. During CUDA graph capture, their forward and backward P2P operations need independent directed communicators so that operations between the same physical ranks cannot conflict through one communicator FIFO.

This depends on upstream PyTorch support for per-direction pipeline process groups and deferred receives.

Change

Expose the upstream deferred-receive and per-direction P2P controls as configuration-only fields. Forward deferred-receive selection to multi-stage schedules and configure the distributed P2P mode before initialization.

Allow CUDA graph capture for looped schedules only when explicit per-direction process groups or automatic TorchComms groups are enabled.

Testing

The unit tests cover looped-schedule validation, TorchComms handling, directed-group initialization, and deferred-receive forwarding.

  • python -m pytest -q tests/unit_tests/cpu/test_cudagraph.py tests/unit_tests/cpu/test_trainer.py tests/unit_tests/cpu/test_config_manager.py tests/unit_tests/cpu/test_pipeline_parallel.py tests/unit_tests/cpu/test_integration_test_definitions.py
  • PYTHONPATH=. python tests/integration_tests/run_tests.py <output> --test_suite features --test_name pp_looped_1f1b_cudagraph --execution_mode real_pg --ngpu 4 --no-parallel (10/10 steps)
  • Deterministic four-GPU eager-versus-graph comparison with seed 42: bitwise-identical loss and grad_norm for all 10 steps

## Why

Looped pipeline schedules map multiple virtual stages onto each physical rank. During CUDA graph capture, their forward and backward P2P operations need independent directed communicators so that operations between the same physical ranks cannot conflict through one communicator FIFO.

This depends on upstream PyTorch support for per-direction pipeline process groups and deferred receives.

## Change

Expose the upstream deferred-receive and per-direction P2P controls as configuration-only fields. Forward deferred-receive selection to multi-stage schedules and configure the distributed P2P mode before initialization.

Allow CUDA graph capture for looped schedules only when explicit per-direction process groups or automatic TorchComms groups are enabled.

## Testing

The unit tests cover looped-schedule validation, TorchComms handling, directed-group initialization, and deferred-receive forwarding.

- `python -m pytest -q tests/unit_tests/cpu/test_cudagraph.py tests/unit_tests/cpu/test_trainer.py tests/unit_tests/cpu/test_config_manager.py tests/unit_tests/cpu/test_pipeline_parallel.py tests/unit_tests/cpu/test_integration_test_definitions.py`
- `PYTHONPATH=. python tests/integration_tests/run_tests.py <output> --test_suite features --test_name pp_looped_1f1b_cudagraph --execution_mode real_pg --ngpu 4 --no-parallel` (10/10 steps)
- Deterministic four-GPU eager-versus-graph comparison with seed 42: bitwise-identical loss and `grad_norm` for all 10 steps

stack-info: PR: #4485, branch: xmfan/stack/54
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 4, 2026
@xmfan xmfan closed this Sep 4, 2026
tianyu-l pushed a commit that referenced this pull request Sep 5, 2026
Stacked PRs:
 * #4485
 * #4435
 * #4433
 * #4432
 * __->__#4430


--- --- ---

### [trainer][pp] Release consumed pipeline loss graphs


## Why

The pipeline schedule needs autograd-connected per-microbatch losses
until backward finishes. At the end of the schedule, PyTorch extends the
Trainer-provided `losses` list with those tensors, then clears its
internal list.

Clearing the schedule's internal list is insufficient because the
Trainer's list now owns the same attached tensors.

Previously, TorchTitan stacked those attached losses. The resulting
reporting loss retained edges to every microbatch graph. Although
`train_step()` later detached a value for metrics, its original `loss`
variable could keep the attached aggregate alive through optimization
and logging.

## Loss ownership and lifetime

### Common pipeline-schedule path

```text
PipelineSchedule._internal_losses
    [loss_mb0*, loss_mb1*, ...]       * attached to autograd
                    |
                    | losses.extend(_internal_losses)
                    | _internal_losses.clear()
                    v
Trainer-owned losses
    [loss_mb0*, loss_mb1*, ...]
```

### Before

```text
Trainer-owned attached losses
           |
           | torch.stack(...).sum()
           v
reporting loss*
           |
           | returned to train_step()
           v
loss.detach()
           |
           +--> accumulated_loss
                    |
                    +--> distributed metric reduction
                    |
                    +--> metrics_processor.log(...)
```

The original reporting loss remains attached while it is still
referenced, keeping all of the microbatch graphs reachable.

### After

```text
Trainer-owned attached losses
           |
           +--> detached_losses = [loss.detach(), ...]
           |
           +--> losses.clear()
                    |
                    +--> attached losses and their graphs can be released here

Detached losses
           |
           | torch.stack(...).sum()
           v
reporting loss without grad_fn
           |
           +--> train_step accumulation
           +--> distributed metric reduction
           +--> metrics_processor.log(...)
```

## Change

Detach each per-microbatch loss at the Trainer ownership boundary, clear
the original loss list, and construct the reporting value only from
detached tensors.

Out-of-place `detach()` is intentional: losses may be views, for which
`detach_()` is invalid, and the Trainer should not mutate aliases held
by pipeline schedule code.

This does not change forward computation, backward gradients, or the
reported loss value. It only shortens the lifetime of completed autograd
graphs and their associated activations.

## Testing

The unit test verifies:

- The reported loss value is unchanged.
- Backward produces the expected gradients.
- The reporting loss has no `grad_fn`.
- The Trainer-owned loss list is cleared.
- The original losses and activations are immediately reclaimable.

Tested with:

- `python -m pytest -q tests/unit_tests/cpu/test_trainer.py -k
pp_forward_backward_step_releases_consumed_loss_graphs`
- Four-GPU `pp_looped_1f1b_cudagraph` integration run, 10/10 steps.
sanketpurandare added a commit that referenced this pull request Sep 14, 2026
…les"

TorchTitan already captures the complete eager pipeline step, but it rejects
all multi-stage schedules because a shared P2P communicator can impose an
ordering cycle during CUDA graph replay. Permit looped schedules when the
PyTorch pipeline runtime is configured to use one process group per directed
physical-rank edge.

Keep the policy explicit: looped schedules without directed P2P groups still
fail configuration validation. Add a four-GPU Interleaved1F1B feature recipe
and integration entry that exercises full-step CUDA graph capture without
making receive deferral a correctness requirement.

This supersedes the closed prototype in #4485.

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

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 replay

[ghstack-poisoned]
sanketpurandare added a commit that referenced this pull request Sep 14, 2026
TorchTitan already captures the complete eager pipeline step, but it rejects
all multi-stage schedules because a shared P2P communicator can impose an
ordering cycle during CUDA graph replay. Permit looped schedules when the
PyTorch pipeline runtime is configured to use one process group per directed
physical-rank edge.

Keep the policy explicit: looped schedules without directed P2P groups still
fail configuration validation. Add a four-GPU Interleaved1F1B feature recipe
and integration entry that exercises full-step CUDA graph capture without
making receive deferral a correctness requirement.

This supersedes the closed prototype in #4485.

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

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 replay

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

TorchTitan already captures the complete eager pipeline step, but it rejects
all multi-stage schedules because a shared P2P communicator can impose an
ordering cycle during CUDA graph replay. Permit looped schedules when the
PyTorch pipeline runtime is configured to use one process group per directed
physical-rank edge.

Keep the policy explicit: looped schedules without directed P2P groups still
fail configuration validation. Add a four-GPU Interleaved1F1B feature recipe
and integration entry that exercises full-step CUDA graph capture without
making receive deferral a correctness requirement.

This supersedes the closed prototype in #4485.

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

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 replay

[ghstack-poisoned]
sanketpurandare added a commit that referenced this pull request Sep 14, 2026
TorchTitan already captures the complete eager pipeline step, but it rejects
all multi-stage schedules because a shared P2P communicator can impose an
ordering cycle during CUDA graph replay. Permit looped schedules when the
PyTorch pipeline runtime is configured to use one process group per directed
physical-rank edge.

Keep the policy explicit: looped schedules without directed P2P groups still
fail configuration validation. Add a four-GPU Interleaved1F1B feature recipe
and integration entry that exercises full-step CUDA graph capture without
making receive deferral a correctness requirement.

This supersedes the closed prototype in #4485.

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

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 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]
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]
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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

1 participant