Skip to content

Commit 7a8e860

Browse files
authored
exir: Normalize constants before memory format conversion (pytorch#22901)
The YOLO26 export graph transposes and unsqueezes its detection-anchor tensor. Folding can leave it as a lifted constant or buffer with shape [1, 2, N] and dim order [2, 0, 1]. torch.memory_format cannot represent that order, so MemoryFormatOpsPass can fail when rewriting its preserve-format copy. ----------------------- without fix with fix input dim order [2, 0, 1] [2, 0, 1] early normalization missing [0, 1, 2] copy conversion assertion succeeds Normalize lifted constants and buffers before MemoryFormatOpsPass. Treat non-persistent buffers as buffer inputs even though ExportedProgram stores their values in constants. Cover both buffer storage modes and verify that to_edge keeps the caller-owned ExportedProgram unchanged. Signed-off-by: Zingo Andersen <Zingo.Andersen@arm.com>
1 parent 6cbdc03 commit 7a8e860

3 files changed

Lines changed: 124 additions & 10 deletions

File tree

‎exir/passes/convert_constant_dim_order_pass.py‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Copyright 2026 Arm Limited and/or its affiliates.
2+
#
3+
# This source code is licensed under the BSD-style license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
16
import torch
27
from torch.export import ExportedProgram
38
from torch.export.graph_signature import InputKind
@@ -47,31 +52,39 @@ def convert_constant_dim_order_pass(
4752
exported_program: ExportedProgram,
4853
) -> ExportedProgram:
4954
"""
50-
Normalize the dim order of constant tensors, ensuring that all constant tensors
51-
have either default or channels_last dim order. Tensors with other dim orders or
52-
striding are converted to contiguous tensors. This pass acts in-place on the
53-
unlifted exported program.
55+
Normalize lifted tensors to default or channels-last dim order.
56+
57+
Tensor constants and buffers with other dim orders or striding are converted
58+
to contiguous tensors. This pass acts in-place on the unlifted exported
59+
program.
5460
5561
Args:
5662
exported_program: The ExportedProgram to transform.
5763
5864
Returns:
59-
The modified ExportedProgram with normalized constant dim order.
65+
The modified ExportedProgram with normalized lifted tensor dim order.
6066
"""
6167

68+
non_persistent_buffer_names = set(
69+
exported_program.graph_signature.non_persistent_buffers
70+
)
71+
6272
for key, const in exported_program.constants.items():
6373
if isinstance(const, torch.Tensor) and _should_transform(const):
6474
exported_program.constants[key] = const.contiguous()
6575

6676
# Also update the corresponding placeholder node meta value. This doesn't
6777
# get automatically updated during retracing as ExportPass uses the placeholder
6878
# meta as the source of truth. TODO(?)
69-
_update_placeholder_meta(exported_program, key, InputKind.CONSTANT_TENSOR)
79+
# Non-persistent buffers are stored as constants but remain buffer inputs.
80+
kind = (
81+
InputKind.BUFFER
82+
if key in non_persistent_buffer_names
83+
else InputKind.CONSTANT_TENSOR
84+
)
85+
_update_placeholder_meta(exported_program, key, kind)
7086

7187
# Convert buffers.
72-
non_persistent_buffer_names = set(
73-
exported_program.graph_signature.non_persistent_buffers
74-
)
7588
for key, buffer in exported_program.named_buffers():
7689
if (
7790
key not in non_persistent_buffer_names

‎exir/program/_program.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,13 @@ def _generate_edge_program(
817817
# Remove unused parameters
818818
program = remove_unused_parameters_pass(program)
819819

820+
if not config._skip_dim_order:
821+
# Normalize constants and buffers before MemoryFormatOpsPass rewrites
822+
# preserve-format copies, which cannot represent arbitrary dim orders.
823+
program = convert_constant_dim_order_pass.convert_constant_dim_order_pass(
824+
program
825+
)
826+
820827
pre_op_replace_passes, post_op_replace_passes = _get_aten_to_edge_passes(config)
821828

822829
passes = [
@@ -858,7 +865,7 @@ def _generate_edge_program(
858865
# Lift the tensor constants created in ScalarToTensorPass
859866
edge_program = lift_constant_tensor_pass(edge_program)
860867

861-
# Normalize constant tensor dim order on the unlifted graph
868+
# Normalize tensor constants introduced by the preceding passes.
862869
edge_program = convert_constant_dim_order_pass.convert_constant_dim_order_pass(
863870
edge_program
864871
)

‎exir/tests/test_passes.py‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2857,6 +2857,42 @@ def forward(self, x):
28572857

28582858
self.assertTrue(found_clone, "Should find a _clone_dim_order node in the graph")
28592859

2860+
def test_clone_non_contiguous_constant_is_normalized(self) -> None:
2861+
"""
2862+
Verify that clone() normalizes a lifted constant with a dim order
2863+
that torch.memory_format cannot represent.
2864+
"""
2865+
2866+
class CloneConstantModel(torch.nn.Module):
2867+
def __init__(self) -> None:
2868+
super().__init__()
2869+
self.anchors = torch.arange(10).reshape(5, 2).T.unsqueeze(0).float()
2870+
2871+
def forward(self, x):
2872+
return self.anchors.clone() + x
2873+
2874+
model = CloneConstantModel()
2875+
self.assertEqual(tuple(model.anchors.dim_order()), (2, 0, 1))
2876+
inputs = (torch.randn(1, 2, 5),)
2877+
ep = torch.export.export(model, inputs)
2878+
2879+
# lifted constant Edge constant clone rewrite
2880+
# [2, 0, 1] --normalize--> [0, 1, 2] ----------> [0, 1, 2]
2881+
edge = to_edge(ep, compile_config=EdgeCompileConfig(_skip_dim_order=False))
2882+
edge_program = edge.exported_program()
2883+
2884+
anchors = edge_program.constants["anchors"]
2885+
self.assertTrue(anchors.is_contiguous())
2886+
2887+
# Find the clone rewrite and verify its normalized dim order.
2888+
clone_nodes = [
2889+
node
2890+
for node in edge_program.graph_module.graph.nodes
2891+
if node.op == "call_function" and "_clone_dim_order" in str(node.target)
2892+
]
2893+
self.assertEqual(len(clone_nodes), 1)
2894+
self.assertEqual(tuple(clone_nodes[0].meta["val"].dim_order()), (0, 1, 2))
2895+
28602896
def test_clone_contiguous_format_kwarg_stays_contiguous(self) -> None:
28612897
"""
28622898
Regression guard: explicit contiguous_format should produce contiguous dim_order.
@@ -2960,6 +2996,64 @@ def forward(self, x):
29602996

29612997
self.assertTrue(found_copy, "Should find a _to_dim_order_copy node")
29622998

2999+
def test_to_copy_non_contiguous_buffer_is_normalized(self) -> None:
3000+
"""
3001+
Verify that folded anchor layouts are normalized for both buffer
3002+
storage modes without mutating the source ExportedProgram.
3003+
"""
3004+
3005+
class ToCopyBufferModel(torch.nn.Module):
3006+
def __init__(self, persistent: bool) -> None:
3007+
super().__init__()
3008+
# Reproduce a folded detection-anchor layout that
3009+
# torch.memory_format cannot represent.
3010+
anchors = torch.arange(10).reshape(5, 2).T.unsqueeze(0).float()
3011+
self.register_buffer("anchors", anchors, persistent=persistent)
3012+
3013+
def forward(self, x):
3014+
return self.anchors.to(dtype=x.dtype) + x
3015+
3016+
# Test both persistent=True and False
3017+
# storage
3018+
# persistent=True state_dict --\
3019+
# +--> to_edge --> Edge [0, 1, 2]
3020+
# persistent=False constants ---/ |
3021+
# `--> source [2, 0, 1]
3022+
for persistent in (True, False):
3023+
with self.subTest(persistent=persistent):
3024+
model = ToCopyBufferModel(persistent)
3025+
self.assertEqual(tuple(model.anchors.dim_order()), (2, 0, 1))
3026+
inputs = (torch.randn(1, 2, 5, dtype=torch.float16),)
3027+
ep = torch.export.export(model, inputs)
3028+
source_anchors = dict(ep.named_buffers())["anchors"]
3029+
3030+
edge = to_edge(
3031+
ep, compile_config=EdgeCompileConfig(_skip_dim_order=False)
3032+
)
3033+
edge_program = edge.exported_program()
3034+
3035+
# Lowering operates on a derived program. The caller-owned buffer
3036+
# must keep both its identity and unsupported source layout.
3037+
source_anchors_after = dict(ep.named_buffers())["anchors"]
3038+
self.assertIs(source_anchors_after, source_anchors)
3039+
self.assertEqual(tuple(source_anchors_after.dim_order()), (2, 0, 1))
3040+
3041+
# Both storage modes must produce the same normalized Edge buffer.
3042+
anchors = dict(edge_program.named_buffers())["anchors"]
3043+
self.assertTrue(anchors.is_contiguous())
3044+
3045+
# The preserve-format copy must use the normalized Edge layout.
3046+
copy_nodes = [
3047+
node
3048+
for node in edge_program.graph_module.graph.nodes
3049+
if node.op == "call_function"
3050+
and "_to_dim_order_copy" in str(node.target)
3051+
]
3052+
self.assertEqual(len(copy_nodes), 1)
3053+
self.assertEqual(
3054+
tuple(copy_nodes[0].meta["val"].dim_order()), (0, 1, 2)
3055+
)
3056+
29633057

29643058
class TestCSEPass(unittest.TestCase):
29653059
"""Tests for Common Subexpression Elimination pass."""

0 commit comments

Comments
 (0)