|
| 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 | +# pyre-strict |
| 8 | + |
| 9 | +from typing import Set |
| 10 | + |
| 11 | +import torch |
| 12 | +from executorch.exir.dialects._ops import ops |
| 13 | +from torch.export import ExportedProgram |
| 14 | + |
| 15 | + |
| 16 | +def _is_view_copy(node: torch.fx.Node) -> bool: |
| 17 | + """Check if a node is a view_copy operation.""" |
| 18 | + return node.op == "call_function" and node.target in ( |
| 19 | + torch.ops.aten.view_copy.default, |
| 20 | + ops.edge.aten.view_copy.default, |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +def _is_index_put(node: torch.fx.Node) -> bool: |
| 25 | + """Check if a node is an index_put operation.""" |
| 26 | + return node.op == "call_function" and node.target in ( |
| 27 | + torch.ops.aten.index_put.default, |
| 28 | + ops.edge.aten.index_put.default, |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +def _is_safe_to_reinplace( |
| 33 | + node: torch.fx.Node, |
| 34 | + later_nodes: Set[torch.fx.Node], |
| 35 | + inputs: Set[torch.fx.Node], |
| 36 | + mutable_inputs: Set[torch.fx.Node], |
| 37 | +) -> bool: |
| 38 | + # This node is used later in the graph so we can't reinplace it |
| 39 | + # There is probably a faster way to do this but this works for now. |
| 40 | + if node in later_nodes: |
| 41 | + return False |
| 42 | + |
| 43 | + # If its not an input then we can reinplace it |
| 44 | + if node not in inputs: |
| 45 | + return True |
| 46 | + # If its a mutable input then we can reinplace it |
| 47 | + elif node in mutable_inputs: |
| 48 | + return True |
| 49 | + else: # input but not mutable input |
| 50 | + return False |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +def _is_user_input(node: torch.fx.Node, exported_program: ExportedProgram) -> bool: |
| 55 | + return node.target in exported_program.graph_signature.user_inputs |
| 56 | + |
| 57 | +def _is_buffer(node: torch.fx.Node, exported_program: ExportedProgram) -> bool: |
| 58 | + return node.target in exported_program.graph_signature.inputs_to_buffers |
| 59 | + |
| 60 | +def _is_mutable_user_input(node: torch.fx.Node, exported_program: ExportedProgram) -> bool: |
| 61 | + return node.target in exported_program.graph_signature.user_inputs_to_mutate.values() |
| 62 | + |
| 63 | +def _is_mutable_buffer(node: torch.fx.Node, exported_program: ExportedProgram) -> bool: |
| 64 | + if node.target not in exported_program.graph_signature.inputs_to_buffers: |
| 65 | + return False |
| 66 | + buf = exported_program.graph_signature.inputs_to_buffers[node.target] |
| 67 | + return buf in exported_program.graph_signature.buffers_to_mutate.values() |
| 68 | + |
| 69 | +def reinplace_pass(ep: ExportedProgram) -> ExportedProgram: |
| 70 | + """ |
| 71 | + Pass that loops over nodes in an exported program and collects the first argument |
| 72 | + of every call_function node that is a view_copy operation. |
| 73 | + |
| 74 | + Args: |
| 75 | + exported_program: The ExportedProgram to analyze |
| 76 | + |
| 77 | + Returns: |
| 78 | + Set of nodes that are first arguments to view_copy operations |
| 79 | + """ |
| 80 | + seen_nodes: Set[torch.fx.Node] = set() |
| 81 | + # Get all placeholders |
| 82 | + placeholders = set() |
| 83 | + for node in ep.graph.nodes: |
| 84 | + if node.op == "placeholder": |
| 85 | + placeholders.add(node) |
| 86 | + # Get all inputs that we could potentially mutate |
| 87 | + inputs = set([node for node in placeholders if _is_user_input(node, ep) or _is_buffer(node, ep)]) |
| 88 | + mutable_nodes = set([node for node in placeholders if _is_mutable_user_input(node, ep) or _is_mutable_buffer(node, ep)]) |
| 89 | + |
| 90 | + results = set() |
| 91 | + for node in reversed(ep.graph.nodes): |
| 92 | + if _is_index_put(node): |
| 93 | + # Check if this index_put node is safe to inplace |
| 94 | + # The first argument is the base tensor being indexed into |
| 95 | + first_arg = node.args[0] |
| 96 | + if _is_safe_to_reinplace(first_arg, seen_nodes, inputs, mutable_nodes): |
| 97 | + # This index_put is safe to reinplace |
| 98 | + with ep.graph.inserting_before(node): |
| 99 | + new_node = ep.graph.call_function( |
| 100 | + ops.edge.aten.index_put_.default, args=node.args |
| 101 | + ) |
| 102 | + new_node.meta["val"] = node.meta["val"] |
| 103 | + node.replace_all_uses_with(new_node) |
| 104 | + ep.graph.erase_node(node) |
| 105 | + results.add(first_arg) |
| 106 | + elif node.op == "call_function": |
| 107 | + seen_nodes.update(node.all_input_nodes) |
| 108 | + |
| 109 | + return ep |
0 commit comments