forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermute_pass_utils.py
More file actions
278 lines (239 loc) · 9.42 KB
/
permute_pass_utils.py
File metadata and controls
278 lines (239 loc) · 9.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
# pyre-unsafe
"""Shared utilities and base classes for permute optimization passes.
These were originally in executorch.backends.cadence.aot and are used by
both the Cadence and Arm backends.
"""
from abc import abstractmethod
from collections import deque
from typing import cast, List, Optional, Type, TypeVar, Union
import torch
import torch.fx
from executorch.exir.dialects._ops import ops as exir_ops
from executorch.exir.dialects.edge._ops import EdgeOpOverload, EdgeOpOverloadPacket
from executorch.exir.pass_base import ExportPass, PassResult
from torch.fx import Node
from torch.fx.node import Argument
T = TypeVar("T")
def get_edge_overload_packet(edge_op: EdgeOpOverload) -> EdgeOpOverloadPacket:
edge_op_namespace, edge_op_name = (
edge_op.namespace,
edge_op._schema.name.split("::")[1],
)
edge_op_overload_packet = getattr(
getattr(exir_ops.edge, edge_op_namespace), edge_op_name
)
return edge_op_overload_packet
def get_shape(
graph_module: torch.fx.GraphModule, node: torch.fx.Node
) -> Union[torch.Size, None]:
"""Return the shape of the tensor corresponding to node."""
try:
if isinstance(node, (float, int, bool)):
return torch.Size([1])
fake_tensor = node.meta.get("val")
if fake_tensor is not None:
return fake_tensor.shape
if node.op == "get_attr":
attr_node = getattr(graph_module, node.target)
return attr_node.shape
return None
except RuntimeError:
return None
def get_transposed_dims(
node: torch.fx.Node, dims: Optional[List[int]] = None
) -> List[int]:
"""Applies the transposition as given by node onto the dimensions given in input."""
assert node.target == exir_ops.edge.aten.transpose_copy.int
assert dims is not None
dim_len = len(dims)
transpose_dims0 = node.args[1]
transpose_dims1 = node.args[2]
assert isinstance(transpose_dims0, int)
assert isinstance(transpose_dims1, int)
dim0 = transpose_dims0 if transpose_dims0 >= 0 else transpose_dims0 + dim_len
dim1 = transpose_dims1 if transpose_dims1 >= 0 else transpose_dims1 + dim_len
new_dims = list(dims)
new_dims[dim0], new_dims[dim1] = dims[dim1], dims[dim0]
return new_dims
def get_permuted_dims(node: torch.fx.Node, dims: List[int]) -> List[int]:
"""Applies the permutation as given by node onto the dimensions given in input."""
assert node.target == exir_ops.edge.aten.permute_copy.default
# pyre-fixme[6]: This combined typecheck isn't supported yet.
permute_dims: List[int] = list(node.args[1])
assert all(isinstance(x, int) for x in permute_dims)
return [dims[x] for x in permute_dims]
def get_arg(
node: torch.fx.Node,
kwarg_name: str,
expected_type: Type[T] = Argument,
) -> T:
"""Get the arg with kwarg_name of the node."""
if kwarg_name in node.kwargs:
value = node.kwargs[kwarg_name]
else:
normalized_args = node.normalized_arguments(
node.graph.owning_module, normalize_to_only_use_kwargs=True
)
if not normalized_args:
raise RuntimeError(
f"get_arg: Node {node} does not support normalization of arguments"
)
value = normalized_args.kwargs[kwarg_name]
if expected_type is not Argument:
try:
type_ok = isinstance(value, expected_type)
except TypeError:
# Subscripted generics (e.g. List[int]) don't support isinstance.
# Fall through — caller is responsible for correctness.
type_ok = True
if not type_ok:
raise TypeError(
f"get_arg: expected {expected_type} for '{kwarg_name}', got {type(value)}"
)
return value # type: ignore[return-value]
def set_arg(
node: torch.fx.Node, kwarg_name: str, value: torch.fx.node.Argument
) -> None:
"""Set the node's arg with its name to the given value."""
if kwarg_name in node.kwargs:
node.update_kwarg(kwarg_name, value)
return
normalized_args = node.normalized_arguments(
node.graph.owning_module, normalize_to_only_use_kwargs=True
)
if not normalized_args:
raise RuntimeError(
f"set_arg: Node {node} does not support normalization of arguments"
)
kwargs = normalized_args.kwargs
if kwarg_name not in kwargs:
raise ValueError(f"set_arg: invalid arg name {kwarg_name} for node {node} used")
idx = list(kwargs.keys()).index(kwarg_name)
if idx < len(node.args):
node.update_arg(idx, value)
else:
node.update_kwarg(kwarg_name, value)
class HierarchicalInplacePassInterface(ExportPass):
"""A base class for passes that apply in-place modification to the graph module and its submodules."""
@abstractmethod
def _apply_flat_inplace(self, graph_module) -> bool:
raise NotImplementedError("`_apply_flat_inplace` must be implemented")
def _apply_hierarchical_inplace(self, graph_module: torch.fx.GraphModule) -> bool:
modified: bool = False
for module in filter(
lambda m: isinstance(m, torch.fx.GraphModule), graph_module.modules()
):
modified |= self._apply_flat_inplace(module)
return modified
def call(self, graph_module: torch.fx.GraphModule) -> PassResult:
modified = self._apply_hierarchical_inplace(graph_module)
if modified:
graph_module.graph.eliminate_dead_code()
graph_module.recompile()
return super().call(graph_module)
return PassResult(graph_module, False)
class RemoveOrReplacePassInterface(HierarchicalInplacePassInterface):
@property
@abstractmethod
def targets(self) -> list[EdgeOpOverload]:
raise NotImplementedError("`targets` must be implemented")
@abstractmethod
def maybe_remove_or_replace(self, node: Node) -> bool:
raise NotImplementedError("`maybe_remove_or_replace` must be implemented")
def _apply_flat_inplace(self, graph_module: torch.fx.GraphModule) -> bool:
changed = False
for target in self.targets:
for node in graph_module.graph.find_nodes(
op="call_function", target=target
):
if len(node.users) == 0:
continue
changed |= self.maybe_remove_or_replace(node)
return changed
class FuseOpPairsAcrossBranchesPass(ExportPass):
"""Base class for passes that fuse op pairs across branches."""
def check_ok_to_fuse(
self,
producer: torch.fx.Node,
consumers: list[torch.fx.Node],
) -> bool:
return True
def can_fuse_for_chain(
self,
producer: torch.fx.Node,
consumer: torch.fx.Node,
consumer_op_packets: set[EdgeOpOverloadPacket],
) -> bool:
if (
isinstance(consumer.target, EdgeOpOverload)
and get_edge_overload_packet(consumer.target) in consumer_op_packets
):
return True
return False
def get_fuse_candidates(
self,
producer: torch.fx.Node,
consumer_op_packets: set[EdgeOpOverloadPacket],
bypass_ops: set[EdgeOpOverload],
) -> list[torch.fx.Node]:
users = deque(producer.users.keys())
removal_candidates = []
while users:
user = users.popleft()
if user.target in bypass_ops:
users.extend(list(user.users.keys()))
elif self.can_fuse_for_chain(producer, user, consumer_op_packets):
removal_candidates.append(user)
else:
removal_candidates.clear()
break
return removal_candidates
def find_and_fuse(
self,
graph_module: torch.fx.GraphModule,
producer_op_packets: set[EdgeOpOverloadPacket],
consumer_op_packets: set[EdgeOpOverloadPacket],
bypass_ops: set[EdgeOpOverload],
) -> bool:
modified = False
for node in graph_module.graph.nodes:
if not (
isinstance(node.target, EdgeOpOverload)
and get_edge_overload_packet(node.target) in producer_op_packets
):
continue
removal_candidates = self.get_fuse_candidates(
node, consumer_op_packets, bypass_ops
)
if len(removal_candidates) == 0:
continue
if not self.check_ok_to_fuse(node, removal_candidates):
continue
self.fuse(node, removal_candidates, graph_module)
modified = True
if modified:
graph_module.recompile()
return modified
def get_fused_node(
self,
producer: torch.fx.Node,
consumer: torch.fx.Node,
graph_module: torch.fx.GraphModule,
) -> torch.fx.Node:
return consumer
def fuse(
self,
node: torch.fx.Node,
removal_candidates: list[torch.fx.Node],
graph_module: torch.fx.GraphModule,
) -> None:
node.replace_all_uses_with(cast(torch.fx.Node, node.args[0]))
graph_module.graph.erase_node(node)
for rnode in removal_candidates:
rnode.replace_all_uses_with(self.get_fused_node(node, rnode, graph_module))
graph_module.graph.erase_node(rnode)