|
| 1 | +# Copyright 2025 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 | + |
| 6 | + |
| 7 | +import pytest |
| 8 | +import torch |
| 9 | +from executorch.backends.cortex_m.test.tester import ( |
| 10 | + CortexMTester, |
| 11 | + McuTestCase, |
| 12 | + ramp_tensor, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +class CortexMLSTM(torch.nn.Module): |
| 17 | + ops_before_transforms = { |
| 18 | + "executorch_exir_dialects_edge__ops_aten_full_default": 2, |
| 19 | + "executorch_exir_dialects_edge__ops_aten_squeeze_copy_dims": 4, |
| 20 | + "executorch_exir_dialects_edge__ops_aten_unsqueeze_copy_default": 2, |
| 21 | + "executorch_exir_dialects_edge__ops_aten_view_copy_default": 6, |
| 22 | + "executorch_exir_dialects_edge__ops_aten_permute_copy_default": 3, |
| 23 | + "executorch_exir_dialects_edge__ops_aten_addmm_default": 3, |
| 24 | + "executorch_exir_dialects_edge__ops_aten_slice_copy_Tensor": 2, |
| 25 | + "executorch_exir_dialects_edge__ops_aten_add_Tensor": 4, |
| 26 | + "executorch_exir_dialects_edge__ops_aten_split_with_sizes_copy_default": 2, |
| 27 | + "executorch_exir_dialects_edge__ops_aten_sigmoid_default": 6, |
| 28 | + "executorch_exir_dialects_edge__ops_aten_tanh_default": 4, |
| 29 | + "executorch_exir_dialects_edge__ops_aten_mul_Tensor": 6, |
| 30 | + "executorch_exir_dialects_edge__ops_aten_cat_default": 1, |
| 31 | + } |
| 32 | + |
| 33 | + ops_after_transforms = {} |
| 34 | + |
| 35 | + def __init__(self, input_size: int = 4, hidden_size: int = 3) -> None: |
| 36 | + super().__init__() |
| 37 | + self.lstm = torch.nn.LSTM(input_size=input_size, hidden_size=hidden_size) |
| 38 | + |
| 39 | + def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 40 | + y, _ = self.lstm(x) |
| 41 | + return y |
| 42 | + |
| 43 | + |
| 44 | +class CortexMQuantizableLSTM(torch.nn.Module): |
| 45 | + ops_before_transforms = { |
| 46 | + "executorch_exir_dialects_edge__ops_aten_add_Tensor": 4, |
| 47 | + "executorch_exir_dialects_edge__ops_aten_addmm_default": 4, |
| 48 | + "executorch_exir_dialects_edge__ops_aten_cat_default": 1, |
| 49 | + "executorch_exir_dialects_edge__ops_aten_full_default": 1, |
| 50 | + "executorch_exir_dialects_edge__ops_aten_mul_Tensor": 6, |
| 51 | + "executorch_exir_dialects_edge__ops_aten_permute_copy_default": 4, |
| 52 | + "executorch_exir_dialects_edge__ops_aten_select_copy_int": 2, |
| 53 | + "executorch_exir_dialects_edge__ops_aten_sigmoid_default": 6, |
| 54 | + "executorch_exir_dialects_edge__ops_aten_split_with_sizes_copy_default": 2, |
| 55 | + "executorch_exir_dialects_edge__ops_aten_squeeze_copy_dims": 1, |
| 56 | + "executorch_exir_dialects_edge__ops_aten_tanh_default": 4, |
| 57 | + "executorch_exir_dialects_edge__ops_aten_view_copy_default": 1, |
| 58 | + "executorch_exir_dialects_edge__ops_quantized_decomposed_dequantize_per_tensor_default": 34, |
| 59 | + "executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default": 27, |
| 60 | + } |
| 61 | + |
| 62 | + ops_after_transforms = {} |
| 63 | + |
| 64 | + def __init__(self, input_size: int = 4, hidden_size: int = 3) -> None: |
| 65 | + super().__init__() |
| 66 | + self.lstm = torch.ao.nn.quantizable.LSTM( |
| 67 | + input_size=input_size, hidden_size=hidden_size |
| 68 | + ) |
| 69 | + |
| 70 | + def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 71 | + y, _ = self.lstm(x) |
| 72 | + return y |
| 73 | + |
| 74 | + |
| 75 | +test_cases = { |
| 76 | + "lstm_fp32": McuTestCase( |
| 77 | + model=CortexMLSTM(), |
| 78 | + example_inputs=(ramp_tensor(-1, 1, (2, 1, 4)),), |
| 79 | + ), |
| 80 | + "lstm_quantizable": McuTestCase( |
| 81 | + model=CortexMQuantizableLSTM(), |
| 82 | + example_inputs=(ramp_tensor(-1, 1, (2, 1, 4)),), |
| 83 | + ), |
| 84 | +} |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.skip("Not implemented yet.") |
| 88 | +def test_dialect_lstm(test_case: McuTestCase) -> None: |
| 89 | + tester = CortexMTester(test_case.model, test_case.example_inputs) |
| 90 | + tester.test_dialect( |
| 91 | + test_case.model.ops_before_transforms, test_case.model.ops_after_transforms |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +@pytest.mark.skip("Not implemented yet.") |
| 96 | +def test_implementation_lstm(test_case: McuTestCase) -> None: |
| 97 | + tester = CortexMTester(test_case.model, test_case.example_inputs) |
| 98 | + tester.test_implementation() |
0 commit comments