Skip to content

Commit d53eff4

Browse files
Change training pybind extension_module linkage to static (#9961)
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: Co-authored-by: Mengwei Liu <[email protected]>
1 parent 16209b6 commit d53eff4

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
@@ -758,10 +758,6 @@ if(EXECUTORCH_BUILD_EXTENSION_MODULE)
758758
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/module)
759759
endif()
760760

761-
if(EXECUTORCH_BUILD_EXTENSION_TRAINING)
762-
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/training)
763-
endif()
764-
765761
if(EXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL)
766762
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/runner_util)
767763
endif()
@@ -862,34 +858,13 @@ if(EXECUTORCH_BUILD_PYBIND)
862858

863859
if(EXECUTORCH_BUILD_EXTENSION_TRAINING)
864860

865-
set(_pybind_training_dep_libs
866-
${TORCH_PYTHON_LIBRARY}
867-
etdump
868-
executorch
869-
util
870-
torch
871-
extension_training
872-
)
873-
874-
if(EXECUTORCH_BUILD_XNNPACK)
875-
# need to explicitly specify XNNPACK and microkernels-prod
876-
# here otherwise uses XNNPACK and microkernel-prod symbols from libtorch_cpu
877-
list(APPEND _pybind_training_dep_libs xnnpack_backend XNNPACK microkernels-prod)
878-
endif()
879-
880-
# pybind training
881-
pybind11_add_module(_training_lib SHARED extension/training/pybindings/_training_lib.cpp)
882-
883-
target_include_directories(_training_lib PRIVATE ${TORCH_INCLUDE_DIRS})
884-
target_compile_options(_training_lib PUBLIC ${_pybind_compile_options})
885-
target_link_libraries(_training_lib PRIVATE ${_pybind_training_dep_libs})
886-
887-
install(TARGETS _training_lib
888-
LIBRARY DESTINATION executorch/extension/training/pybindings
889-
)
890861
endif()
891862
endif()
892863

864+
if(EXECUTORCH_BUILD_EXTENSION_TRAINING)
865+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/training)
866+
endif()
867+
893868
if(EXECUTORCH_BUILD_KERNELS_CUSTOM)
894869
# TODO: move all custom kernels to ${CMAKE_CURRENT_SOURCE_DIR}/kernels/custom
895870
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
@@ -861,7 +861,7 @@ def get_ext_modules() -> List[Extension]:
861861
ext_modules.append(
862862
# Install the prebuilt pybindings extension wrapper for training
863863
BuiltExtension(
864-
"_training_lib.*",
864+
"extension/training/_training_lib.*",
865865
"executorch.extension.training.pybindings._training_lib",
866866
)
867867
)

0 commit comments

Comments
 (0)