|
| 1 | +# Copyright 2026 Arm Limited and/or its affiliates. |
| 2 | +# |
| 3 | +# This source code is licensed under the BSD-style license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | +"""Ethos-U FVP tests for the MLPerf Tiny anomaly detection Deep AutoEncoder.""" |
| 6 | + |
| 7 | +from typing import Tuple |
| 8 | + |
| 9 | +import pytest |
| 10 | +import torch |
| 11 | +import torch.nn as nn |
| 12 | +from executorch.backends.arm.test import common |
| 13 | +from executorch.backends.arm.test.tester.test_pipeline import ( |
| 14 | + EthosU55PipelineINT, |
| 15 | + EthosU85PipelineINT, |
| 16 | + TosaPipelineFP, |
| 17 | + TosaPipelineINT, |
| 18 | +) |
| 19 | + |
| 20 | +from executorch.examples.models.mlperf_tiny import DeepAutoEncoderModel |
| 21 | +from torch.nn.utils.fusion import fuse_linear_bn_eval |
| 22 | + |
| 23 | + |
| 24 | +def _fuse_linear_bn(mod: nn.Module) -> nn.Module: |
| 25 | + """Fuse Linear + BatchNorm1d pairs in the model. |
| 26 | +
|
| 27 | + The TOSA quantizer does not annotate linear+batch_norm patterns, so we fold |
| 28 | + the BatchNorm1d into the preceding Linear before export. |
| 29 | + TODO: Remove once the quantizer supports linear+bn. |
| 30 | +
|
| 31 | + """ |
| 32 | + if not isinstance(mod, nn.Sequential): |
| 33 | + for name, child in mod.named_children(): |
| 34 | + setattr(mod, name, _fuse_linear_bn(child)) |
| 35 | + return mod |
| 36 | + new_layers = [] |
| 37 | + layers = list(mod) |
| 38 | + i = 0 |
| 39 | + while i < len(layers): |
| 40 | + if ( |
| 41 | + isinstance(layers[i], nn.Linear) |
| 42 | + and i + 1 < len(layers) |
| 43 | + and isinstance(layers[i + 1], nn.BatchNorm1d) |
| 44 | + ): |
| 45 | + new_layers.append(fuse_linear_bn_eval(layers[i], layers[i + 1])) # type: ignore[type-var, arg-type] |
| 46 | + i += 2 |
| 47 | + else: |
| 48 | + new_layers.append(_fuse_linear_bn(layers[i])) |
| 49 | + i += 1 |
| 50 | + return nn.Sequential(*new_layers) |
| 51 | + |
| 52 | + |
| 53 | +_wrapper = DeepAutoEncoderModel() |
| 54 | +model = _fuse_linear_bn(_wrapper.get_eager_model()) |
| 55 | +model_inputs = _wrapper.get_example_inputs() |
| 56 | +input_t = Tuple[torch.Tensor] |
| 57 | + |
| 58 | +quant_test_data = { |
| 59 | + "per_channel_quantization=true": True, |
| 60 | + "per_channel_quantization=false": False, |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +def test_deep_autoencoder_tosa_FP(): |
| 65 | + pipeline = TosaPipelineFP[input_t]( |
| 66 | + model, |
| 67 | + model_inputs, |
| 68 | + aten_op=[], |
| 69 | + exir_op=[], |
| 70 | + use_to_edge_transform_and_lower=True, |
| 71 | + ) |
| 72 | + pipeline.run() |
| 73 | + |
| 74 | + |
| 75 | +@common.parametrize("per_channel_quantization", quant_test_data) |
| 76 | +def test_deep_autoencoder_tosa_INT(per_channel_quantization): |
| 77 | + pipeline = TosaPipelineINT[input_t]( |
| 78 | + model, |
| 79 | + model_inputs, |
| 80 | + aten_op=[], |
| 81 | + exir_op=[], |
| 82 | + use_to_edge_transform_and_lower=True, |
| 83 | + per_channel_quantization=per_channel_quantization, |
| 84 | + atol=0.25, |
| 85 | + qtol=1, |
| 86 | + frobenius_threshold=None, |
| 87 | + cosine_threshold=None, |
| 88 | + ) |
| 89 | + pipeline.run() |
| 90 | + |
| 91 | + |
| 92 | +@pytest.mark.slow |
| 93 | +@common.XfailIfNoCorstone300 |
| 94 | +@common.parametrize("per_channel_quantization", quant_test_data) |
| 95 | +def test_deep_autoencoder_u55_INT(per_channel_quantization): |
| 96 | + pipeline = EthosU55PipelineINT[input_t]( |
| 97 | + model, |
| 98 | + model_inputs, |
| 99 | + aten_ops=[], |
| 100 | + exir_ops=[], |
| 101 | + use_to_edge_transform_and_lower=True, |
| 102 | + per_channel_quantization=per_channel_quantization, |
| 103 | + atol=0.25, |
| 104 | + qtol=1, |
| 105 | + ) |
| 106 | + pipeline.run() |
| 107 | + |
| 108 | + |
| 109 | +@pytest.mark.slow |
| 110 | +@common.XfailIfNoCorstone320 |
| 111 | +@common.parametrize("per_channel_quantization", quant_test_data) |
| 112 | +def test_deep_autoencoder_u85_INT(per_channel_quantization): |
| 113 | + pipeline = EthosU85PipelineINT[input_t]( |
| 114 | + model, |
| 115 | + model_inputs, |
| 116 | + aten_ops=[], |
| 117 | + exir_ops=[], |
| 118 | + use_to_edge_transform_and_lower=True, |
| 119 | + per_channel_quantization=per_channel_quantization, |
| 120 | + atol=0.25, |
| 121 | + qtol=1, |
| 122 | + ) |
| 123 | + pipeline.run() |
0 commit comments