Skip to content

Commit 53a39f6

Browse files
authored
Remove unnecessary graph rewrites for both cuda and whole ET pipeline (pytorch#21799)
### CUDA / AOTI backend path - Skip `run_decompositions()` during AOTI preprocessing when none of the backend decomposition table’s operators occur in the graph. - Remove the obsolete CUDA int64 floor-div rewrite pass, since AOTInductor now handles the original operation directly. ### Overall ExecuTorch export pipeline - Only do operator decomposition when the operator exists in original edge graph. ## Performance MG 30B cold export time improved from **26:06 to 21:16.45**, an **18.5% reduction**.
1 parent c0260ec commit 53a39f6

6 files changed

Lines changed: 135 additions & 7 deletions

File tree

‎backends/aoti/aoti_backend.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from executorch.exir._warnings import experimental
2121
from executorch.exir.backend.backend_details import ExportedProgram, PreprocessResult
2222
from executorch.exir.backend.compile_spec_schema import CompileSpec
23+
from executorch.exir.graph_module import contains_any_call_fn_target_op
2324
from torch._inductor.codegen.cpp_wrapper_cpu import CppWrapperCpu
2425
from torch.export.passes import move_to_device_pass
2526

@@ -248,8 +249,13 @@ def preprocess(
248249
else:
249250
custom_pass(device_edge_program.graph_module)
250251

251-
# Run decompositions if any
252-
if decomposition_table:
252+
# ``run_decompositions`` retraces the complete ExportedProgram even
253+
# when none of the table's operators occur in the graph. The in-place
254+
# passes above preserve graph inputs and outputs, so the existing graph
255+
# signature remains valid when the decomposition table cannot apply.
256+
if contains_any_call_fn_target_op(
257+
device_edge_program.graph_module, decomposition_table
258+
):
253259
device_edge_program = device_edge_program.run_decompositions(
254260
decomposition_table
255261
)

‎backends/cuda/passes/replace_int64_floordiv.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
Graph Transformation Pass for Integer Floor-Division Replacement.
99
1010
Rewrites integer (int64/int32) floor-division into a float64-domain floor to
11-
work around a torch-2.12 AOTInductor/Inductor CUDA miscompile:
11+
work around an AOTInductor/Inductor CUDA miscompile that is still present as
12+
of PyTorch 2.13:
1213
1314
floor_divide(a, b) -> floor(a.to(float64) / b.to(float64)).to(orig_int_dtype)
1415
"""
@@ -36,7 +37,7 @@
3637

3738

3839
class ReplaceInt64FloorDivWithFloatPass(PassBase):
39-
# Work around a torch-2.12 AOTInductor/Inductor CUDA miscompile of integer
40+
# Work around an AOTInductor/Inductor CUDA miscompile of integer
4041
# (int64) floor-division: fused/broadcast int64 floor_divide is mis-lowered
4142
# (truncation instead of floor; cross-division term bleed under dynamic shapes).
4243
# TODO(gasoonjia): remove this pass once the upstream issue solved.

‎backends/cuda/passes/tests/test_replace_int64_floordiv.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import unittest
88

99
import torch
10+
from backends.cuda.cuda_backend import CudaBackend
1011
from backends.cuda.passes.replace_int64_floordiv import (
1112
ReplaceInt64FloorDivWithFloatPass,
1213
)
@@ -47,6 +48,14 @@ def _count_int_floordiv(graph_module) -> int:
4748
class TestReplaceInt64FloorDivWithFloatPass(unittest.TestCase):
4849
"""Test the ReplaceInt64FloorDivWithFloatPass transformation pass."""
4950

51+
def test_cuda_backend_keeps_floor_div_workaround_registered(self):
52+
"""The rewrite must remain in the CUDA backend pass pipeline."""
53+
passes = CudaBackend.get_custom_passes([])
54+
self.assertIn(
55+
"ReplaceInt64FloorDivWithFloatPass",
56+
{type(pass_).__name__ for pass_ in passes},
57+
)
58+
5059
def _edge_gm(self, module, inputs):
5160
ep = to_edge(export(module, inputs, strict=True))
5261
return ep, ep.exported_program().graph_module

‎exir/graph_module.py‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# pyre-strict
99

1010
from types import FunctionType as function
11-
from typing import Callable, Dict, List, Tuple, Union
11+
from typing import Any, Callable, Collection, Dict, List, Tuple, Union
1212

1313
import torch
1414
from torch._ops import HigherOrderOperator
@@ -151,3 +151,31 @@ def bfs_trace_with_node_process(
151151
for _, submodule, _ in get_control_flow_submodules(current_graph_module)
152152
]
153153
queue.extend(control_flow_submodules)
154+
155+
156+
def contains_any_call_fn_target_op(
157+
graph_module: torch.fx.GraphModule,
158+
ops: Collection[Any],
159+
) -> bool:
160+
"""Return whether ``graph_module`` or a control-flow subgraph uses ``ops``."""
161+
if not ops:
162+
return False
163+
164+
queue = [graph_module]
165+
while queue:
166+
current_graph_module = queue.pop(0)
167+
for node in current_graph_module.graph.nodes:
168+
if node.op != "call_function":
169+
continue
170+
# EdgeOpOverload wraps the original ATen overload in ``_op``.
171+
# Decomposition tables are keyed by the latter.
172+
target = node.target
173+
if not isinstance(target, torch._ops.OpOverload):
174+
target = getattr(target, "_op", target)
175+
if target in ops:
176+
return True
177+
queue.extend(
178+
submodule
179+
for _, submodule, _ in get_control_flow_submodules(current_graph_module)
180+
)
181+
return False

‎exir/program/_program.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@
3333
from executorch.exir.emit import emit_program, EmitterOutput
3434
from executorch.exir.emit._emitter import _DelegateDebugIdentifierMap
3535
from executorch.exir.error import ExportError
36-
from executorch.exir.graph_module import get_control_flow_submodules
36+
from executorch.exir.graph_module import (
37+
contains_any_call_fn_target_op,
38+
get_control_flow_submodules,
39+
)
3740
from executorch.exir.operator.convert import _pybind_schema_to_native_schema
3841
from executorch.exir.operator.util import _QUANT_PRIMITIVES
3942
from executorch.exir.pass_base import PassBase
@@ -1166,7 +1169,12 @@ def _gen_edge_manager_for_partitioners(
11661169
if table.pop(op, None) is not None:
11671170
ops_needing_preservation.append(op)
11681171

1169-
program = program.run_decompositions(table)
1172+
# The initial ``run_decompositions({})`` above has already
1173+
# functionalized the graph. This second call only applies
1174+
# operator decompositions from the remaining table, so it is
1175+
# safe to skip when none of those targets occur in the graph.
1176+
if contains_any_call_fn_target_op(program.graph_module, table):
1177+
program = program.run_decompositions(table)
11701178
final_ops_to_preserve.update(ops_needing_preservation)
11711179
else:
11721180
# EDGE_DO_NOT_DECOMP path for the partitioner

‎exir/tests/test_graph_module.py‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.exir.graph_module import contains_any_call_fn_target_op
11+
12+
13+
class TestContainsAnyCallFnTargetOp(unittest.TestCase):
14+
@staticmethod
15+
def _add_graph() -> tuple[torch.fx.GraphModule, torch.fx.Node]:
16+
graph = torch.fx.Graph()
17+
lhs = graph.placeholder("lhs")
18+
rhs = graph.placeholder("rhs")
19+
add = graph.call_function(torch.ops.aten.add.Tensor, (lhs, rhs))
20+
graph.output(add)
21+
return torch.fx.GraphModule({}, graph), add
22+
23+
def test_matches_aten_op(self) -> None:
24+
graph_module, _ = self._add_graph()
25+
26+
self.assertTrue(
27+
contains_any_call_fn_target_op(graph_module, {torch.ops.aten.add.Tensor})
28+
)
29+
self.assertFalse(
30+
contains_any_call_fn_target_op(graph_module, {torch.ops.aten.mul.Tensor})
31+
)
32+
33+
def test_matches_wrapped_edge_op(self) -> None:
34+
graph_module, add = self._add_graph()
35+
36+
class EdgeOp:
37+
_op = torch.ops.aten.add.Tensor
38+
39+
add.target = EdgeOp()
40+
41+
self.assertTrue(
42+
contains_any_call_fn_target_op(graph_module, {torch.ops.aten.add.Tensor})
43+
)
44+
self.assertFalse(
45+
contains_any_call_fn_target_op(graph_module, {torch.ops.aten.mul.Tensor})
46+
)
47+
48+
def test_matches_op_in_control_flow_submodule(self) -> None:
49+
true_graph, _ = self._add_graph()
50+
false_graph, _ = self._add_graph()
51+
52+
graph = torch.fx.Graph()
53+
pred = graph.placeholder("pred")
54+
lhs = graph.placeholder("lhs")
55+
rhs = graph.placeholder("rhs")
56+
true_branch = graph.get_attr("true_graph")
57+
false_branch = graph.get_attr("false_graph")
58+
result = graph.call_function(
59+
torch.ops.higher_order.cond,
60+
(pred, true_branch, false_branch, [lhs, rhs]),
61+
)
62+
graph.output(result)
63+
graph_module = torch.fx.GraphModule(
64+
{"true_graph": true_graph, "false_graph": false_graph}, graph
65+
)
66+
67+
self.assertTrue(
68+
contains_any_call_fn_target_op(graph_module, {torch.ops.aten.add.Tensor})
69+
)
70+
self.assertFalse(
71+
contains_any_call_fn_target_op(graph_module, {torch.ops.aten.mul.Tensor})
72+
)
73+
74+
75+
if __name__ == "__main__":
76+
unittest.main()

0 commit comments

Comments
 (0)