|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | + |
| 8 | +from typing import List |
| 9 | + |
| 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 | + torch.testing.assert_close(ref_outputs[i], et_outputs[i], atol=1e-4, rtol=1e-5) |
| 57 | + |
| 58 | + |
| 59 | +def run_tests(model_tests: List[ModelTest]) -> None: |
| 60 | + for model_test in model_tests: |
| 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}.") |
| 65 | + |
| 66 | + |
| 67 | +if __name__ == "__main__": |
| 68 | + run_tests( |
| 69 | + model_tests=[ |
| 70 | + ModelTest( |
| 71 | + model=Model.Mv3, |
| 72 | + backend=Backend.Xnnpack, |
| 73 | + ), |
| 74 | + ] |
| 75 | + ) |
0 commit comments