|
| 1 | +# Copyright 2025 Arm Limited and/or its affiliates. |
| 2 | +# |
| 3 | +# This source code is licensed under the BSD-style license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +import torch |
| 7 | +from executorch.backends.arm.quantizer import ( |
| 8 | + get_symmetric_quantization_config, |
| 9 | + TOSAQuantizer, |
| 10 | +) |
| 11 | +from executorch.backends.arm.test.tester.test_pipeline import TosaPipelineINT |
| 12 | +from executorch.backends.arm.tosa import TosaSpecification |
| 13 | +from executorch.backends.xnnpack.test.tester import Quantize |
| 14 | + |
| 15 | + |
| 16 | +class AddSigmoidMul(torch.nn.Module): |
| 17 | + def __init__(self, *args, **kwargs): |
| 18 | + super().__init__(*args, **kwargs) |
| 19 | + self.sigmoid = torch.nn.Sigmoid() |
| 20 | + |
| 21 | + def forward(self, x, y): |
| 22 | + return self.sigmoid(x + y) * x |
| 23 | + |
| 24 | + |
| 25 | +def get_selective_quantizer(modules): |
| 26 | + quantizer = TOSAQuantizer(TosaSpecification.create_from_string("TOSA-1.0+INT")) |
| 27 | + quantizer.set_global(get_symmetric_quantization_config()) |
| 28 | + for module in modules: |
| 29 | + quantizer.set_module_type(module, None) |
| 30 | + |
| 31 | + return Quantize(quantizer, get_symmetric_quantization_config()) |
| 32 | + |
| 33 | + |
| 34 | +def test_qdq_squeezed_fp_op(): |
| 35 | + """Test that a float operation surrounded by quantize-dequantize pairs |
| 36 | + is correctly handled by the partitioner and the TOSA backend. |
| 37 | + Pattern: |
| 38 | + q -> dq -> add -> q -> dq -> sigmoid -> q -> dq -> mul -> dq -> q |
| 39 | + |_____Non-delegated____| |
| 40 | + """ |
| 41 | + aten_op = "torch.ops.aten.add.Tensor" |
| 42 | + exir_op = "executorch_exir_dialects_edge__ops_aten_add_Tensor" |
| 43 | + module = AddSigmoidMul() |
| 44 | + x = torch.randn(2, 3, 4) |
| 45 | + y = torch.randn(2, 3, 4) |
| 46 | + pipeline = TosaPipelineINT( |
| 47 | + module=module, test_data=(x, y), aten_op=aten_op, exir_op=exir_op |
| 48 | + ) |
| 49 | + pipeline.change_args("quantize", get_selective_quantizer([torch.nn.Sigmoid])) |
| 50 | + pipeline.change_args( |
| 51 | + "check_count.exir", |
| 52 | + { |
| 53 | + "torch.ops.higher_order.executorch_call_delegate": 2, |
| 54 | + "executorch_exir_dialects_edge__ops_aten_sigmoid_default": 1, |
| 55 | + "executorch_exir_dialects_edge__ops_quantized_decomposed_dequantize_per_tensor_default": 2, |
| 56 | + "executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default": 3, |
| 57 | + }, |
| 58 | + ) |
| 59 | + pipeline.run() |
| 60 | + |
| 61 | + |
| 62 | +class MulAddSigmoidConv(torch.nn.Module): |
| 63 | + def __init__(self, *args, **kwargs): |
| 64 | + super().__init__(*args, **kwargs) |
| 65 | + self.sigmoid = torch.nn.Sigmoid() |
| 66 | + self.conv = torch.nn.Conv1d(3, 3, 1) |
| 67 | + |
| 68 | + def forward(self, x, y): |
| 69 | + return self.conv(self.sigmoid(x + y * x)) |
| 70 | + |
| 71 | + |
| 72 | +def test_quantized_to_float_transition(): |
| 73 | + """Test that a model executing quantized ops followed by float ops |
| 74 | + is correctly handled by the partitioner and the TOSA backend. |
| 75 | + Pattern: |
| 76 | + q -> dq -> mul -> q -> dq -> add -> q -> dq -> sigmoid -> conv |
| 77 | + |____Non-delegated___| |
| 78 | + """ |
| 79 | + aten_op = "torch.ops.aten.add.Tensor" |
| 80 | + exir_op = "executorch_exir_dialects_edge__ops_aten_add_Tensor" |
| 81 | + module = MulAddSigmoidConv() |
| 82 | + x = torch.randn(2, 3, 4) |
| 83 | + y = torch.randn(2, 3, 4) |
| 84 | + pipeline = TosaPipelineINT( |
| 85 | + module=module, test_data=(x, y), aten_op=aten_op, exir_op=exir_op |
| 86 | + ) |
| 87 | + pipeline.change_args( |
| 88 | + "quantize", get_selective_quantizer([torch.nn.Sigmoid, torch.nn.Conv1d]) |
| 89 | + ) |
| 90 | + pipeline.change_args( |
| 91 | + "check_count.exir", |
| 92 | + { |
| 93 | + "torch.ops.higher_order.executorch_call_delegate": 1, |
| 94 | + "executorch_exir_dialects_edge__ops_aten_sigmoid_default": 1, |
| 95 | + "executorch_exir_dialects_edge__ops_aten_convolution_default": 1, |
| 96 | + "executorch_exir_dialects_edge__ops_quantized_decomposed_dequantize_per_tensor_default": 1, |
| 97 | + "executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default": 2, |
| 98 | + }, |
| 99 | + ) |
| 100 | + pipeline.run() |
0 commit comments