|
5 | 5 | # This source code is licensed under the BSD-style license found in the |
6 | 6 | # LICENSE file in the root directory of this source tree. |
7 | 7 |
|
8 | | -import os |
9 | | -import subprocess |
10 | | -import test_base |
11 | | -from examples.models import Backend, Model |
12 | | -from test_base import ModelTest |
13 | 8 | from typing import List |
14 | 9 |
|
15 | | -def map_backend_name(name: str) -> str: |
16 | | - # Map the backend name to the string used by the Windows test jobs, which use |
17 | | - # a slightly different convention. This is an artifact of us being mid-update |
18 | | - # of the model test logic. |
19 | | - # TODO(gjcomer) Clean this up when we update the model test CI. |
| 10 | +import torch |
| 11 | +from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner |
| 12 | +from executorch.examples.models import Backend, Model, MODEL_NAME_TO_MODEL |
| 13 | +from executorch.examples.models.model_factory import EagerModelFactory |
| 14 | +from executorch.examples.xnnpack import MODEL_NAME_TO_OPTIONS |
| 15 | +from executorch.examples.xnnpack.quantization.utils import quantize as quantize_xnn |
| 16 | +from executorch.exir import EdgeCompileConfig, to_edge_transform_and_lower |
| 17 | +from executorch.extension.pybindings.portable_lib import ( |
| 18 | + _load_for_executorch_from_buffer, |
| 19 | +) |
| 20 | +from test_base import ModelTest |
| 21 | + |
| 22 | + |
| 23 | +def test_model_xnnpack(model: Model, quantize: bool) -> None: |
| 24 | + model_instance, example_inputs, _, _ = EagerModelFactory.create_model( |
| 25 | + *MODEL_NAME_TO_MODEL[str(model)] |
| 26 | + ) |
| 27 | + |
| 28 | + model_instance.eval() |
| 29 | + ref_outputs = model_instance(*example_inputs) |
| 30 | + |
| 31 | + if quantize: |
| 32 | + quant_type = MODEL_NAME_TO_OPTIONS[str(model)].quantization |
| 33 | + model_instance = torch.export.export_for_training( |
| 34 | + model_instance, example_inputs |
| 35 | + ) |
| 36 | + model_instance = quantize_xnn( |
| 37 | + model_instance.module(), example_inputs, quant_type |
| 38 | + ) |
| 39 | + |
| 40 | + lowered = to_edge_transform_and_lower( |
| 41 | + torch.export.export(model_instance, example_inputs), |
| 42 | + partitioner=[XnnpackPartitioner()], |
| 43 | + compile_config=EdgeCompileConfig( |
| 44 | + _check_ir_validity=False, |
| 45 | + ), |
| 46 | + ).to_executorch() |
| 47 | + |
| 48 | + loaded_model = _load_for_executorch_from_buffer(lowered.buffer) |
| 49 | + et_outputs = loaded_model([*example_inputs]) |
| 50 | + |
| 51 | + if isinstance(ref_outputs, torch.Tensor): |
| 52 | + ref_outputs = (ref_outputs,) |
| 53 | + |
| 54 | + assert len(ref_outputs) == len(et_outputs) |
| 55 | + for i in range(len(ref_outputs)): |
| 56 | + assert torch.allclose(ref_outputs[i], et_outputs[i], atol=1e-5) |
20 | 57 |
|
21 | | - if name == "xnnpack-quantization-delegation": |
22 | | - return "xnnpack-q8" |
23 | | - else: |
24 | | - return name |
25 | 58 |
|
26 | 59 | def run_tests(model_tests: List[ModelTest]) -> None: |
27 | 60 | for model_test in model_tests: |
28 | | - subprocess.run( |
29 | | - [ |
30 | | - "powershell.exe", |
31 | | - os.path.join(test_base._repository_root_dir(), ".ci/scripts/test_model.ps1"), |
32 | | - "-ModelName", |
33 | | - str(model_test.model), |
34 | | - "-Backend", |
35 | | - map_backend_name(str(model_test.backend)), |
36 | | - ], |
37 | | - check=True, |
38 | | - cwd=test_base._repository_root_dir(), |
39 | | - ) |
| 61 | + if model_test.backend == Backend.Xnnpack: |
| 62 | + test_model_xnnpack(model_test.model, quantize=False) |
| 63 | + else: |
| 64 | + raise RuntimeError(f"Unsupported backend {model_test.backend}.") |
40 | 65 |
|
41 | 66 |
|
42 | 67 | if __name__ == "__main__": |
43 | 68 | run_tests( |
44 | 69 | model_tests=[ |
45 | 70 | ModelTest( |
46 | 71 | model=Model.Mv3, |
47 | | - backend=Backend.XnnpackQuantizationDelegation, |
| 72 | + backend=Backend.Xnnpack, |
48 | 73 | ), |
49 | 74 | ] |
50 | 75 | ) |
0 commit comments