Skip to content

Commit 27f1891

Browse files
authored
Update some remove ops passes to correctly set their modified bit
Differential Revision: D87895473 Pull Request resolved: pytorch#16065
1 parent 59caaf0 commit 27f1891

4 files changed

Lines changed: 143 additions & 214 deletions

File tree

‎backends/cadence/aot/pass_utils.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,12 @@ def call(self, graph_module: torch.fx.GraphModule) -> PassResult:
264264
lambda m: isinstance(m, torch.fx.GraphModule), graph_module.modules()
265265
):
266266
for node in module.graph.find_nodes(op="call_function", target=target):
267+
if len(node.users) == 0:
268+
# It is possible that maybe_remove_or_replace would have removed
269+
# this target by starting from a different target. In this case,
270+
# we should ignore it. If it wasn't erased, it will be handled
271+
# in eliminate_dead_code.
272+
continue
267273
changed |= self.maybe_remove_or_replace(node)
268274

269275
if changed:

‎backends/cadence/aot/remove_ops.py‎

Lines changed: 5 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@
2727
from executorch.backends.transforms.remove_clone_ops import RemoveCloneOpsTransform
2828
from executorch.exir.dialects._ops import ops as exir_ops
2929
from executorch.exir.dialects.edge._ops import EdgeOpOverload, EdgeOpOverloadPacket
30-
from executorch.exir.pass_base import ExportPass, NodeMetadata, PassResult, ProxyValue
30+
from executorch.exir.pass_base import ExportPass, PassResult
3131
from executorch.exir.pass_manager import PassManager, PassType
3232
from executorch.exir.passes import dead_code_elimination_pass
33-
from executorch.exir.passes.spec_prop_pass import SpecPropPass
34-
from torch.fx.node import Argument, Node
33+
from torch.fx.node import Node
3534

3635

3736
@register_cadence_pass(CadencePassAttribute(opt_level=0))
@@ -245,117 +244,6 @@ def maybe_remove_or_replace(self, node: Node) -> bool:
245244
return True
246245

247246

