Skip to content

Commit d0bf832

Browse files
authored
Change training pybind extension_module linkage to static (#9809)
Summary: Fixes #9576. Use `extension_module_static` in building `_training_lib`. Test Plan: Rely on unit test, also did a manual install in editable mode: ```bash ./install_executorch.sh --pybind training -e python -c "from executorch.extension.training.pybindings._training_lib import get_sgd_optimizer" ``` Reviewers: Subscribers: Tasks: Tags:
1 parent 19950b3 commit d0bf832

File tree

4 files changed

+34
-31
lines changed

4 files changed

+34
-31
lines changed

CMakeLists.txt

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -764,10 +764,6 @@ if(EXECUTORCH_BUILD_EXTENSION_MODULE)
764764
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/module)
765765
endif()
766766

767-
if(EXECUTORCH_BUILD_EXTENSION_TRAINING)
768-
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/training)
769-
endif()
770-
771767
if(EXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL)
772768
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/runner_util)
773769
endif()
@@ -872,34 +868,13 @@ if(EXECUTORCH_BUILD_PYBIND)
872868

873869
if(EXECUTORCH_BUILD_EXTENSION_TRAINING)
874870

875-
set(_pybind_training_dep_libs
876-
${TORCH_PYTHON_LIBRARY}
877-
etdump
878-
executorch
879-
util
880-
torch
881-
extension_training
882-
)
883-
884-
if(EXECUTORCH_BUILD_XNNPACK)
885-
# need to explicitly specify XNNPACK and microkernels-prod
886-
# here otherwise uses XNNPACK and microkernel-prod symbols from libtorch_cpu
887-
list(APPEND _pybind_training_dep_libs xnnpack_backend XNNPACK microkernels-prod)
888-
endif()
889-
890-
# pybind training
891-
pybind11_add_module(_training_lib SHARED extension/training/pybindings/_training_lib.cpp)
892-
893-
target_include_directories(_training_lib PRIVATE ${TORCH_INCLUDE_DIRS})
894-
target_compile_options(_training_lib PUBLIC ${_pybind_compile_options})
895-
target_link_libraries(_training_lib PRIVATE ${_pybind_training_dep_libs})
896-
897-
install(TARGETS _training_lib
898-
LIBRARY DESTINATION executorch/extension/training/pybindings
899-
)
900871
endif()
901872
endif()
902873

874+
if(EXECUTORCH_BUILD_EXTENSION_TRAINING)
875+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/training)
876+
endif()
877+
903878
if(EXECUTORCH_BUILD_KERNELS_CUSTOM)
904879
# TODO: move all custom kernels to ${CMAKE_CURRENT_SOURCE_DIR}/kernels/custom
905880
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/llm/custom_ops)

extension/training/CMakeLists.txt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ target_include_directories(
2626
target_include_directories(extension_training PUBLIC ${EXECUTORCH_ROOT}/..)
2727
target_compile_options(extension_training PUBLIC ${_common_compile_options})
2828
target_link_libraries(extension_training executorch_core
29-
extension_data_loader extension_module extension_tensor extension_flat_tensor)
29+
extension_data_loader extension_module_static extension_tensor extension_flat_tensor)
3030

3131

3232
list(TRANSFORM _train_xor__srcs PREPEND "${EXECUTORCH_ROOT}/")
@@ -40,6 +40,33 @@ train_xor gflags executorch_core portable_ops_lib extension_tensor
4040
)
4141
target_compile_options(train_xor PUBLIC ${_common_compile_options})
4242

43+
# Pybind library.
44+
set(_pybind_training_dep_libs
45+
${TORCH_PYTHON_LIBRARY}
46+
etdump
47+
executorch
48+
util
49+
torch
50+
extension_training
51+
)
52+
53+
if(EXECUTORCH_BUILD_XNNPACK)
54+
# need to explicitly specify XNNPACK and microkernels-prod
55+
# here otherwise uses XNNPACK and microkernel-prod symbols from libtorch_cpu
56+
list(APPEND _pybind_training_dep_libs xnnpack_backend XNNPACK microkernels-prod)
57+
endif()
58+
59+
# pybind training
60+
pybind11_add_module(_training_lib SHARED ${CMAKE_CURRENT_SOURCE_DIR}/pybindings/_training_lib.cpp)
61+
62+
target_include_directories(_training_lib PRIVATE ${TORCH_INCLUDE_DIRS})
63+
target_compile_options(_training_lib PUBLIC -Wno-deprecated-declarations -fPIC -frtti -fexceptions)
64+
target_link_libraries(_training_lib PRIVATE ${_pybind_training_dep_libs})
65+
66+
install(TARGETS _training_lib
67+
LIBRARY DESTINATION executorch/extension/training/pybindings
68+
)
69+
4370
# Install libraries
4471
install(
4572
TARGETS extension_training

pytest.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ addopts =
5151
extension/llm/modules/test
5252
extension/llm/export
5353
extension/pybindings/test
54+
extension/training/pybindings/test
5455
# Runtime
5556
runtime
5657
# test TODO: fix these tests

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ def get_ext_modules() -> List[Extension]:
869869
ext_modules.append(
870870
# Install the prebuilt pybindings extension wrapper for training
871871
BuiltExtension(
872-
"_training_lib.*",
872+
"extension/training/_training_lib.*",
873873
"executorch.extension.training.pybindings._training_lib",
874874
)
875875
)

0 commit comments

Comments
 (0)