Repository navigation
Commit 00ffa2e
committed
Import torch only when the Python runtime returns torch tensors, and warn before outputs follow inputs
Importing executorch.runtime imports PyTorch whenever it is installed, even
for a program that only passes NumPy arrays. Only setting
EXECUTORCH_PYBINDINGS_TENSOR_OUTPUT=executorch avoids that, and never
loading PyTorch for NumPy callers would change what they get back. So that
change has to be announced first.
Without the setting, importing executorch.runtime no longer imports torch.
The outputs stay as they are today: torch.Tensor whenever PyTorch is
installed, and ExecuTorchResult without it. So a program with only NumPy
inputs still loads torch, at its first call that returns a tensor.
A future release will make tensor outputs follow each call's tensor inputs,
including inputs kept from an earlier call:
- any torch.Tensor input: torch.Tensor outputs;
- only NumPy, other buffer, DLPack or ExecuTorchResult inputs:
ExecuTorchResult outputs, even when PyTorch is installed;
- no tensor inputs: torch.Tensor outputs only if torch is already imported.
get_attribute() and bundled program verification count as calls without
tensor inputs.
The first call whose outputs will change shows one FutureWarning, which
says how to keep torch.Tensor outputs or get ExecuTorchResult outputs now.
A call that runs the method picks the type first, so with an error filter
for this FutureWarning, such a call raises before it runs the method (its
inputs are already installed). A torch that is installed but fails to
import, even with an ImportError, raises its own error, with a note on how
to skip torch. EXECUTORCH_PYBINDINGS_TENSOR_OUTPUT still fixes the type:
"executorch" never imports torch, and "torch" imports it when ExecuTorch is
imported.
Tested on Linux x86_64 with Python 3.12 and a CUDA build of PyTorch: the
pybindings, runtime, bundled program and CUDA DLPack suites give the same
results as on main, and the standalone no-ATen job passes with and without
PyTorch. Importing executorch.runtime takes 0.04 s and 18 MB of peak memory
instead of 1.9 s and 506 MB. A new test runs each case of the rule in a
fresh process, and each of its checks fails when its part of the change is
removed.1 parent c17afa3 commit 00ffa2e
8 files changed
Lines changed: 646 additions & 185 deletions
File tree
- docs/source
- extension/pybindings
- test
- runtime
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
46 | | - | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
47 | 61 | | |
48 | 62 | | |
49 | 63 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
48 | 52 | | |
49 | 53 | | |
50 | 54 | | |
| |||
91 | 95 | | |
92 | 96 | | |
93 | 97 | | |
94 | | - | |
95 | | - | |
96 | | - | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
97 | 108 | | |
98 | 109 | | |
99 | 110 | | |
| |||
105 | 116 | | |
106 | 117 | | |
107 | 118 | | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
113 | 122 | | |
114 | 123 | | |
115 | 124 | | |
| |||
138 | 147 | | |
139 | 148 | | |
140 | 149 | | |
141 | | - | |
142 | | - | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
143 | 153 | | |
144 | 154 | | |
145 | 155 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
32 | 34 | | |
33 | | - | |
34 | | - | |
35 | | - | |
| 35 | + | |
36 | 36 | | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
48 | 44 | | |
49 | 45 | | |
50 | 46 | | |
| |||
152 | 148 | | |
153 | 149 | | |
154 | 150 | | |
155 | | - | |
156 | 151 | | |
157 | 152 | | |
158 | 153 | | |
| |||
0 commit comments