|
| 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 | +from typing import Optional |
| 8 | + |
| 9 | +import torch |
| 10 | + |
| 11 | +from executorch.backends.vulkan.patterns.pattern_registry import ( |
| 12 | + PatternMatch, |
| 13 | + register_pattern_detector, |
| 14 | + register_pattern_replacement, |
| 15 | +) |
| 16 | + |
| 17 | +from executorch.exir import ExportedProgram |
| 18 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 19 | + |
| 20 | + |
| 21 | +class SelectAsSymIntMatch(PatternMatch): |
| 22 | + def __init__(self, local_scalar_dense_node: torch.fx.Node) -> None: |
| 23 | + self.anchor_node = local_scalar_dense_node |
| 24 | + self.match_found = False |
| 25 | + |
| 26 | + # Check if the input to local_scalar_dense is a select_copy node |
| 27 | + if len(local_scalar_dense_node.args) < 1: |
| 28 | + return |
| 29 | + |
| 30 | + select_node = local_scalar_dense_node.args[0] |
| 31 | + if not isinstance(select_node, torch.fx.Node): |
| 32 | + return |
| 33 | + |
| 34 | + if ( |
| 35 | + select_node.op != "call_function" |
| 36 | + or select_node.target != exir_ops.edge.aten.select_copy.int |
| 37 | + ): |
| 38 | + return |
| 39 | + |
| 40 | + # select_copy.int has signature: select_copy(Tensor self, int dim, int index) |
| 41 | + if len(select_node.args) < 3: |
| 42 | + return |
| 43 | + |
| 44 | + self.select_node = select_node |
| 45 | + |
| 46 | + self.tensor_node = select_node.args[0] |
| 47 | + self.dim_node = select_node.args[1] |
| 48 | + self.index_node = select_node.args[2] |
| 49 | + |
| 50 | + self.all_nodes = [ |
| 51 | + self.anchor_node, |
| 52 | + self.select_node, |
| 53 | + self.tensor_node, |
| 54 | + self.dim_node, |
| 55 | + self.index_node, |
| 56 | + ] |
| 57 | + |
| 58 | + self.match_found = True |
| 59 | + |
| 60 | + |
| 61 | +@register_pattern_detector("select_as_symint") |
| 62 | +def find_select_as_symint_patterns( |
| 63 | + node: torch.fx.Node, |
| 64 | +) -> Optional[SelectAsSymIntMatch]: |
| 65 | + if node.target != torch.ops.aten._local_scalar_dense.default: |
| 66 | + return None |
| 67 | + |
| 68 | + matched_pattern = SelectAsSymIntMatch(node) |
| 69 | + if matched_pattern.match_found: |
| 70 | + return matched_pattern |
| 71 | + |
| 72 | + return None |
| 73 | + |
| 74 | + |
| 75 | +## |
| 76 | +## Pattern Replacement |
| 77 | +## |
| 78 | + |
| 79 | + |
| 80 | +@register_pattern_replacement("select_as_symint") |
| 81 | +def replace_select_local_scalar_dense_with_select_as_symint( |
| 82 | + ep: ExportedProgram, |
| 83 | + graph_module: torch.fx.GraphModule, |
| 84 | + match: SelectAsSymIntMatch, |
| 85 | +): |
| 86 | + with graph_module.graph.inserting_before(match.anchor_node): |
| 87 | + new_node = graph_module.graph.create_node( |
| 88 | + "call_function", |
| 89 | + exir_ops.edge.et_vk.select_as_symint.default, |
| 90 | + args=( |
| 91 | + match.tensor_node, |
| 92 | + match.dim_node, |
| 93 | + match.index_node, |
| 94 | + ), |
| 95 | + ) |
| 96 | + |
| 97 | + new_node.meta["val"] = match.anchor_node.meta["val"] |
| 98 | + match.anchor_node.replace_all_uses_with(new_node) |
| 99 | + |
| 100 | + # # Remove both the local_scalar_dense and select_copy nodes |
| 101 | + # graph_module.graph.erase_node(match.anchor_node) |
| 102 | + # # Only erase select_node if it has no other users |
| 103 | + # if len(match.select_node.users) == 0: |
| 104 | + # graph_module.graph.erase_node(match.select_node) |
0 commit comments