Skip to content

Reject non-default dim order in unfold_copy and select_scatter - #21915

Open
SuryanshSS1011 wants to merge 2 commits into
pytorch:mainfrom
SuryanshSS1011:fix/dim-order-gate-select-scatter-unfold
Open

Reject non-default dim order in unfold_copy and select_scatter#21915
SuryanshSS1011 wants to merge 2 commits into
pytorch:mainfrom
SuryanshSS1011:fix/dim-order-gate-select-scatter-unfold

Conversation

@SuryanshSS1011

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #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 #21865 use:

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.

Copilot AI lite review requested due to automatic review settings August 18, 2026 13:27
@pytorch-bot

pytorch-bot Bot commented Aug 18, 2026

Copy link
Copy Markdown

🔗 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 Failures

As of commit f7abaf0 with merge base 02b38d6 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 18, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@SuryanshSS1011

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "release notes: ops & kernels"

@pytorch-bot pytorch-bot Bot added the release notes: ops & kernels Changes to the opset and any new / changed kernel implementations label Aug 18, 2026
Copilot AI review requested due to automatic review settings August 18, 2026 15:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. release notes: ops & kernels Changes to the opset and any new / changed kernel implementations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants