Skip to content

Intorduce torch.accelerator.is_graph_available to check if Graph is available - #171445

Open
guangyey wants to merge 51 commits into
gh/guangyey/270/basefrom
gh/guangyey/270/head
Open

guangyey wants to merge 51 commits into
gh/guangyey/270/basefrom
gh/guangyey/270/head

Conversation

@guangyey

@guangyey guangyey commented Dec 29, 2025 •

Copy link
Copy Markdown
Collaborator

Stack from ghstack (oldest at bottom):

Motivation

This PR aims to introduce a new API torch.accelerator.is_graph_available to detect if the current accelerator supports graph capture and replay feature. This will help the user do something such as:

if torch.accelerator.is_graph_available():
    graph = torch.accelerator.Graph()
    with graph:
        # do something

    graph.replay()
else:
    # do something

@pytorch-bot

pytorch-bot Bot commented Dec 29, 2025 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/171445

Note: Links to docs will display an error until the docs builds have been completed.

❌ You can merge normally! (7 Unrelated Failures), 1 Unclassified Failure

As of commit b88eddd with merge base fc69434 (image):
🟡 GreenLight: changes can't be reviewed due to merge_rules.yaml restrictions

NEW FAILURES SUPPRESSED BY AI (non-blocking) - The following jobs failed, but the AI CI Advisor judged the failures not to be evidence against this change:

UNCLASSIFIED FAILURE - DrCI could not classify the following job because the workflow did not run on the merge base. The failure may be pre-existing on trunk or introduced by this PR:

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

guangyey added a commit that referenced this pull request Dec 29, 2025
@guangyey
guangyey marked this pull request as draft December 29, 2025 06:41
guangyey added a commit that referenced this pull request Dec 29, 2025
guangyey added a commit that referenced this pull request Dec 29, 2025
guangyey added a commit that referenced this pull request Dec 29, 2025
guangyey added a commit that referenced this pull request Dec 29, 2025
guangyey added a commit that referenced this pull request Dec 29, 2025
guangyey added a commit that referenced this pull request Dec 29, 2025
guangyey added a commit that referenced this pull request Dec 29, 2025
guangyey added a commit that referenced this pull request Dec 29, 2025
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
guangyey added a commit that referenced this pull request Dec 29, 2025
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
guangyey added a commit that referenced this pull request Dec 29, 2025
[ghstack-poisoned]
guangyey added 2 commits April 1, 2026 15:23
[ghstack-poisoned]
[ghstack-poisoned]
guangyey added a commit that referenced this pull request Apr 2, 2026
guangyey added a commit that referenced this pull request Apr 2, 2026
@guangyey
guangyey marked this pull request as draft April 2, 2026 10:00
@guangyey
guangyey marked this pull request as ready for review April 2, 2026 10:52
guangyey added 7 commits April 2, 2026 15:16
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
guangyey added a commit that referenced this pull request Apr 14, 2026
[ghstack-poisoned]
guangyey added a commit that referenced this pull request Apr 28, 2026
[ghstack-poisoned]
@eellison
eellison removed their request for review June 11, 2026 16:17

@jeffdaily jeffdaily left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'd like to understand the design intent before this establishes a public API pattern. A few questions:

  1. Semantics on CUDA/ROCm. is_graph_available() resolves through has_graph_impl(device_type), and right now the only REGISTER_GRAPH_IMPL in the tree is XPU (aten/src/ATen/xpu/XPUGraph.cpp:270); CUDA/HIP don't register. So on a CUDA or ROCm build this returns False today, even though both support graph capture/replay via torch.cuda.graph / CUDAGraph. The docstring says it reports "whether the current accelerator supports graph capture and replay," which reads as broadly false on those backends. Is the intended meaning narrower, i.e. "is the unified torch.accelerator.Graph frontend wired up for this backend" rather than "does this device support graphs at all"? If so, could the docstring say that explicitly? And is there a plan to register CUDA/ROCm impls, so the probe doesn't mislead the majority of users in the meantime?

  2. Redundancy with Graph(). Graph's constructor already does TORCH_CHECK(has_graph_impl(...)) (Graph.cpp:8-11), so try: torch.accelerator.Graph(); except: ... probes the same registry. What's the motivating case for a separate non-throwing is_graph_available() over that, and is there a concrete caller that needs it?

  3. Precedent. torch.accelerator currently exposes only is_available() (any accelerator present) and get_device_capability() as capability probes; there's no is_<feature>_available() / supports_<feature>() anywhere in the namespace. Is this intended as the first of a per-feature-probe family, and if so is that a direction the accelerator API owners have signed off on?

@guangyey

guangyey commented Jul 16, 2026 •

Copy link
Copy Markdown
Collaborator Author

Hi @jeffdaily, thanks for your message!

  1. Semantics on CUDA/ROCm.

Previously, we submitted a PR #171313 to support torch.accelerator.Graph on CUDA/Rocm making it share the same implementation with torch.cuda.Graph, but the CUDA maintainers don't like it. If torch.accelerator.Graph is eventually supported on CUDA/Rocm, is_graph_available would provide consistent behavior across all accelerator backends.

  1. Redundancy with Graph()

I think is_graph_available provides a better user experience that relying on try-except. Feature detection through an explict API is more Pythonic, while try-except depends on the implementation details of Graph(). For example, we use torch.cuda.is_available() to check whether CUDA is available before calling APIs such as torch.cuda.synchronize, rather than relying on exceptions.
Similarly, if the user (for example, vLLM) would like to use torch.accelerator.Graph, is_graph_available provides a convenient way to determine whether graph capture is supported without having to catch an exception on backends that do not yet implement the feature (e.g., MPS, or some out-of-tree backends) and give them a chance to run a fallback path at runtiume.

  1. Precedent

I also think each module should provide its own feature detection API. For example, torch.amp.autocast.is_autocast_available belongs to the autocast module, and torch.distributed.is_backend_available belongs to the distributed module. Following this principle, I think torch.accelerator.Graph.is_graph_available is the appropriate place for graph capturing detection. I don't plan to provide generic API such as torch.accelerator.is_distributed_available, torch.accelerator.is_autocast_available etc, since those capabilities belong to their respective modules rather than torch.accelerator.
In addition, because CPU doesn't have graph capture capability, we provide torch.accelerator.Graph instead of torch.Graph, see device generic API design principle.

@github-actions

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request no-stale open source release notes: python_frontend python frontend release notes category Stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants