Skip to content

Commit 2e873cd

Browse files
authored
Add reinplace pass (pytorch#22258)
Differential Revision: D113936371 Pull Request resolved: pytorch#22258
1 parent 60ec827 commit 2e873cd

7 files changed

Lines changed: 165 additions & 9 deletions

File tree

‎backends/native/BUCK‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ fbcode_target(
1717
"__init__.py",
1818
"partitioner.py",
1919
"passes/__init__.py",
20+
"passes/reinplace.py",
2021
"preprocess.py",
2122
"serialization/__init__.py",
2223
"serialization/graph_serialize.py",

‎backends/native/partitioner.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@
99
1010
Claims core ATen ops (torch.Tag.core) plus an explicit opt-in set, and delegates
1111
whole torch.cond control-flow ops whose branches are fully supported. Graph
12-
cleanup (CSE) runs before lowering via transform_passes, since ExecuTorch forbids
13-
a partitioner from mutating the graph module.
12+
cleanup (CSE, reinplace) runs before lowering via transform_passes, since
13+
ExecuTorch forbids a partitioner from mutating the graph module.
1414
"""
1515

1616
from typing import Callable, final, List, Mapping, Optional, Tuple
1717

1818
import torch
19+
from executorch.backends.native.passes import backend_inplace_aten_variants
1920

2021
from executorch.exir.backend.compile_spec_schema import CompileSpec
22+
2123
from executorch.exir.backend.partitioner import (
2224
DelegationSpec,
2325
Partitioner,
@@ -52,7 +54,7 @@
5254

5355

5456
class NativeSupportedOperators(OperatorSupportBase):
55-
_NON_CORE = set(_SUPPORTED_NON_CORE_OPS)
57+
_NON_CORE = set(_SUPPORTED_NON_CORE_OPS) | backend_inplace_aten_variants()
5658

5759
def is_node_supported(
5860
self, submodules: Mapping[str, torch.nn.Module], node: Node

‎backends/native/passes/__init__.py‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,56 @@
77
"""
88
Graph transformation passes for the native backend.
99
10-
Passed to ``to_edge_transform_and_lower`` via its ``transform_passes`` argument so
11-
they run on the edge-dialect graph before partitioning/delegation. Each pass lives
12-
in its own module; this package aggregates them and exposes the default pass list.
10+
These are intended to be passed to ``to_edge_transform_and_lower`` via its
11+
``transform_passes`` argument, so they run on the edge-dialect graph before
12+
partitioning/delegation:
13+
14+
to_edge_transform_and_lower(
15+
ep,
16+
transform_passes=get_default_passes(),
17+
partitioner=[NativePartitioner()],
18+
compile_config=EdgeCompileConfig(_check_ir_validity=False),
19+
)
20+
21+
reinplace rewrites functional ops into their in-place forms (e.g. relu -> relu_),
22+
which are not in the core-ATen opset, so callers must disable IR validity
23+
checking via ``EdgeCompileConfig(_check_ir_validity=False)`` (the same config the
24+
MLX backend requires).
25+
26+
Each pass lives in its own module; this package aggregates them and exposes the
27+
default pass list.
1328
"""
1429

1530
from typing import List, Union
1631

32+
from executorch.backends.native.passes.reinplace import (
33+
backend_inplace_aten_variants,
34+
BACKEND_INPLACE_OPS,
35+
NativeReinplacePass,
36+
)
37+
1738
from executorch.backends.transforms.collapse_view_copy import CollapseViewCopyPass
1839

1940
from executorch.exir.pass_base import ExportedProgramPassBase, ExportPass
2041
from executorch.exir.passes.cse_pass import CSEPass
2142

2243
__all__ = [
44+
"backend_inplace_aten_variants",
45+
"BACKEND_INPLACE_OPS",
2346
"CollapseViewCopyPass",
2447
"get_default_passes",
48+
"NativeReinplacePass",
2549
]
2650

2751

2852
def get_default_passes() -> List[Union[ExportPass, ExportedProgramPassBase]]:
2953
"""Passes enabled by default for the native backend.
3054
31-
CSE runs after view_copy collapsing to dedupe the settled graph.
55+
view_copy collapsing and CSE run first to settle the graph; reinplace runs
56+
last, rewriting functional ops into their in-place edge forms.
3257
"""
3358
return [
3459
CollapseViewCopyPass(),
3560
CSEPass(),
61+
NativeReinplacePass(),
3662
]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
"""
8+
reinplace pass for the native backend.
9+
10+
reinplace rewrites functional ops into their in-place forms (e.g. relu -> relu_),
11+
which are not in the core-ATen opset, so callers must disable IR validity
12+
checking via ``EdgeCompileConfig(_check_ir_validity=False)`` (the same config the
13+
MLX backend requires).
14+
"""
15+
16+
from typing import Tuple
17+
18+
import torch
19+
20+
from executorch.exir.dialects._ops import ops as _edge_ops
21+
from executorch.exir.pass_base import ExportedProgramPassBase, ExportedProgramPassResult
22+
23+
_edge = _edge_ops.edge.aten
24+
25+
# Single source of truth: base names of the ops the backend can execute in-place.
26+
# reinplace rewrites the functional edge op to its mutating variant (e.g.
27+
# relu -> relu_), and the partitioner must claim that variant to keep it in the
28+
# delegate (see backend_inplace_aten_variants).
29+
_INPLACE_OP_NAMES: Tuple[str, ...] = (
30+
"relu",
31+
"gelu",
32+
"sigmoid",
33+
"index_put",
34+
"index_copy",
35+
)
36+
37+
# Edge ops the backend can execute in-place, consumed as reinplace's op set.
38+
BACKEND_INPLACE_OPS: frozenset = frozenset(
39+
getattr(_edge, name).default for name in _INPLACE_OP_NAMES
40+
)
41+
42+
43+
def backend_inplace_aten_variants() -> set:
44+
"""Mutating aten variants that reinplace introduces for BACKEND_INPLACE_OPS.
45+
46+
reinplace rewrites e.g. relu -> aten.relu_.default; those in-place ops are
47+
not core-tagged, so the partitioner must claim them explicitly to keep them
48+
inside the delegate.
49+
"""
50+
ops = set()
51+
for name in _INPLACE_OP_NAMES:
52+
packet = getattr(torch.ops.aten, f"{name}_", None)
53+
overload = getattr(packet, "default", None) if packet is not None else None
54+
if overload is not None:
55+
ops.add(overload)
56+
return ops
57+
58+
59+
class NativeReinplacePass(ExportedProgramPassBase):
60+
"""Rewrite backend-supported elementwise ops into their in-place edge forms.
61+
62+
Runs as an EP-aware pass because ``reinplace_pass`` needs ``graph_signature``
63+
to protect mutable inputs/buffers. A fresh op set is passed each call so
64+
``reinplace_pass`` cannot mutate shared state, and passing an explicit
65+
``ops_to_inplace`` fully replaces reinplace's default set.
66+
"""
67+
68+
def call(self, exported_program) -> ExportedProgramPassResult:
69+
from executorch.exir.passes.reinplace import reinplace_pass
70+
71+
exported_program = reinplace_pass(
72+
exported_program, ops_to_inplace=set(BACKEND_INPLACE_OPS)
73+
)
74+
return ExportedProgramPassResult(exported_program, True)

‎backends/native/test/test_partitioner.py‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
NativeSupportedOperators,
1919
PTN_SERIALIZATION_KEY,
2020
)
21-
from executorch.backends.native.passes import get_default_passes
21+
from executorch.backends.native.passes import (
22+
backend_inplace_aten_variants,
23+
get_default_passes,
24+
)
2225
from executorch.backends.native.serialization import deserialize_graph
2326
from executorch.backends.native.serialization.schema import GraphArg
2427
from executorch.exir import to_edge_transform_and_lower
@@ -68,6 +71,18 @@ def test_accepts_opt_in_ops(self):
6871
self.sup.is_node_supported({}, _make_node("call_function", op))
6972
)
7073

74+
def test_accepts_inplace_aten_variants(self):
75+
# reinplace rewrites e.g. relu -> relu_; the mutating variants are not
76+
# core-tagged, so the partitioner must claim them explicitly.
77+
variants = backend_inplace_aten_variants()
78+
self.assertTrue(variants)
79+
for op in variants:
80+
with self.subTest(op=str(op)):
81+
self.assertNotIn(torch.Tag.core, op.tags)
82+
self.assertTrue(
83+
self.sup.is_node_supported({}, _make_node("call_function", op))
84+
)
85+
7186
def test_accepts_view_copy_tagged_op(self):
7287
op = torch.ops.aten.view_copy.default
7388
self.assertIn(torch.Tag.view_copy, op.tags)

‎backends/native/test/test_passes.py‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
import torch
1010
import torch.nn as nn
11-
1211
from executorch.backends.native import get_default_compile_config
12+
13+
from executorch.backends.native.passes.reinplace import NativeReinplacePass
1314
from executorch.exir import to_edge
1415
from executorch.exir.passes.cse_pass import CSEPass
1516

@@ -37,3 +38,23 @@ def forward(self, x):
3738
if n.op == "call_function" and "add" in str(n.target)
3839
]
3940
self.assertEqual(len(adds), 1, f"expected CSE to leave one add, got {adds}")
41+
42+
43+
class NativeReinplacePassTest(unittest.TestCase):
44+
def test_rewrites_relu_in_place(self):
45+
class ReluModel(nn.Module):
46+
def forward(self, x):
47+
# relu on an intermediate (x + 1) can be rewritten in place;
48+
# relu directly on the immutable user input x cannot.
49+
return torch.relu(x + 1)
50+
51+
ep = _transformed(ReluModel(), (torch.randn(4, 4),), [NativeReinplacePass()])
52+
targets = [
53+
str(n.target)
54+
for n in ep.graph_module.graph.nodes
55+
if n.op == "call_function"
56+
]
57+
self.assertTrue(
58+
any("relu_" in t for t in targets),
59+
f"expected in-place relu_, got {targets}",
60+
)

‎backends/native/test/test_preprocess.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,23 @@ def forward(self, x):
164164
)
165165
)
166166

167+
def test_reinplace_produces_inplace_relu(self):
168+
class ReluModel(nn.Module):
169+
def __init__(self):
170+
super().__init__()
171+
self.linear = nn.Linear(8, 8)
172+
173+
def forward(self, x):
174+
return torch.relu(self.linear(x))
175+
176+
blob = _get_delegate_blob(_lower(ReluModel(), (torch.randn(1, 8),)))
177+
graph = deserialize_graph(blob)
178+
targets = _call_function_targets(graph)
179+
self.assertTrue(
180+
any(t is not None and "relu_" in t for t in targets),
181+
f"expected in-place relu_, got {targets}",
182+
)
183+
167184
def test_constants_shipped_via_named_data(self):
168185
edge = _lower(nn.Linear(4, 4), (torch.randn(1, 4),))
169186
blob = _get_delegate_blob(edge)

0 commit comments

Comments
 (0)