|
| 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 | +from types import SimpleNamespace |
| 8 | + |
| 9 | +import pytest |
| 10 | +import torch |
| 11 | +from executorch.backends.cortex_m.target_config import CortexM, CortexMTargetConfig |
| 12 | +from executorch.backends.cortex_m.test.tester import CortexMSerialize |
| 13 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 14 | + |
| 15 | + |
| 16 | +_LEGACY_SPATIAL_OPS = { |
| 17 | + exir_ops.edge.cortex_m.quantized_conv2d.default, |
| 18 | + exir_ops.edge.cortex_m.quantized_depthwise_conv2d.default, |
| 19 | + exir_ops.edge.cortex_m.quantized_transpose_conv2d.default, |
| 20 | + exir_ops.edge.cortex_m.quantized_avg_pool2d.default, |
| 21 | + exir_ops.edge.cortex_m.quantized_max_pool2d.default, |
| 22 | +} |
| 23 | + |
| 24 | +# Temporary dual-mode acceptance coverage; see "Explicit-layout migration" in |
| 25 | +# the Cortex-M README. |
| 26 | + |
| 27 | + |
| 28 | +class Conv2d(torch.nn.Module): |
| 29 | + def __init__(self) -> None: |
| 30 | + super().__init__() |
| 31 | + self.conv = torch.nn.Conv2d(3, 4, 3, padding=1) |
| 32 | + |
| 33 | + def forward(self, x): |
| 34 | + return self.conv(x) |
| 35 | + |
| 36 | + |
| 37 | +class Conv1d(torch.nn.Module): |
| 38 | + def __init__(self) -> None: |
| 39 | + super().__init__() |
| 40 | + self.conv = torch.nn.Conv1d(2, 4, 3, padding=1) |
| 41 | + |
| 42 | + def forward(self, x): |
| 43 | + return self.conv(x) |
| 44 | + |
| 45 | + |
| 46 | +def _compile(module, inputs, *, explicit_layout: bool, quantize: bool = True): |
| 47 | + from executorch.backends.arm.scripts.aot_arm_compiler import _to_edge_cortex_m |
| 48 | + |
| 49 | + exported_program = torch.export.export(module, inputs, strict=True) |
| 50 | + return _to_edge_cortex_m( |
| 51 | + exported_program, |
| 52 | + SimpleNamespace( |
| 53 | + cortex_m_explicit_layout=explicit_layout, |
| 54 | + quantize=quantize, |
| 55 | + strict_export=True, |
| 56 | + ), |
| 57 | + exported_program.module(), |
| 58 | + inputs, |
| 59 | + None, |
| 60 | + CortexMTargetConfig(cpu=CortexM.M55), |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +def _count(exported_program, target) -> int: |
| 65 | + return sum(node.target == target for node in exported_program.graph.nodes) |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize( |
| 69 | + "explicit_layout,expected,unexpected", |
| 70 | + [ |
| 71 | + ( |
| 72 | + False, |
| 73 | + exir_ops.edge.cortex_m.quantized_conv2d.default, |
| 74 | + exir_ops.edge.cortex_m.quantized_conv2d_nhwc.default, |
| 75 | + ), |
| 76 | + ( |
| 77 | + True, |
| 78 | + exir_ops.edge.cortex_m.quantized_conv2d_nhwc.default, |
| 79 | + exir_ops.edge.cortex_m.quantized_conv2d.default, |
| 80 | + ), |
| 81 | + ], |
| 82 | +) |
| 83 | +def test_aot_layout_mode_selects_operator_family(explicit_layout, expected, unexpected): |
| 84 | + _, edge, _ = _compile( |
| 85 | + Conv2d().eval(), |
| 86 | + (torch.randn(1, 3, 8, 8),), |
| 87 | + explicit_layout=explicit_layout, |
| 88 | + ) |
| 89 | + program = edge.exported_program() |
| 90 | + |
| 91 | + assert _count(program, expected) == 1 |
| 92 | + assert _count(program, unexpected) == 0 |
| 93 | + |
| 94 | + |
| 95 | +def test_aot_explicit_layout_requires_quantization(): |
| 96 | + with pytest.raises(RuntimeError, match="requires --quantize"): |
| 97 | + _compile( |
| 98 | + Conv2d().eval(), |
| 99 | + (torch.randn(1, 3, 8, 8),), |
| 100 | + explicit_layout=True, |
| 101 | + quantize=False, |
| 102 | + ) |
| 103 | + |
| 104 | + |
| 105 | +def _assert_explicit_copy_ceiling(module, inputs, ceiling): |
| 106 | + _, edge, _ = _compile(module, inputs, explicit_layout=True) |
| 107 | + program = edge.exported_program() |
| 108 | + copies = [ |
| 109 | + node |
| 110 | + for node in program.graph.nodes |
| 111 | + if node.target |
| 112 | + in { |
| 113 | + exir_ops.edge.cortex_m.transpose.default, |
| 114 | + exir_ops.edge.aten.view_copy.default, |
| 115 | + } |
| 116 | + ] |
| 117 | + |
| 118 | + assert len(copies) <= ceiling |
| 119 | + assert all( |
| 120 | + node.args[0].meta["val"].dtype == torch.int8 |
| 121 | + for node in copies |
| 122 | + if node.target == exir_ops.edge.cortex_m.transpose.default |
| 123 | + ) |
| 124 | + assert not any(node.target in _LEGACY_SPATIAL_OPS for node in program.graph.nodes) |
| 125 | + assert not any( |
| 126 | + getattr(node.target, "namespace", None) == "channels_last" |
| 127 | + for node in program.graph.nodes |
| 128 | + ) |
| 129 | + |
| 130 | + |
| 131 | +def test_mobilenet_v2_explicit_copy_ceiling(): |
| 132 | + torchvision = pytest.importorskip("torchvision") |
| 133 | + _assert_explicit_copy_ceiling( |
| 134 | + torchvision.models.mobilenet_v2(weights=None).eval(), |
| 135 | + (torch.randn(1, 3, 224, 224),), |
| 136 | + ceiling=2, |
| 137 | + ) |
| 138 | + |
| 139 | + |
| 140 | +def test_resnet8_explicit_copy_ceiling(): |
| 141 | + from executorch.examples.models.mlperf_tiny.resnet8 import ResNet8 |
| 142 | + |
| 143 | + _assert_explicit_copy_ceiling( |
| 144 | + ResNet8().eval(), |
| 145 | + (torch.rand(1, 3, 32, 32) * 2 - 1,), |
| 146 | + ceiling=2, |
| 147 | + ) |
| 148 | + |
| 149 | + |
| 150 | +def test_silero_explicit_copy_ceiling(): |
| 151 | + from executorch.examples.models.silero_vad.export_silero_vad import ( |
| 152 | + CONTEXT_SIZE, |
| 153 | + HIDDEN_DIM, |
| 154 | + SileroVAD16k, |
| 155 | + WINDOW_SIZE, |
| 156 | + ) |
| 157 | + |
| 158 | + _assert_explicit_copy_ceiling( |
| 159 | + SileroVAD16k().eval(), |
| 160 | + ( |
| 161 | + torch.randn(1, CONTEXT_SIZE + WINDOW_SIZE), |
| 162 | + torch.zeros(2, 1, HIDDEN_DIM), |
| 163 | + ), |
| 164 | + ceiling=12, |
| 165 | + ) |
| 166 | + |
| 167 | + |
| 168 | +def test_explicit_conv1d_runs_on_fvp(): |
| 169 | + inputs = (torch.linspace(-5, 5, steps=16).reshape(1, 2, 8),) |
| 170 | + model_quant, edge, runtime_inputs = _compile( |
| 171 | + Conv1d().eval(), inputs, explicit_layout=True |
| 172 | + ) |
| 173 | + program = edge.exported_program() |
| 174 | + assert _count(program, exir_ops.edge.cortex_m.quantized_conv2d_nhwc.default) == 1 |
| 175 | + |
| 176 | + serialized = CortexMSerialize(CortexMTargetConfig(cpu=CortexM.M55)) |
| 177 | + serialized.run(edge.to_executorch()) |
| 178 | + [actual] = serialized.run_artifact(runtime_inputs) |
| 179 | + expected = model_quant(*runtime_inputs) |
| 180 | + torch.testing.assert_close(actual, expected, atol=0.05, rtol=1e-3) |
0 commit comments