|
27 | 27 | from executorch.backends.transforms.remove_clone_ops import RemoveCloneOpsTransform |
28 | 28 | from executorch.exir.dialects._ops import ops as exir_ops |
29 | 29 | 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 |
31 | 31 | from executorch.exir.pass_manager import PassManager, PassType |
32 | 32 | 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 |
35 | 34 |
|
36 | 35 |
|
37 | 36 | @register_cadence_pass(CadencePassAttribute(opt_level=0)) |
@@ -245,117 +244,6 @@ def maybe_remove_or_replace(self, node: Node) -> bool: |
245 | 244 | return True |
246 | 245 |
|
247 | 246 |
|
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 | | - |
359 | 247 | @register_cadence_pass(CadencePassAttribute(opt_level=1)) |
360 | 248 | class RemoveContiguousOpPass(RemoveOrReplacePassInterface): |
361 | 249 | """ |
@@ -533,10 +421,9 @@ class Subgraph: |
533 | 421 | def call(self, graph_module: torch.fx.GraphModule) -> PassResult: |
534 | 422 | subgraphs_found: list[RemovePermutesAroundElementwiseOps.Subgraph] = [] |
535 | 423 | 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 | + ): |
540 | 427 | start_permute = self.get_permutation(node) |
541 | 428 | # Expected end permutation for the subgraph. |
542 | 429 | end_permute = [start_permute.index(i) for i in range(len(start_permute))] |
|
0 commit comments