Repository navigation
Commit 55a969e
authored
Arm backend: fix round() decomposition to round-half-to-even (pytorch#21065)
### Summary
`DecomposeRoundPass` lowers `aten.round` with
round-half-**away-from-zero**, but `torch.round` is
round-half-**to-even** (banker's rounding). (see
[`torch.round`](https://pytorch.org/docs/stable/generated/torch.round.html)).
| input | `torch.round` (reference) | delegated output | fixed |
| --- | --- | --- | --- |
| `0.5` | `0` | `1` | `0` |
| `2.5` | `2` | `3` | `2` |
| `-1.5` | `-2` | `-2` | `-2` |
| `-2.5` | `-2` | `-3` | `-2` |
### The fix
round(x) now picks whichever of floor(x) and ceil(x) is nearer, and the
even one of the two when x is exactly halfway.
```
dist_to_floor = x - floor(x)
halved = floor(x) * 0.5
floor_is_odd = (halved - floor(halved)) == 0.5
take_ceil = (dist_to_floor > 0.5) | ((dist_to_floor == 0.5) & floor_is_odd)
result = where(take_ceil, ceil(x), floor(x))
```
| `x` | `floor(x)` | `ceil(x)` | `dist_to_floor` | `floor_is_odd` |
`take_ceil` | `result` |
| --- | --- | --- | --- | --- | --- | --- |
| `-2.5` | `-3` | `-2` | `0.5` | `true` | `true` | `-2` |
| `-2.25` | `-3` | `-2` | `0.75` | ~~`true`~~ | `true` | `-2` |
| `-1.5` | `-2` | `-1` | `0.5` | `false` | `false` | `-2` |
| `0.49999997` | `0` | `1` | `0.49999997` | ~~`false`~~ | `false` | `0`
|
| `0.50000006` | `0` | `1` | `0.50000006` | ~~`false`~~ | `true` | `1` |
| `3.5` | `3` | `4` | `0.5` | `true` | `true` | `4` |
### Testing
| case | data |
| --- | --- |
| `halfway_ties` | exact `.5`, both signs and both parities |
| `halfway_neighbors` | one ulp either side of a tie |
```bash
FILES="backends/arm/test/ops/test_round.py backends/arm/test/misc/test_mixed_type_lowering.py"
PYTEST="pytest --config-file=backends/arm/test/pytest.ini --numprocesses=auto"
$PYTEST $FILES -k tosa # 17 passed
$PYTEST $FILES -k "u55 or u65" # 7 passed
$PYTEST $FILES -k u85 # 7 passed
lintrunner backends/arm/_passes/decompose_round_pass.py \
backends/arm/test/ops/test_round.py \
backends/arm/test/misc/test_mixed_type_lowering.py
# No lint issues.
```
cc @digantdesai @freddan80 @per @zingo @oscarandersson8218 @mansnils
@Sebastian-Larsson @robell @rascani
---------
Signed-off-by: Youngsik Yang <vacu9708@gmail.com>1 parent 4ce2ec2 commit 55a969e
4 files changed
Lines changed: 74 additions & 68 deletions
File tree
- backends/arm
- _passes
- test
- misc
- ops
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
11 | 10 | | |
12 | | - | |
13 | 11 | | |
14 | 12 | | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
36 | 16 | | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
37 | 20 | | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | 21 | | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
51 | 29 | | |
52 | 30 | | |
53 | 31 | | |
| |||
60 | 38 | | |
61 | 39 | | |
62 | 40 | | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
37 | | - | |
38 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
39 | 40 | | |
40 | | - | |
41 | 41 | | |
42 | | - | |
43 | | - | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
44 | 47 | | |
45 | 48 | | |
46 | 49 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
37 | 41 | | |
38 | 42 | | |
39 | 43 | | |
| |||
110 | 114 | | |
111 | 115 | | |
112 | 116 | | |
113 | | - | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
114 | 120 | | |
115 | 121 | | |
116 | 122 | | |
| |||
140 | 146 | | |
141 | 147 | | |
142 | 148 | | |
143 | | - | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
144 | 152 | | |
145 | 153 | | |
146 | 154 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
32 | 41 | | |
33 | 42 | | |
34 | 43 | | |
| |||
41 | 50 | | |
42 | 51 | | |
43 | 52 | | |
44 | | - | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
45 | 56 | | |
46 | 57 | | |
47 | 58 | | |
| |||
88 | 99 | | |
89 | 100 | | |
90 | 101 | | |
91 | | - | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
92 | 105 | | |
93 | 106 | | |
94 | 107 | | |
| |||
0 commit comments