Skip to content

Set thread stream capture mode to relaxed only when current stream is capturing - #174724

Closed
galv wants to merge 1 commit into
pytorch:mainfrom
galv:conditional_relaxed_host_alloc
Closed

galv wants to merge 1 commit into
pytorch:mainfrom
galv:conditional_relaxed_host_alloc

Conversation

@galv

@galv galv commented Feb 10, 2026

Copy link
Copy Markdown
Collaborator

This matches the behavior of CUDACachingAllocator.cpp.

If a user wants to prevent a pin_memory() call from a data loading thread from disrupting their stream capture, they use "thread_local" stream caputre mode for that stream capture.

Requested by @ngimel as a follow up to #167507

… capturing

This matches the behavior of CUDACachingAllocator.cpp.

If a user wants to prevent a pin_memory() call from a data loading
thread from disrupting their stream capture, they use "thread_local"
stream caputre mode for that stream capture.
@galv
galv requested a review from ngimel February 10, 2026 21:51
@pytorch-bot

pytorch-bot Bot commented Feb 10, 2026

Copy link
Copy Markdown

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.

@pytorch-bot

pytorch-bot Bot commented Feb 10, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 1 New Failure

As of commit b190451 with merge base c24b6a2 (image):

NEW FAILURE - The following job has failed:

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

@galv galv added the topic: not user facing topic category label Feb 10, 2026

@ngimel ngimel 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.

Let's try this, if someone complains we can try a weaker version with checking captures_underway.empty() that should work even with global capture mode.

@galv

galv commented Feb 10, 2026

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Feb 10, 2026
@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

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: trunk / linux-jammy-rocm-py3.10 / test (default, 5, 6, linux.rocm.gpu.gfx950.1)

Details for Dev Infra team Raised by workflow job

@ngimel

ngimel commented Feb 11, 2026

Copy link
Copy Markdown
Collaborator

@pytorchbot merge -i

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged while ignoring the following 1 checks: trunk / linux-jammy-rocm-py3.10 / test (default, 5, 6, linux.rocm.gpu.gfx950.1)

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

sujuyu added a commit to sujuyu/pytorch that referenced this pull request Nov 20, 2026
…ng captures

cudaHostAlloc and cudaHostRegister ran in relaxed capture mode
unconditionally. They now do so only while a capture is registered with the
allocator, like the event query (the captures_underway check from pytorch#174724).
That still covers what main's current-stream check does not: inside a
HostMemPool context, a capture begun on this thread takes only the capturing
stream, so an allocation on another stream goes to the pool, and allocating
a block or querying a block's event there runs on the capturing thread while
its current stream is not capturing. Frees stay relaxed unconditionally
(pytorch#200733).

Test Plan:

test_side_stream_alloc_during_global_capture allocates on a side stream from
the routed pool during a global-mode capture, querying a pending event and
allocating a new block, in both pinned_use_cuda_host_register modes. With
main's current-stream check instead, both cases fail with
cudaErrorStreamCaptureUnsupported and invalidate the capture.

```bash
python -m pytest test/test_cuda.py -k test_side_stream_alloc_during_global_capture
```

Authored with assistance from Claude Code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
pytorchmergebot pushed a commit to sujuyu/pytorch that referenced this pull request Nov 25, 2026
…ng captures

cudaHostAlloc and cudaHostRegister ran in relaxed capture mode
unconditionally. They now do so only while a capture is registered with the
allocator, like the event query (the captures_underway check from pytorch#174724).
That still covers what main's current-stream check does not: inside a
HostMemPool context, a capture begun on this thread takes only the capturing
stream, so an allocation on another stream goes to the pool, and allocating
a block or querying a block's event there runs on the capturing thread while
its current stream is not capturing. Frees stay relaxed unconditionally
(pytorch#200733).

Test Plan:

test_side_stream_alloc_during_global_capture allocates on a side stream from
the routed pool during a global-mode capture, querying a pending event and
allocating a new block, in both pinned_use_cuda_host_register modes. With
main's current-stream check instead, both cases fail with
cudaErrorStreamCaptureUnsupported and invalidate the capture.

```bash
python -m pytest test/test_cuda.py -k test_side_stream_alloc_during_global_capture
```

Authored with assistance from Claude Code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants