Metal backend: exported `.pte` does not load outside a process that already has PyTorch's libomp (embedded AOTI object links `/opt/llvm-openmp/lib/libomp.dylib`)
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 48/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Domain
- build-system, embedded-iot, machine-learning, mobile-dev
Research direction
Start with metal_backend.cpp around line 352 and the MetalBackend.codesign_so path, then inspect run_metal_test.sh's existing library-reference rewrite. Reproduce export and loading outside executor_runner, and determine whether the accepted fix should remove unused dependencies or reject unsupported ones at export time. Done means the chosen behavior is verified for an app without PyTorch's libomp.
Written by the indexing model from the issue text.
Description
Summary
A Metal .pte loads under executor_runner built by run_metal_test.sh, but fails in an application that links the ExecuTorch runtime on its own:
E executorch:metal_backend.cpp:352] Failed to load shared library: dlopen(.../<hash>_so_blob<pid>.so, 0x0005):
Library not loaded: /opt/llvm-openmp/lib/libomp.dylib
E executorch:method.cpp:132] Init failed for backend MetalBackend: 0x22
Cause
On macOS inductor's _get_openmp_args always adds -lomp. The libomp it resolves is the one bundled in the PyTorch wheel (torch/lib/libomp.dylib), whose LC_ID_DYLIB is the absolute path /opt/llvm-openmp/lib/libomp.dylib, and that path is recorded as a dependency of the AOTI object embedded in the .pte. The path exists on no end-user machine.
It goes unnoticed in-tree because run_metal_test.sh already rewrites the same reference in executor_runner to @rpath/libomp.dylib with an rpath into torch/lib. The runner therefore loads PyTorch's libomp first, and dyld then satisfies the model's dependency by install name. An app that ships its own libomp (or none) has no image with that install name loaded, so dlopen fails.
The dependency is not used: nm -u on the embedded object shows no omp / kmp symbol for a Metal-delegated MobileNet, as expected for a graph that runs on the GPU.
Possible directions
- Link the AOTI object with
-Wl,-dead_strip_dylibsfor the Metal backend, so dylibs the object does not reference are dropped and libomp stays only if a model really needs it. Inductor has no option for extra link flags today; we do it from outside by pointingtorch._inductor.config.cpp.cxxat a wrapper compiler that appends the flag on link steps. The resulting object links onlylibc++andlibSystemand loads in a process with no libomp at all. - Independently, a check next to
MetalBackend.codesign_sothat the compiled object has no dependency outside/usr/liband/Systemwould turn this from a load-time surprise on someone else's machine into an export-time error.
Happy to send a PR for the check if that direction sounds right; the link-flag part probably belongs in inductor.
- Dominant language
- Python
- Stars
- 5k
- Forks
- 1.2k
- Avg merge
- 2d 11h
- Merged PRs (30d)
- 559
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from pytorch/executorch
-
enhancement triaged
Difficulty 2/5 Half a day Newbie friendliness 68/100
pytorch/executorch#21640 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
pytorch/executorch#22956 · 2 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
pytorch/executorch#22953 ·
-
pytorch/executorch#22845 · 1 assignee ·
-
bug module: vulkan triaged
pytorch/executorch#22775 · 3 comments · 1 assignee ·
All issues in pytorch/executorch
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
zostera/django-bootstrap4#894 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
use-agent-os/agent-os#3276 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
NousResearch/hermes-agent#117848 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
zilliztech/memsearch#759 ·