|
| 1 | +# Copyright 2025 NXP |
| 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 | +import itertools |
| 6 | + |
| 7 | +import executorch.kernels.quantized # noqa F401 |
| 8 | +import torch |
| 9 | +from executorch.backends.nxp.tests.executorch_pipeline import to_quantized_edge_program |
| 10 | +from executorch.backends.nxp.tests.models import Conv2dReLUModule |
| 11 | +from executorch.examples.nxp.experimental.cifar_net.cifar_net import CifarNet |
| 12 | +from executorch.exir import ExecutorchBackendConfig |
| 13 | +from executorch.exir.passes.quantize_io_pass import get_config_method_name |
| 14 | + |
| 15 | + |
| 16 | +def test_remove_io_quant_ops_pass__conv_relu(): |
| 17 | + model = Conv2dReLUModule() |
| 18 | + model.eval() |
| 19 | + |
| 20 | + input_shape = (1, 4, 32, 32) |
| 21 | + edge_program_manager = to_quantized_edge_program( |
| 22 | + model, input_shape, remove_quant_io_ops=True |
| 23 | + ) |
| 24 | + |
| 25 | + exec_prog = edge_program_manager.to_executorch( |
| 26 | + config=ExecutorchBackendConfig(extract_delegate_segments=False) |
| 27 | + ) |
| 28 | + |
| 29 | + nodes = list(exec_prog.exported_program().graph.nodes) |
| 30 | + assert ( |
| 31 | + nodes[0].meta["val"].dtype == torch.int8 |
| 32 | + ), "Input tensor doesn't have type INT8." |
| 33 | + assert nodes[2].name == "executorch_call_delegate" |
| 34 | + assert ( |
| 35 | + nodes[4].meta["val"][0].dtype == torch.int8 |
| 36 | + ), "Output tensor doesn't have type INT8." |
| 37 | + |
| 38 | + assert ( |
| 39 | + get_config_method_name(None, "input", 0, "scale") in exec_prog._config_methods |
| 40 | + ) |
| 41 | + assert get_config_method_name(None, "input", 0, "zp") in exec_prog._config_methods |
| 42 | + assert ( |
| 43 | + get_config_method_name(None, "output", 0, "scale") in exec_prog._config_methods |
| 44 | + ) |
| 45 | + assert get_config_method_name(None, "output", 0, "zp") in exec_prog._config_methods |
| 46 | + |
| 47 | + |
| 48 | +def test_remove_io_quant_ops_pass__cifarnet(): |
| 49 | + model = CifarNet().get_eager_model() |
| 50 | + input_shape = (1, 3, 32, 32) |
| 51 | + edge_program_manager = to_quantized_edge_program( |
| 52 | + model, input_shape, remove_quant_io_ops=True |
| 53 | + ) |
| 54 | + |
| 55 | + exec_prog = edge_program_manager.to_executorch( |
| 56 | + config=ExecutorchBackendConfig(extract_delegate_segments=False) |
| 57 | + ) |
| 58 | + |
| 59 | + nodes = list(exec_prog.exported_program().graph.nodes) |
| 60 | + assert len(nodes) == 17 |
| 61 | + assert ( |
| 62 | + nodes[0].meta["val"].dtype == torch.int8 |
| 63 | + ), "Input tensor doesn't have type INT8." |
| 64 | + assert ( |
| 65 | + nodes[16].meta["val"][0].dtype == torch.int8 |
| 66 | + ), "Output tensor doesn't have type INT8." |
| 67 | + |
| 68 | + assert ( |
| 69 | + get_config_method_name(None, "input", 0, "scale") in exec_prog._config_methods |
| 70 | + ) |
| 71 | + assert get_config_method_name(None, "input", 0, "zp") in exec_prog._config_methods |
| 72 | + assert ( |
| 73 | + get_config_method_name(None, "output", 0, "scale") in exec_prog._config_methods |
| 74 | + ) |
| 75 | + assert get_config_method_name(None, "output", 0, "zp") in exec_prog._config_methods |
| 76 | + |
| 77 | + |
| 78 | +class MultiInputOutputModule(torch.nn.Module): |
| 79 | + def __init__(self): |
| 80 | + super().__init__() |
| 81 | + |
| 82 | + self.conv = torch.nn.Conv2d(4, 64, 2, bias=False) |
| 83 | + self.relu = torch.nn.ReLU() |
| 84 | + |
| 85 | + def forward(self, x, y): |
| 86 | + z = self.relu(x) |
| 87 | + x = self.conv(z) |
| 88 | + return x + y, z |
| 89 | + |
| 90 | + |
| 91 | +def test_multiple_inputs__multiple_outputs(): |
| 92 | + model = MultiInputOutputModule() |
| 93 | + model.eval() |
| 94 | + |
| 95 | + input_shape = [(1, 4, 32, 32), (1, 1, 1, 31)] |
| 96 | + edge_program_manager = to_quantized_edge_program( |
| 97 | + model, input_shape, remove_quant_io_ops=True |
| 98 | + ) |
| 99 | + |
| 100 | + exec_prog = edge_program_manager.to_executorch( |
| 101 | + config=ExecutorchBackendConfig(extract_delegate_segments=False) |
| 102 | + ) |
| 103 | + |
| 104 | + nodes = list(exec_prog.exported_program().graph.nodes) |
| 105 | + print(nodes) |
| 106 | + assert ( |
| 107 | + nodes[0].meta["val"].dtype == torch.int8 |
| 108 | + ), "Input tensor doesn't have type INT8." |
| 109 | + assert nodes[3].name == "executorch_call_delegate" |
| 110 | + assert ( |
| 111 | + nodes[-1].meta["val"][0].dtype == torch.int8 |
| 112 | + ), "Output tensor doesn't have type INT8." |
| 113 | + |
| 114 | + quant_method_variants = itertools.product( |
| 115 | + ["input", "output"], [0, 1], ["scale", "zp"] |
| 116 | + ) |
| 117 | + |
| 118 | + expected_methods = [ |
| 119 | + get_config_method_name(None, arg_type, index, key) |
| 120 | + for arg_type, index, key in quant_method_variants |
| 121 | + ] |
| 122 | + assert all(method in exec_prog._config_methods for method in expected_methods) |
0 commit comments