Skip to content

Fix the torch.Stream context manager reentrance#176568

Closed
guangyey wants to merge 4 commits intogh/guangyey/297/basefrom
gh/guangyey/297/head
Closed

Fix the torch.Stream context manager reentrance#176568
guangyey wants to merge 4 commits intogh/guangyey/297/basefrom
gh/guangyey/297/head

Conversation

@guangyey
Copy link
Collaborator

@guangyey guangyey commented Mar 5, 2026

Stack from ghstack (oldest at bottom):

Motivation

This PR aims to fix torch.Stream as a context manager nested/reentrance scenario. torch.cuda.stream and torch.xpu.stream could support these usages.

The following scenario would be fixed with this PR:

import torch
s0 = torch.Stream()
with s0, s0:
    pass
import torch
s0 = torch.Stream()
s1 = torch.Stream()
with s0, s1:
    with s0, s1:
        pass

Addtional Context

Fix #176560

@pytorch-bot
Copy link

pytorch-bot bot commented Mar 5, 2026

🔗 Helpful Links

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

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

✅ No Failures

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

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

@pytorch-bot
Copy link

pytorch-bot bot commented Mar 5, 2026

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

guangyey added a commit that referenced this pull request Mar 5, 2026
ghstack-source-id: 8ad4df4
Pull Request resolved: #176568
@guangyey guangyey added ciflow/trunk Trigger trunk jobs on your pull request topic: bug fixes topic category labels Mar 5, 2026
@guangyey guangyey marked this pull request as draft March 5, 2026 06:56
guangyey added a commit that referenced this pull request Mar 5, 2026
ghstack-source-id: 269dac4
Pull Request resolved: #176568
guangyey added a commit that referenced this pull request Mar 5, 2026
ghstack-source-id: ef358dd
Pull Request resolved: #176568
@guangyey guangyey marked this pull request as ready for review March 5, 2026 09:27
guangyey added a commit that referenced this pull request Mar 5, 2026
ghstack-source-id: bd81b41
Pull Request resolved: #176568
@guangyey guangyey requested a review from albanD March 5, 2026 12:13
[ghstack-poisoned]
Copy link
Collaborator

@albanD albanD left a comment

Choose a reason for hiding this comment

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

Sounds good!

@albanD
Copy link
Collaborator

albanD commented Mar 5, 2026

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: This PR needs a release notes: label
If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Details for Dev Infra team Raised by workflow job

@pytorch-bot
Copy link

pytorch-bot bot commented Mar 5, 2026

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@guangyey
Copy link
Collaborator Author

guangyey commented Mar 5, 2026

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: This PR needs a release notes: label
If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Details for Dev Infra team Raised by workflow job

@pytorch-bot
Copy link

pytorch-bot bot commented Mar 5, 2026

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@guangyey guangyey added the topic: not user facing topic category label Mar 5, 2026
@guangyey
Copy link
Collaborator Author

guangyey commented Mar 5, 2026

@pytorchbot merge

@pytorchmergebot
Copy link
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

@guangyey
Copy link
Collaborator Author

guangyey commented Mar 5, 2026

@pytorchbot cherry-pick --onto release/2.11 --fixes 'Bug fix for the reentrant behavior of the torch.Stream context manager.' -c critical

pytorchbot pushed a commit that referenced this pull request Mar 5, 2026
# Motivation
This PR aims to fix `torch.Stream` as a context manager nested/reentrance scenario. `torch.cuda.stream` and `torch.xpu.stream` could support these usages.

The following scenario would be fixed with this PR:
```python
import torch
s0 = torch.Stream()
with s0, s0:
    pass
```
```python
import torch
s0 = torch.Stream()
s1 = torch.Stream()
with s0, s1:
    with s0, s1:
        pass
```

# Addtional Context
Fix #176560

Pull Request resolved: #176568
Approved by: https://github.com/albanD

(cherry picked from commit d43570c)
@pytorchbot
Copy link
Collaborator

Cherry picking #176568

The cherry pick PR is at #176603 and it is linked with issue Bug fix for the reentrant behavior of the torch.Stream context manager.. The following tracker issues are updated:

Details for Dev Infra team Raised by workflow job

guangyey added 3 commits March 5, 2026 16:11
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
atalman pushed a commit that referenced this pull request Mar 9, 2026
Fix the torch.Stream context manager reentrance (#176568)

# Motivation
This PR aims to fix `torch.Stream` as a context manager nested/reentrance scenario. `torch.cuda.stream` and `torch.xpu.stream` could support these usages.

The following scenario would be fixed with this PR:
```python
import torch
s0 = torch.Stream()
with s0, s0:
    pass
```
```python
import torch
s0 = torch.Stream()
s1 = torch.Stream()
with s0, s1:
    with s0, s1:
        pass
```

# Addtional Context
Fix #176560

Pull Request resolved: #176568
Approved by: https://github.com/albanD

(cherry picked from commit d43570c)

Co-authored-by: Yu, Guangye <guangye.yu@intel.com>
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 open source topic: bug fixes topic category topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants