Skip to content

Commit 47f046d

Browse files
Fix unsafe copies in EXIR and permute transforms (pytorch#19873)
Replace deepcopy-based copies of shape lists with shallow container copies in paths that can carry torch.SymInt values from fake tensor metadata. This is safe as torch.Size is immutable, so creating a shallow list copy of it is safe: we can insert/delete elements in the new list without affecting the original shape. This fixes symbolic-shape failures in exir.tensor.stride_from_dim_order() and in PostponePermuteOpBelowSqueezeOrUnsqueezeLikeView, where deepcopy() could crash while handling SymInt-backed shapes. The error that was produced: "RuntimeError: Cannot access data pointer of Tensor (e.g. FakeTensor, FunctionalTensor). If you're using torch.compile/export/fx, it is likely that we are erroneously tracing into a custom kernel. To fix this, please wrap the custom kernel into an opaque custom op. Please see the following for details: https://pytorch.org/tutorials/advanced/custom_ops_landing_page.html" Add focused regression coverage for both cases: one test for stride computation with symbolic sizes, and one dynamic-shape edge-graph test for the permute/view transform pass. cc @digantdesai @freddan80 @per @zingo @mansnils @Sebastian-Larsson @robell @rascani Signed-off-by: Oscar Andersson <oscar.andersson@arm.com>
1 parent 2d9e5b9 commit 47f046d

4 files changed

Lines changed: 59 additions & 6 deletions

File tree

‎backends/transforms/postpone_permute_below_squeeze_view.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
22
# All rights reserved.
3+
# Copyright 2026 Arm Limited and/or its affiliates.
34
#
45
# This source code is licensed under the BSD-style license found in the
56
# LICENSE file in the root directory of this source tree.
67

78
# pyre-unsafe
89

9-
import copy
1010
from typing import cast, List
1111

1212
import torch
@@ -108,7 +108,7 @@ def maybe_remove_or_replace(self, node: torch.fx.Node) -> bool:
108108
# view_node_shape is almost same as permute_node_shape
109109
# except it has one more dim somewhere
110110
# and the extra dim has value of 1.
111-
new_view_shape = copy.deepcopy(pred_shape)
111+
new_view_shape = list(pred_shape)
112112
new_view_shape.insert(index, 1)
113113
new_permute_dims = [x + 1 if x >= index else x for x in permute_dims]
114114
new_permute_dims.insert(index, index)
@@ -132,7 +132,7 @@ def maybe_remove_or_replace(self, node: torch.fx.Node) -> bool:
132132
# and the extra dim has value of 1.
133133
# Convert permute_dims to list of ints
134134
index_to_remove = permute_dims[index]
135-
new_view_shape = copy.deepcopy(pred_shape)
135+
new_view_shape = list(pred_shape)
136136
del new_view_shape[index_to_remove]
137137
new_permute_dims = [
138138
x - 1 if x > index_to_remove else x for x in permute_dims

‎backends/transforms/test/test_permute_optimization_passes.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
22
# All rights reserved.
3+
# Copyright 2026 Arm Limited and/or its affiliates.
34
#
45
# This source code is licensed under the BSD-style license found in the
56
# LICENSE file in the root directory of this source tree.
@@ -25,6 +26,8 @@
2526
from executorch.backends.transforms.replace_nop_transpose_or_permute_with_view import (
2627
ReplaceNopTransposeOrPermuteWithViewPass,
2728
)
29+
30+
from executorch.exir import EdgeCompileConfig, to_edge
2831
from executorch.exir.dialects._ops import ops as exir_ops
2932
from executorch.exir.pass_base import PassResult
3033
from torch.utils import _pytree as pytree
@@ -477,6 +480,38 @@ def test_permute4_view3_chains(self) -> None:
477480
"PostponePermuteOpBelowSqueezeOrUnsqueezeLikeView",
478481
)
479482

483+
def test_postpone_permute_with_symbolic_shapes(self) -> None:
484+
class DynamicPermuteViewModule(torch.nn.Module):
485+
def forward(self, x: torch.Tensor) -> torch.Tensor:
486+
y = x.view(x.shape[0], 12, 64)
487+
y = y.permute(1, 0, 2)
488+
y = y.view(1, 12, x.shape[0], 64)
489+
return y.permute(0, 1, 3, 2)
490+
491+
exported_program = torch.export.export(
492+
DynamicPermuteViewModule(),
493+
(torch.randn(3, 1, 768),),
494+
dynamic_shapes={"x": {0: torch.export.Dim("batch", min=1, max=8)}},
495+
)
496+
edge_program = to_edge(
497+
exported_program,
498+
compile_config=EdgeCompileConfig(_check_ir_validity=False),
499+
)
500+
graph_module = edge_program.exported_program().graph_module
501+
502+
result = cast(
503+
PassResult,
504+
PostponePermuteOpBelowSqueezeOrUnsqueezeLikeView().call(graph_module),
505+
)
506+
507+
self.assertTrue(result.modified)
508+
self.assertEqual(
509+
count_node(result.graph_module, exir_ops.edge.aten.view_copy.default), 2
510+
)
511+
self.assertEqual(
512+
count_node(result.graph_module, exir_ops.edge.aten.permute_copy.default), 2
513+
)
514+
480515
def test_negative_not_squeeze_like(self) -> None:
481516
"""View that reshapes (not just squeeze/unsqueeze) should NOT be reordered."""
482517
builder = GraphBuilder()

‎exir/tensor.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
# pyre-ignore-all-errors[16]
1111
from __future__ import annotations
1212

13-
import copy
14-
1513
import math
1614
import typing
1715
from typing import Dict, List, NamedTuple, Optional, Tuple, Union
@@ -112,7 +110,7 @@ def stride_from_dim_order(sizes: List[int], dim_order: List[int]) -> List[int]:
112110
"""
113111
if len(sizes) == 0:
114112
return []
115-
strides = copy.deepcopy(sizes)
113+
strides = list(sizes)
116114
ndim = len(sizes)
117115
strides[dim_order[ndim - 1]] = 1
118116
for i in range(ndim - 2, -1, -1):

‎exir/tests/test_tensor.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,26 @@ def test_strides_from_dim_order(self) -> None:
388388
strides = stride_from_dim_order(sizes, dim_order)
389389
self.assertEqual(expected_strides, strides)
390390

391+
def test_strides_from_dim_order_with_symbolic_sizes(self) -> None:
392+
class ViewModule(torch.nn.Module):
393+
def forward(self, x: torch.Tensor) -> torch.Tensor:
394+
return x.view(x.shape[0], -1)
395+
396+
exported_program = torch.export.export(
397+
ViewModule(),
398+
(torch.randn(2, 3, 4),),
399+
dynamic_shapes={"x": {0: torch.export.Dim("batch", min=1, max=8)}},
400+
)
401+
placeholder = next(
402+
node
403+
for node in exported_program.graph_module.graph.nodes
404+
if node.op == "placeholder"
405+
)
406+
sizes = list(placeholder.meta["val"].shape)
407+
408+
self.assertIsInstance(sizes[0], torch.SymInt)
409+
self.assertEqual([12, 4, 1], stride_from_dim_order(sizes, [0, 1, 2]))
410+
391411
def test_num_bytes_from_shape_and_dtype(self) -> None:
392412
shape = (2, 3, 4)
393413
self.assertEqual(24, num_bytes_from_shape_and_dtype(shape, torch.int8))

0 commit comments

Comments
 (0)