Skip to content

Conversation

@lucylq
Copy link
Contributor

@lucylq lucylq commented Jan 15, 2026

Summary

At the emitter, resolved tensors should be in contiguous (NCHW) or channels-last (NHWC) format, with the correct corresponding physical memory.

This PR checks that

  1. If a tensor is not contiguous or channels-last, we call .contiguous on it.
  2. After calling .contiguous, make sure the spec is updated with the new dim order.

Fixes a bug where channels-last tensors were made into NCHW, with the dim order not updated.

Test plan

python -m unittest executorch.exir.emit.test.test_emit.TestEmit.test_emit_channels_last_constant

@pytorch-bot
Copy link

pytorch-bot bot commented Jan 15, 2026

🔗 Helpful Links

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

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

❌ 2 New Failures, 2 Unrelated Failures

As of commit 31335c6 with merge base 33974d5 (image):

NEW FAILURES - The following jobs have failed:

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

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 Jan 15, 2026
@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.

@lucylq lucylq force-pushed the lfq.update-dim-order-after-contiguous branch 6 times, most recently from 76c83f3 to b787b2a Compare January 15, 2026 21:42
@lucylq lucylq changed the title Update dim order after contiguous [emitter] Ensure dim order and physical tensor memory match Jan 15, 2026
@lucylq lucylq requested a review from Gasoonjia January 15, 2026 21:46

spec.storage = real_tensor.untyped_storage()

spec.stride = real_tensor.stride()
Copy link
Contributor

@Gasoonjia Gasoonjia Jan 15, 2026

Choose a reason for hiding this comment

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

Why didn't we reset stride before?

Copy link
Contributor Author

@lucylq lucylq Jan 15, 2026

Choose a reason for hiding this comment

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

edit: set it here instead of after .contiguous() call to make sure it's correct.

Copy link
Contributor

Choose a reason for hiding this comment

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

ahh no no i mean before this PR there should be some incontiguous tensor being replaced by contiguous one by calling .contiguous(), and for those tensors they should also update spec. Wondering why we didn't do that before?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah not sure. We do update in tensor.py after calling contiguous though:

executorch/exir/tensor.py

Lines 189 to 209 in d58c8ee

def from_tensor(cls, tensor: torch.Tensor, const: bool = False) -> TensorSpec:
if const:
# for non-contigous tensors, convert to a contiguous one
tensor = tensor.contiguous()
# Weights cannot be views during emission or serialization
if tensor.nbytes != tensor.untyped_storage().nbytes():
tensor = tensor.clone()
spec = cls(
dtype=tensor.dtype,
shape=tensor.shape,
layout=tensor.layout,
const=const,
is_sparse=tensor.is_sparse,
)
spec.stride = tensor.stride()
spec.dim_order = dim_order_from_stride(spec.stride)
spec.requires_grad = tensor.requires_grad
spec.storage = tensor.untyped_storage() if const else None
return spec

@lucylq lucylq force-pushed the lfq.update-dim-order-after-contiguous branch from b787b2a to 6654912 Compare January 15, 2026 21:57
@lucylq lucylq marked this pull request as ready for review January 15, 2026 21:57
@lucylq lucylq requested a review from JacobSzwejbka as a code owner January 15, 2026 21:57
Copilot AI review requested due to automatic review settings January 15, 2026 21:57
@lucylq lucylq requested a review from larryliu0820 as a code owner January 15, 2026 21:57
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 fixes a bug where channels-last tensors were emitted with mismatched dim_order metadata and physical memory layout. The fix ensures that when processing constant tensors at emission time, both contiguous and channels-last formats are preserved correctly, and the TensorSpec metadata (stride and dim_order) is updated to match the actual tensor's memory layout.

Changes:

  • Updated _emitter.py to check if tensors are either contiguous or channels_last, and update TensorSpec metadata accordingly
  • Added dim_order_from_stride import to support computing correct dim_order from stride
  • Added comprehensive test case validating channels-last constant tensor emission with verification of both dim_order metadata and physical storage layout

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
exir/emit/_emitter.py Updated tensor contiguity check to allow channels-last format and ensure spec metadata is synchronized with actual tensor layout
exir/emit/test/test_emit.py Added test validating channels-last constant tensors are emitted with correct dim_order and NHWC physical storage layout

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

@lucylq lucylq force-pushed the lfq.update-dim-order-after-contiguous branch from 6654912 to 31335c6 Compare January 15, 2026 22:04
@lucylq lucylq requested a review from GregoryComer January 15, 2026 22:07
Copy link
Member

@GregoryComer GregoryComer left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this! It might be worth adding one test for a constant tensor that has neither contiguous nor channels-last dim order. One motivating case was something like torch.randn(2, 16).transpose(1, 0).

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.

4 participants