|
2 | 2 |
|
3 | 3 | import torch |
4 | 4 | from executorch.backends.qualcomm._passes import ( |
| 5 | + AnnotateQuantAttrs, |
5 | 6 | ConvertBmmToMatmul, |
6 | 7 | ConvertMhaToSha, |
| 8 | + FoldQDQ, |
| 9 | + InsertIOQDQ, |
7 | 10 | InsertReshapeForReduceOps, |
8 | 11 | RemoveRedundancy, |
9 | 12 | ) |
| 13 | +from executorch.backends.qualcomm.quantizer.quantizer import QnnQuantizer, QuantDtype |
10 | 14 | from executorch.backends.qualcomm.serialization.qc_schema import QcomChipset |
11 | 15 | from executorch.backends.qualcomm.tests.models import TopKandIndex |
12 | 16 | from executorch.backends.qualcomm.utils.utils import ( |
|
17 | 21 | from executorch.exir import to_edge |
18 | 22 | from executorch.exir.debug_handle_utils import DEBUG_HANDLE_KEY |
19 | 23 | from executorch.exir.dialects._ops import ops as exir_ops |
| 24 | +from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e |
20 | 25 |
|
21 | 26 |
|
22 | 27 | class TestPasses(unittest.TestCase): |
| 28 | + def _build_quantized_graph(self): |
| 29 | + """Build a quantized graph through AnnotateQuantAttrs + FoldQDQ.""" |
| 30 | + |
| 31 | + class AddModule(torch.nn.Module): |
| 32 | + def forward(self, x): |
| 33 | + return x + 1 |
| 34 | + |
| 35 | + module = AddModule().eval() |
| 36 | + sample_input = (torch.randn(1, 4),) |
| 37 | + |
| 38 | + exported = torch.export.export(module, sample_input, strict=True).module() |
| 39 | + quantizer = QnnQuantizer() |
| 40 | + quantizer.set_default_quant_config(quant_dtype=QuantDtype.use_8a8w) |
| 41 | + prepared = prepare_pt2e(exported, quantizer) |
| 42 | + prepared(*sample_input) |
| 43 | + qdq_module = convert_pt2e(prepared) |
| 44 | + |
| 45 | + edge_program = to_edge( |
| 46 | + torch.export.export(qdq_module, sample_input, strict=True) |
| 47 | + ) |
| 48 | + ep = edge_program.exported_program() |
| 49 | + gm = ep.graph_module |
| 50 | + |
| 51 | + gm = AnnotateQuantAttrs(ep)(gm).graph_module |
| 52 | + gm = FoldQDQ(ep)(gm).graph_module |
| 53 | + return gm, ep |
| 54 | + |
| 55 | + def test_insert_io_qdq_handles_dequant_encoding(self): |
| 56 | + """InsertIOQDQ should not KeyError when a node with a dequantize |
| 57 | + encoding feeds the output node (e.g. pre-quantized LLM parameters).""" |
| 58 | + gm, ep = self._build_quantized_graph() |
| 59 | + |
| 60 | + # Wire b__frozen_param0 (which has dequantize encoding) to output, |
| 61 | + # simulating the LLM topology from github issue #17732. |
| 62 | + param_node = None |
| 63 | + output_node = None |
| 64 | + for n in gm.graph.nodes: |
| 65 | + if n.name == "b__frozen_param0": |
| 66 | + param_node = n |
| 67 | + if n.op == "output": |
| 68 | + output_node = n |
| 69 | + |
| 70 | + self.assertIsNotNone(param_node) |
| 71 | + old_args = output_node.args[0] |
| 72 | + output_node.args = ( |
| 73 | + ((old_args,) if not isinstance(old_args, tuple) else old_args) |
| 74 | + + (param_node,), |
| 75 | + ) |
| 76 | + gm.graph.lint() |
| 77 | + gm.recompile() |
| 78 | + |
| 79 | + pass_instance = InsertIOQDQ(ep) |
| 80 | + pass_instance._insert(gm) |
| 81 | + |
| 82 | + dq_nodes = [ |
| 83 | + n |
| 84 | + for n in gm.graph.nodes |
| 85 | + if n.op == "call_function" |
| 86 | + and hasattr(n.target, "__name__") |
| 87 | + and "dequantize" in n.target.__name__ |
| 88 | + and any(u.op == "output" for u in n.users.keys()) |
| 89 | + ] |
| 90 | + self.assertGreaterEqual(len(dq_nodes), 1) |
| 91 | + |
| 92 | + def test_insert_io_qdq_no_revisit(self): |
| 93 | + """InsertIOQDQ must not revisit newly inserted nodes.""" |
| 94 | + gm, ep = self._build_quantized_graph() |
| 95 | + |
| 96 | + node_count_before = len(list(gm.graph.nodes)) |
| 97 | + pass_instance = InsertIOQDQ(ep) |
| 98 | + pass_instance._insert(gm) |
| 99 | + node_count_after = len(list(gm.graph.nodes)) |
| 100 | + |
| 101 | + # AddModule with one input and one output should insert exactly |
| 102 | + # one quantize (input) and one dequantize (output) = +2 nodes. |
| 103 | + self.assertEqual(node_count_after, node_count_before + 2) |
| 104 | + |
23 | 105 | def test_insert_reshape_for_argmax(self): |
24 | 106 | class ArgmaxModule(torch.nn.Module): |
25 | 107 | def forward(self, x): |
|
0 commit comments