Reject non-default dim order in unfold_copy and select_scatter - #21915
Open
SuryanshSS1011 wants to merge 2 commits into
Open
Reject non-default dim order in unfold_copy and select_scatter#21915SuryanshSS1011 wants to merge 2 commits into
SuryanshSS1011 wants to merge 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
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.