Skip to content

Commit 9c857da

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

File tree

10 files changed

+196
-138
lines changed

10 files changed

+196
-138
lines changed

CMakeLists.txt

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,12 @@ install(FILES tools/cmake/executorch-config.cmake
561561
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ExecuTorch
562562
)
563563

564+
# Build devtools first if needed - some backends depend on protobuf from
565+
# devtools
566+
if(EXECUTORCH_BUILD_DEVTOOLS)
567+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/devtools)
568+
endif()
569+
564570
if(EXECUTORCH_BUILD_ARM_BAREMETAL)
565571
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/arm)
566572
list(APPEND _executorch_backends executorch_delegate_ethos_u)
@@ -609,10 +615,6 @@ if(EXECUTORCH_BUILD_CORTEX_M)
609615
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/cortex_m)
610616
endif()
611617

612-
if(EXECUTORCH_BUILD_DEVTOOLS)
613-
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/devtools)
614-
endif()
615-
616618
if(EXECUTORCH_BUILD_EXTENSION_APPLE)
617619
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/apple)
618620
list(APPEND _executorch_extensions apple_extension)
@@ -718,6 +720,30 @@ if(EXECUTORCH_BUILD_PYBIND)
718720
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/devtools)
719721
endif()
720722

723+
# Create bundled_module target only for pybindings when bundled_program exists
724+
# This target has hard dependencies on devtools generated headers
725+
if(TARGET bundled_program)
726+
add_library(
727+
bundled_module STATIC
728+
${CMAKE_CURRENT_SOURCE_DIR}/extension/module/bundled_module.cpp
729+
)
730+
731+
# Ensure bundled_module waits for bundled_program's generated headers
732+
add_dependencies(bundled_module bundled_program)
733+
734+
target_link_libraries(bundled_module PRIVATE extension_data_loader)
735+
target_link_libraries(
736+
bundled_module PUBLIC extension_module_static bundled_program
737+
)
738+
739+
target_include_directories(
740+
bundled_module PUBLIC ${_common_include_directories}
741+
)
742+
target_compile_options(
743+
bundled_module PUBLIC -Wno-deprecated-declarations -fPIC
744+
)
745+
endif()
746+
721747
# find pytorch lib, to allow pybind to take at::Tensor as input/output
722748
find_package_torch()
723749
find_library(
@@ -735,6 +761,16 @@ if(EXECUTORCH_BUILD_PYBIND)
735761
torch
736762
)
737763

764+
if(EXECUTORCH_BUILD_EXTENSION_MODULE)
765+
# Always use static linking for pybindings to avoid runtime symbol
766+
# resolution issues
767+
list(APPEND _dep_libs extension_module_static)
768+
# Add bundled_module if available
769+
if(TARGET bundled_module)
770+
list(APPEND _dep_libs bundled_module)
771+
endif()
772+
endif()
773+
738774
if(EXECUTORCH_BUILD_TESTS)
739775
list(APPEND _dep_libs test_backend_compiler_lib)
740776
endif()

backends/apple/coreml/CMakeLists.txt

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,19 +205,34 @@ if(APPLE)
205205
)
206206

207207
if(EXECUTORCH_BUILD_DEVTOOLS)
208-
target_sources(coremldelegate PRIVATE ${SDK_SOURCES} ${PROTOBUF_SOURCES})
208+
# Always add SDK sources
209+
target_sources(coremldelegate PRIVATE ${SDK_SOURCES})
209210
target_include_directories(
210211
coremldelegate
211212
PRIVATE
212213
${CMAKE_CURRENT_SOURCE_DIR}/runtime/sdk
213-
${CMAKE_CURRENT_SOURCE_DIR}/third-party/coremltools/deps/protobuf/src
214-
)
215-
add_subdirectory(
216-
${CMAKE_CURRENT_SOURCE_DIR}/third-party/coremltools/deps/protobuf/cmake
217214
)
218215

219-
executorch_target_link_options_shared_lib(libprotobuf-lite)
220-
target_link_libraries(coremldelegate PRIVATE libprotobuf-lite)
216+
# Check if CoreML's protobuf sources exist, otherwise skip protobuf features
217+
set(COREML_PROTOBUF_CMAKE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/third-party/coremltools/deps/protobuf/cmake")
218+
if(EXISTS "${COREML_PROTOBUF_CMAKE_DIR}")
219+
message(STATUS "CoreML: Using vendored protobuf from coremltools")
220+
target_include_directories(
221+
coremldelegate
222+
PRIVATE
223+
${CMAKE_CURRENT_SOURCE_DIR}/third-party/coremltools/deps/protobuf/src
224+
)
225+
add_subdirectory("${COREML_PROTOBUF_CMAKE_DIR}")
226+
executorch_target_link_options_shared_lib(libprotobuf-lite)
227+
target_link_libraries(coremldelegate PRIVATE libprotobuf-lite)
228+
229+
# Only add protobuf sources after protobuf has been configured
230+
target_sources(coremldelegate PRIVATE ${PROTOBUF_SOURCES})
231+
set(COREML_HAS_PROTOBUF TRUE)
232+
else()
233+
message(WARNING "CoreML: Protobuf sources not found at ${COREML_PROTOBUF_CMAKE_DIR}. Some CoreML debugging features will be disabled.")
234+
set(COREML_HAS_PROTOBUF FALSE)
235+
endif()
221236
endif()
222237

223238
target_link_libraries(
@@ -243,7 +258,10 @@ if(APPLE)
243258
if(EXECUTORCH_BUILD_DEVTOOLS)
244259
target_compile_options(executorch_core PUBLIC -DET_EVENT_TRACER_ENABLED)
245260
target_compile_options(coremldelegate PRIVATE "-frtti")
246-
target_compile_options(libprotobuf-lite PRIVATE "-frtti")
261+
# Only set compile options on protobuf if it was successfully configured
262+
if(COREML_HAS_PROTOBUF AND TARGET libprotobuf-lite)
263+
target_compile_options(libprotobuf-lite PRIVATE "-frtti")
264+
endif()
247265
else()
248266
target_compile_options(coremldelegate PRIVATE "-fno-rtti")
249267
endif()

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)