Ship the merged CPU kernels as their own library - #21524
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/21524
Note: Links to docs will display an error until the docs builds have been completed. ❌ 2 New Failures, 466 PendingAs of commit de364f8 with merge base d632341 ( NEW FAILURES - The following jobs have failed:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
There was a problem hiding this comment.
Pull request overview
This PR makes the merged CPU operator kernels available as a standalone shared library in the Python wheel so that (1) the pybindings extension no longer carries its own full copy of the kernels, and (2) multiple components in the same process can share a single kernel registration site instead of registering duplicates.
Changes:
- Adds an opt-in
SHAREDmode togen_operators_lib()and uses it to buildoptimized_native_cpu_ops_libas a versioned shared library whenEXECUTORCH_BUILD_SHAREDis enabled. - Ships the new CPU-kernels shared library in the wheel and forces the pybindings module to retain it on the link line (so its static initializers run reliably).
- Extends the wheel smoke test to assert that exactly one shipped shared object defines a representative CPU kernel symbol.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tools/cmake/Codegen.cmake | Adds SHARED option to gen_operators_lib() and sets SONAME/RPATH properties for shared builds. |
| configurations/CMakeLists.txt | Builds optimized_native_cpu_ops_lib as SHARED when EXECUTORCH_BUILD_SHARED is enabled. |
| CMakeLists.txt | Ensures the pybindings module retains the kernels shared library so it isn’t dropped by the linker. |
| setup.py | Installs the new kernels shared library into executorch/lib/ inside the wheel. |
| .ci/scripts/wheel/test_cpp_sdk.py | Adds a smoke test asserting only one shipped library defines a representative CPU kernel symbol. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The CPU operator kernels are compiled into whichever component links them, so
the Python bindings extension carries its own full copy. That makes the
extension large, and it means a C++ application cannot get the same operators
without building them from source. It also means two components that both want
CPU operators end up with two independent registrations of the same operator
set in one process.
Build the merged CPU kernels as a shared library and ship it in the wheel, so
the operators live in one place and both the Python bindings and a C++
application can link the same copy.
The library type is opt-in rather than a global change:
gen_operators_libhasmany callers that want an ordinary static library, and only the one shipped in
the wheel needs to be shared. Callers that do not pass
SHAREDare unaffected,and the whole change is gated on the existing
EXECUTORCH_BUILD_SHAREDoption,so iOS, Android, and embedded builds keep linking static libraries exactly as
before.
Test plan:
The wheel smoke test now asserts that exactly one shipped library defines the
CPU kernels, alongside the existing backend-registry and thread-pool
assertions, so a change that reintroduces a second copy fails in CI rather than
aborting at startup with a duplicate operator registration. The symbol used by
the assertion was confirmed to exist in the shipped libraries first, so the
check cannot pass by matching nothing.
Built the wheel from a clean checkout and verified against a fresh virtual
environment with a normal dependency-resolving install:
runtime and the thread pool.
nm -DCacross every shipped shared object shows exactly one definition of arepresentative CPU operator, in the new library rather than in the bindings
extension.
library loaded.
import executorch, the registered backend list, and.pteexecutionthrough the Python bindings are unchanged, with outputs matching eager
PyTorch.
EXECUTORCH_BUILD_SHAREDoff, the kernels remain a static library andno new shared object is produced, so every build that does not opt in is
unaffected.