Skip to content

Check dim order in the optimized mm and bmm as the portable ones do - #22355

Merged
JakeStevens merged 1 commit into
pytorch:mainfrom
SuryanshSS1011:fix/dim-order-gate-optimized-matmul
Sep 10, 2026
Merged

JakeStevens merged 1 commit into
pytorch:mainfrom
SuryanshSS1011:fix/dim-order-gate-optimized-matmul

Conversation

@SuryanshSS1011

@SuryanshSS1011 SuryanshSS1011 commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

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:

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

Copilot AI lite review requested due to automatic review settings August 31, 2026 16:16
@pytorch-bot

pytorch-bot Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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 1249f01 with merge base 60cb889 (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

  • pull / test-moshi-linux / linux-job (gh) (matched linux rule in flaky-rules.json)
    E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/libs/libssh/libssh-gcrypt-4_0.9.6-2ubuntu0.22.04.7_amd64.deb 404 Not Found [IP: 185.125.190.82 80]

BROKEN TRUNK - The following job failed but was 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 Aug 31, 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 31, 2026
@nil-is-all nil-is-all added the module: kernels Issues related to kernel libraries and utilities, and code under kernels/ label Sep 10, 2026
@JakeStevens
JakeStevens merged commit 588260d into pytorch:main Sep 10, 2026
74 of 77 checks passed
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. module: kernels Issues related to kernel libraries and utilities, and code under kernels/ 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