|
| 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 | +from typing import Callable, Optional, Tuple, Union |
| 10 | + |
| 11 | +import executorch.backends.test.harness.stages as BaseStages |
| 12 | + |
| 13 | +import torch |
| 14 | +from executorch.backends.test.harness.stages import StageType |
| 15 | +from executorch.backends.xnnpack.partition.config.xnnpack_config import ( |
| 16 | + ConfigPrecisionType, |
| 17 | +) |
| 18 | +from executorch.backends.xnnpack.partition.xnnpack_partitioner import ( |
| 19 | + XnnpackFloatingPointPartitioner, |
| 20 | + XnnpackPartitioner, |
| 21 | +) |
| 22 | + |
| 23 | +from executorch.backends.xnnpack.quantizer.xnnpack_quantizer import ( |
| 24 | + get_symmetric_quantization_config, |
| 25 | +) |
| 26 | +from executorch.backends.xnnpack.test.tester import ( |
| 27 | + Quantize as XNNPackQuantize, |
| 28 | + RunPasses, |
| 29 | + Tester, |
| 30 | +) |
| 31 | +from executorch.backends.xnnpack.test.tester.tester import ToEdgeTransformAndLower |
| 32 | +from executorch.exir import ( |
| 33 | + EdgeCompileConfig, |
| 34 | + ExecutorchBackendConfig, |
| 35 | + to_edge_transform_and_lower, |
| 36 | +) |
| 37 | +from executorch.exir.passes.external_constants_pass import ( |
| 38 | + delegate_external_constants_pass_unlifted, |
| 39 | +) |
| 40 | +from torch.export import export, ExportedProgram |
| 41 | + |
| 42 | +from torchao.quantization.granularity import PerAxis, PerGroup |
| 43 | +from torchao.quantization.quant_api import ( |
| 44 | + Int8DynamicActivationIntxWeightConfig, |
| 45 | + IntxWeightOnlyConfig, |
| 46 | + quantize_, |
| 47 | +) |
| 48 | +from torchao.utils import unwrap_tensor_subclass |
| 49 | + |
| 50 | +try: |
| 51 | + import executorch.extension.pybindings.portable_lib # noqa[F401] |
| 52 | + import executorch.kernels.quantized # noqa[F401] |
| 53 | + |
| 54 | + has_quantized_ops = True |
| 55 | +except: |
| 56 | + has_quantized_ops = False |
| 57 | + print("Missing quantized ops") |
| 58 | + |
| 59 | + |
| 60 | +class TestPropagateCustomMetaPass(unittest.TestCase): |
| 61 | + class ModuleLinear(torch.nn.Module): |
| 62 | + def __init__( |
| 63 | + self, |
| 64 | + in_size: int = 2, |
| 65 | + input_channels: int = 4, |
| 66 | + output_channels: int = 4, |
| 67 | + dtype: torch.dtype = torch.float, |
| 68 | + use_bias: bool = False, |
| 69 | + ): |
| 70 | + super().__init__() |
| 71 | + self.linear = torch.nn.Linear( |
| 72 | + input_channels, output_channels, bias=use_bias |
| 73 | + ).to(dtype=dtype) |
| 74 | + |
| 75 | + self.ic = input_channels |
| 76 | + self.oc = output_channels |
| 77 | + assert dtype in [torch.float, torch.half], "Unsupported op dtype" |
| 78 | + self.op_dtype = dtype |
| 79 | + self.in_size = in_size |
| 80 | + |
| 81 | + def forward(self, x: torch.Tensor): |
| 82 | + return self.linear(x) |
| 83 | + |
| 84 | + def get_random_inputs(self): |
| 85 | + inp = torch.randn(self.in_size, self.ic).to(self.op_dtype) |
| 86 | + return (inp,) |
| 87 | + |
| 88 | + class Export(BaseStages.Export): |
| 89 | + def run( |
| 90 | + self, |
| 91 | + artifact: torch.nn.Module, |
| 92 | + inputs: Tuple[torch.Tensor], |
| 93 | + ) -> None: |
| 94 | + |
| 95 | + tagged_module = export( |
| 96 | + artifact, inputs, dynamic_shapes=self.dynamic_shapes, strict=True |
| 97 | + ).module() |
| 98 | + delegate_external_constants_pass_unlifted( |
| 99 | + module=tagged_module, |
| 100 | + gen_tag_fn=lambda x: "model", # This is the filename the weights will be saved to. In this case, weights will be saved as "model.ptd" |
| 101 | + ) |
| 102 | + self.exported_program = export( |
| 103 | + tagged_module, inputs, dynamic_shapes=self.dynamic_shapes, strict=True |
| 104 | + ) |
| 105 | + |
| 106 | + def _test_linear( |
| 107 | + self, |
| 108 | + partitioner: XnnpackPartitioner, |
| 109 | + quantization_stage: Union[BaseStages.Quantize, BaseStages.Quantize_], |
| 110 | + ): |
| 111 | + eager_model = self.ModuleLinear( |
| 112 | + in_size=1, |
| 113 | + input_channels=32, |
| 114 | + output_channels=2, |
| 115 | + ) |
| 116 | + test_inputs = eager_model.get_random_inputs() |
| 117 | + |
| 118 | + tester = Tester(eager_model, test_inputs) |
| 119 | + tester.quantize(quantization_stage) |
| 120 | + tester.export(self.Export()) |
| 121 | + tester.to_edge_transform_and_lower( |
| 122 | + ToEdgeTransformAndLower([partitioner]) |
| 123 | + ).to_executorch() |
| 124 | + tester.run_method_and_compare_outputs() |
| 125 | + |
| 126 | + exec = tester.get_artifact() |
| 127 | + program_buffer = exec.buffer |
| 128 | + self.assertEqual(len(exec._tensor_data), 1) |
| 129 | + data_buffer = bytes(exec._tensor_data.pop("model")) |
| 130 | + from executorch.extension.pybindings import portable_lib as runtime |
| 131 | + |
| 132 | + module = runtime._load_for_executorch_from_buffer(program_buffer, data_buffer) |
| 133 | + output = module.forward(test_inputs) |
| 134 | + |
| 135 | + def test_quantize_(self): |
| 136 | + # Quantize with torchao quantize_ API. |
| 137 | + DynamicallyQuantizedPartitioner = XnnpackPartitioner( |
| 138 | + config_precisions=ConfigPrecisionType.DYNAMIC_QUANT, |
| 139 | + per_op_mode=False, |
| 140 | + ) |
| 141 | + linear_config = Int8DynamicActivationIntxWeightConfig( |
| 142 | + weight_dtype=torch.int4, |
| 143 | + weight_granularity=PerGroup(32), |
| 144 | + ) |
| 145 | + self._test_linear( |
| 146 | + DynamicallyQuantizedPartitioner, BaseStages.Quantize_(config=linear_config) |
| 147 | + ) |
| 148 | + |
| 149 | + def test_pt2e_quantize(self): |
| 150 | + # Quantize with pt2e quantize. |
| 151 | + quant_configs = [ |
| 152 | + # per_channel |
| 153 | + get_symmetric_quantization_config(is_per_channel=True, is_dynamic=False), |
| 154 | + # per_tensor |
| 155 | + get_symmetric_quantization_config(is_per_channel=False, is_dynamic=False), |
| 156 | + # per_channel_dynamic |
| 157 | + get_symmetric_quantization_config(is_per_channel=True, is_dynamic=True), |
| 158 | + ] |
| 159 | + partitioners = [] |
| 160 | + for config_precision in [ |
| 161 | + ConfigPrecisionType.STATIC_QUANT, |
| 162 | + ConfigPrecisionType.DYNAMIC_QUANT, |
| 163 | + ]: |
| 164 | + for per_op_mode in [True, False]: |
| 165 | + partitioners.append( |
| 166 | + XnnpackPartitioner( |
| 167 | + config_precisions=config_precision, |
| 168 | + per_op_mode=per_op_mode, |
| 169 | + ) |
| 170 | + ) |
| 171 | + for quant_config in quant_configs: |
| 172 | + for partitioner in partitioners: |
| 173 | + self._test_linear( |
| 174 | + partitioner, XNNPackQuantize(quantization_config=quant_config) |
| 175 | + ) |
0 commit comments