|
| 1 | +# Copyright (c) Qualcomm Innovation Center, Inc. |
| 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 | +from typing import Sequence |
| 7 | + |
| 8 | +import torch |
| 9 | +from executorch.backends.qualcomm.quantizer.quantizer import ( |
| 10 | + get_16a8w_qnn_ptq_config, |
| 11 | + get_default_8bit_qnn_ptq_config, |
| 12 | + QuantizationConfig, |
| 13 | +) |
| 14 | +from executorch.backends.qualcomm.quantizer.utils import QUANT_ANNOTATION_KEY |
| 15 | +from torch.ao.quantization.quantizer import ( |
| 16 | + QuantizationAnnotation, |
| 17 | + SharedQuantizationSpec, |
| 18 | +) |
| 19 | +from torch.fx import Node |
| 20 | + |
| 21 | + |
| 22 | +def custom_annotate_llama_matmul_16a8w(gm: torch.fx.GraphModule) -> None: # noqa: C901 |
| 23 | + """ |
| 24 | + This function is specific for llama matmul op 16a8w. |
| 25 | + """ |
| 26 | + |
| 27 | + def annotate_matmul(node: Node, quantization_config: QuantizationConfig): |
| 28 | + input_qspec_map = {} |
| 29 | + input_act = node.args[0] |
| 30 | + input_spec = quantization_config.input_activation |
| 31 | + input_qspec_map[input_act] = input_spec |
| 32 | + input_act1 = node.args[1] |
| 33 | + input_spec1 = quantization_config.weight |
| 34 | + input_qspec_map[input_act1] = input_spec1 |
| 35 | + node.meta[QUANT_ANNOTATION_KEY] = QuantizationAnnotation( |
| 36 | + input_qspec_map=input_qspec_map, |
| 37 | + output_qspec=quantization_config.output_activation, |
| 38 | + _annotated=True, |
| 39 | + ) |
| 40 | + |
| 41 | + def annotate_index_put(node: Node, quantization_config: QuantizationConfig) -> None: |
| 42 | + input = node.args[0] |
| 43 | + value = node.args[2] |
| 44 | + input_qspec_map = {} |
| 45 | + input_qspec_map[input] = quantization_config.input_activation |
| 46 | + input_qspec_map[value] = SharedQuantizationSpec((input, node)) |
| 47 | + node.meta[QUANT_ANNOTATION_KEY] = QuantizationAnnotation( |
| 48 | + input_qspec_map=input_qspec_map, |
| 49 | + output_qspec=SharedQuantizationSpec((input, node)), |
| 50 | + _annotated=True, |
| 51 | + ) |
| 52 | + |
| 53 | + def annotate_single_in_single_out( |
| 54 | + node: Node, quantization_config: QuantizationConfig |
| 55 | + ) -> None: |
| 56 | + input_qspec_map = {} |
| 57 | + input_act = node.args[0] |
| 58 | + input_qspec_map[input_act] = quantization_config.input_activation |
| 59 | + node.meta[QUANT_ANNOTATION_KEY] = QuantizationAnnotation( |
| 60 | + input_qspec_map=input_qspec_map, |
| 61 | + output_qspec=quantization_config.output_activation, |
| 62 | + _annotated=True, |
| 63 | + ) |
| 64 | + |
| 65 | + def annotate_cat(node: Node, quantization_config: QuantizationConfig): |
| 66 | + input_nodes = node.args[0] |
| 67 | + assert isinstance(input_nodes, Sequence) |
| 68 | + first_input_node = input_nodes[0] |
| 69 | + input_qspec_map = {} |
| 70 | + assert isinstance(first_input_node, Node) |
| 71 | + assert isinstance(node, Node) |
| 72 | + input_qspec_map[first_input_node] = quantization_config.input_activation |
| 73 | + share_qparams_with_input_act0_qspec = SharedQuantizationSpec( |
| 74 | + (first_input_node, node) |
| 75 | + ) |
| 76 | + for input_node in input_nodes[1:]: |
| 77 | + if input_node not in input_qspec_map: |
| 78 | + assert isinstance(input_node, Node) |
| 79 | + input_qspec_map[input_node] = share_qparams_with_input_act0_qspec |
| 80 | + node.meta[QUANT_ANNOTATION_KEY] = QuantizationAnnotation( |
| 81 | + input_qspec_map=input_qspec_map, |
| 82 | + output_qspec=share_qparams_with_input_act0_qspec, |
| 83 | + _annotated=True, |
| 84 | + ) |
| 85 | + |
| 86 | + def is_edge_condition(node: Node): |
| 87 | + if not isinstance(node, Node) or node.op != "call_function": |
| 88 | + return True |
| 89 | + return False |
| 90 | + |
| 91 | + def annotate_matmul_input1(node: Node, quantization_config: QuantizationConfig): |
| 92 | + if is_edge_condition(node): |
| 93 | + return |
| 94 | + if node.target == torch.ops.aten.index_put_.default: |
| 95 | + annotate_index_put(node, quantization_config) |
| 96 | + annotate_matmul_input1(node.args[0], quantization_config) |
| 97 | + elif node.target == torch.ops.aten.cat.default: |
| 98 | + annotate_cat(node, quantization_config) |
| 99 | + # Expect that the inputs of the cat op are select ops |
| 100 | + for arg in node.args[0][1:]: |
| 101 | + annotate_single_in_single_out(arg, quantization_config) |
| 102 | + annotate_matmul_input1(node.args[0][0], quantization_config) |
| 103 | + else: |
| 104 | + annotate_single_in_single_out(node, quantization_config) |
| 105 | + annotate_matmul_input1(node.args[0], quantization_config) |
| 106 | + |
| 107 | + # Annotate 16a8w for matmul op to get better performance |
| 108 | + quantization_config_16a8w = get_16a8w_qnn_ptq_config() |
| 109 | + # Annotate 8a8w for second input of matmul until past_kv_cache |
| 110 | + quantization_config_8a8w = get_default_8bit_qnn_ptq_config(act_symmetric=True) |
| 111 | + for node in gm.graph.nodes: |
| 112 | + if node.op == "call_function" and node.target == torch.ops.aten.matmul.default: |
| 113 | + if "nn_module_stack" in node.meta: |
| 114 | + module_values_list = list(node.meta["nn_module_stack"].values()) |
| 115 | + full_qualified_name = module_values_list[-1][0] |
| 116 | + if "SDPA" in full_qualified_name: |
| 117 | + annotate_matmul(node, quantization_config_16a8w) |
| 118 | + annotate_matmul_input1(node.args[1], quantization_config_8a8w) |
0 commit comments