Skip to content

[MPS][AOTI] Check shader-library shim return codes in generated wrapper - #196634

Closed
saud5150 wants to merge 1 commit into
pytorch:mainfrom
saud5150:fix-mps-aoti-shader-error-code-check
Closed

saud5150 wants to merge 1 commit into
pytorch:mainfrom
saud5150:fix-mps-aoti-shader-error-code-check

Conversation

@saud5150

@saud5150 saud5150 commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #196233.

Summary

cpp_wrapper_mps.py generated AOTI code that ignored the return codes from:

  • aoti_torch_mps_create_shader_library
  • aoti_torch_mps_get_kernel_function

When shader compilation failed, aoti_torch_mps_create_shader_library returned an error without initializing library_handle. The generated wrapper then passed the null handle to aoti_torch_mps_get_kernel_function, causing a SIGSEGV and discarding the original Metal compiler diagnostic.

Wrap both calls with AOTI_TORCH_ERROR_CODE_CHECK, matching every other cpp_wrapper backend (cpp_wrapper_cpu.py, cpp_wrapper_cpu_array_ref.py, cpp_wrapper_gpu.py) and the hand-written consumer of this same shim API in test/cpp_extensions/libtorch_agn_2_14_extension/csrc/mps/mps_set_arg_bytes_test.cpp, which wraps every one of these calls. cpp_wrapper_mps.py was the only cpp_wrapper backend that did not reference the macro at all.

Also move the shader-library handle into its unique_ptr immediately after a successful create call. With the new error check, ownership must be established before the subsequent kernel-function lookup to avoid leaking the library on failure.

Update the codegen_additional_funcs docstring to match the generated code.

Test plan

python test/inductor/test_mps_basic.py MPSBasicTestsAOTI.test_shader_compile_error_raises
python test/inductor/test_mps_basic.py MPSBasicTestsAOTI.test_kernel_function_lookup_error_raises
python test/inductor/test_mps_basic.py
python test/inductor/test_aot_inductor.py AOTInductorTestABICompatibleMps
spin quicklint

Regression coverage

One test per shim call. Each forces a real failure and asserts the diagnostic
reaches Python; each segfaults without the fix.

  • test_shader_compile_error_raises injects invalid MSL, so
    aoti_torch_mps_create_shader_library fails.
  • test_kernel_function_lookup_error_raises renames the kernel symbol, so the
    library compiles but aoti_torch_mps_get_kernel_function cannot find
    generated_kernel. This covers the second call, which the first test never
    reaches because it fails earlier.

Before this change both segfault with no output:

$ python test/inductor/test_mps_basic.py MPSBasicTestsAOTI.test_shader_compile_error_raises; echo "exit=$?"
exit=139
$ python test/inductor/test_mps_basic.py MPSBasicTestsAOTI.test_kernel_function_lookup_error_raises; echo "exit=$?"
exit=139

After:

$ python test/inductor/test_mps_basic.py MPSBasicTestsAOTI.test_shader_compile_error_raises MPSBasicTestsAOTI.test_kernel_function_lookup_error_raises
..
----------------------------------------------------------------------
Ran 2 tests in 4.642s

OK

Results

Test Result
test/inductor/test_mps_basic.py 63 run, 63 passed
test/inductor/test_aot_inductor.py (MPS class) 386 run: 196 passed, 157 skipped, 33 expected failures
spin quicklint Passed

Environment

  • macOS 15.5, arm64
  • Apple M4 Pro
  • 2.15.0a0+gitf62c80c, built from source
  • No CUDA available; non-MPS test_aot_inductor.py device classes were not run locally

Note: #195587 currently prevents editable builds from resolving shader #includes because torch/include is absent. Full-suite results above used a local torch/include/c10 -> ../../c10 symlink. Without that workaround, Metal shader compilation fails due to the unrelated issue; this PR now reports the shim error rather than segfaulting. The two regression tests pass with or without the workaround.

BC-breaking?

No. Successful generated code is unchanged; only error handling on failure paths changes.

Follow-ups

Out of scope for this PR:

  • Add return-code checks for other emitted aoti_torch_mps_* calls.
  • Fix JIT cpp_wrapper on MPS, which currently fails on main with undeclared identifier 'get_mps_lib_0_handle'.

The implementation was prepared with an AI assistant (Claude) and reviewed and validated by the author. The reproduction, negative control, and suite runs were executed locally on the environment above.

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo

@pytorch-bot

pytorch-bot Bot commented Sep 10, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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 51ddba5 with merge base f62c80c (image):

BROKEN TRUNK - The following job 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.

@pytorch-bot pytorch-bot Bot added ciflow/inductor ciflow/mps Run MPS tests (subset of trunk) ciflow/torchtitan Run TorchTitan integration tests module: inductor labels Sep 10, 2026
@pytorch-bot

pytorch-bot Bot commented Sep 10, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@pytorch-bot

pytorch-bot Bot commented Sep 10, 2026

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/mps
  • ciflow/inductor
  • ciflow/torchtitan

Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows.

1 similar comment
@pytorch-bot

pytorch-bot Bot commented Sep 10, 2026

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/mps
  • ciflow/inductor
  • ciflow/torchtitan

Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows.

