Skip to content

[2/2] Introduce torch.accelerator.Graph as a unified frontend Graph interface - #171285

Closed
guangyey wants to merge 30 commits into
gh/guangyey/267/basefrom
gh/guangyey/267/head
Closed

guangyey wants to merge 30 commits into
gh/guangyey/267/basefrom
gh/guangyey/267/head

Conversation

@guangyey

@guangyey guangyey commented Dec 25, 2025 •

Copy link
Copy Markdown
Collaborator

Stack from ghstack (oldest at bottom):

Motivation

This PR introduces a unified frontend API torch.accelerator.Graph which wraps at::accelerator::Graph and provides capture and replay functionality. The interface is largely aligned with torch.cuda.Graph, and can be extended on demand in the future.
Inspired by the design and usage of torch.Stream, torch.accelerator.Graph is intended to act as a context manager that captures work on the current of the current device. This enables a simple and intuitive usage pattern, for example:

s = torch.Stream()
g = torch.accelerator.Graph()
with s, g:
    # capture ops
    pass

g.replay()

There are, however, some subtle but important differences compared to the existing torch.cuda.graph context manager:

  1. Stream selection semantics
    torch.cuda.graph implicitly creates and uses a non-default torch.cuda.Stream as the capture stream if none is explicitly provided. In contrast, torch.accelerator.Graph always captures on the current stream. On CUDA, if no additional stream is set and the current stream is the default stream, this may raise an error such as CUDA graphs must be captured on a non-default stream. Refer to
    default_capture_stream: torch.cuda.Stream | None = None
  2. Graph memory pool support
    torch.cuda.graph allows users to specify a graph pool to share memory across graph captures. See
    pool (optional): Opaque token (returned by a call to :func:`~torch.cuda.graph_pool_handle()` or
    torch.accelerator.Graph currently supports specifying a graph pool at construction time.

So, I think we don't provide torch.accelerator.graph to align with the behavior of torch.cuda.graph, torch.accelerator.Graph is enough as the context manager.

cc @albanD @eellison @EikanWang

@pytorch-bot

pytorch-bot Bot commented Dec 25, 2025 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit facd52b with merge base b699fa5 (image):
💚 Looks good so far! There are no failures yet. 💚

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

guangyey added a commit that referenced this pull request Dec 25, 2025
@guangyey
guangyey marked this pull request as draft December 25, 2025 03:02
@guangyey guangyey changed the title [2/2] Introduce torch.accelerator.Graph as a unified frontend Graph interface [wip] [2/2] Introduce torch.accelerator.Graph as a unified frontend Graph interface Dec 25, 2025
@guangyey guangyey changed the title [wip] [2/2] Introduce torch.accelerator.Graph as a unified frontend Graph interface [WIP] [2/2] Introduce torch.accelerator.Graph as a unified frontend Graph interface Dec 25, 2025
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@guangyey guangyey changed the title [WIP] [2/2] Introduce torch.accelerator.Graph as a unified frontend Graph interface [2/2] Introduce torch.accelerator.Graph as a unified frontend Graph interface Dec 30, 2025
[ghstack-poisoned]
// Dump the captured graph to a file for debugging purposes. The file format
// and content are backend-specific.
void debug_dump(const std::string& path) const {
void debug_dump(const std::string& path) {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

minor fix: this function should not be defined as const.

guangyey added 3 commits March 4, 2026 10:21
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@EikanWang

Copy link
Copy Markdown
Collaborator

@guangyey , test plan?

@guangyey

guangyey commented Mar 5, 2026

Copy link
Copy Markdown
Collaborator Author

@guangyey , test plan?

The simple test case was added in #171313 for CUDA support, another test case was added in #176421 for XPU support.

@guangyey

guangyey commented Mar 6, 2026

Copy link
Copy Markdown
Collaborator Author

Hi @eellison, just wanted to check if you have any additional comments or concerns on this PR.

sandy-gags pushed a commit to sandy-gags/pytorch that referenced this pull request Mar 12, 2026
[ghstack-poisoned]
@guangyey

Copy link
Copy Markdown
Collaborator Author

Thanks very much~
@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

ryanzhang22 pushed a commit to ryanzhang22/pytorch that referenced this pull request Mar 19, 2026
…nterface (pytorch#171285)

# Motivation
This PR introduces a unified frontend API `torch.accelerator.Graph` which wraps `at::accelerator::Graph` and provides capture and replay functionality. The interface is largely aligned with `torch.cuda.Graph`, and can be extended on demand in the future.
Inspired by the design and usage of `torch.Stream`, `torch.accelerator.Graph` is intended to act as a context manager that captures work on the current of the current device. This enables a simple and intuitive usage pattern, for example:
```python
s = torch.Stream()
g = torch.accelerator.Graph()
with s, g:
    # capture ops
    pass

g.replay()
```
There are, however, some subtle but important differences compared to the existing `torch.cuda.graph` context manager:
1. Stream selection semantics
    `torch.cuda.graph` implicitly creates and uses a non-default `torch.cuda.Stream` as the capture stream if none is explicitly provided. In contrast, `torch.accelerator.Graph` always captures on the current stream. On CUDA, if no additional stream is set and the current stream is the default stream, this may raise an error such as `CUDA graphs must be captured on a non-default stream.` Refer to https://github.com/pytorch/pytorch/blob/0db3b1eee4dde4dede75904914fc04cf1f9f65b9/torch/cuda/graphs.py#L212
2. Graph memory pool support
    `torch.cuda.graph` allows users to specify a graph pool to share memory across graph captures. See https://github.com/pytorch/pytorch/blob/0db3b1eee4dde4dede75904914fc04cf1f9f65b9/torch/cuda/graphs.py#L190 `torch.accelerator.Graph` currently supports specifying a graph pool at construction time.

So, I think we don't provide `torch.accelerator.graph` to align with the behavior of `torch.cuda.graph`, `torch.accelerator.Graph` is enough as the context manager.

Pull Request resolved: pytorch#171285
Approved by: https://github.com/EikanWang, https://github.com/eellison
pytorchmergebot pushed a commit that referenced this pull request Mar 20, 2026
# Motivation
This PR introduces a unified API, `torch.accelerator.empty_host_cache`, for releasing unused host (pinned) memory. This API can also be used prior to graph capture. For example, as discussed in #167507, CUDA supports graph capture and replay involving host memory as well. Therefore, it is important to clean up both the device and host memory before starting graph capture to ensure correct and predictable behavior.

Pull Request resolved: #171270
Approved by: https://github.com/eellison
ghstack dependencies: #171285
EmanueleCoradin pushed a commit to EmanueleCoradin/pytorch that referenced this pull request Mar 30, 2026
…nterface (pytorch#171285)

# Motivation
This PR introduces a unified frontend API `torch.accelerator.Graph` which wraps `at::accelerator::Graph` and provides capture and replay functionality. The interface is largely aligned with `torch.cuda.Graph`, and can be extended on demand in the future.
Inspired by the design and usage of `torch.Stream`, `torch.accelerator.Graph` is intended to act as a context manager that captures work on the current of the current device. This enables a simple and intuitive usage pattern, for example:
```python
s = torch.Stream()
g = torch.accelerator.Graph()
with s, g:
    # capture ops
    pass

g.replay()
```
There are, however, some subtle but important differences compared to the existing `torch.cuda.graph` context manager:
1. Stream selection semantics
    `torch.cuda.graph` implicitly creates and uses a non-default `torch.cuda.Stream` as the capture stream if none is explicitly provided. In contrast, `torch.accelerator.Graph` always captures on the current stream. On CUDA, if no additional stream is set and the current stream is the default stream, this may raise an error such as `CUDA graphs must be captured on a non-default stream.` Refer to https://github.com/pytorch/pytorch/blob/0db3b1eee4dde4dede75904914fc04cf1f9f65b9/torch/cuda/graphs.py#L212
2. Graph memory pool support
    `torch.cuda.graph` allows users to specify a graph pool to share memory across graph captures. See https://github.com/pytorch/pytorch/blob/0db3b1eee4dde4dede75904914fc04cf1f9f65b9/torch/cuda/graphs.py#L190 `torch.accelerator.Graph` currently supports specifying a graph pool at construction time.

So, I think we don't provide `torch.accelerator.graph` to align with the behavior of `torch.cuda.graph`, `torch.accelerator.Graph` is enough as the context manager.

Pull Request resolved: pytorch#171285
Approved by: https://github.com/EikanWang, https://github.com/eellison
AaronWang04 pushed a commit to AaronWang04/pytorch that referenced this pull request Mar 31, 2026
…nterface (pytorch#171285)

# Motivation
This PR introduces a unified frontend API `torch.accelerator.Graph` which wraps `at::accelerator::Graph` and provides capture and replay functionality. The interface is largely aligned with `torch.cuda.Graph`, and can be extended on demand in the future.
Inspired by the design and usage of `torch.Stream`, `torch.accelerator.Graph` is intended to act as a context manager that captures work on the current of the current device. This enables a simple and intuitive usage pattern, for example:
```python
s = torch.Stream()
g = torch.accelerator.Graph()
with s, g:
    # capture ops
    pass

g.replay()
```
There are, however, some subtle but important differences compared to the existing `torch.cuda.graph` context manager:
1. Stream selection semantics
    `torch.cuda.graph` implicitly creates and uses a non-default `torch.cuda.Stream` as the capture stream if none is explicitly provided. In contrast, `torch.accelerator.Graph` always captures on the current stream. On CUDA, if no additional stream is set and the current stream is the default stream, this may raise an error such as `CUDA graphs must be captured on a non-default stream.` Refer to https://github.com/pytorch/pytorch/blob/0db3b1eee4dde4dede75904914fc04cf1f9f65b9/torch/cuda/graphs.py#L212
2. Graph memory pool support
    `torch.cuda.graph` allows users to specify a graph pool to share memory across graph captures. See https://github.com/pytorch/pytorch/blob/0db3b1eee4dde4dede75904914fc04cf1f9f65b9/torch/cuda/graphs.py#L190 `torch.accelerator.Graph` currently supports specifying a graph pool at construction time.

So, I think we don't provide `torch.accelerator.graph` to align with the behavior of `torch.cuda.graph`, `torch.accelerator.Graph` is enough as the context manager.

Pull Request resolved: pytorch#171285
Approved by: https://github.com/EikanWang, https://github.com/eellison
AaronWang04 pushed a commit to AaronWang04/pytorch that referenced this pull request Mar 31, 2026
# Motivation
This PR introduces a unified API, `torch.accelerator.empty_host_cache`, for releasing unused host (pinned) memory. This API can also be used prior to graph capture. For example, as discussed in pytorch#167507, CUDA supports graph capture and replay involving host memory as well. Therefore, it is important to clean up both the device and host memory before starting graph capture to ensure correct and predictable behavior.

Pull Request resolved: pytorch#171270
Approved by: https://github.com/eellison
ghstack dependencies: pytorch#171285
nklshy-aws pushed a commit to nklshy-aws/pytorch that referenced this pull request Apr 7, 2026
# Motivation
This PR introduces a unified API, `torch.accelerator.empty_host_cache`, for releasing unused host (pinned) memory. This API can also be used prior to graph capture. For example, as discussed in pytorch#167507, CUDA supports graph capture and replay involving host memory as well. Therefore, it is important to clean up both the device and host memory before starting graph capture to ensure correct and predictable behavior.

Pull Request resolved: pytorch#171270
Approved by: https://github.com/eellison
ghstack dependencies: pytorch#171285
@github-actions
github-actions Bot deleted the gh/guangyey/267/head branch April 18, 2026 02:22
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…nterface (pytorch#171285)

# Motivation
This PR introduces a unified frontend API `torch.accelerator.Graph` which wraps `at::accelerator::Graph` and provides capture and replay functionality. The interface is largely aligned with `torch.cuda.Graph`, and can be extended on demand in the future.
Inspired by the design and usage of `torch.Stream`, `torch.accelerator.Graph` is intended to act as a context manager that captures work on the current of the current device. This enables a simple and intuitive usage pattern, for example:
```python
s = torch.Stream()
g = torch.accelerator.Graph()
with s, g:
    # capture ops
    pass

g.replay()
```
There are, however, some subtle but important differences compared to the existing `torch.cuda.graph` context manager:
1. Stream selection semantics
    `torch.cuda.graph` implicitly creates and uses a non-default `torch.cuda.Stream` as the capture stream if none is explicitly provided. In contrast, `torch.accelerator.Graph` always captures on the current stream. On CUDA, if no additional stream is set and the current stream is the default stream, this may raise an error such as `CUDA graphs must be captured on a non-default stream.` Refer to https://github.com/pytorch/pytorch/blob/0db3b1eee4dde4dede75904914fc04cf1f9f65b9/torch/cuda/graphs.py#L212
2. Graph memory pool support
    `torch.cuda.graph` allows users to specify a graph pool to share memory across graph captures. See https://github.com/pytorch/pytorch/blob/0db3b1eee4dde4dede75904914fc04cf1f9f65b9/torch/cuda/graphs.py#L190 `torch.accelerator.Graph` currently supports specifying a graph pool at construction time.

So, I think we don't provide `torch.accelerator.graph` to align with the behavior of `torch.cuda.graph`, `torch.accelerator.Graph` is enough as the context manager.

Pull Request resolved: pytorch#171285
Approved by: https://github.com/EikanWang, https://github.com/eellison
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
# Motivation
This PR introduces a unified API, `torch.accelerator.empty_host_cache`, for releasing unused host (pinned) memory. This API can also be used prior to graph capture. For example, as discussed in pytorch#167507, CUDA supports graph capture and replay involving host memory as well. Therefore, it is important to clean up both the device and host memory before starting graph capture to ensure correct and predictable behavior.

Pull Request resolved: pytorch#171270
Approved by: https://github.com/eellison
ghstack dependencies: pytorch#171285
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 Merged module: accelerator Issues related to the shared accelerator API open source release notes: python_frontend python frontend release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants