|
| 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 | + |
| 9 | +from executorch.backends.nxp.edge_passes.fold_redundant_qdq_pass import ( |
| 10 | + FoldRedundantDequantizeQuantizePass, |
| 11 | +) |
| 12 | +from executorch.backends.nxp.tests.executorch_pipeline import to_quantized_edge_program |
| 13 | + |
| 14 | +ExecutorchDelegateCall = torch.ops.higher_order.executorch_call_delegate |
| 15 | + |
| 16 | + |
| 17 | +class ConvDropoutConvModule(torch.nn.Module): |
| 18 | + """Two conv clusters separated by an eval-mode dropout (an identity). |
| 19 | +
|
| 20 | + In eval mode the dropout is a no-op, but the quantizer still wraps it in a |
| 21 | + shared-spec ``dequantize -> quantize`` cluster (identical scale/zero-point on |
| 22 | + both sides). Lowering eliminates the dropout itself, yet leaves that wrapping |
| 23 | + quantization behind as a redundant ``dequantize -> quantize`` pair with no |
| 24 | + compute node between the two conv clusters. The Neutron partitioner cannot |
| 25 | + delegate a compute-free QDQ island, so it splits ``conv1`` and ``conv2`` into |
| 26 | + two separate subgraphs -- the same split that |
| 27 | + ``FoldRedundantDequantizeQuantizePass`` was written to prevent. Folding that |
| 28 | + pair away lets both convs delegate as a single subgraph. |
| 29 | + """ |
| 30 | + |
| 31 | + def __init__(self) -> None: |
| 32 | + super().__init__() |
| 33 | + self.conv1 = torch.nn.Conv2d(4, 8, kernel_size=2, bias=False) |
| 34 | + self.dropout = torch.nn.Dropout(0.5) |
| 35 | + self.conv2 = torch.nn.Conv2d(8, 8, kernel_size=2, bias=False) |
| 36 | + |
| 37 | + def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 38 | + x = self.conv1(x) |
| 39 | + x = self.dropout(x) |
| 40 | + x = self.conv2(x) |
| 41 | + return x |
| 42 | + |
| 43 | + |
| 44 | +def _count_delegates(edge_program) -> int: |
| 45 | + graph = edge_program.exported_program().graph_module.graph |
| 46 | + return sum( |
| 47 | + 1 |
| 48 | + for node in graph.nodes |
| 49 | + if node.op == "call_function" and node.target == ExecutorchDelegateCall |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +INPUT_SHAPE = (1, 4, 8, 8) |
| 54 | + |
| 55 | + |
| 56 | +def test_fold_pass_present_merges_into_single_delegate(): |
| 57 | + # The fold pass is part of the default NeutronEdgePassManager. |
| 58 | + edge_program = to_quantized_edge_program(ConvDropoutConvModule(), INPUT_SHAPE) |
| 59 | + |
| 60 | + num_delegates = _count_delegates(edge_program) |
| 61 | + assert ( |
| 62 | + num_delegates == 1 |
| 63 | + ), f"expected a single delegate with the fold pass, got {num_delegates}" |
| 64 | + |
| 65 | + |
| 66 | +def test_fold_pass_removes_redundant_qdq(): |
| 67 | + graph = torch.fx.Graph() |
| 68 | + quantized_input = graph.placeholder("quantized_input") |
| 69 | + qparams = (0.25, 3, -128, 127, torch.int8) |
| 70 | + dequantize = graph.call_function( |
| 71 | + torch.ops.quantized_decomposed.dequantize_per_tensor.default, |
| 72 | + args=(quantized_input, *qparams), |
| 73 | + ) |
| 74 | + quantize = graph.call_function( |
| 75 | + torch.ops.quantized_decomposed.quantize_per_tensor.default, |
| 76 | + args=(dequantize, *qparams), |
| 77 | + ) |
| 78 | + graph.output(quantize) |
| 79 | + graph_module = torch.fx.GraphModule(torch.nn.Module(), graph) |
| 80 | + |
| 81 | + result = FoldRedundantDequantizeQuantizePass().run(graph_module) |
| 82 | + |
| 83 | + assert result.modified |
| 84 | + remaining_nodes = list(result.graph_module.graph.nodes) |
| 85 | + assert [node.op for node in remaining_nodes] == ["placeholder", "output"] |
| 86 | + assert remaining_nodes[-1].args == (quantized_input,) |
0 commit comments