|
| 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 List |
| 10 | + |
| 11 | +import executorch.backends.vulkan._passes.custom_ops_defs # noqa |
| 12 | + |
| 13 | +import torch |
| 14 | + |
| 15 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 16 | + |
| 17 | +from torch._export.utils import is_buffer, is_param |
| 18 | +from torch.export import ExportedProgram |
| 19 | + |
| 20 | +USES_WEIGHTS: List[torch._ops.OpOverload] = [ |
| 21 | + exir_ops.edge.aten.embedding.default, |
| 22 | + exir_ops.edge.aten.convolution.default, |
| 23 | + exir_ops.edge.et_vk.conv_with_clamp.default, |
| 24 | + exir_ops.edge.aten.linear.default, |
| 25 | + exir_ops.edge.aten._weight_int8pack_mm.default, |
| 26 | + exir_ops.edge.et_vk.linear_weight_int4.default, |
| 27 | + exir_ops.edge.aten._native_batch_norm_legit_no_training.default, |
| 28 | + exir_ops.edge.aten.native_layer_norm.default, |
| 29 | + "llama::sdpa_with_kv_cache", |
| 30 | +] |
| 31 | + |
| 32 | + |
| 33 | +def insert_prepack_nodes(program: ExportedProgram) -> ExportedProgram: |
| 34 | + """ |
| 35 | + Insert `et_vk.prepack` nodes for constant tensors in the graph. The prepack operator |
| 36 | + is responsible for transferring the tensor data, which is serialized with the model, |
| 37 | + to a GPU tensor object during the prepacking stage of model execution. |
| 38 | +
|
| 39 | + Some operators, listed in `USES_WEIGHTS` above, are performance sensitive and will |
| 40 | + prefer to handle prepacking within the operator. For these ops, the constant tensor |
| 41 | + data will be passed directly as an argument into the operator implementation. |
| 42 | + """ |
| 43 | + |
| 44 | + def is_get_attr_node(node: torch.fx.Node) -> bool: |
| 45 | + return isinstance(node, torch.fx.Node) and node.op == "get_attr" |
| 46 | + |
| 47 | + def is_constant(node: torch.fx.Node) -> bool: |
| 48 | + return node.name in program.graph_signature.inputs_to_lifted_tensor_constants |
| 49 | + |
| 50 | + def is_param_node(node: torch.fx.Node) -> bool: |
| 51 | + """ |
| 52 | + Check if the given node is a parameter within the exported program |
| 53 | + """ |
| 54 | + return ( |
| 55 | + is_get_attr_node(node) |
| 56 | + or is_param(program, node) |
| 57 | + or is_buffer(program, node) |
| 58 | + or is_constant(node) |
| 59 | + ) |
| 60 | + |
| 61 | + def is_non_weight_param_tensor(node: torch.fx.Node) -> bool: |
| 62 | + if not is_param_node(node): |
| 63 | + return False |
| 64 | + |
| 65 | + for user in node.users: |
| 66 | + if user.op == "call_function" and ( |
| 67 | + # pyre-ignore [16] |
| 68 | + user.target in USES_WEIGHTS |
| 69 | + or user.target.name() in USES_WEIGHTS |
| 70 | + ): |
| 71 | + return False |
| 72 | + |
| 73 | + return True |
| 74 | + |
| 75 | + for node in program.graph_module.graph.nodes: |
| 76 | + if not is_non_weight_param_tensor(node): |
| 77 | + continue |
| 78 | + |
| 79 | + with program.graph_module.graph.inserting_after(node): |
| 80 | + prepack_node = program.graph_module.graph.create_node( |
| 81 | + "call_function", |
| 82 | + exir_ops.edge.et_vk.prepack.default, |
| 83 | + (node,), |
| 84 | + ) |
| 85 | + prepack_node.meta["spec"] = node.meta["spec"] |
| 86 | + # Set the mem_obj_id to -1 to indicate that this node requires a dedicated |
| 87 | + # memory object. This pass must be executed AFTER the memory planning pass. |
| 88 | + prepack_node.meta["spec"].mem_obj_id = -1 |
| 89 | + node.replace_all_uses_with(prepack_node, lambda x: x != prepack_node) |
| 90 | + |
| 91 | + program.graph.eliminate_dead_code() |
| 92 | + return program |
0 commit comments