Skip to content

Commit 66562b1

Browse files
angelayifacebook-github-bot
authored andcommitted
Patch executorch.pytree dependency
Summary: For running the AOT side of executorch w/o needing to build the pybindings, we can load the executorch.pytree.pybinding functions as the torch.utils._pytree functions. Reviewed By: larryliu0820 Differential Revision: D47136847 fbshipit-source-id: 0da9ce09636f91ed3368c83f1867f6c70544d736
1 parent 2493a87 commit 66562b1

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

pytree/__init__.py

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,43 @@
11
# flake8: noqa: F401
22

3-
# pyre-fixme[21]: Could not find module `executorch.pytree.pybindings`.
4-
# @manual=//executorch/pytree:pybindings
5-
from executorch.pytree.pybindings import (
6-
broadcast_to_and_flatten as broadcast_to_and_flatten,
7-
from_str as from_str,
8-
register_custom as register_custom,
9-
tree_flatten as tree_flatten,
10-
tree_map as tree_map,
11-
tree_unflatten as tree_unflatten,
12-
TreeSpec as TreeSpec,
13-
)
3+
import warnings
4+
5+
try:
6+
"""
7+
Internally we link the respective c++ library functions but for the OSS pip
8+
build we will just use the python library for now. The python library is not
9+
exactly the same so it will not work for the runtime, but it'll be fine for
10+
now as in most cases the runtime will not need it.
11+
"""
12+
13+
# pyre-fixme[21]: Could not find module `executorch.pytree.pybindings`.
14+
# @manual=//executorch/pytree:pybindings
15+
from executorch.pytree.pybindings import (
16+
broadcast_to_and_flatten as broadcast_to_and_flatten,
17+
from_str as from_str,
18+
register_custom as register_custom,
19+
tree_flatten as tree_flatten,
20+
tree_map as tree_map,
21+
tree_unflatten as tree_unflatten,
22+
TreeSpec as TreeSpec,
23+
)
24+
except:
25+
warnings.warn(
26+
"Unable to import executorch.pytree, using native torch pytree instead."
27+
)
28+
29+
from torch.utils._pytree import (
30+
_broadcast_to_and_flatten,
31+
_register_pytree_node,
32+
pytree_to_str,
33+
str_to_pytree,
34+
tree_flatten,
35+
tree_map,
36+
tree_unflatten,
37+
TreeSpec,
38+
)
39+
40+
broadcast_to_and_flatten = _broadcast_to_and_flatten
41+
from_str = str_to_pytree
42+
register_custom = _register_pytree_node
43+
TreeSpec.to_str = pytree_to_str # pyre-ignore

0 commit comments

Comments
 (0)