|
| 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 unittest |
| 8 | + |
| 9 | +import torch |
| 10 | +from executorch.backends.transforms.remove_clone_ops import RemoveCloneOpsTransform |
| 11 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 12 | +from torch.fx import GraphModule |
| 13 | +from torch.testing import FileCheck |
| 14 | +from torch.testing._internal.common_utils import TestCase |
| 15 | + |
| 16 | + |
| 17 | +class TestRemoveCloneOpsTransform(TestCase): |
| 18 | + def test_dq_clone_q_linear(self): |
| 19 | + """ |
| 20 | + Test RemoveCloneOpsTransform on a graph with d/q -> clone -> q -> linear pattern |
| 21 | +
|
| 22 | + Before: Should contain all nodes |
| 23 | + After: Should only have the linear operation |
| 24 | + """ |
| 25 | + |
| 26 | + # Create a graph module directly with the pattern: quant -> clone -> dequant -> fp linear |
| 27 | + class TestModule(torch.nn.Module): |
| 28 | + def __init__(self): |
| 29 | + super().__init__() |
| 30 | + self.linear = torch.nn.Linear(10, 5) |
| 31 | + |
| 32 | + def forward(self, x): |
| 33 | + # This will be replaced with our custom graph |
| 34 | + return self.linear(x) |
| 35 | + |
| 36 | + # Create a module instance |
| 37 | + module = TestModule() |
| 38 | + |
| 39 | + # Create a new graph with our desired pattern |
| 40 | + graph = torch.fx.Graph() |
| 41 | + |
| 42 | + # Add placeholders |
| 43 | + input_node = graph.placeholder("x") |
| 44 | + |
| 45 | + # Create nodes for our pattern: quant -> clone -> dequant -> fp linear |
| 46 | + # Constants for quantization parameters |
| 47 | + scale = graph.create_node( |
| 48 | + "call_function", torch.tensor, args=([0.1],), kwargs={} |
| 49 | + ) |
| 50 | + zero_point = graph.create_node( |
| 51 | + "call_function", torch.tensor, args=([0],), kwargs={} |
| 52 | + ) |
| 53 | + |
| 54 | + # Dequantize node |
| 55 | + dequant_node = graph.create_node( |
| 56 | + "call_function", |
| 57 | + torch.ops.quantized_decomposed.dequantize_per_tensor.default, |
| 58 | + args=(input_node, scale, zero_point, torch.int8), |
| 59 | + kwargs={}, |
| 60 | + ) |
| 61 | + |
| 62 | + # Clone node. |
| 63 | + # Use Edge op as this is an executorch pass |
| 64 | + clone_node = graph.create_node( |
| 65 | + "call_function", |
| 66 | + exir_ops.edge.aten.clone.default, |
| 67 | + args=(dequant_node,), |
| 68 | + kwargs={}, |
| 69 | + ) |
| 70 | + |
| 71 | + # Quantize node |
| 72 | + quant_node = graph.create_node( |
| 73 | + "call_function", |
| 74 | + torch.ops.quantized_decomposed.quantize_per_tensor.default, |
| 75 | + args=(clone_node, scale, zero_point, torch.int8), |
| 76 | + kwargs={}, |
| 77 | + ) |
| 78 | + |
| 79 | + # Linear node (using the module's linear layer) |
| 80 | + # Technically, should use quantized weight and bias |
| 81 | + # but we are just inspecting graph patterns in this test |
| 82 | + weight = graph.create_node("get_attr", "linear.weight") |
| 83 | + bias = graph.create_node("get_attr", "linear.bias") |
| 84 | + linear_node = graph.create_node( |
| 85 | + "call_function", |
| 86 | + torch.nn.functional.linear, |
| 87 | + args=(quant_node, weight, bias), |
| 88 | + kwargs={}, |
| 89 | + ) |
| 90 | + |
| 91 | + # Output |
| 92 | + graph.output(linear_node) |
| 93 | + |
| 94 | + # Create a GraphModule with our custom graph |
| 95 | + gm = GraphModule(module, graph) |
| 96 | + |
| 97 | + # Verify we have the expected nodes before transformation using FileCheck |
| 98 | + FileCheck().check( |
| 99 | + "torch.ops.quantized_decomposed.dequantize_per_tensor.default", |
| 100 | + ).check( |
| 101 | + "executorch_exir_dialects_edge__ops_aten_clone_default", |
| 102 | + ).check( |
| 103 | + "torch.ops.quantized_decomposed.quantize_per_tensor.default", |
| 104 | + ).check( |
| 105 | + "torch._C._nn.linear", |
| 106 | + ).run( |
| 107 | + gm.code |
| 108 | + ) |
| 109 | + |
| 110 | + # Apply the transform |
| 111 | + transformed_gm = RemoveCloneOpsTransform()(gm).graph_module |
| 112 | + |
| 113 | + # Verify the dq -> clone -> q pattern is removed and linear op is still present using FileCheck |
| 114 | + FileCheck().check_not( |
| 115 | + "executorch_exir_dialects_edge__ops_aten_clone_default" |
| 116 | + ).check_not("quantized_decomposed.dequantize_per_tensor.default").check_not( |
| 117 | + "quantized_decomposed.quantize_per_tensor.default" |
| 118 | + ).check_count( |
| 119 | + "torch._C._nn.linear", |
| 120 | + 1, |
| 121 | + exactly=True, |
| 122 | + ).run( |
| 123 | + transformed_gm.code |
| 124 | + ) |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + unittest.main() |
0 commit comments