Skip to content

Commit 32eacf2

Browse files
dbortfacebook-github-bot
authored andcommitted
Add a pure python wrapper fo pybindings.{aten,portable}_lib
Summary: When installed as a pip wheel, we must import `torch` before trying to import the pybindings shared library extension. This will load libtorch.so and related libs, ensuring that the pybindings lib can resolve those runtime dependencies. So, add a pure python wrapper that lets us do this when users say `import executorch.extension.pybindings.portable_lib` We only need this for OSS, so don't bother doing this for other pybindings targets. Differential Revision: D56317150
1 parent 29faa2e commit 32eacf2

File tree

5 files changed

+78
-19
lines changed

5 files changed

+78
-19
lines changed

CMakeLists.txt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -581,15 +581,16 @@ if(EXECUTORCH_BUILD_PYBIND)
581581
target_compile_options(util PUBLIC ${_pybind_compile_options})
582582
target_link_libraries(util PRIVATE torch c10 executorch)
583583

584-
# pybind portable_lib
585-
pybind11_add_module(portable_lib extension/pybindings/pybindings.cpp)
586-
target_compile_definitions(portable_lib
587-
PUBLIC EXECUTORCH_PYTHON_MODULE_NAME=portable_lib)
588-
target_include_directories(portable_lib PRIVATE ${TORCH_INCLUDE_DIRS})
589-
target_compile_options(portable_lib PUBLIC ${_pybind_compile_options})
590-
target_link_libraries(portable_lib PUBLIC ${_dep_libs})
591-
592-
install(TARGETS portable_lib
584+
# pybind _portable_lib. Note that the leading underscore is important, because
585+
# it needs to coexist with portable_lib.py in the same python package.
586+
pybind11_add_module(_portable_lib extension/pybindings/pybindings.cpp)
587+
target_compile_definitions(_portable_lib
588+
PUBLIC EXECUTORCH_PYTHON_MODULE_NAME=_portable_lib)
589+
target_include_directories(_portable_lib PRIVATE ${TORCH_INCLUDE_DIRS})
590+
target_compile_options(_portable_lib PUBLIC ${_pybind_compile_options})
591+
target_link_libraries(_portable_lib PUBLIC ${_dep_libs})
592+
593+
install(TARGETS _portable_lib
593594
LIBRARY DESTINATION executorch/extension/pybindings)
594595
endif()
595596

extension/pybindings/TARGETS

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ runtime.genrule(
2929
name = "pybindings_types_gen",
3030
srcs = [":pybinding_types"],
3131
outs = {
32-
"aten_lib.pyi": ["aten_lib.pyi"],
33-
"portable_lib.pyi": ["portable_lib.pyi"],
32+
"_aten_lib.pyi": ["_aten_lib.pyi"],
33+
"_portable_lib.pyi": ["_portable_lib.pyi"],
3434
},
35-
cmd = "cp $(location :pybinding_types)/* $OUT/portable_lib.pyi && cp $(location :pybinding_types)/* $OUT/aten_lib.pyi",
35+
cmd = "cp $(location :pybinding_types)/* $OUT/_portable_lib.pyi && cp $(location :pybinding_types)/* $OUT/_aten_lib.pyi",
3636
visibility = ["//executorch/extension/pybindings/..."],
3737
)
3838

@@ -46,15 +46,29 @@ executorch_pybindings(
4646
executorch_pybindings(
4747
compiler_flags = ["-std=c++17"],
4848
cppdeps = PORTABLE_MODULE_DEPS + MODELS_ATEN_OPS_LEAN_MODE_GENERATED_LIB,
49-
python_module_name = "portable_lib",
50-
types = ["//executorch/extension/pybindings:pybindings_types_gen[portable_lib.pyi]"],
49+
python_module_name = "_portable_lib",
50+
types = ["//executorch/extension/pybindings:pybindings_types_gen[_portable_lib.pyi]"],
5151
visibility = ["PUBLIC"],
5252
)
5353

5454
executorch_pybindings(
5555
compiler_flags = ["-std=c++17"],
5656
cppdeps = ATEN_MODULE_DEPS + MODELS_ATEN_OPS_ATEN_MODE_GENERATED_LIB,
57-
python_module_name = "aten_lib",
58-
types = ["//executorch/extension/pybindings:pybindings_types_gen[aten_lib.pyi]"],
57+
python_module_name = "_aten_lib",
58+
types = ["//executorch/extension/pybindings:pybindings_types_gen[_aten_lib.pyi]"],
5959
visibility = ["PUBLIC"],
6060
)
61+
62+
runtime.python_library(
63+
name = "portable_lib",
64+
srcs = ["portable_lib.py"],
65+
visibility = ["@EXECUTORCH_CLIENTS"],
66+
deps = [":_portable_lib"],
67+
)
68+
69+
runtime.python_library(
70+
name = "aten_lib",
71+
srcs = ["aten_lib.py"],
72+
visibility = ["@EXECUTORCH_CLIENTS"],
73+
deps = [":_aten_lib"],
74+
)

extension/pybindings/aten_lib.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
# pyre-strict
8+
9+
# When installed as a pip wheel, we must import `torch` before trying to import
10+
# the pybindings shared library extension. This will load libtorch.so and
11+
# related libs, ensuring that the pybindings lib can resolve those runtime
12+
# dependencies.
13+
import torch
14+
15+
# Import everything from the shared library extension into our namespace.
16+
from executorch.extension.pybindings._aten_lib import *
17+
18+
# Let users import everything from _aten_lib as if our current file defined
19+
# them.
20+
from executorch.extension.pybindings import _aten_lib
21+
__all__ = [name for name in dir(_aten_lib) if not name.startswith('_')]
22+
del _aten_lib

extension/pybindings/portable_lib.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
# pyre-strict
8+
9+
# When installed as a pip wheel, we must import `torch` before trying to import
10+
# the pybindings shared library extension. This will load libtorch.so and
11+
# related libs, ensuring that the pybindings lib can resolve those runtime
12+
# dependencies.
13+
import torch
14+
15+
# Import everything from the shared library extension into our namespace.
16+
from executorch.extension.pybindings._portable_lib import *
17+
18+
# Let users import everything from _portable_lib as if our current file defined
19+
# them.
20+
from executorch.extension.pybindings import _portable_lib
21+
__all__ = [name for name in dir(_portable_lib) if not name.startswith('_')]
22+
del _portable_lib

setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,13 @@ def run(self):
376376
cmake_args += [
377377
"-DEXECUTORCH_BUILD_PYBIND=ON",
378378
]
379-
build_args += ["--target", "portable_lib"]
379+
build_args += ["--target", "_portable_lib"]
380380
if ShouldBuild.xnnpack:
381381
cmake_args += [
382382
"-DEXECUTORCH_BUILD_XNNPACK=ON",
383383
]
384384
# No target needed; the cmake arg will link xnnpack
385-
# into the portable_lib target.
385+
# into the _portable_lib target.
386386
# TODO(dbort): Add MPS/CoreML backends when building on macos.
387387

388388
if ShouldBuild.llama_custom_ops:
@@ -444,7 +444,7 @@ def get_ext_modules() -> list[Extension]:
444444
# portable kernels, and a selection of backends. This lets users
445445
# load and execute .pte files from python.
446446
BuiltExtension(
447-
"portable_lib.*", "executorch.extension.pybindings.portable_lib"
447+
"_portable_lib.*", "executorch.extension.pybindings._portable_lib"
448448
)
449449
)
450450
if ShouldBuild.llama_custom_ops:

0 commit comments

Comments
 (0)