Skip to content

Commit 6faf63e

Browse files
authored
Do not move a view's shape earlier than the nodes that compute it (pytorch#22403)
### Summary `_merge_view_copy_chains` gives the first view in a chain the shape of the last one: ```python final_shape = view_nodes_to_remove[-1].args[1] node.args = (node.args[0], final_shape) ``` Under dynamic shapes a view's shape is not a list of ints, it is a list that can contain nodes computing symbolic sizes. Those nodes may be defined anywhere between the first and the last view, so assigning the later shape to the earlier node can make that node read a value that does not exist yet. The result is a graph that fails fx's own `lint()`, which the pass then hits itself on the very next line via `eliminate_dead_code()`: ``` RuntimeError: Argument 'mul' of Node 'aten_view_copy_default_21' was used before it has been defined! Please check that Nodes in the graph are topologically ordered ``` I hit this lowering a TTS model to Vulkan. The chain ends on a `view_copy(x, [2*s, 512])` whose `2*s` is produced exactly one node after the first view of the chain, so the rewrite moves the shape one position too far back. The submodule handed to the backend is correctly ordered; this pass is what breaks it. ### Fix Fuse the longest prefix of the chain whose shape is available where the first view sits, and leave the rest alone. Chains with static shapes, which is the common case, keep fusing exactly as before. ### Test plan New `backends/transforms/test/test_fuse_view_copy.py`: - a static view chain still collapses to a single view - a chain whose final shape is computed after the first view leaves the graph topologically ordered (asserted with `graph.lint()`) The second test fails on the current pass and passes with this change; the first passes either way, so the existing fusion is not narrowed for static shapes. cc @cccclai
1 parent b0883bd commit 6faf63e

2 files changed

Lines changed: 118 additions & 0 deletions

File tree

‎backends/transforms/fuse_view_copy.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,26 @@ def _find_view_copy_chain(
9090

9191
return view_nodes
9292

93+
@staticmethod
94+
def _shape_is_available_at(
95+
shape: object,
96+
node: torch.fx.Node,
97+
order: dict[torch.fx.Node, int],
98+
) -> bool:
99+
"""Whether every element of ``shape`` is already defined before ``node``.
100+
101+
A view's shape is not always a list of ints: under dynamic shapes a
102+
dimension is a node computing a symbolic size. Giving ``node`` the shape
103+
of a later view moves that shape's arguments backwards in the graph, so
104+
it is only valid when they are available at ``node`` to begin with.
105+
"""
106+
if not isinstance(shape, (list, tuple)):
107+
return True
108+
node_pos = order[node]
109+
return all(
110+
order[dim] < node_pos for dim in shape if isinstance(dim, torch.fx.Node)
111+
)
112+
93113
def _merge_view_copy_chains(
94114
self, graph: torch.fx.Graph
95115
) -> tuple[torch.fx.Graph, bool]:
@@ -110,9 +130,20 @@ def _merge_view_copy_chains(
110130
"""
111131
modified = False
112132
ops: list[EdgeOpOverload] = self.UNARY_ELEMENTWISE_OPS + [self.VIEW_OP]
133+
# Nothing below inserts or moves nodes, so a single ordering snapshot
134+
# stays valid for the whole sweep.
135+
order = {n: i for i, n in enumerate(graph.nodes)}
113136
for node in graph.find_nodes(op="call_function", target=self.VIEW_OP):
114137
view_nodes_to_remove = self._find_view_copy_chain(node, ops)
115138

139+
# Fuse the longest prefix of the chain whose shape `node` can
140+
# actually take on; a later view whose shape is computed after
141+
# `node` has to stay where it is.
142+
while view_nodes_to_remove and not self._shape_is_available_at(
143+
view_nodes_to_remove[-1].args[1], node, order
144+
):
145+
view_nodes_to_remove.pop()
146+
116147
if len(view_nodes_to_remove) > 0:
117148
modified = True
118149

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
import unittest
8+
9+
import torch
10+
from executorch.backends.transforms.fuse_view_copy import FuseViewCopyTransform
11+
from executorch.exir import to_edge
12+
from executorch.exir.dialects._ops import ops as exir_ops
13+
14+
15+
class TestFuseViewCopyTransform(unittest.TestCase):
16+
def _fuse(self, model, example_inputs, dynamic_shapes=None):
17+
ep = torch.export.export(
18+
model, example_inputs, dynamic_shapes=dynamic_shapes, strict=False
19+
)
20+
program = to_edge(ep).exported_program()
21+
gm = FuseViewCopyTransform()(program.graph_module).graph_module
22+
# lint() is what catches a node whose argument is defined after it.
23+
gm.graph.lint()
24+
return gm.graph
25+
26+
@staticmethod
27+
def _count_views(graph: torch.fx.Graph) -> int:
28+
return sum(
29+
1
30+
for n in graph.nodes
31+
if n.op == "call_function"
32+
and n.target == exir_ops.edge.aten.view_copy.default
33+
)
34+
35+
def test_static_view_chain_is_fused(self):
36+
class Model(torch.nn.Module):
37+
def forward(self, x):
38+
return x.view(4, 8).relu().view(32).sqrt().view(2, 16)
39+
40+
graph = self._fuse(Model().eval(), (torch.rand(2, 16) + 1.0,))
41+
self.assertEqual(self._count_views(graph), 1)
42+
43+
def test_chain_ending_in_a_later_computed_shape_stays_ordered(self):
44+
# The final view's shape is only known after `y` has been reduced, so
45+
# the first view cannot take that shape on: it runs earlier.
46+
class Model(torch.nn.Module):
47+
def forward(self, x):
48+
n = x.shape[0]
49+
y = x.view(n * 4, 8).relu()
50+
return y.view(n * 2, 16) + float(0)
51+
52+
dim = torch.export.Dim("n", min=2, max=64)
53+
graph = self._fuse(
54+
Model().eval(),
55+
(torch.rand(8, 32),),
56+
dynamic_shapes={"x": {0: dim}},
57+
)
58+
# The chain is one view long and its shape is unavailable, so nothing
59+
# fuses and both views stay.
60+
self.assertEqual(self._count_views(graph), 2)
61+
62+
def test_chain_fuses_up_to_the_first_unavailable_shape(self):
63+
# Three views. The middle shape reuses `n * 4`, computed before the
64+
# first view, so it can move onto it. The last needs `n * 2`, computed
65+
# only after the first view runs, so it has to stay put. Fusing the
66+
# whole chain would drag `n * 2` backwards past its definition.
67+
class Model(torch.nn.Module):
68+
def forward(self, x):
69+
n = x.shape[0]
70+
a = x.view(n * 4, 8)
71+
b = a.relu()
72+
c = b.view(n * 4, 4, 2)
73+
d = c.sqrt()
74+
return d.view(n * 2, 16) + float(0)
75+
76+
dim = torch.export.Dim("n", min=2, max=64)
77+
graph = self._fuse(
78+
Model().eval(),
79+
(torch.rand(8, 32) + 1.0,),
80+
dynamic_shapes={"x": {0: dim}},
81+
)
82+
# The middle view folded into the first; the last one survives.
83+
self.assertEqual(self._count_views(graph), 2)
84+
85+
86+
if __name__ == "__main__":
87+
unittest.main()

0 commit comments

Comments
 (0)