|
| 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 itertools import chain |
| 8 | + |
| 9 | +import torch |
| 10 | +from executorch.backends.example.example_operators.ops import module_to_annotator |
| 11 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 12 | +from executorch.exir.dim_order_utils import get_dim_order |
| 13 | +from executorch.exir.pass_base import ExportPass, PassResult |
| 14 | +from torch.ao.quantization.pt2e.graph_utils import find_sequential_partitions |
| 15 | + |
| 16 | + |
| 17 | +class PermuteMemoryFormatsPass(ExportPass): |
| 18 | + """ |
| 19 | + This pass will insert to_dim ops to the pattern if satisfis requirement, like pattern_op.permuate_memory_format is set as True. |
| 20 | + Example 1: |
| 21 | + before pass: x -> conv -> out |
| 22 | + after pass: x -> to_dim(channel_last) -> conv -> to_dim_(contiguous) -> out |
| 23 | +
|
| 24 | + before pass: x -> conv -> conv -> out |
| 25 | + after pass: x -> to_dim(channel_last) -> conv -> to_dim_(contiguous) -> to_dim(channel_last) -> conv -> to_dim_(contiguous) -> out |
| 26 | +
|
| 27 | + before pass: x -> conv -> linear -> out |
| 28 | + after pass: x -> to_dim(channel_last) -> conv -> to_dim_(contiguous) -> to_dim(channel_last) -> linear -> to_dim_(contiguous) -> out |
| 29 | + """ |
| 30 | + |
| 31 | + def call(self, graph_module: torch.fx.GraphModule) -> PassResult: |
| 32 | + for pattern in list(module_to_annotator.keys()): |
| 33 | + pattern_op = module_to_annotator[pattern] |
| 34 | + if pattern_op.permuate_memory_format: |
| 35 | + partitions = find_sequential_partitions( |
| 36 | + graph_module, |
| 37 | + pattern, |
| 38 | + ) |
| 39 | + for partition in partitions: |
| 40 | + # Some unpacking logic to get a flatten exit nodes list |
| 41 | + output_nodes = [ |
| 42 | + node |
| 43 | + for node in partition[0].output_nodes |
| 44 | + if node.op != "placeholder" |
| 45 | + ] |
| 46 | + exit_nodes = [output_node.users for output_node in output_nodes] |
| 47 | + exit_nodes = list(chain.from_iterable(exit_nodes)) |
| 48 | + |
| 49 | + """ |
| 50 | + # Step 1. Insert to_dim op when exit the pattern |
| 51 | + # for example, if the pattern is conv, x -> conv -> out will become x -> conv -> to_dim(contiguous) -> out when permute memory format |
| 52 | + # for x -> conv -> conv -> out, it will become x -> conv -> to_dim(contiguous) -> conv -> to_dim(contiguous) -> out |
| 53 | + """ |
| 54 | + for exit_node in exit_nodes: |
| 55 | + with graph_module.graph.inserting_before(exit_node): |
| 56 | + # Handle the case when the pattern output is also the graph output, |
| 57 | + # like, x -> conv -> out will become x -> conv -> to_dim(contiguous) -> out |
| 58 | + if exit_node.op == "output": |
| 59 | + exit_to_dim_op = graph_module.graph.call_function( |
| 60 | + exir_ops.edge.dim_order_ops._to_dim_order_copy.default, |
| 61 | + exit_node.args, |
| 62 | + { |
| 63 | + "dtype": torch.float64, |
| 64 | + "dim_order": get_dim_order( |
| 65 | + torch.contiguous_format, 4 |
| 66 | + ), |
| 67 | + }, |
| 68 | + ) |
| 69 | + # Insert to_dim op and it'll be the return op |
| 70 | + _ = graph_module.graph.output(exit_to_dim_op) |
| 71 | + # Remove the old return op. |
| 72 | + graph_module.graph.erase_node(exit_node) |
| 73 | + # Handle the case when the pattern output is intermediate output, |
| 74 | + # like, x -> conv -> conv -> out will become x -> conv -> to_dim(contiguous) -> conv -> out |
| 75 | + elif exit_node.op == "call_function": |
| 76 | + exit_node_args = [] |
| 77 | + for exit_node_arg in exit_node.args: |
| 78 | + if ( |
| 79 | + isinstance(exit_node_arg, torch.fx.Node) |
| 80 | + and exit_node_arg.op != "placeholder" |
| 81 | + ): |
| 82 | + exit_to_dim_op = graph_module.graph.call_function( |
| 83 | + exir_ops.edge.dim_order_ops._to_dim_order_copy.default, |
| 84 | + (exit_node_arg,), |
| 85 | + { |
| 86 | + "dtype": torch.float64, |
| 87 | + "dim_order": get_dim_order( |
| 88 | + torch.contiguous_format, 4 |
| 89 | + ), |
| 90 | + }, |
| 91 | + ) |
| 92 | + exit_node_args.append(exit_to_dim_op) |
| 93 | + else: |
| 94 | + exit_node_args.append(exit_node_arg) |
| 95 | + exit_node.args = list(exit_node_args) |
| 96 | + |
| 97 | + """ |
| 98 | + # Step 2. Insert to_dim op when enter the pattern. After the first step, we already have to_dim(default) when exiting the pattern. |
| 99 | + # Now we need to insert to_dim(channel_last) when enter the pattern. |
| 100 | + # for example, if the pattern is conv, x -> conv -> to_dim(contiguous) -> out will become x -> to_dim(channel_last) -> conv -> to_dim(contiguous) -> out |
| 101 | + # for x -> conv -> to_dim(contiguous) -> conv -> to_dim(contiguous) -> out, it will become x -> to_dim(channel_last) -> conv -> to_dim(contiguous) -> to_dim(channel_last) -> conv -> to_dim(contiguous) -> out |
| 102 | + """ |
| 103 | + # create the input_node and the to_dim_op map |
| 104 | + # for example, if the pattern is conv, x -> conv -> out, node |
| 105 | + input_node_map = {} # key: input_node, value: to_dim_op |
| 106 | + to_dim_op_set = set() |
| 107 | + for input_node in partition[0].input_nodes: |
| 108 | + with graph_module.graph.inserting_after(input_node): |
| 109 | + to_dim_op = graph_module.graph.call_function( |
| 110 | + # Insert the to_dim op and update input_node_map |
| 111 | + exir_ops.edge.dim_order_ops._to_dim_order_copy.default, |
| 112 | + ( |
| 113 | + input_node, |
| 114 | + { |
| 115 | + "dtype": torch.float64, |
| 116 | + "dim_order": get_dim_order( |
| 117 | + torch.channels_last, 4 |
| 118 | + ), |
| 119 | + }, |
| 120 | + ), |
| 121 | + ) |
| 122 | + input_node_map[input_node] = to_dim_op |
| 123 | + to_dim_op_set.add(to_dim_op) |
| 124 | + |
| 125 | + # Update the args to the new to_dim op, skip if it's already set |
| 126 | + for input_node in partition[0].input_nodes: |
| 127 | + for user in list(input_node.users): |
| 128 | + # if user is in to_dim_op_set, it means the user's arg is already set to_dim op |
| 129 | + if user not in to_dim_op_set: |
| 130 | + user_new_arg = [ |
| 131 | + input_node_map[user_arg] |
| 132 | + if user_arg in input_node_map |
| 133 | + else user_arg |
| 134 | + for user_arg in user.args |
| 135 | + ] |
| 136 | + # Update input node's users arg |
| 137 | + user.args = tuple(user_new_arg) |
| 138 | + |
| 139 | + # Ensure the graph is still valid |
| 140 | + graph_module.graph.lint() |
| 141 | + graph_module.recompile() |
| 142 | + return PassResult(graph_module, True) |
0 commit comments