Commit a1a43ff
authored
Support negative padding in constant_pad_nd (pytorch#23116)
### Summary
Fixes pytorch#13554.
Match PyTorch's `constant_pad_nd` behavior for negative padding by
cropping the input before applying positive padding. The optimized
library uses the same portable implementation through its fallback
registration, so both paths receive the fix.
Handle mixed cropping and padding, unchanged output shapes, empty
tensors, fully cropped inputs, dynamic outputs, and channels-last
layouts. Reject excessive cropping and output-dimension overflow, and
ignore unused fill values for crop-only operations as PyTorch does. Add
regression coverage for these cases, including the input from pytorch#13554.
This branch starts at upstream `main` (`283297e430`) and contains only
this fix, without the earlier local commits.
### Test plan
Validation used a fresh runtime build from this branch and a focused
local CMake harness compiling the shared operator tests with generated
portable and optimized bindings.
| Check | Result |
| --- | --- |
| Portable `OpConstantPadNDOutTest` | 32/32 passed |
| Optimized `OpConstantPadNDOutTest` | 31/32 passed; existing
scalar-overflow failure described below |
| PyTorch parity under UBSan, portable and optimized | 3,253 cases per
build passed: 2,803 valid results and 450 expected rejections |
| `clang-format --dry-run --Werror` on the three changed files | Passed
|
| `git diff --check` | Passed |
Commands run against the focused build:
```sh
cmake --build /private/tmp/executorch-pad-pr.r6f52N/tests --parallel 8
ctest --test-dir /private/tmp/executorch-pad-pr.r6f52N/tests --output-on-failure
python .outputs/issue-13554/parity.py \
/private/tmp/executorch-pad-pr.r6f52N/tests/libportable_parity.dylib \
/private/tmp/executorch-pad-pr.r6f52N/tests/liboptimized_parity.dylib
```
The optimized `LongTensorTooLargeScalarDies` test fails with the locally
installed PyTorch 2.11 development headers, whose
floating-point-to-int64 overflow check accepts `2^63`. The same failure
was reproduced using the unmodified operator from upstream `main`; the
positive-padding scalar conversion is unchanged by this PR. Portable
tests use the repository's current vendored headers and pass this check.
Full repository CI and lintrunner were not run locally.
Authored with assistance from OpenAI Codex.1 parent abedcd2 commit a1a43ff
3 files changed
Lines changed: 259 additions & 60 deletions
File tree
- kernels
- portable/cpu
- util
- test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
40 | | - | |
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | | - | |
| 50 | + | |
| 51 | + | |
51 | 52 | | |
52 | 53 | | |
53 | 54 | | |
54 | 55 | | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
| 56 | + | |
| 57 | + | |
65 | 58 | | |
66 | 59 | | |
67 | | - | |
68 | | - | |
| 60 | + | |
| 61 | + | |
69 | 62 | | |
70 | 63 | | |
71 | 64 | | |
| |||
92 | 85 | | |
93 | 86 | | |
94 | 87 | | |
95 | | - | |
| 88 | + | |
96 | 89 | | |
97 | 90 | | |
98 | 91 | | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | 92 | | |
109 | 93 | | |
110 | 94 | | |
| |||
119 | 103 | | |
120 | 104 | | |
121 | 105 | | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
122 | 115 | | |
123 | 116 | | |
124 | | - | |
125 | 117 | | |
126 | 118 | | |
127 | 119 | | |
128 | 120 | | |
129 | | - | |
| 121 | + | |
130 | 122 | | |
131 | 123 | | |
132 | 124 | | |
| 125 | + | |
133 | 126 | | |
134 | 127 | | |
135 | 128 | | |
136 | 129 | | |
137 | 130 | | |
138 | | - | |
139 | 131 | | |
140 | 132 | | |
141 | 133 | | |
| |||
147 | 139 | | |
148 | 140 | | |
149 | 141 | | |
150 | | - | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
151 | 145 | | |
152 | 146 | | |
153 | 147 | | |
| |||
181 | 175 | | |
182 | 176 | | |
183 | 177 | | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
184 | 182 | | |
185 | 183 | | |
186 | 184 | | |
| |||
193 | 191 | | |
194 | 192 | | |
195 | 193 | | |
196 | | - | |
197 | 194 | | |
198 | 195 | | |
199 | 196 | | |
200 | 197 | | |
201 | 198 | | |
| 199 | + | |
202 | 200 | | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
207 | 205 | | |
208 | | - | |
| 206 | + | |
209 | 207 | | |
210 | | - | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
211 | 213 | | |
212 | 214 | | |
213 | 215 | | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
214 | 220 | | |
215 | 221 | | |
216 | 222 | | |
217 | 223 | | |
218 | | - | |
219 | 224 | | |
220 | 225 | | |
221 | 226 | | |
222 | 227 | | |
223 | 228 | | |
224 | 229 | | |
225 | 230 | | |
226 | | - | |
| 231 | + | |
| 232 | + | |
227 | 233 | | |
228 | 234 | | |
229 | 235 | | |
230 | 236 | | |
231 | | - | |
232 | 237 | | |
233 | 238 | | |
234 | 239 | | |
| |||
244 | 249 | | |
245 | 250 | | |
246 | 251 | | |
247 | | - | |
248 | | - | |
249 | 252 | | |
250 | 253 | | |
251 | 254 | | |
252 | 255 | | |
253 | 256 | | |
254 | 257 | | |
255 | | - | |
256 | | - | |
257 | 258 | | |
258 | 259 | | |
259 | 260 | | |
| |||
267 | 268 | | |
268 | 269 | | |
269 | 270 | | |
| 271 | + | |
| 272 | + | |
270 | 273 | | |
271 | | - | |
272 | | - | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
273 | 277 | | |
274 | 278 | | |
275 | 279 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
| 12 | + | |
11 | 13 | | |
12 | 14 | | |
13 | 15 | | |
| |||
616 | 618 | | |
617 | 619 | | |
618 | 620 | | |
619 | | - | |
620 | | - | |
621 | | - | |
622 | | - | |
623 | | - | |
624 | | - | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
625 | 636 | | |
626 | 637 | | |
627 | 638 | | |
| |||
0 commit comments