Fix tensors_have_same_dim_order for degenerate shapes (semantic equivalence)#17612
Open
nefainl wants to merge 3 commits intopytorch:mainfrom
Open
Fix tensors_have_same_dim_order for degenerate shapes (semantic equivalence)#17612nefainl wants to merge 3 commits intopytorch:mainfrom
nefainl wants to merge 3 commits intopytorch:mainfrom
Conversation
…ytorch#16032) Fix tensors_have_same_dim_order() to correctly identify semantically equivalent memory layouts for tensors with degenerate shapes (size-1 dimensions). Problem: The previous implementation used label-only checking (all contiguous OR all channels_last). This failed for degenerate shapes like [N,1,H,W] (C=1) or [N,C,1,1] (H=W=1) where NCHW and NHWC tensors have identical physical memory layouts but different dim_order labels. Solution: Implement semantic equivalence checking that mirrors PyTorch's is_contiguous logic from c10/core/Contiguity.h: 1. Fast path: If dim_order labels match exactly -> return true 2. Semantic equivalence: If labels differ, compare strides but skip dimensions where both tensors have size=1 (these don't affect memory traversal order) Performance impact: Neutral to positive - Common case (same labels): ~75% faster (early exit after label match) - Bug fix case (degenerate shapes): ~50% faster (was failing before) - Error case (different layouts): Similar (early exit on mismatch) Test Plan: - Added 11 new test cases covering degenerate shapes, non-degenerate shapes, partial degenerates, different ranks, and edge cases - All existing tests continue to pass
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/17612
Note: Links to docs will display an error until the docs builds have been completed.
|
Author
|
@pytorchbot label "release notes: runtime" |
Author
|
Please also add @GregoryComer as one of the reviewers, his guidance has been helpful in the comments section of the original pull request and can help in potential improvements.. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Partial fix for #16032 -
tensors_have_same_dim_order()now correctly identifies semantically equivalent memory layouts for tensors with degenerate shapes (size-1 dimensions).This is PR C in the fix series for #16032, providing defense-in-depth at the C++ runtime level. PR A (#17611) fixes the Python export pipeline (
MemoryFormatOpsPass).Problem
The current implementation uses label-only checking (all contiguous OR all channels_last). This fails for degenerate shapes like
[N,1,H,W](C=1) or[N,C,1,1](H=W=1) where NCHW and NHWC tensors have identical physical memory layouts but different dim_order labels.Example: A grayscale image tensor
[2,1,224,224]exported as NHWC failsclone()because the output tensor has NCHW dim_order, even though both have identical memory traversal patterns.Solution
Implement semantic equivalence checking that mirrors PyTorch's
is_contiguouslogic:trueThis follows PyTorch's established semantics from
c10/core/Contiguity.hwhich explicitly skips size-1 dimensions when checking contiguity.Performance Impact
Neutral to positive - the common case is actually faster:
Key optimizations:
Test Plan
truetruefalsefalsefalsecmake --build && ./runtime_core_exec_aten_util_testSameDimOrderContiguous,SameDimOrderChannelsLast,SameShapesDifferentDimOrder)Related