248-
@register_cadence_pass(CadencePassAttribute(opt_level=1))
249-
class RemoveNopSelectOpPass(ExportPass):
250-
"""
251-
A select op that selects from a dimension that is size 1 can be eliminated
252-
in a few cases. For example,
253-
```
254-
x = view (x, [1, 3, 16])
255-
y = select(x, 0, 0)
256-
z = add(m, y)
257-
```
258-
The special thing about this pattern is the add op, which allows
259-
broadcasting. So adding an operand with shape [3, 16] is the same as
260-
adding an operand with shape [1, 3, 16]. Therefore, if m has the same
261-
shape as x, then this select op is a nop, and can be eliminated:
262-
```
263-
x = view (x, [1, 3, 16])
264-
z = add(x, m)
265-
```
266-
"""
267-
268-
# A set of binary operators that could require broadcasting, and are
269-
# critical to this transformation if their operand is select op.
270-
binary_broadcast_ops: set[EdgeOpOverload] = {
271-
exir_ops.edge.aten.add.Tensor,
272-
exir_ops.edge.aten.mul.Tensor,
273-
exir_ops.edge.aten.div.Tensor,
274-
}
275-
276-
def __init__(self) -> None:
277-
super().__init__()
278-
self.op_sizes: dict[str, tuple[torch.Size, torch.Size]] = {}
279-
280-
# For select, view, or any op in binary_broadcast_ops, record the shapes of
281-
# input and output tensors.
282-
def call_operator(
283-
self,
284-
op, # pyre-ignore
285-
args: tuple[Argument, ...],
286-
kwargs: dict[str, Argument],
287-
meta: NodeMetadata,
288-
) -> ProxyValue:
289-
res = super().call_operator(op, args, kwargs, meta)
290-
# Unary ops: input and output
291-
if op in {
292-
exir_ops.edge.aten.select_copy.int,
293-
exir_ops.edge.aten.view_copy.default,
294-
}:
295-
arg0 = cast(ProxyValue, args[0])
296-
self.op_sizes[res.node.name] = (arg0.to_tensor().shape, meta["val"].shape)
297-
# Binary ops: two inputs, output shape can be inferred
298-
elif op in self.binary_broadcast_ops:
299-
arg0 = cast(ProxyValue, args[0])
300-
arg1 = cast(ProxyValue, args[1])
301-
self.op_sizes[res.node.name] = (
302-
arg0.to_tensor().shape,
303-
arg1.to_tensor().shape,
304-
)
305-
return res
306-
307-
# Eliminate nop select ops. We begin by inspecting the binary_broadcast_ops,
308-
# and check if their arg is a select op.
309-
def eliminate_nop_select_op(self, graph_module: torch.fx.GraphModule) -> None:
310-
for sel_node in graph_module.graph.nodes:
311-
# We are only interested in select ops
312-
if sel_node.target != exir_ops.edge.aten.select_copy.int:
313-
continue
314-
# The shape of the input/output operands for this select op should
315-
# have been precomputed.
316-
assert sel_node.name in self.op_sizes
317-
(sel_in_shape, sel_out_shape) = self.op_sizes[sel_node.name]
318-
# Get the select dimension
319-
sel_dim = (
320-
sel_node.args[1]
321-
if sel_node.args[1] >= 0
322-
else sel_node.args[1] + len(sel_in_shape)
323-
)
324-
# If the input size along select dimension is not 1, bail.
325-
if sel_in_shape[sel_dim] != 1:
326-
continue
327-
328-
# Get all the users of the select op that are either view, or
329-
# binary_broadcast_ops.
330-
users = [x for x in list(sel_node.users.keys()) if x.name in self.op_sizes]
331-
sel_in = sel_node.args[0]
332-
333-
# Iterate over the users of select op, and remove the use of the
334-
# select op in the user if feasible.
335-
for node in users:
336-
args = list(node.args)
337-
for idx, sel_arg in enumerate(args):
338-
# Check if the arg is the select op
339-
if sel_arg != sel_node:
340-
continue
341-
# If the input of select has the same shape as the other arg
342-
# of the binary op, the select op can be bypassed.
343-
if sel_in_shape == self.op_sizes[node.name][(idx + 1) % 2]:
344-
args[idx] = sel_in
345-
# update the node's args
346-
node.args = tuple(args)
347-
348-
graph_module.recompile()
349-
graph_module.graph.eliminate_dead_code()
350-
351-
def call(self, graph_module: torch.fx.GraphModule) -> PassResult:
352-
result = SpecPropPass()(graph_module)
353-
assert result is not None
354-
result = super().call(result.graph_module)
355-
self.eliminate_nop_select_op(result.graph_module)
356-
return result
357-
358-
359247
@register_cadence_pass(CadencePassAttribute(opt_level=1))
360248
class RemoveContiguousOpPass(RemoveOrReplacePassInterface):
361249
"""
@@ -533,10 +421,9 @@ class Subgraph:
533421
def call(self, graph_module: torch.fx.GraphModule) -> PassResult:
534422
subgraphs_found: list[RemovePermutesAroundElementwiseOps.Subgraph] = []
535423
processed_nodes: set[torch.fx.Node] = set()
536-
for node in graph_module.graph.nodes:
537-
if node.target != exir_ops.edge.aten.permute_copy.default:
538-
continue
539-
424+
for node in graph_module.graph.find_nodes(
425+
op="call_function", target=exir_ops.edge.aten.permute_copy.default
426+
):
540427
start_permute = self.get_permutation(node)
541428
# Expected end permutation for the subgraph.
542429
end_permute = [start_permute.index(i) for i in range(len(start_permute))]

‎backends/cadence/aot/replace_ops.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
register_cadence_pass,
3030
RemoveOrReplacePassInterface,
3131
)
32-
from executorch.backends.cadence.aot.remove_ops import RemoveNopSelectOpPass
3332
from executorch.backends.transforms.replace_scalar_with_tensor import (
3433
ReplaceScalarWithTensorArgPass,
3534
)
@@ -2503,7 +2502,6 @@ class CadenceReplaceOpsInGraph:
25032502
ReplacePermuteWithTransposePass,
25042503
ReplaceConvolutionOptionalArgsWithConcreteArgsPass,
25052504
ReplaceAddMMWithLinearPass,
2506-
RemoveNopSelectOpPass,
25072505
ReplacePadWithCatPass,
25082506
ReplaceConstantPadNdWithSlicePass,
25092507
ReplaceConvWithChannelLastConvPass,

0 commit comments

Comments
 (0)