Repository navigation
Conversation
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/197836
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (4 Unrelated Failures)As of commit dca90ec with merge base b7ab856 ( BROKEN TRUNK - The following jobs failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
CRCR (non-blocking) - The following CRCR downstream CI jobs failed but are non-blocking:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
[ghstack-poisoned]
MPSStream serializes GPU work through a per-stream serial dispatch queue, and `commandEncoder()`/`endKernelCoalescing()`/`commit()` all mutate the stream's `_commandBuffer` and `_commandEncoder`. Several callers touched that state from the calling thread instead, so a second thread running MPS ops concurrently could have its encoder ended and released mid-use. The result is a SIGSEGV binding a released encoder, "A command encoder is already encoding to this command buffer" when a second encoder is opened on a buffer that still has one, or `_status < MTLCommandBufferStatusCommitted` when encoding into a committed buffer. A thread that aborts while owning the queue also leaves the other thread's `dispatch_sync` waiting forever, so the process sometimes hangs rather than crashes. Two groups of callers are fixed: - arange/linspace/logspace in RangeFactories.mm, both branches of Eye.mm, and RenormKernel.mm took the encoder before entering `dispatch_sync_with_rethrow` and used it inside the block. The `commandEncoder()` call moves inside. Every other caller in the tree already acquires it inside the block, or is a helper only ever invoked from one. - `MPSHooks::deviceSynchronize`, `commitStream` and `getCommandBuffer` drove `endKernelCoalescing()` and commit straight from the calling thread. These are what `torch.mps.synchronize()` and extension code reach, and they are now wrapped in `dispatch_sync_with_rethrow` like the neighbouring `synchronizeAllMPSStreams` already was. The second group is why the regression test uses an explicit `torch.mps.synchronize()`: syncing via `float(tensor)` goes through `copy_and_sync`, which was already correctly queued, and hides the MPSHooks half of the bug. The companion cache-guarding fix for this issue landed as #167541. Fixes #197805 Test Plan: ```bash python test/test_mps.py TestMPS.test_multithreaded_arange ``` 20 runs on each build, Apple M4 Max running macOS 26.5: | build | failures | | --- | --- | | main | 20/20 (SIGABRT) | | main + the RangeFactories/Eye/Renorm half | 20/20 (SIGABRT, SIGSEGV) | | main + both halves | 0/20 | The test was minimized against main: dropping the dtype and unary-op variation left the crash rate unchanged at 20/20, while dropping the `synchronize()` call took warm runs to 0/20, which is what identified the MPSHooks path. ```bash python test/test_mps.py ``` `failures=2, errors=1` with and without the change, same three pre-existing failures either way. Authored with the assistance of Claude Code. Co-Authored-By: Claude Opus 5 <[email protected]> ghstack-source-id: 5f691ae Pull-Request: #197836
MPSStream serializes GPU work through a per-stream serial dispatch queue, and `commandEncoder()`/`endKernelCoalescing()`/`commit()` all mutate the stream's `_commandBuffer` and `_commandEncoder`. Several callers touched that state from the calling thread instead, so a second thread running MPS ops concurrently could have its encoder ended and released mid-use. The result is a SIGSEGV binding a released encoder, "A command encoder is already encoding to this command buffer" when a second encoder is opened on a buffer that still has one, or `_status < MTLCommandBufferStatusCommitted` when encoding into a committed buffer. A thread that aborts while owning the queue also leaves the other thread's `dispatch_sync` waiting forever, so the process sometimes hangs rather than crashes. Two groups of callers are fixed: - arange/linspace/logspace in RangeFactories.mm, both branches of Eye.mm, and RenormKernel.mm took the encoder before entering `dispatch_sync_with_rethrow` and used it inside the block. The `commandEncoder()` call moves inside. Every other caller in the tree already acquires it inside the block, or is a helper only ever invoked from one. - `MPSHooks::deviceSynchronize`, `commitStream` and `getCommandBuffer` drove `endKernelCoalescing()` and commit straight from the calling thread. These are what `torch.mps.synchronize()` and extension code reach, and they are now wrapped in `dispatch_sync_with_rethrow` like the neighbouring `synchronizeAllMPSStreams` already was. The second group is why the regression test uses an explicit `torch.mps.synchronize()`: syncing via `float(tensor)` goes through `copy_and_sync`, which was already correctly queued, and hides the MPSHooks half of the bug. The companion cache-guarding fix for this issue landed as #167541. Fixes #197805 Test Plan: ```bash python test/test_mps.py TestMPS.test_multithreaded_arange ``` 20 runs on each build, Apple M4 Max running macOS 26.5: | build | failures | | --- | --- | | main | 20/20 (SIGABRT) | | main + the RangeFactories/Eye/Renorm half | 20/20 (SIGABRT, SIGSEGV) | | main + both halves | 0/20 | The test was minimized against main: dropping the dtype and unary-op variation left the crash rate unchanged at 20/20, while dropping the `synchronize()` call took warm runs to 0/20, which is what identified the MPSHooks path. ```bash python test/test_mps.py ``` `failures=2, errors=1` with and without the change, same three pre-existing failures either way. Authored with the assistance of Claude Code. Co-Authored-By: Claude Opus 5 <[email protected]> ghstack-source-id: 5f691ae Pull-Request: #197836
|
@pytorchbot merge -f "Lint + MPS are green" |
Merge startedYour change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
|
@pytorchbot revert -m "Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable" -c autorevert This PR is attributed to have caused regression in: Note: This revert was accelerated by the AI advisor: AI advisor: related (confidence=0.97) Please investigate and fix the issues. |
|
Claude finished @pytorch-auto-revert[bot]'s task in 4m 22s —— View job Autorevert diagnostics
The revert looks legitimate. Details: evidence, root cause, and suggested fixEvidenceFrom the failing job log: Three attempts, all SIGTRAP ( Root cause: re-entrant
|
|
@pytorchbot successfully started a revert job. Check the current status here. |
…197836)" This reverts commit 00ec4ca. Reverted #197836 on behalf of https://github.com/pytorch-auto-revert due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](#197836 (comment)))
|
@malfet your PR has been successfully reverted. |
[ghstack-poisoned]
MPSStream serializes GPU work through a per-stream serial dispatch queue, and `commandEncoder()`/`endKernelCoalescing()`/`commit()` all mutate the stream's `_commandBuffer` and `_commandEncoder`. Several callers touched that state from the calling thread instead, so a second thread running MPS ops concurrently could have its encoder ended and released mid-use: SIGSEGV binding a released encoder, "A command encoder is already encoding to this command buffer" when a second encoder is opened on a buffer that still has one, or `_status < MTLCommandBufferStatusCommitted` when encoding into a committed buffer. A thread that aborts while owning the queue also leaves the other thread's `dispatch_sync` waiting forever, so the process sometimes hangs rather than crashes. arange/linspace/logspace in RangeFactories.mm, both branches of Eye.mm, and RenormKernel.mm took the encoder before entering `dispatch_sync_with_rethrow` and used it inside the block; the `commandEncoder()` call moves inside. Every other caller in the tree already acquires it inside the block, or is a helper only ever invoked from one. The three MPSHooks entry points need splitting rather than one blanket rule, which is what got the first attempt reverted (see below). They have opposite queue-ownership contracts: - `deviceSynchronize()` is reached from `torch.mps.synchronize()` and `torch::mps::synchronize()`. Both callers are off the serial queue, so it takes the queue itself. - `commitStream()` and `getCommandBuffer()` back `torch::mps::commit()` and `get_command_buffer()`, which per torch/mps.h are encoding-time calls the caller drives from inside its own `dispatch_sync()` on `get_dispatch_queue()`. They must not dispatch again, so they assert the contract with `dispatch_assert_queue()` instead. Note `dispatch_assert_queue` is live in release builds, so an extension calling `torch::mps::commit()` or `get_command_buffer()` from outside the queue now traps at the call site rather than racing the encoder. Nothing in tree does this: the only callers are torch/csrc/api/src/mps.cpp, and neither is bound to Python. The MPSHooks half is why the regression test syncs with an explicit `torch.mps.synchronize()`: syncing via `float(tensor)` goes through `copy_and_sync`, which was already correctly queued, and hides it. The companion cache-guarding fix for this issue landed as #167541. Reland of #197836, reverted in 74acb12. The first version wrapped all three hooks in `dispatch_sync`, which made `torch::mps::commit()` and `get_command_buffer()` re-enter the serial queue their caller already owned; libdispatch traps on that, so test_mps_extension failed with SIGTRAP. That test lives in the macos-py3-arm64 default shard, not the mps shard, so it was not covered by the signal the original was merged on. Fixes #197805 Test Plan: ```bash python test/test_cpp_extensions_jit.py -k test_mps_extension python test/test_cpp_extensions_jit.py ``` The first is the test that forced the revert: 0/5 failures now, and the full file is 41 passed / 15 skipped. ```bash python test/test_mps.py TestMPS.test_multithreaded_arange ``` 20 runs on each build, Apple M4 Max running macOS 26.5: | build | failures | | --- | --- | | main | 20/20 (SIGABRT) | | main + the RangeFactories/Eye/Renorm half | 20/20 (SIGABRT, SIGSEGV) | | main + both halves | 0/20 | The test was minimized against main: dropping the dtype and unary-op variation left the crash rate unchanged at 20/20, while dropping the `synchronize()` call took warm runs to 0/20, which is what identified the MPSHooks path. ```bash python test/test_mps.py ``` `failures=2, errors=1` with and without the change, same three pre-existing failures either way. Authored with the assistance of Claude Code. Co-Authored-By: Claude Opus 5 <[email protected]> ghstack-source-id: 18d2b50 Pull-Request: #197836
|
@pytorchbot merge -i |
Merge startedYour change will be merged while ignoring the following 2 checks: trunk / linux-noble-rocm-py3.11-mi350 / test (default, 8, 8, amd-dpx-linux.rocm.gpu.gfx950.1), trunk / linux-jammy-cuda13.2-py3.10-gcc11 / test (default, 11, 14, mt-l-x86aavx2-29-113-l4) Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
MPSStream::commandEncoder() hands out the encoder shared by every kernel coalesced on the stream, and endKernelCoalescing() ends and releases it. Only the stream's serial queue orders the two, so the encoder has to be fetched from a block running on that queue. Store the stream pointer on its serial queue with dispatch_queue_set_specific() and assert in commandEncoder() that dispatch_get_specific() returns this stream, so a caller outside the queue fails on its first call with a Python exception. Without the check such a caller crashes the process (SIGSEGV or a Metal assertion abort) once another thread ends the shared encoder between the fetch and its use. Follow-up to pytorch#197836, see pytorch#197805. Test Plan: ``` python test/test_mps.py -k test_command_encoder_outside_stream_queue ``` Co-authored-by: Claude Fable 5.1 <[email protected]>
The guard that landed in #167541 uses `std::recursive_mutex`, that changes `sizeof(MetalShaderLibrary)` and therefore breaks ABI with existing out-of-tree C++ extensions, and recursive calls were made from other methods within the same class. Make it a file-static `std::mutex` in OperationUtils.mm, where every accessor that touches library caches are defined. Dropping recursion means untangling the two chains that nested: - `hasFunction` called `getFunctionNames`, which locks. It now checks the populated flag under the lock, computes the names unlocked, then reacquires to fill the set, re-testing the flag in case another thread got there first. - `getCachedKernelFunctionPtr` called `getLibrary` + `getLibraryPipelineState`, both of which lock. It now looks up under the lock, builds the kernel unlocked and reacquires to insert; `try_emplace` keeps whichever thread won and the loser's kernel is dropped. `getPipelineStateForFunc` never nested: `getLibraryPipelineState(getLibrary(), fname)` evaluates the argument before the call, so those locks are sequential. No behavior change intended; this is the cleanup discussed on #167541. Authored with the assistance of Claude Code. Co-Authored-By: Claude Opus 5 <[email protected]> Pull Request resolved: #197837 Approved by: https://github.com/Skylion007 ghstack dependencies: #197836
MPSStream::commandEncoder() hands out the encoder shared by every kernel coalesced on the stream, and endKernelCoalescing() ends and releases it. Only the stream's serial queue orders the two, so the encoder has to be fetched from a block running on that queue. Store the stream pointer on its serial queue with dispatch_queue_set_specific() and assert in commandEncoder() that dispatch_get_specific() returns this stream, so a caller outside the queue fails on its first call with a Python exception. Without the check such a caller crashes the process (SIGSEGV or a Metal assertion abort) once another thread ends the shared encoder between the fetch and its use. Follow-up to pytorch#197836, see pytorch#197805. Test Plan: ``` python test/test_mps.py -k test_command_encoder_outside_stream_queue ``` Co-authored-by: Claude Fable 5.1 <[email protected]>
Stack from ghstack (oldest at bottom):
MPSStream serializes GPU work through a per-stream serial dispatch queue, and
commandEncoder()/endKernelCoalescing()/commit()all mutate the stream's_commandBufferand_commandEncoder. Several callers touched that state fromthe calling thread instead, so a second thread running MPS ops concurrently
could have its encoder ended and released mid-use: SIGSEGV binding a released
encoder, "A command encoder is already encoding to this command buffer" when a
second encoder is opened on a buffer that still has one, or
_status < MTLCommandBufferStatusCommittedwhen encoding into a committedbuffer. A thread that aborts while owning the queue also leaves the other
thread's
dispatch_syncwaiting forever, so the process sometimes hangs ratherthan crashes.
arange/linspace/logspace in RangeFactories.mm, both branches of Eye.mm, and
RenormKernel.mm took the encoder before entering
dispatch_sync_with_rethrowand used it inside the block; the
commandEncoder()call moves inside. Everyother caller in the tree already acquires it inside the block, or is a helper
only ever invoked from one.
The three MPSHooks entry points need splitting rather than one blanket rule,
which is what got the first attempt reverted (see below). They have opposite
queue-ownership contracts:
deviceSynchronize()is reached fromtorch.mps.synchronize()andtorch::mps::synchronize(). Both callers are off the serial queue, so ittakes the queue itself.
commitStream()andgetCommandBuffer()backtorch::mps::commit()andget_command_buffer(), which per torch/mps.h are encoding-time calls thecaller drives from inside its own
dispatch_sync()onget_dispatch_queue(). They must not dispatch again, so they assert thecontract with
dispatch_assert_queue()instead.Note
dispatch_assert_queueis live in release builds, so an extension callingtorch::mps::commit()orget_command_buffer()from outside the queue nowtraps at the call site rather than racing the encoder. Nothing in tree does
this: the only callers are torch/csrc/api/src/mps.cpp, and neither is bound to
Python.
The MPSHooks half is why the regression test syncs with an explicit
torch.mps.synchronize(): syncing viafloat(tensor)goes throughcopy_and_sync, which was already correctly queued, and hides it.The companion cache-guarding fix for this issue landed as #167541.
Reland of #197836, reverted in 74acb12. The first version wrapped all three
hooks in
dispatch_sync, which madetorch::mps::commit()andget_command_buffer()re-enter the serial queue their caller already owned;libdispatch traps on that, so test_mps_extension failed with SIGTRAP. That test
lives in the macos-py3-arm64 default shard, not the mps shard, so it was not
covered by the signal the original was merged on.
Fixes #197805
Test Plan:
The first is the test that forced the revert: 0/5 failures now, and the full
file is 41 passed / 15 skipped.
20 runs on each build, Apple M4 Max running macOS 26.5:
The test was minimized against main: dropping the dtype and unary-op variation
left the crash rate unchanged at 20/20, while dropping the
synchronize()calltook warm runs to 0/20, which is what identified the MPSHooks path.
failures=2, errors=1with and without the change, same three pre-existingfailures either way.
Authored with the assistance of Claude Code.
Co-Authored-By: Claude Opus 5 [email protected]