|
| 1 | +# Copyright © 2023 Apple Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Please refer to the license found in the LICENSE file in the root directory of the source tree. |
| 4 | + |
| 5 | +import platform |
| 6 | +import sys |
| 7 | +import unittest |
| 8 | + |
| 9 | +import coremltools as ct |
| 10 | + |
| 11 | +import executorch.exir |
| 12 | + |
| 13 | +import torch |
| 14 | + |
| 15 | +from executorch.backends.apple.coreml.compiler import CoreMLBackend |
| 16 | +from executorch.backends.apple.coreml.partition import CoreMLPartitioner |
| 17 | +from executorch.runtime import Runtime |
| 18 | +from torchao.quantization import IntxWeightOnlyConfig, PerAxis, PerGroup, quantize_ |
| 19 | + |
| 20 | +_TEST_RUNTIME = sys.platform == "darwin" and tuple( |
| 21 | + map(int, platform.mac_ver()[0].split(".")) |
| 22 | +) >= (15, 0) |
| 23 | + |
| 24 | + |
| 25 | +class TestTorchOps(unittest.TestCase): |
| 26 | + edge_compile_config = executorch.exir.EdgeCompileConfig() |
| 27 | + |
| 28 | + def _coreml_partitioner(self): |
| 29 | + compile_specs = CoreMLBackend.generate_compile_specs( |
| 30 | + minimum_deployment_target=ct.target.iOS18 |
| 31 | + ) |
| 32 | + return CoreMLPartitioner(compile_specs=compile_specs) |
| 33 | + |
| 34 | + def _get_test_model(self): |
| 35 | + model = torch.nn.Sequential( |
| 36 | + torch.nn.Embedding(64, 128), torch.nn.Linear(128, 128), torch.nn.ReLU() |
| 37 | + ) |
| 38 | + example_inputs = (torch.LongTensor([0]),) |
| 39 | + return model, example_inputs |
| 40 | + |
| 41 | + def _compare_outputs(self, executorch_program, eager_program, example_inputs): |
| 42 | + if not _TEST_RUNTIME: |
| 43 | + return |
| 44 | + runtime = Runtime.get() |
| 45 | + program = runtime.load_program(executorch_program.buffer) |
| 46 | + method = program.load_method("forward") |
| 47 | + et_outputs = method.execute(example_inputs)[0] |
| 48 | + eager_outputs = eager_program(*example_inputs) |
| 49 | + self.assertTrue( |
| 50 | + torch.allclose(et_outputs, eager_outputs, atol=1e-02, rtol=1e-02) |
| 51 | + ) |
| 52 | + |
| 53 | + def test_dequantize_affine_b4w_embedding(self): |
| 54 | + model, example_inputs = self._get_test_model() |
| 55 | + quantize_( |
| 56 | + model, |
| 57 | + IntxWeightOnlyConfig(weight_dtype=torch.int4, granularity=PerGroup(32)), |
| 58 | + lambda m, fqn: isinstance(m, torch.nn.Embedding), |
| 59 | + ) |
| 60 | + ep = torch.export.export(model, example_inputs) |
| 61 | + delegated_program = executorch.exir.to_edge_transform_and_lower( |
| 62 | + ep, |
| 63 | + partitioner=[self._coreml_partitioner()], |
| 64 | + ) |
| 65 | + for node in delegated_program.exported_program().graph.nodes: |
| 66 | + if node.op == "call_function": |
| 67 | + assert node.target.__name__ in [ |
| 68 | + "executorch_call_delegate", |
| 69 | + "getitem", |
| 70 | + ], f"Got unexpected node target after delegation: {node.target.__name__}" |
| 71 | + et_prog = delegated_program.to_executorch() |
| 72 | + self._compare_outputs(et_prog, model, example_inputs) |
| 73 | + |
| 74 | + def test_dequantize_affine_b4w_linear(self): |
| 75 | + model, example_inputs = self._get_test_model() |
| 76 | + quantize_( |
| 77 | + model, |
| 78 | + IntxWeightOnlyConfig(weight_dtype=torch.int4, granularity=PerGroup(32)), |
| 79 | + ) |
| 80 | + ep = torch.export.export(model, example_inputs) |
| 81 | + delegated_program = executorch.exir.to_edge_transform_and_lower( |
| 82 | + ep, |
| 83 | + partitioner=[self._coreml_partitioner()], |
| 84 | + ) |
| 85 | + for node in delegated_program.exported_program().graph.nodes: |
| 86 | + if node.op == "call_function": |
| 87 | + assert node.target.__name__ in [ |
| 88 | + "executorch_call_delegate", |
| 89 | + "getitem", |
| 90 | + ], f"Got unexpected node target after delegation: {node.target.__name__}" |
| 91 | + et_prog = delegated_program.to_executorch() |
| 92 | + self._compare_outputs(et_prog, model, example_inputs) |
| 93 | + |
| 94 | + def test_dequantize_affine_c4w_embedding(self): |
| 95 | + model, example_inputs = self._get_test_model() |
| 96 | + quantize_( |
| 97 | + model, |
| 98 | + IntxWeightOnlyConfig(weight_dtype=torch.int4, granularity=PerAxis(0)), |
| 99 | + lambda m, fqn: isinstance(m, torch.nn.Embedding), |
| 100 | + ) |
| 101 | + ep = torch.export.export(model, example_inputs) |
| 102 | + delegated_program = executorch.exir.to_edge_transform_and_lower( |
| 103 | + ep, |
| 104 | + partitioner=[self._coreml_partitioner()], |
| 105 | + ) |
| 106 | + for node in delegated_program.exported_program().graph.nodes: |
| 107 | + if node.op == "call_function": |
| 108 | + assert node.target.__name__ in [ |
| 109 | + "executorch_call_delegate", |
| 110 | + "getitem", |
| 111 | + ], f"Got unexpected node target after delegation: {node.target.__name__}" |
| 112 | + et_prog = delegated_program.to_executorch() |
| 113 | + self._compare_outputs(et_prog, model, example_inputs) |
| 114 | + |
| 115 | + def test_dequantize_affine_c4w_linear(self): |
| 116 | + model, example_inputs = self._get_test_model() |
| 117 | + quantize_( |
| 118 | + model, IntxWeightOnlyConfig(weight_dtype=torch.int4, granularity=PerAxis(0)) |
| 119 | + ) |
| 120 | + ep = torch.export.export(model, example_inputs) |
| 121 | + delegated_program = executorch.exir.to_edge_transform_and_lower( |
| 122 | + ep, |
| 123 | + partitioner=[self._coreml_partitioner()], |
| 124 | + ) |
| 125 | + for node in delegated_program.exported_program().graph.nodes: |
| 126 | + if node.op == "call_function": |
| 127 | + assert node.target.__name__ in [ |
| 128 | + "executorch_call_delegate", |
| 129 | + "getitem", |
| 130 | + ], f"Got unexpected node target after delegation: {node.target.__name__}" |
| 131 | + et_prog = delegated_program.to_executorch() |
| 132 | + self._compare_outputs(et_prog, model, example_inputs) |
| 133 | + |
| 134 | + def test_dequantize_affine_c8w_embedding_b4w_linear(self): |
| 135 | + model, example_inputs = self._get_test_model() |
| 136 | + quantize_( |
| 137 | + model, |
| 138 | + IntxWeightOnlyConfig(weight_dtype=torch.int8, granularity=PerAxis(0)), |
| 139 | + lambda m, fqn: isinstance(m, torch.nn.Embedding), |
| 140 | + ) |
| 141 | + quantize_( |
| 142 | + model, |
| 143 | + IntxWeightOnlyConfig(weight_dtype=torch.int4, granularity=PerGroup(32)), |
| 144 | + ) |
| 145 | + ep = torch.export.export(model, example_inputs) |
| 146 | + delegated_program = executorch.exir.to_edge_transform_and_lower( |
| 147 | + ep, |
| 148 | + partitioner=[self._coreml_partitioner()], |
| 149 | + ) |
| 150 | + for node in delegated_program.exported_program().graph.nodes: |
| 151 | + if node.op == "call_function": |
| 152 | + assert node.target.__name__ in [ |
| 153 | + "executorch_call_delegate", |
| 154 | + "getitem", |
| 155 | + ], f"Got unexpected node target after delegation: {node.target.__name__}" |
| 156 | + et_prog = delegated_program.to_executorch() |
| 157 | + self._compare_outputs(et_prog, model, example_inputs) |
| 158 | + |
| 159 | + |
| 160 | +if __name__ == "__main__": |
| 161 | + test_runner = TestTorchOps() |
| 162 | + test_runner.test_dequantize_affine_b4w_embedding() |
| 163 | + test_runner.test_dequantize_affine_b4w_linear() |
| 164 | + test_runner.test_dequantize_affine_c4w_embedding() |
| 165 | + test_runner.test_dequantize_affine_c4w_linear() |
| 166 | + test_runner.test_dequantize_affine_c8w_embedding_b4w_linear() |
0 commit comments