Skip to content

Commit f32194f

Browse files
Gasoonjiafacebook-github-bot
authored andcommitted
Extend PyBundledModule with extension.BundledModule (pytorch#12839)
Summary: Pull Request resolved: pytorch#12839 Reviewed By: lucylq Differential Revision: D78938344
1 parent 5b743f5 commit f32194f

File tree

8 files changed

+179
-129
lines changed

8 files changed

+179
-129
lines changed

CMakeLists.txt

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,12 @@ if(MAX_KERNEL_NUM)
423423
)
424424
endif()
425425

426+
# Build devtools first if needed - some backends depend on protobuf from
427+
# devtools
428+
if(EXECUTORCH_BUILD_DEVTOOLS)
429+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/devtools)
430+
endif()
431+
426432
if(EXECUTORCH_BUILD_PYBIND AND APPLE)
427433
# shared version
428434
add_library(executorch_core_shared SHARED ${_executorch_core__srcs})
@@ -588,10 +594,6 @@ if(EXECUTORCH_BUILD_CORTEX_M)
588594
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/cortex_m)
589595
endif()
590596

591-
if(EXECUTORCH_BUILD_DEVTOOLS)
592-
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/devtools)
593-
endif()
594-
595597
if(EXECUTORCH_BUILD_EXTENSION_APPLE)
596598
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/apple)
597599
list(APPEND _executorch_extensions apple_extension)
@@ -756,6 +758,30 @@ if(EXECUTORCH_BUILD_PYBIND)
756758
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/devtools)
757759
endif()
758760

761+
# Create bundled_module target only for pybindings when bundled_program exists
762+
# This target has hard dependencies on devtools generated headers
763+
if(TARGET bundled_program)
764+
add_library(
765+
bundled_module STATIC
766+
${CMAKE_CURRENT_SOURCE_DIR}/extension/module/bundled_module.cpp
767+
)
768+
769+
# Ensure bundled_module waits for bundled_program's generated headers
770+
add_dependencies(bundled_module bundled_program)
771+
772+
target_link_libraries(bundled_module PRIVATE extension_data_loader)
773+
target_link_libraries(
774+
bundled_module PUBLIC extension_module_static bundled_program
775+
)
776+
777+
target_include_directories(
778+
bundled_module PUBLIC ${_common_include_directories}
779+
)
780+
target_compile_options(
781+
bundled_module PUBLIC -Wno-deprecated-declarations -fPIC
782+
)
783+
endif()
784+
759785
# find pytorch lib, to allow pybind to take at::Tensor as input/output
760786
find_package_torch()
761787
find_library(
@@ -773,6 +799,16 @@ if(EXECUTORCH_BUILD_PYBIND)
773799
torch
774800
)
775801

802+
if(EXECUTORCH_BUILD_EXTENSION_MODULE)
803+
# Always use static linking for pybindings to avoid runtime symbol
804+
# resolution issues
805+
list(APPEND _dep_libs extension_module_static)
806+
# Add bundled_module if available
807+
if(TARGET bundled_module)
808+
list(APPEND _dep_libs bundled_module)
809+
endif()
810+
endif()
811+
776812
if(EXECUTORCH_BUILD_TESTS)
777813
list(APPEND _dep_libs test_backend_compiler_lib)
778814
endif()
@@ -830,8 +866,18 @@ if(EXECUTORCH_BUILD_PYBIND)
830866
target_compile_definitions(
831867
portable_lib PUBLIC EXECUTORCH_PYTHON_MODULE_NAME=_portable_lib
832868
)
869+
# For portable_lib, do not define USE_ATEN_LIB (portable mode) This ensures it
870+
# uses the correct bundled_module namespace
833871
target_include_directories(portable_lib PRIVATE ${TORCH_INCLUDE_DIRS})
834872
target_compile_options(portable_lib PUBLIC ${_pybind_compile_options})
873+
874+
# Ensure bundled_module is linked to portable_lib since it uses bundled_module
875+
# functionality when USE_ATEN_LIB is not defined (which is the case for
876+
# portable_lib)
877+
if(TARGET bundled_module)
878+
list(APPEND _dep_libs bundled_module)
879+
endif()
880+
835881
target_link_libraries(portable_lib PRIVATE ${_dep_libs})
836882

837883
install(

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/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)