|
| 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 | +import torch |
| 10 | +import torchvision.models as models |
| 11 | +from executorch.backends.xnnpack.partition.xnnpack_partitioner import ( |
| 12 | + XnnpackQuantizedPartitioner2, |
| 13 | +) |
| 14 | +from executorch.backends.xnnpack.test.tester import Partition, Tester |
| 15 | +from executorch.backends.xnnpack.test.tester.tester import Export |
| 16 | +from executorch.backends.xnnpack.utils.configs import get_xnnpack_capture_config |
| 17 | +from torchvision.models.mobilenetv2 import MobileNet_V2_Weights |
| 18 | + |
| 19 | + |
| 20 | +class TestXNNPACKMobileNetV2(unittest.TestCase): |
| 21 | + export_stage = Export(get_xnnpack_capture_config(enable_aot=True)) |
| 22 | + |
| 23 | + mv2 = models.__dict__["mobilenet_v2"](weights=MobileNet_V2_Weights) |
| 24 | + mv2 = mv2.eval() |
| 25 | + model_inputs = (torch.ones(1, 3, 224, 244),) |
| 26 | + |
| 27 | + all_operators = { |
| 28 | + "executorch_exir_dialects_edge__ops_aten__native_batch_norm_legit_no_training_default", |
| 29 | + "executorch_exir_dialects_edge__ops_aten_add_Tensor", |
| 30 | + "executorch_exir_dialects_edge__ops_aten_permute_copy_default", |
| 31 | + "executorch_exir_dialects_edge__ops_aten_addmm_default", |
| 32 | + "executorch_exir_dialects_edge__ops_aten_mean_dim", |
| 33 | + "executorch_exir_dialects_edge__ops_aten_hardtanh_default", |
| 34 | + "executorch_exir_dialects_edge__ops_aten_convolution_default", |
| 35 | + } |
| 36 | + |
| 37 | + def test_fp32(self): |
| 38 | + |
| 39 | + ( |
| 40 | + Tester(self.mv2, self.model_inputs) |
| 41 | + .export(self.export_stage) |
| 42 | + .to_edge() |
| 43 | + .check(list(self.all_operators)) |
| 44 | + .partition() |
| 45 | + .check(["torch.ops.executorch_call_delegate"]) |
| 46 | + .check_not(list(self.all_operators)) |
| 47 | + .to_executorch() |
| 48 | + .serialize() |
| 49 | + .run_method() |
| 50 | + .compare_outputs() |
| 51 | + ) |
| 52 | + |
| 53 | + def test_qs8_pt2e(self): |
| 54 | + # Quantization fuses away batchnorm, so it is no longer in the graph |
| 55 | + ops_after_quantization = self.all_operators - { |
| 56 | + "executorch_exir_dialects_edge__ops_aten__native_batch_norm_legit_no_training_default", |
| 57 | + } |
| 58 | + |
| 59 | + ( |
| 60 | + Tester(self.mv2, self.model_inputs) |
| 61 | + .quantize2() |
| 62 | + .export(self.export_stage) |
| 63 | + .to_edge() |
| 64 | + .check(list(ops_after_quantization)) |
| 65 | + .partition(Partition(partitioner=XnnpackQuantizedPartitioner2)) |
| 66 | + .check(["torch.ops.executorch_call_delegate"]) |
| 67 | + .check_not(list(ops_after_quantization)) |
| 68 | + .to_executorch() |
| 69 | + .serialize() |
| 70 | + .run_method() |
| 71 | + .compare_outputs() |
| 72 | + ) |
0 commit comments