@saud5150

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "release notes: inductor"

Fixes #196233 — the generated AOTI MPS wrapper discarded both shim return codes, so a Metal compile failure dereferenced a null library handle instead of raising. @yushangdi confirmed the approach on the issue.

Could someone add the actionable label to #196233 and approve the CI workflows? Opened as draft until then.

Fixes pytorch#196233.

## Summary

`cpp_wrapper_mps.py` generated AOTI code that ignored the return codes from:

- `aoti_torch_mps_create_shader_library`
- `aoti_torch_mps_get_kernel_function`

When shader compilation failed, `aoti_torch_mps_create_shader_library` returned an error without initializing `library_handle`. The generated wrapper then passed the null handle to `aoti_torch_mps_get_kernel_function`, causing a SIGSEGV and discarding the original Metal compiler diagnostic.

Wrap both calls with `AOTI_TORCH_ERROR_CODE_CHECK`, matching every other `cpp_wrapper` backend (`cpp_wrapper_cpu.py`, `cpp_wrapper_cpu_array_ref.py`, `cpp_wrapper_gpu.py`) and the hand-written consumer of this same shim API in `test/cpp_extensions/libtorch_agn_2_14_extension/csrc/mps/mps_set_arg_bytes_test.cpp`, which wraps every one of these calls. `cpp_wrapper_mps.py` was the only `cpp_wrapper` backend that did not reference the macro at all.

Also move the shader-library handle into its `unique_ptr` immediately after a successful create call. With the new error check, ownership must be established before the subsequent kernel-function lookup to avoid leaking the library on failure.

Update the `codegen_additional_funcs` docstring to match the generated code.

## Test plan

```bash
python test/inductor/test_mps_basic.py MPSBasicTestsAOTI.test_shader_compile_error_raises
python test/inductor/test_mps_basic.py MPSBasicTestsAOTI.test_kernel_function_lookup_error_raises
python test/inductor/test_mps_basic.py
python test/inductor/test_aot_inductor.py AOTInductorTestABICompatibleMps
spin quicklint
```

### Regression coverage

One test per shim call. Each forces a real failure and asserts the diagnostic
reaches Python; each segfaults without the fix.

- `test_shader_compile_error_raises` injects invalid MSL, so
  `aoti_torch_mps_create_shader_library` fails.
- `test_kernel_function_lookup_error_raises` renames the kernel symbol, so the
  library compiles but `aoti_torch_mps_get_kernel_function` cannot find
  `generated_kernel`. This covers the second call, which the first test never
  reaches because it fails earlier.

Before this change both segfault with no output:

```bash
$ python test/inductor/test_mps_basic.py MPSBasicTestsAOTI.test_shader_compile_error_raises; echo "exit=$?"
exit=139
$ python test/inductor/test_mps_basic.py MPSBasicTestsAOTI.test_kernel_function_lookup_error_raises; echo "exit=$?"
exit=139
```

After:

```bash
$ python test/inductor/test_mps_basic.py MPSBasicTestsAOTI.test_shader_compile_error_raises MPSBasicTestsAOTI.test_kernel_function_lookup_error_raises
..
----------------------------------------------------------------------
Ran 2 tests in 4.642s

OK
```

### Results

| Test | Result |
|---|---|
| `test/inductor/test_mps_basic.py` | 63 run, 63 passed |
| `test/inductor/test_aot_inductor.py` (MPS class) | 386 run: 196 passed, 157 skipped, 33 expected failures |
| `spin quicklint` | Passed |

## Environment

- macOS 15.5, arm64
- Apple M4 Pro
- `2.15.0a0+gitf62c80c`, built from source
- No CUDA available; non-MPS `test_aot_inductor.py` device classes were not run locally

Note: pytorch#195587 currently prevents editable builds from resolving shader `#include`s because `torch/include` is absent. Full-suite results above used a local `torch/include/c10 -> ../../c10` symlink. Without that workaround, Metal shader compilation fails due to the unrelated issue; this PR now reports the shim error rather than segfaulting. The two regression tests pass with or without the workaround.

## BC-breaking?

No. Successful generated code is unchanged; only error handling on failure paths changes.

## Follow-ups

Out of scope for this PR:

- Add return-code checks for other emitted `aoti_torch_mps_*` calls.
- Fix JIT `cpp_wrapper` on MPS, which currently fails on main with `undeclared identifier 'get_mps_lib_0_handle'`.

***

The implementation was prepared with an AI assistant (Claude) and reviewed and validated by the author. The reproduction, negative control, and suite runs were executed locally on the environment above.

Co-Authored-By: Claude Opus 5 <[email protected]>
@saud5150
saud5150 force-pushed the fix-mps-aoti-shader-error-code-check branch from 74e3541 to 51ddba5 Compare September 13, 2026 07:32
@saud5150
saud5150 marked this pull request as ready for review September 16, 2026 14:41
@malfet

malfet commented Sep 18, 2026

Copy link
Copy Markdown
Collaborator

@pytorchbot merge -f "Lint + MPS are green, though test location is wrong"

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MPS][AOTI] Shader compile failures segfault instead of raising: generated code ignores aoti_torch_mps_create_shader_library's return code

4 participants