Repository navigation
Commit 722e5cf
Fix two crashes caused by constant_prop_pass (#24224)
## What is wrong today
Programs that go through `constant_prop_pass` can crash in two ways: one
inside the pass, and one later when the program is lowered.
**1. A folded value that is not a tensor.** The pass also folds values
such as the int from `.item()` on a buffer, and the bools that
`torch._check` turns into runtime checks. When a node that stays in the
graph reads such a value, the pass tries to turn it into a constant
tensor input and fails:
```python
n = self.n.item() # self.n is a buffer
torch._check(n >= 1)
torch._check(n <= x.shape[0])
return x[:n] * self.w[:n]
```
```
TypeError: cannot create weak reference to 'bool' object
```
**2. A folded parameter that still requires grad.** Folding
`self.w.t()`, or one chunk of a packed weight, gives a view that still
has `requires_grad=True`, because `torch.no_grad()` does not clear that
flag on a view. For some views, such as a transposed one, `to_edge` then
copies the constant into a non-leaf tensor, and lowering to a backend
can fail when it deep-copies the program:
```
RuntimeError: Only Tensors created explicitly by the user (graph leaves) support the deepcopy protocol at the moment.
```
An op can also return the parameter itself, like
`self.w.to(torch.float32)` on a float32 parameter. Then the pass stores
the `nn.Parameter` as a constant, and `to_edge` fails with
`AttributeError: ... is not a buffer`.
## What this change does
Two commits, one per crash:
1. Only tensors can become constant inputs. So a non-tensor value that a
node outside the fold reads now stays in the graph, together with the
non-tensor values it is computed from. Tensors computed from it still
fold: `self.w[:n]` still becomes one constant. Programs that
`constant_prop_pass` handled before are unchanged.
2. A folded constant must be a plain tensor that does not require grad,
so the pass now detaches every folded tensor.
## What was tested
On macOS arm64:
- Two new tests in `exir/tests/test_passes.py`, one per commit. Each
fails before its commit and passes after: the first with `TypeError:
cannot create weak reference to 'int' object`, the second with the
`AttributeError` above.
- The first covers a scalar read at runtime, one returned as an output,
one whose first reader is folded, and one read only by folded nodes (it
folds away). The program also lowers with
`to_edge(...).to_executorch()`.
- The second covers the transposed view and the `to` case, and checks
that the output matches eager.
- The rest of `exir/tests/test_passes.py`, `exir/emit/test/test_emit.py`
and `exir/tests/test_quant_fusion_pass.py`. Five tests in those files
fail the same way with and without this change, because they need the
quantized and custom op libraries.
Co-authored-by: Anthony Shoumikhin <anthony@shoumikh.in>1 parent 363be8c commit 722e5cf
2 files changed
Lines changed: 75 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
182 | 182 | | |
183 | 183 | | |
184 | 184 | | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
185 | 191 | | |
186 | 192 | | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
187 | 202 | | |
188 | 203 | | |
189 | 204 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2902 | 2902 | | |
2903 | 2903 | | |
2904 | 2904 | | |
| 2905 | + | |
| 2906 | + | |
| 2907 | + | |
| 2908 | + | |
| 2909 | + | |
| 2910 | + | |
| 2911 | + | |
| 2912 | + | |
| 2913 | + | |
| 2914 | + | |
| 2915 | + | |
| 2916 | + | |
| 2917 | + | |
| 2918 | + | |
| 2919 | + | |
| 2920 | + | |
| 2921 | + | |
| 2922 | + | |
| 2923 | + | |
| 2924 | + | |
| 2925 | + | |
| 2926 | + | |
| 2927 | + | |
| 2928 | + | |
| 2929 | + | |
| 2930 | + | |
| 2931 | + | |
| 2932 | + | |
| 2933 | + | |
| 2934 | + | |
| 2935 | + | |
| 2936 | + | |
| 2937 | + | |
| 2938 | + | |
| 2939 | + | |
| 2940 | + | |
| 2941 | + | |
| 2942 | + | |
| 2943 | + | |
| 2944 | + | |
| 2945 | + | |
| 2946 | + | |
| 2947 | + | |
| 2948 | + | |
| 2949 | + | |
| 2950 | + | |
| 2951 | + | |
| 2952 | + | |
| 2953 | + | |
| 2954 | + | |
| 2955 | + | |
| 2956 | + | |
| 2957 | + | |
| 2958 | + | |
| 2959 | + | |
| 2960 | + | |
| 2961 | + | |
| 2962 | + | |
| 2963 | + | |
| 2964 | + | |
2905 | 2965 | | |
2906 | 2966 | | |
2907 | 2967 | | |
| |||
0 commit comments