Skip to content

[BC] Make graph capture backend-agnostic with torch.accelerator - #5353

Merged
tianyu-l merged 10 commits into
pytorch:mainfrom
jemitche1:jerome_m/feature/accelerator-graph
Oct 29, 2026
Merged

tianyu-l merged 10 commits into
pytorch:mainfrom
jemitche1:jerome_m/feature/accelerator-graph

Conversation

@jemitche1

@jemitche1 jemitche1 commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

This PR makes the training step graph wrapper work on any accelerator instead of only on CUDA, so backends such as XPU no longer need a near duplicate module. torchtitan/distributed/cuda_graph.py becomes the accelerator_graph package, which uses torch's device generic torch.accelerator.Graph. CUDA keeps an explicit torch.cuda.graph branch because it does not register with torch.accelerator.Graph, and that branch also keeps the profiler's kernel annotations.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 13, 2026
@pytorch-bot

pytorch-bot Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@jemitche1
jemitche1 force-pushed the jerome_m/feature/accelerator-graph branch from 5644661 to d4d94c0 Compare October 16, 2026 05:45
@jemitche1
jemitche1 marked this pull request as ready for review October 22, 2026 16:59
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 22, 2026
@jemitche1
jemitche1 force-pushed the jerome_m/feature/accelerator-graph branch from bab9c29 to 1bbbf76 Compare October 22, 2026 17:12
@SherlockNoMad

Copy link
Copy Markdown
Contributor

@claude summarize the unique API changes in a table, from source to destination.

@claude

claude Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @SherlockNoMad's task in 58s —— View job


API changes in this PR (source -> destination)

