Repository navigation
Commit 254635c
authored
Keep float scalars exact in float64 ops in ScalarToTensorPass (#24168)
### Summary
Fixes #24167.
`ScalarToTensorPass` turns a Python scalar operand into
`torch.tensor(value)`, which is float32 for a float. In a float64
program, `RemoveMixedTypeOperators` then casts that constant to float64,
but the value has already been rounded, so `x * 0.1` multiplies by
`0.10000000149011612`. `x + 0.1`, `x - 0.1`, `x / 0.1`, `0.1 - x` and
`torch.deg2rad(x)` were off by about 1e-9 to 1.5e-8 relative.
When the op has a float64 tensor operand, the pass now creates float
scalars as float64 constants. Those graphs also lose the
`_to_dim_order_copy` that cast the constant. Float32, float16 and
integer graphs get the same constants as before.
### Test plan
`test_scalar_to_tensor_pass_keeps_float64_scalars` in
`exir/tests/test_passes.py` runs `(x * 0.1 + 0.3) / 0.7` on a float64
input through `ScalarToTensorPass` and `RemoveMixedTypeOperators` and
checks the result equals eager exactly. It fails before this change and
passes after.
I ran it against executorch 1.5.1 and torch 2.14.1+cpu on Linux x86_64
(WSL2), with this file swapped in (the pass is the same on main). Of the
37 tests in `test_passes.py` matching `scalar`, `mixed`,
`constant_prop`, `to_copy`, `debug_handle`, `symint` or `dim_order`, the
same ones pass before and after apart from the new test. Five fail on
1.5.1 both with and without this change, because main's test file needs
newer `constant_prop_pass` and memory-format code than that release has.
Lowering `x * 0.1`, `x + 0.1`, `x / 0.1`, `0.1 - x` and
`torch.deg2rad(x)` with `to_edge(...).to_executorch()` and running them
through the portable pybindings now matches eager for float64, and gives
the same graph and output as before for float32, float16, and int32
inputs. `ufmt` (black 24.4.2, usort 1.0.8.post1) and `flake8` pass on
the changed files.
This PR was authored with AI assistance (Claude Code).
cc @nil-is-all @JacobSzwejbka @angelayi1 parent fd4cccf commit 254635c
2 files changed
Lines changed: 29 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
20 | 29 | | |
21 | 30 | | |
22 | 31 | | |
| |||
29 | 38 | | |
30 | 39 | | |
31 | 40 | | |
32 | | - | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
33 | 47 | | |
34 | 48 | | |
35 | 49 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
609 | 609 | | |
610 | 610 | | |
611 | 611 | | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
612 | 625 | | |
613 | 626 | | |
614 | 627 | | |
| |||
0 commit comments