Skip to content

Commit 438bc2e

Browse files
authored
Canonicalize squeeze/unsqueeze to view_copy (pytorch#21605)
Differential Revision: D114508253 Pull Request resolved: pytorch#21605
1 parent 32e164a commit 438bc2e

7 files changed

Lines changed: 347 additions & 161 deletions

File tree

‎backends/cadence/aot/BUCK‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ fbcode_target(_kind = runtime.python_library,
297297
"//executorch/backends/cadence/aot:simplify_ops",
298298
"//executorch/backends/transforms:remove_clone_ops",
299299
"//executorch/backends/transforms:remove_permutes_around_elementwise_ops",
300+
"//executorch/backends/transforms:replace_squeeze_unsqueeze_with_view",
300301
"//executorch/exir:pass_base",
301302
"//executorch/exir/dialects:lib",
302303
"//executorch/exir/dialects/edge:lib",

‎backends/cadence/aot/remove_ops.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
from executorch.backends.transforms.remove_permutes_around_elementwise_ops import (
2525
RemovePermutesAroundElementwiseOps as _SharedRemovePermutesAroundElementwiseOps,
2626
)
27+
from executorch.backends.transforms.replace_squeeze_unsqueeze_with_view import (
28+
ReplaceSqueezeAndUnsqueezeWithViewPass as _SharedReplaceSqueezeAndUnsqueezeWithViewPass,
29+
)
2730
from executorch.exir.dialects._ops import ops as exir_ops
2831
from executorch.exir.dialects.edge._ops import EdgeOpOverload, EdgeOpOverloadPacket
2932
from executorch.exir.pass_base import (
@@ -590,6 +593,12 @@ def maybe_remove_or_replace(self, node: Node) -> bool:
590593
return True
591594

592595

596+
class ReplaceSqueezeAndUnsqueezeWithViewPassImported(
597+
_SharedReplaceSqueezeAndUnsqueezeWithViewPass
598+
):
599+
pass
600+
601+
593602
class RemovePermutesAroundElementwiseOps(_SharedRemovePermutesAroundElementwiseOps):
594603
def __init__(self) -> None:
595604
super().__init__(
@@ -840,6 +849,9 @@ def maybe_remove_or_replace(self, node: Node) -> bool:
840849

841850
class CommonRemovePasses:
842851
passes: List[Type[ExportPass]] = [
852+
# Canonicalise squeeze/unsqueeze to view_copy first: the nop-view and
853+
# permute passes below both reason about view_copy only.
854+
ReplaceSqueezeAndUnsqueezeWithViewPassImported,
843855
RemoveAliasCopyOpPass,
844856
RemoveNopExpandOpPass,
845857
RemoveNopSliceOrViewOpPass,

‎backends/cadence/aot/replace_ops.py‎

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
from executorch.backends.transforms.replace_scalar_with_tensor import (
3333
ReplaceScalarWithTensorArgPass,
3434
)
35+
from executorch.backends.transforms.replace_squeeze_unsqueeze_with_view import (
36+
ReplaceSqueezeAndUnsqueezeWithViewPass as _SharedReplaceSqueezeAndUnsqueezeWithViewPass,
37+
)
3538
from executorch.exir.dialects._ops import ops as exir_ops
3639
from executorch.exir.dialects.edge._ops import EdgeOpOverload
3740
from executorch.exir.pass_base import ExportPass, PassResult
@@ -184,40 +187,10 @@ def maybe_remove_or_replace(self, node: torch.fx.Node) -> bool:
184187
return True
185188

186189

187-
class ReplaceSqueezeAndUnsqueezeWithViewPass(RemoveOrReplacePassInterface):
188-
"""
189-
When the shape is static, replace squeeze_copy and unsqueeze_copy ops with
190-
view_copy op
191-
"""
192-
193-
@property
194-
def targets(self) -> list[EdgeOpOverload]:
195-
return [
196-
exir_ops.edge.aten.squeeze_copy.default,
197-
exir_ops.edge.aten.squeeze_copy.dim,
198-
exir_ops.edge.aten.squeeze_copy.dims,
199-
exir_ops.edge.aten.unsqueeze_copy.default,
200-
]
201-
202-
def maybe_remove_or_replace(self, node: torch.fx.Node) -> bool:
203-
# Get the output tensor shape
204-
out_shape = node.meta["val"].shape
205-
206-
# Bail out if any dim is not an int (dynamic shape)
207-
for dim in list(out_shape):
208-
if not isinstance(dim, int):
209-
return False
210-
211-
# Replace with view op with the new shape
212-
with node.graph.inserting_before(node):
213-
new_node = node.graph.call_function(
214-
exir_ops.edge.aten.view_copy.default,
215-
args=(node.args[0], list(out_shape)),
216-
)
217-
# Do not remove the metadata copy!
218-
new_node.meta = node.meta
219-
node.replace_all_uses_with(new_node)
220-
return True
190+
class ReplaceSqueezeAndUnsqueezeWithViewPass(
191+
_SharedReplaceSqueezeAndUnsqueezeWithViewPass
192+
):
193+
pass
221194

222195

223196
class ReplaceFunctionallyEquivalentOpTargets(RemoveOrReplacePassInterface):

‎backends/transforms/remove_permutes_around_elementwise_ops.py‎

Lines changed: 101 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -78,49 +78,71 @@ def __init__(self, extra_permutable_ops: set | None = None) -> None:
7878
exir_ops.edge.aten.view.default,
7979
)
8080

81-
_UNSQUEEZE_OPS = (exir_ops.edge.aten.unsqueeze_copy.default,)
81+
@staticmethod
82+
def _concrete_shape(node: torch.fx.Node) -> list[int] | None:
83+
"""Return a node's shape, or None if it is missing or symbolic."""
84+
val = node.meta.get("val")
85+
if val is None:
86+
return None
87+
shape = val.shape
88+
if not all(isinstance(d, int) for d in shape):
89+
return None
90+
return [int(d) for d in shape]
8291

83-
_SQUEEZE_OPS = (exir_ops.edge.aten.squeeze_copy.dim,)
92+
def _view_shapes(self, node: torch.fx.Node) -> tuple[list[int], list[int]] | None:
93+
"""Concrete (input, output) shapes of a view op, else None."""
94+
if node.target not in self._VIEW_OPS:
95+
return None
96+
inp = node.args[0] if node.args else None
97+
if not isinstance(inp, torch.fx.Node):
98+
return None
99+
in_shape = self._concrete_shape(inp)
100+
out_shape = self._concrete_shape(node)
101+
if in_shape is None or out_shape is None:
102+
return None
103+
return in_shape, out_shape
84104

85105
_PAD_OPS = (
86106
exir_ops.edge.aten.constant_pad_nd.default,
87107
exir_ops.edge.aten.pad.default,
88108
)
89109

90110
@staticmethod
91-
def _find_extra_one(longer: list[int], shorter: list[int]) -> int:
92-
"""If longer has exactly one more element of value 1, return its index. Else -1."""
93-
if len(longer) != len(shorter) + 1:
94-
return -1
95-
for i in range(len(shorter)):
96-
if longer[i] != shorter[i]:
97-
if longer[i] == 1 and shorter[i:] == longer[i + 1 :]:
98-
return i
99-
return -1
100-
return len(shorter) if longer[-1] == 1 else -1
111+
def _find_extra_ones(longer: list[int], shorter: list[int]) -> list[int] | None:
112+
"""Positions in ``longer`` whose removal yields ``shorter``.
113+
114+
Every such position must hold a size-1 dim, so the two shapes differ by
115+
unit dims alone. Returns None when no such set of positions exists.
116+
"""
117+
extra: list[int] = []
118+
j = 0
119+
for i, dim in enumerate(longer):
120+
if j < len(shorter) and dim == shorter[j]:
121+
j += 1
122+
continue
123+
if dim != 1:
124+
return None
125+
extra.append(i)
126+
return extra if j == len(shorter) else None
101127

102128
def _is_squeeze_unsqueeze_view(self, node: torch.fx.Node) -> bool:
103-
"""Check if a node is a squeeze, unsqueeze, or view_copy that only
104-
adds or removes a single dim of size 1."""
129+
"""Check if a node is a view_copy that only adds or removes dims of
130+
size 1."""
105131
if node in self._sq_unsq_cache:
106132
return self._sq_unsq_cache[node]
107133
result = self._check_squeeze_unsqueeze_view(node)
108134
self._sq_unsq_cache[node] = result
109135
return result
110136

111137
def _check_squeeze_unsqueeze_view(self, node: torch.fx.Node) -> bool:
112-
if node.target in self._UNSQUEEZE_OPS or node.target in self._SQUEEZE_OPS:
113-
return True
114-
if node.target not in self._VIEW_OPS:
138+
shapes = self._view_shapes(node)
139+
if shapes is None:
115140
return False
116-
inp = node.args[0]
117-
assert isinstance(inp, torch.fx.Node)
118-
in_shape = inp.meta["val"].shape
119-
out_shape = node.meta["val"].shape
120-
if len(out_shape) == len(in_shape) + 1:
121-
return self._find_extra_one(out_shape, in_shape) != -1
122-
if len(in_shape) == len(out_shape) + 1:
123-
return self._find_extra_one(in_shape, out_shape) != -1
141+
in_shape, out_shape = shapes
142+
if len(out_shape) > len(in_shape):
143+
return self._find_extra_ones(out_shape, in_shape) is not None
144+
if len(in_shape) > len(out_shape):
145+
return self._find_extra_ones(in_shape, out_shape) is not None
124146
return False
125147

126148
def _is_permutation_sink_view(self, node: torch.fx.Node) -> bool:
@@ -151,71 +173,45 @@ def _adapt_permute_across_view(
151173
Adapts from input-rank to output-rank space (downstream direction).
152174
Returns the adjusted permutation, or None if not possible.
153175
"""
154-
inp = node.args[0]
155-
assert isinstance(inp, torch.fx.Node)
156-
inp_val = inp.meta.get("val")
157-
if inp_val is None:
176+
shapes = self._view_shapes(node)
177+
if shapes is None:
158178
return None
159-
in_shape = inp_val.shape
179+
in_shape, out_shape = shapes
160180
# ``permute`` must live in the view's input-rank space. It does not when
161181
# the view is reached by upstream traversal, where the permutation is
162-
# expressed at the view's output rank; bail out so the caller drops the
163-
# subgraph instead of indexing out of range or silently mis-adapting.
182+
# expressed at the view's output rank.
164183
if len(permute) != len(in_shape):
165184
return None
166185

167-
# Handle explicit unsqueeze_copy(dim)
168-
if node.target in self._UNSQUEEZE_OPS:
169-
dim = cast(int, node.args[1])
170-
rank = len(permute)
171-
index = dim if dim >= 0 else dim + rank + 1
172-
# `index` is a POSITION in the permuted output; the un-permuted
173-
# position the new dim lands at is the permutation VALUE there
174-
# (or the end, when appending). permute_subgraph rewrites the dim
175-
# arg to exactly this value, so the two must agree -- using `index`
176-
# here instead silently desynchronises them whenever
177-
# permute[index] != index.
178-
inserted_value = permute[index] if index < rank else rank
179-
new_perm = [x + 1 if x >= inserted_value else x for x in permute]
180-
new_perm.insert(index, inserted_value)
186+
if len(out_shape) > len(in_shape):
187+
# unsqueeze: insert an identity mapping at each added position.
188+
# Positions are ascending and index into the output, so inserting
189+
# them in order lands each one at its final index.
190+
positions = self._find_extra_ones(out_shape, in_shape)
191+
if positions is None:
192+
return None
193+
new_perm = list(permute)
194+
for index in positions:
195+
new_perm = [x + 1 if x >= index else x for x in new_perm]
196+
new_perm.insert(index, index)
181197
return new_perm
182198

183-
# Handle explicit squeeze_copy(dim)
184-
if node.target in self._SQUEEZE_OPS:
185-
dim = cast(int, node.args[1])
186-
rank = len(permute)
187-
index = dim if dim >= 0 else dim + rank
188-
# index is a POSITION in the tensor; the permutation VALUE at
199+
if len(in_shape) > len(out_shape):
200+
positions = self._find_extra_ones(in_shape, out_shape)
201+
# Positions index into the node's input rank, which can differ from
202+
# the permutation's rank when the view is reached via upstream
203+
# traversal after an earlier rank change.
204+
if positions is None or positions[-1] >= len(permute):
205+
return None
206+
# A position is a POSITION in the tensor; the permutation VALUE at
189207
# that position is the logical dim being removed.
190-
squeezed_value = permute[index]
191-
new_perm = [
192-
x - 1 if x > squeezed_value else x
208+
squeezed_values = {permute[p] for p in positions}
209+
return [
210+
x - sum(1 for v in squeezed_values if v < x)
193211
for x in permute
194-
if x != squeezed_value
212+
if x not in squeezed_values
195213
]
196-
return new_perm
197214

198-
# Handle view_copy (squeeze/unsqueeze-like reshape)
199-
out_shape = node.meta["val"].shape
200-
201-
if len(out_shape) == len(in_shape) + 1:
202-
# unsqueeze: insert identity mapping at the new dim
203-
index = self._find_extra_one(out_shape, in_shape)
204-
new_perm = [x + 1 if x >= index else x for x in permute]
205-
new_perm.insert(index, index)
206-
return new_perm
207-
elif len(in_shape) == len(out_shape) + 1:
208-
# squeeze via view_copy: find the squeezed dim and remove it
209-
index = self._find_extra_one(in_shape, out_shape)
210-
# index is a POSITION in in_shape; the permutation VALUE at
211-
# that position is the logical dim being removed.
212-
squeezed_value = permute[index]
213-
new_perm = [
214-
x - 1 if x > squeezed_value else x
215-
for x in permute
216-
if x != squeezed_value
217-
]
218-
return new_perm
219215
return None
220216

221217
def call(self, graph_module: torch.fx.GraphModule) -> PassResult: # noqa: C901
@@ -377,12 +373,13 @@ def visit( # noqa: C901
377373
return False
378374
elif user.op == "output":
379375
return False
380-
elif not self._is_squeeze_unsqueeze_view(
381-
user
382-
) and self._is_permutation_sink_view(user):
376+
elif self._is_permutation_sink_view(user):
383377
# The permutation dies at this reshape (see
384378
# _is_permutation_sink_view), so terminate the region here with
385379
# no compensating permute and no further downstream traversal.
380+
# Checked before the rank-change handling below: a sink always
381+
# terminates cleanly, whereas crossing it would leave the region
382+
# hunting for an end permute that layout-invariance made moot.
386383
continue
387384
elif not self.visit(
388385
user, subgraph, processed_nodes, downstream_end, downstream_start
@@ -526,20 +523,6 @@ def permute_subgraph(self, subgraph: Subgraph) -> bool: # noqa: C901
526523
self.update_pad(node, node_start_perm)
527524
elif node.target in self._VIEW_OPS:
528525
self.update_view_copy(node, node_start_perm)
529-
elif node.target in self._UNSQUEEZE_OPS:
530-
# unsqueeze dim is in output space (rank + 1)
531-
dim = cast(int, node.args[1])
532-
rank = len(node_start_perm)
533-
index = dim if dim >= 0 else dim + rank + 1
534-
if index < rank:
535-
node.update_arg(1, node_start_perm[index])
536-
else:
537-
# Inserting at or beyond existing dims — position unchanged
538-
node.update_arg(1, index)
539-
elif node.target in self._SQUEEZE_OPS:
540-
# squeeze dim is in input space (rank)
541-
dim = get_arg(node, "dim", int)
542-
set_arg(node, "dim", node_start_perm[dim])
543526

544527
# Skip incoming permutes.
545528
for inp, out in subgraph.edges_in:
@@ -649,34 +632,35 @@ def update_view_copy(self, node: torch.fx.Node, start_permute: list[int]) -> Non
649632
After removing the start permute, the view's input is in the original
650633
(un-permuted) layout. Recompute the view's target shape accordingly.
651634
"""
652-
inp = node.args[0]
653-
assert isinstance(inp, torch.fx.Node)
654-
655-
in_shape = inp.meta["val"].shape
656-
out_shape = node.meta["val"].shape
635+
shapes = self._view_shapes(node)
636+
if shapes is None:
637+
return
638+
in_shape, out_shape = shapes
657639

658640
# Compute un-permuted input shape
659641
inverse_permute = [start_permute.index(i) for i in range(len(start_permute))]
660642
unpermuted_in = [in_shape[inverse_permute[i]] for i in range(len(in_shape))]
661643

662-
if len(out_shape) == len(in_shape) + 1:
663-
# unsqueeze: find the inserted dim in the permuted output,
664-
# then determine where it goes in the un-permuted layout
665-
index = self._find_extra_one(out_shape, in_shape)
666-
if index != -1:
667-
new_shape = list(unpermuted_in)
644+
if len(out_shape) > len(in_shape):
645+
# unsqueeze: the added unit dims keep their output positions in the
646+
# un-permuted layout too
647+
positions = self._find_extra_ones(out_shape, in_shape)
648+
if positions is None:
649+
return
650+
new_shape = list(unpermuted_in)
651+
for index in positions:
668652
new_shape.insert(index, 1)
669-
node.update_arg(1, new_shape)
670-
elif len(in_shape) == len(out_shape) + 1:
671-
# squeeze: find the removed dim in the permuted input,
672-
# map it to the un-permuted position, and remove it
673-
index = self._find_extra_one(in_shape, out_shape)
674-
if index != -1:
675-
# Map the squeezed dim from permuted to un-permuted space
676-
unpermuted_index = start_permute[index]
677-
new_shape = list(unpermuted_in)
678-
del new_shape[unpermuted_index]
679-
node.update_arg(1, new_shape)
653+
node.update_arg(1, new_shape)
654+
elif len(in_shape) > len(out_shape):
655+
# squeeze: map each removed dim from permuted to un-permuted space,
656+
# deleting from the back so earlier indices stay valid
657+
positions = self._find_extra_ones(in_shape, out_shape)
658+
if positions is None or positions[-1] >= len(start_permute):
659+
return
660+
new_shape = list(unpermuted_in)
661+
for index in sorted((start_permute[p] for p in positions), reverse=True):
662+
del new_shape[index]
663+
node.update_arg(1, new_shape)
680664

681665
def get_permutation(self, permute_node: torch.fx.Node) -> list[int] | None:
682666
assert permute_node.target == exir_ops.edge.aten.permute_copy.default

0 commit comments

Comments
 (0)