The diff covers d681f48..1bbbf76 (the PR's 2 commits). No compatibility aliases are kept, so every old name below is removed. Anything still importing torchtitan.distributed.cuda_graph will break.

Module

Source Destination
torchtitan/distributed/cuda_graph.py torchtitan/distributed/accelerator_graph.py
— torchtitan/distributed/cuda_graph_backend.py (new; registers the CUDA backend when imported)
— torchtitan/distributed/__init__.py now does from torchtitan.distributed import cuda_graph_backend so the registration always runs

Public names (renamed)

Kind Source (distributed.cuda_graph) Destination
const NUM_CUDA_GRAPH_WARMUP_STEPS accelerator_graph.NUM_ACCELERATOR_GRAPH_WARMUP_STEPS
class CUDAGraphInputSpec accelerator_graph.AcceleratorGraphInputSpec
class CUDAGraphManager accelerator_graph.AcceleratorGraphManager
singleton cuda_graph_manager accelerator_graph.accelerator_graph_manager
class CUDAGraphWrapper accelerator_graph.AcceleratorGraphWrapper
fn cuda_graphs_supported() accelerator_graph.accelerator_graphs_supported()
fn wrap_with_cuda_graph() accelerator_graph.wrap_with_accelerator_graph()
fn wrap_fwd_bwd_with_cuda_graph() accelerator_graph.wrap_fwd_bwd_with_accelerator_graph()
fn cuda_graph_teardown() accelerator_graph.accelerator_graph_teardown()
fn get_cuda_graph_annotations() accelerator_graph.get_accelerator_graph_annotations()
fn get_cuspy_cuda_graph_annotation_config() moved, name unchanged -> cuda_graph_backend.get_cuspy_cuda_graph_annotation_config()

Private names (renamed)

Source Destination
_CUDAGraphGradientState _AcceleratorGraphGradientState
_ForwardBackwardCUDAGraphWrapper _ForwardBackwardAcceleratorGraphWrapper
_wrap_with_cuda_graph _wrap_with_accelerator_graph
_run_eager_on_cuda_graph_stream _run_eager_on_accelerator_graph_stream

New APIs

Location API Purpose
accelerator_graph class GraphBackend Default backend built on torch.accelerator.Graph. Hooks: unsupported_reason(), new_stream(), initialize(stream), capture(fn, args, *, pool, stream, annotation_config), annotations(), teardown()
accelerator_graph register_graph_backend(device_type, backend) Sets the backend used for a device_type
accelerator_graph AcceleratorGraphManager.capture(fn, args, *, annotation_config) Sends capture to the active backend and collects its annotations. Before, the wrapper called torch.cuda.graph itself
accelerator_graph _backend(), _unsupported_reason() (private) Look up the backend; return a reason string, or None if capture is supported
cuda_graph_backend class CUDAGraphBackend(GraphBackend) Keeps the old CUDA path (torch.cuda.graph, dummy-graph pool warmup, thread_local capture mode, kernel annotations, ROCm excluded)

Signature and type changes on existing members

Member Source Destination
Manager.stream torch.cuda.Stream torch.Stream
Manager._dummy_graph torch.cuda.CUDAGraph | None attribute removed; moved to CUDAGraphBackend._dummy_graph
Manager.graph_pool asserted non-None can be None (taken from the first graph's pool() on non-CUDA backends)
Manager.teardown() no sync calls torch.accelerator.synchronize() first, then backend.teardown()
Wrapper._graph torch.cuda.CUDAGraph | None Any
annotation_config docstring "Passed to torch.cuda.graph" "Backend-specific annotation options"

Torch calls replaced inside the wrapper

Source Destination
torch.cuda.current_stream() torch.accelerator.current_stream()
with torch.cuda.stream(s): with s:
torch.cuda.empty_cache() torch.accelerator.empty_cache()
torch.cuda.memory_stats() / mem_get_info() torch.accelerator.memory_stats() / get_memory_info()
utils.device_type == "cuda" and torch.cuda.is_available() and hip is None current_accelerator(check_available=True).type == utils.device_type, then backend.unsupported_reason()
Optim: parameter.device.type == "cuda" parameter.device.type == device_type

Unchanged (still CUDA-named)

These keep their CUDA names, so the naming is now mixed:

  • Config fields: training.disable_cuda_graphs, training.cuda_graph_per_microbatch_group, Optim.Config.enable_cuda_graph, profiler.maybe_cuda_graph_annotation_config()
  • Helpers: post_validate_fsdp_for_cuda_graph, prepare_pipeline_schedule_for_cuda_graph
  • Internal attributes: _cuda_graph_enabled, _forward_backward_cuda_graph_enabled, _cuda_graph_annotation_config
  • Docs and experiments: distributed/CUDA_GRAPH.md (the body text is updated), experiments/graph_trainer/cuda_graph.py (cuda_graph_pass, reset_cuda_graphs)
  • One user-facing message: validator.py:~187 still warns "CUDA graph capture is only supported on NVIDIA CUDA". _wrap_with_accelerator_graph's message was changed to Graph capture is unavailable ({reason}), so the validator warning is now out of date.

@@ -211,15 +209,81 @@ def teardown(self) -> None:
self._captured_parameter_gradients = None


class CUDAGraphManager:
class GraphBackend:

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 wondering if we really need this new abstraction. What's wrong with using torch.accelerator.Graph directly? Doesn't that API already handle registration of the accelerator?

@jemitche1

jemitche1 commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor Author

@wconstab torch.accelerator.Graph only works for backends that register an implementation, and currently, only xpu does. On cuda it raises "Graph is not supported on device type: CUDA". The cuda path also needs the kernel annotations that only torch.cuda.graph provides. So, the default backend uses torch.accelerator.Graph directly and only cuda overrides it. Once torch registers cuda, cuda_graph_backend.py can be removed.

https://github.com/pytorch/pytorch/blob/8bf2072b6d5e19818b08b9e77fd23615fc553b6c/aten/src/ATen/accelerator/Graph.cpp#L8-L10

Comment thread torchtitan/distributed/__init__.py Resolved

@fegin fegin left a comment

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.

Codex caught these configurations should also be change as well:

These still keep the CUDA wording:
     - optim.enable_cuda_graph
     - training.disable_cuda_graphs
     - training.cuda_graph_per_microbatch_group
     - profiler.maybe_cuda_graph_annotation_config
     - post_validate_fsdp_for_cuda_graph
     - prepare_pipeline_schedule_for_cuda_graph
     - Validator._cuda_graph_enabled

@tianyu-l
tianyu-l requested a review from fegin October 28, 2026 02:46

@fegin fegin left a comment

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.

LGTM, please

  1. rebase
  2. change the remaining cudagraph naming for consistency.
  3. update the PR summary

@jemitche1
jemitche1 force-pushed the jerome_m/feature/accelerator-graph branch from 85add64 to 69c9e1f Compare October 28, 2026 23:22
@pytorch-bot pytorch-bot Bot added the ciflow/b200 Trigger B200 CI label Oct 28, 2026
@tianyu-l
tianyu-l requested a review from fegin October 29, 2026 00:17
@fegin fegin changed the title Make graph capture backend-agnostic with torch.accelerator [BC] Make graph capture backend-agnostic with torch.accelerator Oct 29, 2026

@fegin fegin left a comment

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.

Please fix the CI failures.

@tianyu-l
tianyu-l merged commit c8f0265 into pytorch:main Oct 29, 2026
36 checks passed
fegin added a commit that referenced this pull request Oct 30, 2026
)

## Summary (human-written)

Merge conflicts from the previous PRs cause CI failing.
`disable_cuda_graph`->`disable_accelerator_graphs`

## Why
CPU Unit Test fails on main:
https://github.com/pytorch/torchtitan/actions/runs/40973844709/job/123796998693

#5353 renamed `training.disable_cuda_graphs` to
`training.disable_accelerator_graphs`. #4920 and #5556 landed after it
and still use the old name.
`TestShippedCpRecipes::test_every_cp_recipe_passes_the_gate` builds
`deepseek_v4_debugmodel_fsdp4_ep4` and fails:

AttributeError: 'TrainingConfig' object has no attribute
'disable_cuda_graphs'

`rl_grpo_deepseek_v3_debug_varlen` in `alphabet_sort.py` has the same
stale keyword. No CPU test builds it, but it raises `TypeError` when
run.

## What changed
- Mechanical rename in two places: `tests/integration_tests/models.py`
and `torchtitan_recipes/tests/rl/alphabet_sort.py`.

## Testing
- `git grep disable_cuda_graphs` returns nothing.
- Expect the CPU Unit Test job on this PR to pass
`test_every_cp_recipe_passes_the_gate`.
saforem2 added a commit to saforem2/torchtitan that referenced this pull request Oct 31, 2026
)

