|
| 1 | +# Copyright 2024 Arm Limited and/or its 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 | +from typing import Tuple |
| 10 | + |
| 11 | +import torch |
| 12 | +from executorch.backends.arm.test import common |
| 13 | +from executorch.backends.arm.test.tester.arm_tester import ArmTester |
| 14 | +from parameterized import parameterized |
| 15 | + |
| 16 | +torch.manual_seed(1) |
| 17 | + |
| 18 | + |
| 19 | +class TestBMM(unittest.TestCase): |
| 20 | + """Tests Batch MatMul""" |
| 21 | + |
| 22 | + class BMM(torch.nn.Module): |
| 23 | + test_parameters = [ |
| 24 | + (torch.rand(5, 3, 5), torch.rand(5, 5, 2)), |
| 25 | + (torch.rand(2, 1, 1), torch.rand(2, 1, 1)), |
| 26 | + (torch.ones(1, 55, 3), torch.ones(1, 3, 44)), |
| 27 | + (10000 * torch.randn(10, 1, 10), torch.randn(10, 10, 5)), |
| 28 | + (-10 * torch.randn(2, 32, 64), 5 + 5 * torch.randn(2, 64, 32)), |
| 29 | + ] |
| 30 | + |
| 31 | + def forward(self, x, y): |
| 32 | + return torch.bmm(x, y) |
| 33 | + |
| 34 | + class BMMSingleInput(torch.nn.Module): |
| 35 | + test_parameters = [ |
| 36 | + (torch.rand(20, 3, 3),), |
| 37 | + (torch.ones(2, 128, 128),), |
| 38 | + (10000 * torch.randn(4, 25, 25),), |
| 39 | + (5 + 5 * torch.randn(3, 64, 64),), |
| 40 | + ] |
| 41 | + |
| 42 | + def forward(self, x): |
| 43 | + return torch.bmm(x, x) |
| 44 | + |
| 45 | + def _test_bmm_tosa_MI_pipeline( |
| 46 | + self, module: torch.nn.Module, test_data: Tuple[torch.Tensor, ...] |
| 47 | + ): |
| 48 | + ( |
| 49 | + ArmTester( |
| 50 | + module, |
| 51 | + example_inputs=test_data, |
| 52 | + compile_spec=common.get_tosa_compile_spec(), |
| 53 | + ) |
| 54 | + .export() |
| 55 | + .check_count({"torch.ops.aten.bmm.default": 1}) |
| 56 | + .check_not(["torch.ops.quantized_decomposed"]) |
| 57 | + .to_edge() |
| 58 | + .partition() |
| 59 | + .check_not(["executorch_exir_dialects_edge__ops_aten_bmm_default"]) |
| 60 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
| 61 | + .to_executorch() |
| 62 | + .run_method_and_compare_outputs(inputs=test_data) |
| 63 | + ) |
| 64 | + |
| 65 | + def _test_bmm_tosa_BI_pipeline( |
| 66 | + self, module: torch.nn.Module, test_data: Tuple[torch.Tensor, ...] |
| 67 | + ): |
| 68 | + ( |
| 69 | + ArmTester( |
| 70 | + module, |
| 71 | + example_inputs=test_data, |
| 72 | + compile_spec=common.get_tosa_compile_spec(), |
| 73 | + ) |
| 74 | + .quantize() |
| 75 | + .export() |
| 76 | + .check_count({"torch.ops.aten.bmm.default": 1}) |
| 77 | + .check(["torch.ops.quantized_decomposed"]) |
| 78 | + .to_edge() |
| 79 | + .partition() |
| 80 | + .check_not(["executorch_exir_dialects_edge__ops_aten_bmm_default"]) |
| 81 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
| 82 | + .to_executorch() |
| 83 | + .run_method_and_compare_outputs(inputs=test_data) |
| 84 | + ) |
| 85 | + |
| 86 | + def _test_bmm_u55_BI_pipeline( |
| 87 | + self, module: torch.nn.Module, test_data: Tuple[torch.Tensor, ...] |
| 88 | + ): |
| 89 | + ( |
| 90 | + ArmTester( |
| 91 | + module, |
| 92 | + example_inputs=test_data, |
| 93 | + compile_spec=common.get_u55_compile_spec(), |
| 94 | + ) |
| 95 | + .quantize() |
| 96 | + .export() |
| 97 | + .check_count({"torch.ops.aten.bmm.default": 1}) |
| 98 | + .check(["torch.ops.quantized_decomposed"]) |
| 99 | + .to_edge() |
| 100 | + .partition() |
| 101 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
| 102 | + .to_executorch() |
| 103 | + ) |
| 104 | + |
| 105 | + @parameterized.expand(BMM.test_parameters) |
| 106 | + def test_bmm_tosa_MI(self, operand1: torch.Tensor, operand2: torch.Tensor): |
| 107 | + test_data = (operand1, operand2) |
| 108 | + self._test_bmm_tosa_MI_pipeline(self.BMM(), test_data) |
| 109 | + |
| 110 | + @parameterized.expand(BMMSingleInput.test_parameters) |
| 111 | + def test_bmm_single_input_tosa_MI(self, operand1: torch.Tensor): |
| 112 | + test_data = (operand1,) |
| 113 | + self._test_bmm_tosa_MI_pipeline(self.BMMSingleInput(), test_data) |
| 114 | + |
| 115 | + @parameterized.expand(BMM.test_parameters) |
| 116 | + def test_bmm_tosa_BI(self, operand1: torch.Tensor, operand2: torch.Tensor): |
| 117 | + test_data = (operand1, operand2) |
| 118 | + self._test_bmm_tosa_BI_pipeline(self.BMM(), test_data) |
| 119 | + |
| 120 | + @parameterized.expand(BMMSingleInput.test_parameters) |
| 121 | + def test_bmm_single_input_tosa_BI(self, operand1: torch.Tensor): |
| 122 | + test_data = (operand1,) |
| 123 | + self._test_bmm_tosa_BI_pipeline(self.BMMSingleInput(), test_data) |
| 124 | + |
| 125 | + @parameterized.expand(BMM.test_parameters) |
| 126 | + def test_bmm_u55_BI(self, operand1: torch.Tensor, operand2: torch.Tensor): |
| 127 | + test_data = (operand1, operand2) |
| 128 | + self._test_bmm_tosa_BI_pipeline(self.BMM(), test_data) |
| 129 | + |
| 130 | + # Expected to fail with error: Warning, unsupported fusing of TOSA Rescale previous operator is of type: Memcpy |
| 131 | + @parameterized.expand(BMMSingleInput.test_parameters) |
| 132 | + @unittest.expectedFailure |
| 133 | + def test_bmm_single_input_u55_BI(self, operand1: torch.Tensor): |
| 134 | + test_data = (operand1,) |
| 135 | + self._test_bmm_u55_BI_pipeline(self.BMMSingleInput(), test_data) |
0 commit comments