Skip to content

Commit 9e2cf2f

Browse files
committed
[build] Add cpp_extension.py and publicly link torch.so
Summary: Test Plan: Reviewers: Subscribers: Tasks: Tags: ghstack-source-id: f246b0a Pull Request resolved: #2183
1 parent ce56f85 commit 9e2cf2f

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ if(EXECUTORCH_BUILD_PYBIND)
416416
target_include_directories(util PUBLIC ${_common_include_directories}
417417
${TORCH_INCLUDE_DIRS})
418418
target_compile_options(util PUBLIC ${_pybind_compile_options})
419-
target_link_libraries(util PRIVATE torch c10 executorch)
419+
target_link_libraries(util PUBLIC torch c10 executorch)
420420

421421
# pybind portable_lib
422422
pybind11_add_module(portable_lib extension/pybindings/pybindings.cpp)

extension/pybindings/cpp_extension.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from torch.utils import cpp_extension
2+
import os
3+
4+
_HERE = os.path.abspath(__file__)
5+
_EXECUTORCH_PATH = os.path.dirname(os.path.dirname(_HERE))
6+
7+
def load_inline(name,
8+
cpp_sources,
9+
functions=None,
10+
extra_cflags=None,
11+
extra_ldflags=None,
12+
extra_include_paths=None,
13+
build_directory=None,
14+
verbose=False,
15+
is_python_module=True,
16+
with_pytorch_error_handling=True,
17+
keep_intermediates=True,
18+
use_pch=False):
19+
# Register the code into PyTorch
20+
aten_extra_cflags = ["-DUSE_ATEN_LIB"] + (extra_cflags if extra_cflags else [])
21+
extra_ldflags = [f"-L{_EXECUTORCH_PATH}", f"-Wl,-rpath,{_EXECUTORCH_PATH}", "-lexecutorch"] + (extra_ldflags if extra_ldflags else [])
22+
module = cpp_extension.load_inline(
23+
name,
24+
cpp_sources,
25+
functions=functions,
26+
extra_cflags=aten_extra_cflags,
27+
extra_ldflags=extra_ldflags,
28+
extra_include_paths=extra_include_paths,
29+
build_directory=build_directory,
30+
verbose=verbose,
31+
is_python_module=is_python_module,
32+
with_pytorch_error_handling=with_pytorch_error_handling,
33+
keep_intermediates=keep_intermediates,
34+
use_pch=use_pch,
35+
)
36+
# Now register the code into ExecuTorch
37+
cpp_extension.load_inline(
38+
name,
39+
cpp_sources,
40+
functions=None, # leave this out since we are not passing out any python module
41+
extra_cflags=extra_cflags,
42+
extra_ldflags=extra_ldflags,
43+
extra_include_paths=extra_include_paths,
44+
build_directory=build_directory,
45+
verbose=verbose,
46+
is_python_module=False, # don't register as a python module. Load shared library as a side effect.
47+
with_pytorch_error_handling=with_pytorch_error_handling,
48+
keep_intermediates=keep_intermediates,
49+
use_pch=use_pch,
50+
)
51+
return module

0 commit comments

Comments
 (0)