Skip to content

Revert "[cortex_m] Fix linear weight layout: transpose in AOT pass, a…#16910

Merged
psiddh merged 1 commit intopytorch:mainfrom
psiddh:main
Jan 27, 2026
Merged

Revert "[cortex_m] Fix linear weight layout: transpose in AOT pass, a…#16910
psiddh merged 1 commit intopytorch:mainfrom
psiddh:main

Conversation

@psiddh
Copy link
Contributor

@psiddh psiddh commented Jan 27, 2026

…lign meta/ref impl (#16782)"

This reverts commit 06f10b9.

Summary

[PLEASE REMOVE] See CONTRIBUTING.md's Pull Requests for ExecuTorch PR guidelines.

[PLEASE REMOVE] If this PR closes an issue, please add a Fixes #<issue-id> line.

[PLEASE REMOVE] If this PR introduces a fix or feature that should be the upcoming release notes, please add a "Release notes: " label. For a list of available release notes labels, check out CONTRIBUTING.md's Pull Requests.

Test plan

[PLEASE REMOVE] How did you test this PR? Please write down any manual commands you used and note down tests that you have written if applicable.

Copilot AI review requested due to automatic review settings January 27, 2026 18:21
@pytorch-bot pytorch-bot bot added the ci-no-td label Jan 27, 2026
@pytorch-bot
Copy link

pytorch-bot bot commented Jan 27, 2026

🔗 Helpful Links

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

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

⏳ 2 Pending, 1 Unrelated Failure

As of commit 59fdd72 with merge base 5690d26 (image):

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

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 Jan 27, 2026
@psiddh psiddh requested a review from GregoryComer January 27, 2026 18:21
@github-actions
Copy link

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.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR reverts commit 06f10b9 (PR #16782), which fixed the linear weight layout by transposing weights in the AOT pass to align with CMSIS-NN expectations. The revert removes the weight transpose from the AOT compilation pass and reintroduces transpose operations in the Python reference implementation.

Changes:

  • Removes weight transpose from _get_linear_replacement in the AOT pass
  • Adds weight transpose back to the Python reference implementation at runtime
  • Changes meta function to use weights.shape[0] instead of weights.shape[1]

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
backends/cortex_m/passes/convert_to_cortex_m_pass.py Removes AOT weight transpose; passes non-transposed weights to runtime; adds transpose back to kernel_sum computation
backends/cortex_m/ops/operators.py Updates meta function to use shape[0]; adds .T transpose operations in reference implementation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

args = (
node.args[0],
weights_transposed_node,
weights,
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

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

This revert creates an inconsistency between the Python reference implementation and the C++ CMSIS-NN runtime. After this revert, weights are passed to the C++ runtime in PyTorch's [out_features, in_features] format without transposition. However, the C++ runtime at op_quantized_linear.cpp:80 sets filter_dims to {in_feat, 1, 1, out_feat}, indicating CMSIS-NN expects weights in [in_features, out_features] format. The reference implementation compensates by adding .T at runtime (operators.py:389, 399), but the C++ runtime does not perform this transpose, which will cause incorrect results when running on actual hardware.

Copilot uses AI. Check for mistakes.
) -> torch.Tensor:

shape = (*input.shape[:-1], weights.shape[1])
shape = (*input.shape[:-1], weights.shape[0])
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

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

The meta function now uses weights.shape[0] which assumes weights are in [out_features, in_features] format (PyTorch convention). However, this is inconsistent with the C++ runtime expectation. If weights were properly transposed in the AOT pass (as PR #16782 attempted), this should use weights.shape[1] instead. The current implementation will produce incorrect output shapes when used with the C++ runtime.

Suggested change
shape = (*input.shape[:-1], weights.shape[0])
shape = (*input.shape[:-1], weights.shape[1])

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@psiddh psiddh merged commit 403b03e into pytorch:main Jan 27, 2026
149 of 152 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-no-td 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.

4 participants