Repository navigation
[CUDA] Fix reentrant deadlock in torch.cuda._lazy_call - #182948
iupaikov-amd wants to merge 4 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/182948
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (9 Unrelated Failures)As of commit 8e86afd with merge base 2472115 ( BROKEN TRUNK - The following jobs failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
UNSTABLE - The following jobs are marked as unstable, possibly due to flakiness on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@pytorchbot label "topic: not user facing" |
|
Warning: Unknown label
Please add the new label to .github/pytorch-probot.yml |
|
|
|
@liangel-02 Hello, can you tell me if the triaged tag on this PR is signaling that it is ready to be merged or should we ping additional people to take a look at this? |
|
cc @jeffdaily |
This fixes deadlock with torch.compile while running bigger models in MAD engine. This is a cherry-pick of upstream PR: pytorch#182948 Made with [Cursor](https://cursor.com)
This fixes deadlock with torch.compile while running bigger models in MAD engine. This is a cherry-pick of upstream PR: pytorch#182948 Made with [Cursor](https://cursor.com)
This fixes deadlock with torch.compile while running bigger models in MAD engine. This is a cherry-pick of upstream PR: pytorch#182948 Made with [Cursor](https://cursor.com)
This fixes deadlock with torch.compile while running bigger models in MAD engine. This is a cherry-pick of upstream PR: pytorch#182948 Made with [Cursor](https://cursor.com)
|
@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here |
The earlier commits on this PR fix the reentrant deadlock only for the already-initialized path. The queued path still deadlocks: if _lazy_call(outer) runs before CUDA is initialized, outer lands in _queued_calls and is later invoked by _lazy_init, which drains the queue while holding _initialization_lock and with _initialized still False. When outer re-enters _lazy_call (e.g. via torch.cuda.set_rng_state or torch.cuda.manual_seed), the nested call falls through the is_initialized() fast path and blocks on the non-reentrant lock its own thread already holds.
Fix by mirroring _lazy_init's own reentrancy guard: also run the callback immediately when _tls.is_initializing is set. torch._C._cuda_init() has already completed at that point, so immediate execution is safe and matches what the drain would do anyway.
Also refactor the regression test into a shared helper, add a queued-path variant, and raise the subprocess timeout from 15s to 60s: the pass path exits quickly regardless of the limit, and 15s is tight for torch import plus CUDA init on slow CI.
Test Plan:
On a ROCm host (gfx90a), ran the new tests against a pre-fix build; both fail with the deadlock timeout, confirming they catch both bug variants:
cd test && python test_cuda.py -v -k lazy_call_reentrant
Then exec'd the patched _lazy_call from this branch into the installed torch and ran both subprocess scenarios directly; both complete without deadlock. CI runs the tests against the fully built fix.
Authored with the assistance of Claude Code.
|
Successfully rebased |
5b648fa to
8e86afd
Compare
|
@pytorchbot merge |
Merge startedYour 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 |
Merge failedReason: 3 mandatory check(s) failed. The first few are: Dig deeper by viewing the failures on hud |
|
Warning: Unknown label
Please add the new label to .github/pytorch-probot.yml |
|
@pytorchbot merge |
Merge startedYour 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 |
…atter_realizes_expensive_src ### Summary Resolve conflict with pytorch main by merging the upstream addition of test_scatter_realizes_expensive_src into the refactored TestSchedulerCuda class. ### Changes - Add DeterministicGuard to common_utils imports (required by the new test method) - Add test_scatter_realizes_expensive_src to TestSchedulerCuda, adapted to the refactored class structure: remove @onlyCUDA (class is already only_for="cuda"), change device="cuda" to device parameter, add device to method signature ### Motivation Upstream commit cec50d8 (pytorch#182948) added test_scatter_realizes_expensive_src to the original TestScheduler class in test_inductor_scheduler.py. Our refactoring PR split TestScheduler into Generic/Accelerator/Cuda classes, causing a merge conflict. This commit resolves the conflict by placing the new method in TestSchedulerCuda (the CUDA-specific class) and adapting it to use the device parameter pattern consistent with the other CUDA methods in the refactored file. ### Test Plan python test/inductor/test_inductor_scheduler.py Authored with AI assistant.
This fixes deadlock with torch.compile while running bigger models in MAD engine. This is a cherry-pick of upstream PR: pytorch#182948 Made with [Cursor](https://cursor.com) (cherry picked from commit 24cf333)
This fixes deadlock with torch.compile while running bigger models in MAD engine. This is a cherry-pick of upstream PR: pytorch#182948 Made with [Cursor](https://cursor.com) (cherry picked from commit 24cf333)
Fixes a deadlock where a callback passed to `torch.cuda._lazy_call` re-enters `_lazy_call`, e.g. via `torch.cuda.set_rng_state` or `torch.cuda.manual_seed`. `_initialization_lock` is a non-reentrant `threading.Lock`, so the nested acquisition blocks forever. Originally encountered on ROCm while running larger models under `torch.compile`. There are two reentrancy paths, both fixed here: 1. **Already initialized**: callbacks used to run while holding `_initialization_lock`, so a nested `_lazy_call` deadlocked. Callbacks now run outside the lock: a fast path runs them immediately when CUDA is initialized, and otherwise the lock is held only for the queue-or-run decision, with the callback invoked after release if initialization won the race. 2. **Queued callback during initialization**: `_lazy_init` drains `_queued_calls` while holding the lock and with `_initialized` still `False`, so a queued callback that re-enters `_lazy_call` deadlocked on its own thread's lock. `_lazy_call` now mirrors `_lazy_init`'s reentrancy guard and runs the callback immediately when `_tls.is_initializing` is set; `torch._C._cuda_init()` has already completed at that point, so this is safe. Regression tests `test_lazy_call_reentrant_set_rng_state_does_not_deadlock` and `test_lazy_call_reentrant_queued_does_not_deadlock` run each scenario in a subprocess with a generous timeout. Both fail with a deadlock timeout before the fix and pass after (verified on a ROCm gfx90a host). `torch/mtia/__init__.py` and `torch/xpu/__init__.py` have the same callback-under-lock pattern and would need the same treatment in a follow-up. *Parts of this PR (the queued-path fix, second regression test, and this description) were drafted with the assistance of Claude Code and reviewed by a human.* Pull Request resolved: pytorch#182948 Approved by: https://github.com/jeffdaily Co-authored-by: Jeff Daily <[email protected]>
Fixes a deadlock where a callback passed to
torch.cuda._lazy_callre-enters_lazy_call, e.g. viatorch.cuda.set_rng_stateortorch.cuda.manual_seed._initialization_lockis a non-reentrantthreading.Lock, so the nested acquisition blocks forever. Originally encountered on ROCm while running larger models undertorch.compile.There are two reentrancy paths, both fixed here:
_initialization_lock, so a nested_lazy_calldeadlocked. Callbacks now run outside the lock: a fast path runs them immediately when CUDA is initialized, and otherwise the lock is held only for the queue-or-run decision, with the callback invoked after release if initialization won the race._lazy_initdrains_queued_callswhile holding the lock and with_initializedstillFalse, so a queued callback that re-enters_lazy_calldeadlocked on its own thread's lock._lazy_callnow mirrors_lazy_init's reentrancy guard and runs the callback immediately when_tls.is_initializingis set;torch._C._cuda_init()has already completed at that point, so this is safe.Regression tests
test_lazy_call_reentrant_set_rng_state_does_not_deadlockandtest_lazy_call_reentrant_queued_does_not_deadlockrun each scenario in a subprocess with a generous timeout. Both fail with a deadlock timeout before the fix and pass after (verified on a ROCm gfx90a host).torch/mtia/__init__.pyandtorch/xpu/__init__.pyhave the same callback-under-lock pattern and would need the same treatment in a follow-up.Parts of this PR (the queued-path fix, second regression test, and this description) were drafted with the assistance of Claude Code and reviewed by a human.