Commit e56c7c3
authored
Arm backend: add argmin support and int32 overflow guard to ConvertIn… (pytorch#19918)
## Summary
Follow-up to pytorch#13803. Two changes to `ConvertInt64OutputOpsToInt32Pass`.
## 1. argmin support
`ConvertInt64OutputOpsToInt32Pass` inserts an `int64 → int32` cast after
`aten.argmax` nodes so that the index output (TOSA has no int64) becomes
int32 and downstream consumers can be delegated. `aten.argmin` returns
int64 identically but was not handled — the committer explicitly
deferred it as a future extension:
> *"Future extensions may include operators that return int64 outputs by
default (e.g., `argmin`) …"*
```mermaid
flowchart LR
subgraph before["Before"]
direction LR
A1["argmin\nint64"]:::cpu --> B1["mul\nint64"]:::blocked --> C1["add\nint64"]:::blocked
end
subgraph after["After"]
direction LR
A2["argmin\nint64"]:::cpu --> T["to_int32"]:::cpu
T --> B2["mul\nint32"]:::delegated --> C2["add\nint32"]:::delegated
end
before ~~~ after
classDef cpu fill:#f5c542,stroke:#b8962e,color:#000
classDef blocked fill:#e05c5c,stroke:#a33,color:#fff
classDef delegated fill:#4caf7d,stroke:#2d7a54,color:#fff
```
**Changes:** Mirror the existing argmax registration to cover argmin.
Rename the cast helper — it operates on the node's output dtype, not the
op name, so the old name was misleading once argmin was added.
---
## 2. int32 overflow guard
The pass previously had an open TODO:
```python
# TODO: Add range check based on the input tensor shape before casting the output
```
`argmax`/`argmin` return an index in `[0, size)` where `size` is the
number of elements searched. If `size > INT32_MAX`, casting to int32
silently truncates, producing a wrong index with no error.
**Changes:** Add a compile-time shape check (`shape[dim]` or `numel()`
for the no-dim form) and an `on_overflow` constructor param (`"raise"` /
`"warn"` / `"skip"`, default `"raise"`). A compile-time error is
preferable to a silent wrong result at runtime.
---
## Tests
```bash
$ python -m pytest backends/arm/test/passes/test_convert_int64_output_ops_to_int32.py -v
9 passed # 5 existing + 2 parametrized [argmax]/[argmin] delegation + 4 overflow (raise/warn/skip/invalid)
$ lintrunner backends/arm/_passes/convert_int64_output_ops_to_int32.py \
backends/arm/test/passes/test_convert_int64_output_ops_to_int32.py
ok No lint issues.
```
The argmax and argmin delegation cases are unified into a single
`@pytest.mark.parametrize` test.
Signed-off-by: Youngsik Yang <vacu9708@gmail.com>1 parent f0d9991 commit e56c7c3
2 files changed
Lines changed: 139 additions & 50 deletions
File tree
- backends/arm
- _passes
- test/passes
Lines changed: 61 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
| 28 | + | |
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
34 | | - | |
| 33 | + | |
| 34 | + | |
35 | 35 | | |
36 | | - | |
37 | | - | |
38 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
39 | 39 | | |
40 | | - | |
41 | | - | |
42 | | - | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
43 | 46 | | |
44 | 47 | | |
45 | 48 | | |
46 | 49 | | |
47 | 50 | | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
48 | 76 | | |
49 | 77 | | |
50 | 78 | | |
| |||
54 | 82 | | |
55 | 83 | | |
56 | 84 | | |
57 | | - | |
58 | | - | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
59 | 90 | | |
60 | 91 | | |
61 | 92 | | |
| |||
104 | 135 | | |
105 | 136 | | |
106 | 137 | | |
107 | | - | |
| 138 | + | |
108 | 139 | | |
109 | 140 | | |
110 | 141 | | |
| |||
138 | 169 | | |
139 | 170 | | |
140 | 171 | | |
141 | | - | |
142 | | - | |
143 | | - | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
144 | 189 | | |
145 | 190 | | |
146 | 191 | | |
| |||
Lines changed: 78 additions & 34 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| 15 | + | |
14 | 16 | | |
15 | 17 | | |
16 | 18 | | |
| |||
86 | 88 | | |
87 | 89 | | |
88 | 90 | | |
89 | | - | |
90 | | - | |
91 | | - | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
92 | 94 | | |
93 | 95 | | |
94 | | - | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
95 | 108 | | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | 109 | | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
127 | 117 | | |
128 | 118 | | |
129 | 119 | | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
0 commit comments