Upstream c8f0265 (pytorch#5353) made accelerator graph capture backend-agnostic
via torch.accelerator. On Sunspot XPU this now captures oneCCL collectives
inside the graph (was a CUDA-only no-op before). The captured region hits
`ur_eventWait: wait method cannot be used for an event associated with a
command graph` on every arm (agpt TP=1/TP=2 + moe for_loop tolist sync).

The smoke validates numerics/sharding, not graph throughput. Disable
graphs here so Sunspot VERDICT can pass; production remains graph-enabled
(training.disable_accelerator_graphs=False by default).

Provenance: 12481831 F Exit 1 3/3 rc143 on 7d4c8e8 — re-armed here.
saforem2 added a commit to saforem2/torchtitan that referenced this pull request Oct 31, 2026
- README.md hub: refresh Recently Updated table (2026-10-31 now at top).
- running-with-newer-pytorch: 2.15 -> 2.16 (xpu-torch216 gate 12481837).
- xpu-graphs-block-oneccl-collectives: note pytorch#5353 re-break (accelerator
  graphs capture oneCCL again on XPU even without CCL_OP_SYNC=1).

Co-authored-by: internal-model
tianyu-l pushed a commit that referenced this pull request Nov 3, 2026
## Summary (human-written)
<!-- Write this yourself, not with an AI tool. 2-5 sentences: the
problem, why
     this approach, and what you checked yourself. -->

## Why

#5353 renamed `torchtitan/distributed/cuda_graph.py` and `CUDA_GRAPH.md`
to `accelerator_graph.py` and `ACCELERATOR_GRAPH.md`. #5711 fixed
`disable_cuda_graphs` call sites and left these globs.

An accelerator_graph-only PR no longer gets `ciflow/rl`, so the RL
workflows that label starts do not run, and the `cuda_graph` area no
longer requests fegin. Nothing else under `ciflow/rl` matches that diff:
not `torchtitan/rl/**`, `tests/rl/**`, `scripts/rl/**`,
`torchtitan/trainer.py`, the checkpointer, data,
`torchtitan/models/common/**`, qwen3_5, qwen3_8, or the RL workflow yaml
names.

**Example.** A PR that only edits
`torchtitan/distributed/accelerator_graph.py`:

| | Before | After |
| --- | --- | --- |
| `ciflow/rl` | not applied; RL workflows stay off | applied; RL
workflows can run |
| area reviewers | `cuda_graph` does not match, so fegin is not
requested | `cuda_graph` matches and requests fegin |

## What changed

Path metadata only. The label name `ciflow/rl`, the area name
`cuda_graph`, and reviewer `fegin` are unchanged.

- `.github/labeler.yml`: `torchtitan/distributed/cuda_graph.py` ->
`torchtitan/distributed/accelerator_graph.py`
- `.github/area_reviewers.yml`: `torchtitan/distributed/cuda_graph.py`
and `torchtitan/distributed/CUDA_GRAPH.md` ->
`torchtitan/distributed/accelerator_graph.py` and
`torchtitan/distributed/ACCELERATOR_GRAPH.md`

`torchtitan/experiments/graph_trainer/cuda_graph.py` is a different file
that still exists. The distributed area does not point at it.

## Alternatives considered

- Do not fold `accelerator_graph.py` under
`torchtitan/models/common/**`. That glob already labels model changes,
and it would not request fegin for the graph files.

## User-facing changes

None for training. CI label behavior only.

| Before | After |
| --- | --- |
| Editing `torchtitan/distributed/accelerator_graph.py` or
`ACCELERATOR_GRAPH.md` does not add `ciflow/rl` and does not request
fegin | Those edits add `ciflow/rl`, and the `cuda_graph` area requests
fegin |

## Testing

`git grep` under `.github/` shows no remaining
`torchtitan/distributed/cuda_graph.py` or
`torchtitan/distributed/CUDA_GRAPH.md`. No GPU run. No new test: this is
yaml path metadata.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/b200 Trigger B200 CI ciflow/fake-pg Run 1-GPU Fake PG integration tests 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.

5 participants