Skip to content

Commit dd7d9a6

Browse files
Gasoonjiafacebook-github-bot
authored andcommitted
Extend PyBundledModule with extension.BundledModule (#12839)
Summary: Pull Request resolved: #12839 Differential Revision: D78938344
1 parent ec35f56 commit dd7d9a6

File tree

10 files changed

+154
-128
lines changed

10 files changed

+154
-128
lines changed

CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,11 @@ if(EXECUTORCH_BUILD_PYBIND)
731731
torch
732732
)
733733

734+
if(EXECUTORCH_BUILD_EXTENSION_MODULE)
735+
# Always use static linking for pybindings to avoid runtime symbol resolution issues
736+
list(APPEND _dep_libs extension_module_static)
737+
endif()
738+
734739
if(EXECUTORCH_BUILD_TESTS)
735740
list(APPEND _dep_libs test_backend_compiler_lib)
736741
endif()
@@ -788,6 +793,17 @@ if(EXECUTORCH_BUILD_PYBIND)
788793
target_compile_options(portable_lib PUBLIC ${_pybind_compile_options})
789794
target_link_libraries(portable_lib PRIVATE ${_dep_libs})
790795

796+
# Set RPATH for portable_lib to find libextension_module.so
797+
if(APPLE)
798+
set_target_properties(portable_lib PROPERTIES
799+
BUILD_RPATH "@loader_path"
800+
INSTALL_RPATH "@loader_path")
801+
else()
802+
set_target_properties(portable_lib PROPERTIES
803+
BUILD_RPATH "$ORIGIN"
804+
INSTALL_RPATH "$ORIGIN")
805+
endif()
806+
791807
install(
792808
TARGETS portable_lib
793809
EXPORT ExecuTorchTargets

devtools/bundled_program/test/test_end2end.py

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,7 @@
55
# LICENSE file in the root directory of this source tree.
66

77
# flake8: noqa: F401
8-
import functools
9-
import inspect
10-
import os
11-
import random
128
import unittest
13-
from typing import Callable, Dict, Optional, Tuple, Type
14-
15-
import executorch.exir as exir
16-
17-
import executorch.exir.control_flow as control_flow
18-
19-
# @manual=//executorch/extension/pytree:pybindings
20-
import executorch.extension.pytree as pytree
21-
22-
import torch
239

2410
from executorch.devtools.bundled_program.core import BundledProgram
2511
from executorch.devtools.bundled_program.serialize import (
@@ -35,8 +21,6 @@
3521
try:
3622
from executorch.extension.pybindings.portable_lib import (
3723
_load_bundled_program_from_buffer,
38-
_load_for_executorch_from_buffer,
39-
_load_for_executorch_from_bundled_program,
4024
)
4125

4226
kernel_mode = "lean"
@@ -47,8 +31,6 @@
4731
try:
4832
from executorch.extension.pybindings.aten_lib import ( # @manual=//executorch/extension/pybindings:aten_lib
4933
_load_bundled_program_from_buffer,
50-
_load_for_executorch_from_buffer,
51-
_load_for_executorch_from_bundled_program,
5234
)
5335

5436
assert kernel_mode is None
@@ -75,19 +57,8 @@ def test_sample_model_e2e(self):
7557
bundled_program_buffer
7658
)
7759

78-
executorch_module = _load_for_executorch_from_bundled_program(
79-
executorch_bundled_program
80-
)
81-
8260
for method_name in eager_model.method_names:
83-
executorch_module.load_bundled_input(
84-
executorch_bundled_program,
85-
method_name,
86-
0,
87-
)
88-
executorch_module.plan_execute(method_name)
89-
executorch_module.verify_result_with_bundled_expected_output(
90-
executorch_bundled_program,
61+
executorch_bundled_program.verify_result_with_bundled_expected_output(
9162
method_name,
9263
0,
9364
)

extension/module/CMakeLists.txt

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ if(NOT EXECUTORCH_ROOT)
1717
endif()
1818

1919
list(TRANSFORM _extension_module__srcs PREPEND "${EXECUTORCH_ROOT}/")
20+
21+
# Always add bundled_module.cpp to ensure it's included
22+
list(APPEND _extension_module__srcs "${EXECUTORCH_ROOT}/extension/module/bundled_module.cpp")
23+
24+
# Also add bundled_module sources if they exist (in case there are additional files)
25+
if(DEFINED _extension_bundled_module__srcs)
26+
list(TRANSFORM _extension_bundled_module__srcs PREPEND "${EXECUTORCH_ROOT}/")
27+
list(APPEND _extension_module__srcs ${_extension_bundled_module__srcs})
28+
endif()
29+
2030
if(CMAKE_TOOLCHAIN_IOS
2131
OR CMAKE_TOOLCHAIN_ANDROID
2232
OR APPLE
@@ -29,7 +39,7 @@ else()
2939
endif()
3040
target_link_libraries(
3141
extension_module PRIVATE executorch_core extension_data_loader
32-
extension_flat_tensor
42+
extension_flat_tensor bundled_program
3343
)
3444
target_include_directories(
3545
extension_module PUBLIC ${_common_include_directories}
@@ -38,12 +48,25 @@ target_compile_options(
3848
extension_module PUBLIC -Wno-deprecated-declarations -fPIC
3949
)
4050

51+
# Only set USE_ATEN_LIB if PyTorch is actually available
52+
# This prevents build failures in OSS CMake CI where ATen headers are not available
53+
if(EXECUTORCH_BUILD_KERNELS_OPTIMIZED)
54+
# Try to find PyTorch headers to check if ATen is available
55+
find_package(Torch CONFIG QUIET)
56+
if(TARGET torch)
57+
target_compile_definitions(extension_module PRIVATE USE_ATEN_LIB)
58+
message(STATUS "extension_module: Using ATen library (USE_ATEN_LIB enabled)")
59+
else()
60+
message(STATUS "extension_module: PyTorch not found, building without ATen support")
61+
endif()
62+
endif()
63+
4164
# Module extension built as a static library. TODO(gjcomer) Remove this target
4265
# after cleaning up CMake targets.
4366
add_library(extension_module_static STATIC ${_extension_module__srcs})
4467
target_link_libraries(
4568
extension_module_static PRIVATE executorch_core extension_data_loader
46-
extension_flat_tensor
69+
extension_flat_tensor bundled_program
4770
)
4871
target_include_directories(
4972
extension_module_static PUBLIC ${_common_include_directories}
@@ -52,6 +75,19 @@ target_compile_options(
5275
extension_module_static PUBLIC -Wno-deprecated-declarations -fPIC
5376
)
5477

78+
# Only set USE_ATEN_LIB if PyTorch is actually available
79+
# This prevents build failures in OSS CMake CI where ATen headers are not available
80+
if(EXECUTORCH_BUILD_KERNELS_OPTIMIZED)
81+
# Try to find PyTorch headers to check if ATen is available
82+
find_package(Torch CONFIG QUIET)
83+
if(TARGET torch)
84+
target_compile_definitions(extension_module_static PRIVATE USE_ATEN_LIB)
85+
message(STATUS "extension_module_static: Using ATen library (USE_ATEN_LIB enabled)")
86+
else()
87+
message(STATUS "extension_module_static: PyTorch not found, building without ATen support")
88+
endif()
89+
endif()
90+
5591
# Install libraries
5692
install(
5793
TARGETS extension_module extension_module_static

extension/pybindings/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ CMAKE_ARGS="-DEXECUTORCH_BUILD_MPS=ON" ./install_executorch.sh
2727
- `_reset_profile_results()`: Reset profile results.
2828
## Classes
2929
### ExecuTorchModule
30-
- `load_bundled_input()`: Load bundled input.
31-
- `verify_result_with_bundled_expected_output(bundle: str, method_name: str, testset_idx: int, rtol: float = 1e-5, atol: float = 1e-8)`: Verify result with bundled expected output.
3230
- `plan_execute()`: Plan and execute.
3331
- `run_method()`: Run method.
3432
- `forward()`: Forward. This takes a pytree-flattend PyTorch-tensor-based input.
@@ -37,5 +35,6 @@ CMAKE_ARGS="-DEXECUTORCH_BUILD_MPS=ON" ./install_executorch.sh
3735
- `__call__()`: Call method.
3836
### BundledModule
3937
This class is currently empty and serves as a placeholder for future methods and attributes.
38+
- `verify_result_with_bundled_expected_output(method_name: str, testset_idx: int, rtol: float = 1e-5, atol: float = 1e-8)`: Verify result with bundled expected output.
4039
## Note
4140
All functions and methods are guarded by a call guard that redirects `cout` and `cerr` to the Python environment.

0 commit comments

Comments
 (0)