Skip to content

Fix dim_order derivation for channels-last tensors with size-1 dims - #22745

Draft
JakeStevens wants to merge 2 commits into
pytorch:mainfrom
JakeStevens:fix/canonical-dim-order-size1-channel
Draft

Fix dim_order derivation for channels-last tensors with size-1 dims#22745
JakeStevens wants to merge 2 commits into
pytorch:mainfrom
JakeStevens:fix/canonical-dim-order-size1-channel

Conversation

@JakeStevens

@JakeStevens JakeStevens commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Fixes #22520. Stride sorting can give a channels-last tensor with a size-1 channel an unsupported dimension order, such as (0, 2, 1, 3) for the KWS input (1, 1, 49, 10). Pass sizes through tensor specs, weight emission, and serialization so these orders can be corrected when strides exactly match channels-last. Preserve existing default orders, which keeps physically equivalent singleton layouts compatible with portable kernels. Reuse PyTorch's stride calculation; memory-format pass behavior is unchanged.

Add coverage for the convolution export, mixed-format addition, 5D ReLU, and symbolic sizes. Re-enable the NXP channels-last PTQ/QAT cases with the portable C++ reference and the comparator's standard finite MSE tolerance (3.5e-4).

Validation: 287 tests passed across tensor, dim-order, passes, native serialization, EXIR serialization, and layout transforms; 49 skipped and 1 expected failure. The unrelated test_to_out_variant_none_output failure was confirmed with pre-PR stride inference. Lintrunner is clean on all touched files. Portable runtime probes match Python for addition, 5D ReLU, and PTQ/QAT convolution and DSCNN KWS models (XNNPACK PT2E quantization, no delegation). The full NXP simulator suite requires CI and was not run locally.

This PR was authored with AI assistance (Muse Code and Codex).

A channels-last tensor with a size-1 channel (e.g. the (1, 1, 49, 10)
input of MLPerf Tiny keyword spotting) has strides whose stable sort is
the non-canonical dim order (0, 2, 1, 3). dim_order_from_stride now takes
the tensor sizes and returns the canonical dim order when the strides
exactly match a contiguous or channels-last layout, which portable
kernels require. The canonical order describes the identical physical
layout, and genuinely non-canonical layouts still fall back to sorting.

Fixes pytorch#22520
@pytorch-bot

pytorch-bot Bot commented Sep 11, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/22745

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (2 Unrelated Failures)

As of commit 66bd993 with merge base a3ae329 (image):

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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 Sep 11, 2026
@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Comment thread exir/tests/test_dim_order_utils.py Outdated
edge_prog.to_executorch()

def test_channels_last_single_channel_conv_dim_order(self) -> None:
# Regression test for https://github.com/pytorch/executorch/issues/22520:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

do not reference the issue in a comment.

in general, comment too verbose

Comment thread exir/tests/test_tensor.py Outdated
dim_order_from_stride((u0, 0, 1))

def test_dim_order_from_stride_with_sizes(self) -> None:
# Regression test for https://github.com/pytorch/executorch/issues/22520:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

same as above

Reuse PyTorch stride calculation while keeping existing default orders and preserve_format inference. Cover singleton-layout regressions and symbolic sizes, and restore portable NXP KWS comparisons with finite tolerances.

Authored with AI assistance (Codex).
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Depthwise separable convolution computation by portable kernels expects wrong dim_order

1 participant