|
| 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 | +import torch |
| 8 | +from torch._export import ExportedProgram |
| 9 | +from torch._export.utils import get_buffer, get_param, is_buffer, is_param |
| 10 | +from torch._guards import detect_fake_mode |
| 11 | +from torch.export.exported_program import InputKind, InputSpec, TensorArgument |
| 12 | + |
| 13 | + |
| 14 | +def is_const(arg, exported_program, const_data_list) -> bool: |
| 15 | + if isinstance(arg, (tuple, list)): |
| 16 | + return all(is_const(x, exported_program, const_data_list) for x in arg) |
| 17 | + elif isinstance(arg, dict): |
| 18 | + return all(is_const(x, exported_program, const_data_list) for x in arg.values()) |
| 19 | + elif not isinstance(arg, torch.fx.Node) or arg.op != "placeholder": |
| 20 | + return False |
| 21 | + elif ( |
| 22 | + is_param(exported_program, arg) |
| 23 | + or is_buffer(exported_program, arg) |
| 24 | + or arg.name in const_data_list |
| 25 | + ): |
| 26 | + return True |
| 27 | + return False |
| 28 | + |
| 29 | + |
| 30 | +def get_data(exported_program, arg): |
| 31 | + if isinstance(arg, (tuple, list)): |
| 32 | + return [get_data(exported_program, x) for x in arg] |
| 33 | + elif is_param(exported_program, arg): |
| 34 | + return get_param(exported_program, arg) |
| 35 | + elif is_buffer(exported_program, arg): |
| 36 | + return get_buffer(exported_program, arg) |
| 37 | + return None |
| 38 | + |
| 39 | + |
| 40 | +def constant_prop_pass(exported_program: ExportedProgram) -> ExportedProgram: |
| 41 | + """ |
| 42 | + This pass is for constant propagation for Exported Program with lifted parameters, |
| 43 | + as the parameters will not be shown up as `get_attr` but as `placeholder` to the graph. |
| 44 | + """ |
| 45 | + if ( |
| 46 | + len([node for node in exported_program.graph.nodes if node.op == "placeholder"]) |
| 47 | + == 0 |
| 48 | + ): |
| 49 | + return exported_program |
| 50 | + |
| 51 | + has_cond = [ |
| 52 | + node |
| 53 | + for node in exported_program.graph.nodes |
| 54 | + if node.target == torch.ops.higher_order.cond |
| 55 | + ] |
| 56 | + if len(has_cond) > 0: |
| 57 | + raise RuntimeError("constant_prop_pass for control flow is not supported yet.") |
| 58 | + |
| 59 | + first_user_input = None |
| 60 | + for node in exported_program.graph.nodes: |
| 61 | + if ( |
| 62 | + node.op == "placeholder" |
| 63 | + and node.name in exported_program.graph_signature.user_inputs |
| 64 | + ): |
| 65 | + first_user_input = node |
| 66 | + break |
| 67 | + |
| 68 | + buffers = exported_program.graph_signature.buffers |
| 69 | + prop_constant_data = [] |
| 70 | + const_data_to_be_removed = set() |
| 71 | + |
| 72 | + fake_mode = detect_fake_mode( |
| 73 | + tuple( |
| 74 | + node.meta["val"] |
| 75 | + for node in exported_program.graph.nodes |
| 76 | + if node.op == "placeholder" |
| 77 | + ) |
| 78 | + ) |
| 79 | + assert fake_mode is not None |
| 80 | + |
| 81 | + for node in exported_program.graph.nodes: |
| 82 | + if node.op == "call_function": |
| 83 | + constant_data_name_list = [ |
| 84 | + input_spec.target for input_spec in prop_constant_data |
| 85 | + ] |
| 86 | + if is_const(node.args, exported_program, constant_data_name_list): |
| 87 | + args_data = [get_data(exported_program, arg) for arg in node.args] |
| 88 | + kwargs_data = node.kwargs |
| 89 | + const_data_to_be_removed.update(node.args) |
| 90 | + prop_constant_tensor = node.target(*args_data, **kwargs_data) |
| 91 | + prop_constant_tensor_fqn = f"_prop_tensor_constant{len(buffers)}" |
| 92 | + |
| 93 | + with exported_program.graph.inserting_before(first_user_input): |
| 94 | + const_placeholder_node = exported_program.graph.placeholder( |
| 95 | + prop_constant_tensor_fqn |
| 96 | + ) |
| 97 | + # Update the meta data of the new placeholder (buffer) node |
| 98 | + for k, v in node.meta.items(): |
| 99 | + const_placeholder_node.meta[k] = v |
| 100 | + const_placeholder_node.meta["val"] = fake_mode.from_tensor( |
| 101 | + prop_constant_tensor, static_shapes=True |
| 102 | + ) |
| 103 | + const_placeholder_node.meta["val"].constant = prop_constant_tensor |
| 104 | + |
| 105 | + node.replace_all_uses_with(const_placeholder_node) |
| 106 | + exported_program.graph.erase_node(node) |
| 107 | + prop_constant_node_input_spec = InputSpec( |
| 108 | + kind=InputKind.BUFFER, |
| 109 | + arg=TensorArgument(name=const_placeholder_node.name), |
| 110 | + target=prop_constant_tensor_fqn, |
| 111 | + ) |
| 112 | + prop_constant_data.append(prop_constant_node_input_spec) |
| 113 | + buffers.append(prop_constant_tensor_fqn) |
| 114 | + exported_program.state_dict[ |
| 115 | + prop_constant_tensor_fqn |
| 116 | + ] = prop_constant_tensor |
| 117 | + exported_program.graph_signature.input_specs.append( |
| 118 | + prop_constant_node_input_spec |
| 119 | + ) |
| 120 | + |
| 121 | + # Remove the propogated buffer from the state dict |
| 122 | + for node in exported_program.graph.nodes: |
| 123 | + if ( |
| 124 | + node.op == "placeholder" |
| 125 | + and node in const_data_to_be_removed |
| 126 | + and len(node.users) == 0 |
| 127 | + ): |
| 128 | + exported_program.state_dict.pop(node.name, None) |
| 129 | + exported_program.graph.erase_node(node) |
| 130 | + |
| 131 | + exported_program.graph_module.recompile() |
| 132 | + return exported_program |
0 commit comments