Repository navigation
Commit 2287a8b
authored
Reject non-default dim order in unfold_copy and select_scatter (pytorch#21915)
### Summary
Follow-up to pytorch#21865. Two more portable kernels index with
`getLeadingDims` and `getTrailingDims`, which describe a contiguous run
only in the default dim order.
`unfold_copy` carries no dim order check at all. Checked against the
same logical data in two memory formats:
| kernel | contiguous input | channels-last input |
|---|---|---|
| `aten.unfold_copy` | matches | wrong, max abs diff 14.0 |
It reads `self` as a contiguous run and writes `out` as a flat run, so
neither side survives a non-default layout. `unfold` appears in shipped
example models including the Qualcomm llama vision encoder and the
gemma4 speech transform, where a channels-last input is ordinary.
`select_scatter` has the same arithmetic and the same defect, but it is
not reachable from export today. It carries
`tensors_have_same_dim_order(in, src, out)`, which is not sufficient on
its own, because `is_channels_last_dim_order` accepts 4 and 5 dims, so a
rank-5 input and its rank-4 `src` can both be channels-last and pass
together. At rank 4 the `src` is rank 3 and the check does reject.
Reaching the rank-5 case needs a channels-last dim order that
`exir/dim_order_utils.py` refuses to generate, since it raises for any
rank other than 4. So the kernel is protected by a gap in the export
layer rather than by its own checks, and the guard closes that before 5D
channels-last support makes it reachable.
Both get the guard `op_addmm`, `op_bmm` and the kernels in pytorch#21865 use:
```cpp
ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(in), InvalidArgument, out);
```
`unfold_copy` needs it on `out` as well, since `out` is one rank above
`self` and is written as a flat run. Checking the pair with
`tensors_have_same_dim_order` would not work there, for the same rank
reason as above.
### Test plan
A `NonDefaultDimOrderDies` per kernel, plus a second `unfold_copy` case
covering the `out` guard. Reverting the two kernel sources while keeping
the tests fails all three.
The `select_scatter` test has to be rank 5. A rank-4 case is rejected by
the pre-existing same dim order check on current main, so it would pass
without reaching the new guard. It builds its tensors with
`make_with_dimorder`, because `channels_last_like` asserts `sizes.size()
== 4` and cannot express the failing case.
All three tests skip under `is_aten`, since the ATen kernels handle a
non-default dim order and do not throw.
This also registers `op_unfold_copy_test.cpp` in
`kernels/test/CMakeLists.txt`, without which neither the existing tests
in that file nor the new ones run in a CMake build.1 parent 5d22fd9 commit 2287a8b
5 files changed
Lines changed: 61 additions & 0 deletions
File tree
- kernels
- portable/cpu
- test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| 40 | + | |
| 41 | + | |
40 | 42 | | |
41 | 43 | | |
42 | 44 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
42 | 46 | | |
43 | 47 | | |
44 | 48 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
306 | 306 | | |
307 | 307 | | |
308 | 308 | | |
| 309 | + | |
309 | 310 | | |
310 | 311 | | |
311 | 312 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
665 | 665 | | |
666 | 666 | | |
667 | 667 | | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
| 17 | + | |
16 | 18 | | |
17 | 19 | | |
18 | 20 | | |
| |||
127 | 129 | | |
128 | 130 | | |
129 | 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 | + | |
0 commit comments