Skip to content

[BE] Move MetalShaderLibrary cache guard out of the class - #197837

Closed
malfet wants to merge 2 commits into
gh/malfet/1056/basefrom
gh/malfet/1056/head
Closed

malfet wants to merge 2 commits into
gh/malfet/1056/basefrom
gh/malfet/1056/head

Conversation

@malfet

@malfet malfet commented Sep 20, 2026 •

Copy link
Copy Markdown
Collaborator

Stack from ghstack (oldest at bottom):

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]

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Sep 20, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ You can merge normally! (3 Unrelated Failures)

As of commit 34c6ac6 with merge base b7ab856 (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

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.

@pytorch-bot pytorch-bot Bot added ciflow/mps Run MPS tests (subset of trunk) release notes: mps Release notes category labels Sep 20, 2026
[ghstack-poisoned]
@malfet malfet changed the title [MPS] Guard MetalShaderLibrary's lazily populated caches [BE][MPS] Move MetalShaderLibrary's cache guard out of the class Sep 22, 2026
malfet added a commit that referenced this pull request Sep 22, 2026
The guard that landed in #167541 is a `std::recursive_mutex` member. Two
problems with that: a member changes `sizeof(MetalShaderLibrary)`, so
out-of-tree C++ extensions compiled against the older header break, and the
recursion was only needed because the accessors call one another rather than
because anything genuinely reenters.

Make it a file-static `std::mutex` in OperationUtils.mm, where every accessor
that touches `library` or the caches is defined. Callers outside that file reach
them through out-of-line functions, so nothing else needs the mutex, and the
header goes back to its original layout -- the diff against the pre-#167541
header is a comment only.

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.

Test Plan:

```bash
python test/test_mps.py TestMPS.test_multithreaded_arange
python test/test_cpp_extensions_jit.py -k test_mps_extension
```

Both clean with the previous commit in this stack applied, on an Apple M4 Max
running macOS 26.5. The extension test matters here specifically because it
builds against the installed header, so it catches the layout change.

The header change against the pre-#167541 baseline is a comment only, so
`sizeof` is unchanged:

```bash
git diff c7e434c^ -- aten/src/ATen/native/mps/MetalShaderLibrary.h
```

```bash
python test/test_mps.py
```

`failures=2, errors=1`, the same three pre-existing failures as main.

Authored with the assistance of Claude Code.

Co-Authored-By: Claude Opus 5 <[email protected]>
ghstack-source-id: edeb7f8
Pull-Request: #197837
malfet added a commit that referenced this pull request Sep 22, 2026
The guard that landed in #167541 is a `std::recursive_mutex` member. Two
problems with that: a member changes `sizeof(MetalShaderLibrary)`, so
out-of-tree C++ extensions compiled against the older header break, and the
recursion was only needed because the accessors call one another rather than
because anything genuinely reenters.

Make it a file-static `std::mutex` in OperationUtils.mm, where every accessor
that touches `library` or the caches is defined. Callers outside that file reach
them through out-of-line functions, so nothing else needs the mutex, and the
header goes back to its original layout -- the diff against the pre-#167541
header is a comment only.

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.

Test Plan:

```bash
python test/test_mps.py TestMPS.test_multithreaded_arange
python test/test_cpp_extensions_jit.py -k test_mps_extension
```

Both clean with the previous commit in this stack applied, on an Apple M4 Max
running macOS 26.5. The extension test matters here specifically because it
builds against the installed header, so it catches the layout change.

The header change against the pre-#167541 baseline is a comment only, so
`sizeof` is unchanged:

```bash
git diff c7e434c^ -- aten/src/ATen/native/mps/MetalShaderLibrary.h
```

```bash
python test/test_mps.py
```

`failures=2, errors=1`, the same three pre-existing failures as main.

Authored with the assistance of Claude Code.

Co-Authored-By: Claude Opus 5 <[email protected]>
ghstack-source-id: edeb7f8
Pull-Request: #197837
@malfet malfet changed the title [BE][MPS] Move MetalShaderLibrary's cache guard out of the class [BE] Move MetalShaderLibrary cache guard out of the class Sep 23, 2026
@malfet

malfet commented Sep 23, 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 Sep 23, 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

@github-actions
github-actions Bot deleted the gh/malfet/1056/head branch November 29, 2026 01:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/mps Run MPS tests (subset of trunk) ciflow/trunk Trigger trunk jobs on your pull request Merged open source release notes: mps Release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants