Repository navigation
Reject non-default dim order in unfold_copy and select_scatter - #21915
Merged
JakeStevens merged 2 commits intoAug 24, 2026
Merged
JakeStevens merged 2 commits into
JakeStevens merged 2 commits into
Conversation
SuryanshSS1011
requested review from
kirklandsign and
manuelcandales
as code owners
August 18, 2026 13:27
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/21915
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit f7abaf0 with merge base 02b38d6 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
Contributor
Author
|
@pytorchbot label "release notes: ops & kernels" |
JakeStevens
approved these changes
Aug 18, 2026
Neilblaze
approved these changes
Aug 24, 2026
JakeStevens
pushed a commit
that referenced
this pull request
Sep 10, 2026
…22355) ### Summary Follow-up to #21866, which did the same for the optimized `layer_norm`. `opt_mm_out` and `opt_bmm_out` carry no dim order check, while portable `mm_out` and `bmm_out` each carry two: ```cpp ET_KERNEL_CHECK( ctx, tensors_have_same_dim_order(in, mat2, out), InvalidArgument, out); ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(in), InvalidArgument, out); ``` The shared `check_mm_args` and `check_bmm_args` do not supply them either, so choosing the optimized kernels quietly drops a check the portable path enforces. `opt_mm_out` passes raw data pointers to `cpublas::gemm` with the leading dimensions taken from sizes rather than strides, which describes a row-major matrix only in the default dim order. `opt_bmm_out` does the same per batch. Thus, the arithmetic also needs it. This PR adds portable guards to both the kernels, placed in the same position relative to the resize. Not reachable from export today, in the same way the `select_scatter` half of #21915 was not, since `_get_channels_last_dim_order` raises for any rank other than 4 and so a 2D `mm` or 3D `bmm` input cannot be given a non-default dim order. It is still worth adding because the optimized kernels are currently less strict than the portable ones with nothing recording that as intentional, and because #16429 asks for these layouts to be supported, at which point the optimized path would return wrong numbers where the portable path errors. ### Test plan A `NonDefaultDimOrderDies` per op. `op_mm_test.cpp` and `op_bmm_test.cpp` are both in `_optimized_kernels_test_sources`, so the same test body runs against both kernel sets, which makes the gap visible directly: | target | optimized kernels | mm and bmm | |---|---|---| | `optimized_kernels_test` | guards reverted, tests kept | both fail | | `optimized_kernels_test` | with the guards | both pass | | `portable_kernels_test` | untouched either way | both pass | All three tensors in each test share the same non-default dim order, so the same dim order check passes and only the default dim order check can reject. Both skip under `is_aten`. cc @larryliu0820 @manuelcandales @JakeStevens
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #21865. Two more portable kernels index with
getLeadingDimsandgetTrailingDims, which describe a contiguous run only in the default dim order.unfold_copycarries no dim order check at all. Checked against the same logical data in two memory formats:aten.unfold_copyIt reads
selfas a contiguous run and writesoutas a flat run, so neither side survives a non-default layout.unfoldappears in shipped example models including the Qualcomm llama vision encoder and the gemma4 speech transform, where a channels-last input is ordinary.select_scatterhas the same arithmetic and the same defect, but it is not reachable from export today. It carriestensors_have_same_dim_order(in, src, out), which is not sufficient on its own, becauseis_channels_last_dim_orderaccepts 4 and 5 dims, so a rank-5 input and its rank-4srccan both be channels-last and pass together. At rank 4 thesrcis rank 3 and the check does reject. Reaching the rank-5 case needs a channels-last dim order thatexir/dim_order_utils.pyrefuses 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_bmmand the kernels in #21865 use:ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(in), InvalidArgument, out);unfold_copyneeds it onoutas well, sinceoutis one rank aboveselfand is written as a flat run. Checking the pair withtensors_have_same_dim_orderwould not work there, for the same rank reason as above.Test plan
A
NonDefaultDimOrderDiesper kernel, plus a secondunfold_copycase covering theoutguard. Reverting the two kernel sources while keeping the tests fails all three.The
select_scattertest 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 withmake_with_dimorder, becausechannels_last_likeassertssizes.size() == 4and 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.cppinkernels/test/CMakeLists.txt, without which neither the existing tests in that file nor the new ones run in a CMake build.cc @larryliu0820 @manuelcandales @JakeStevens