|
| 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 | +from typing import Tuple |
| 7 | + |
| 8 | +import torch |
| 9 | +from executorch.backends.arm.test import common |
| 10 | +from executorch.backends.arm.test.tester.test_pipeline import ( |
| 11 | + EthosU55PipelineBI, |
| 12 | + EthosU85PipelineBI, |
| 13 | + TosaPipelineBI, |
| 14 | + TosaPipelineMI, |
| 15 | +) |
| 16 | + |
| 17 | +aten_op_mm = "torch.ops.aten.matmul.default" |
| 18 | +exir_op_mm = "executorch_exir_dialects_edge__ops_aten_matmul_default" |
| 19 | +input_t1 = Tuple[torch.Tensor, torch.Tensor] # Input x |
| 20 | + |
| 21 | + |
| 22 | +class MatMul(torch.nn.Module): |
| 23 | + test_data_generators = { |
| 24 | + "rand_rand_3d": lambda: (torch.rand(2, 3, 5), torch.rand(2, 5, 2)), |
| 25 | + "rand_rand_4d": lambda: (torch.rand(1, 2, 3, 5), torch.rand(1, 2, 5, 2)), |
| 26 | + } |
| 27 | + |
| 28 | + def forward(self, x: torch.Tensor, y: torch.Tensor): |
| 29 | + return torch.matmul(x, y) |
| 30 | + |
| 31 | + |
| 32 | +class MatMulSingleInput(torch.nn.Module): |
| 33 | + test_data_generators = { |
| 34 | + "rand_3d": lambda: (torch.rand(2, 5, 5),), |
| 35 | + "rand_4d": lambda: (torch.rand(1, 2, 5, 5),), |
| 36 | + } |
| 37 | + |
| 38 | + def forward(self, x: torch.Tensor): |
| 39 | + return torch.matmul(x, x) |
| 40 | + |
| 41 | + |
| 42 | +class MatMulCombo(torch.nn.Module): |
| 43 | + test_data_generators = { |
| 44 | + "rand_rand_rand_3d": lambda: ( |
| 45 | + torch.rand(2, 5, 5), |
| 46 | + torch.rand(2, 5, 2), |
| 47 | + torch.rand(2, 2, 5), |
| 48 | + ), |
| 49 | + "rand_rand_rand_4d": lambda: ( |
| 50 | + torch.rand(1, 2, 5, 5), |
| 51 | + torch.rand(1, 2, 5, 2), |
| 52 | + torch.rand(1, 2, 2, 5), |
| 53 | + ), |
| 54 | + } |
| 55 | + |
| 56 | + def forward(self, x1: torch.Tensor, x2: torch.Tensor, x3: torch.Tensor): |
| 57 | + y1 = torch.matmul(x1, x1) |
| 58 | + y2 = torch.matmul(x2, x3) |
| 59 | + return y1 + y2 |
| 60 | + |
| 61 | + |
| 62 | +@common.parametrize("test_data", MatMul.test_data_generators) |
| 63 | +def test_matmul_tosa_MI(test_data: input_t1): |
| 64 | + pipeline = TosaPipelineMI[input_t1](MatMul(), test_data(), aten_op_mm, exir_op_mm) |
| 65 | + pipeline.run() |
| 66 | + |
| 67 | + |
| 68 | +@common.parametrize("test_data", MatMulSingleInput.test_data_generators) |
| 69 | +def test_matmul_single_input_tosa_MI(test_data: input_t1): |
| 70 | + pipeline = TosaPipelineMI[input_t1]( |
| 71 | + MatMulSingleInput(), test_data(), aten_op_mm, exir_op_mm |
| 72 | + ) |
| 73 | + pipeline.run() |
| 74 | + |
| 75 | + |
| 76 | +@common.parametrize("test_data", MatMulCombo.test_data_generators) |
| 77 | +def test_matmul_combo_tosa_MI(test_data: input_t1): |
| 78 | + pipeline = TosaPipelineMI[input_t1]( |
| 79 | + MatMulCombo(), test_data(), aten_op_mm, exir_op_mm |
| 80 | + ) |
| 81 | + pipeline.run() |
| 82 | + |
| 83 | + |
| 84 | +@common.parametrize("test_data", MatMul.test_data_generators) |
| 85 | +def test_matmul_tosa_BI(test_data: input_t1): |
| 86 | + pipeline = TosaPipelineBI[input_t1]( |
| 87 | + MatMul(), test_data(), aten_op_mm, exir_op_mm, qtol=1 |
| 88 | + ) |
| 89 | + pipeline.run() |
| 90 | + |
| 91 | + |
| 92 | +@common.parametrize("test_data", MatMulSingleInput.test_data_generators) |
| 93 | +def test_matmul_single_input_tosa_BI(test_data: input_t1): |
| 94 | + pipeline = TosaPipelineMI[input_t1]( |
| 95 | + MatMulSingleInput(), |
| 96 | + test_data(), |
| 97 | + aten_op_mm, |
| 98 | + exir_op_mm, |
| 99 | + qtol=1, |
| 100 | + ) |
| 101 | + pipeline.run() |
| 102 | + |
| 103 | + |
| 104 | +@common.parametrize("test_data", MatMulCombo.test_data_generators) |
| 105 | +def test_matmul_combo_tosa_BI(test_data: input_t1): |
| 106 | + pipeline = TosaPipelineBI[input_t1]( |
| 107 | + MatMulCombo(), |
| 108 | + test_data(), |
| 109 | + aten_op_mm, |
| 110 | + exir_op_mm, |
| 111 | + qtol=1, |
| 112 | + ) |
| 113 | + pipeline.run() |
| 114 | + |
| 115 | + |
| 116 | +@common.parametrize("test_data", MatMul.test_data_generators) |
| 117 | +@common.XfailIfNoCorstone300 |
| 118 | +def test_matmul_u55_BI(test_data: input_t1): |
| 119 | + pipeline = EthosU55PipelineBI[input_t1]( |
| 120 | + MatMul(), |
| 121 | + test_data(), |
| 122 | + aten_op_mm, |
| 123 | + exir_op_mm, |
| 124 | + run_on_fvp=True, |
| 125 | + use_to_edge_transform_and_lower=True, |
| 126 | + ) |
| 127 | + pipeline.run() |
| 128 | + |
| 129 | + |
| 130 | +@common.parametrize("test_data", MatMulSingleInput.test_data_generators) |
| 131 | +@common.XfailIfNoCorstone300 |
| 132 | +def test_matmul_single_input_u55_BI(test_data: input_t1): |
| 133 | + pipeline = EthosU55PipelineBI[input_t1]( |
| 134 | + MatMulSingleInput(), |
| 135 | + test_data(), |
| 136 | + aten_op_mm, |
| 137 | + exir_op_mm, |
| 138 | + run_on_fvp=True, |
| 139 | + use_to_edge_transform_and_lower=True, |
| 140 | + ) |
| 141 | + pipeline.run() |
| 142 | + |
| 143 | + |
| 144 | +@common.parametrize("test_data", MatMulCombo.test_data_generators) |
| 145 | +@common.XfailIfNoCorstone300 |
| 146 | +def test_matmul_combo_u55_BI(test_data: input_t1): |
| 147 | + pipeline = EthosU55PipelineBI[input_t1]( |
| 148 | + MatMulCombo(), |
| 149 | + test_data(), |
| 150 | + aten_op_mm, |
| 151 | + exir_op_mm, |
| 152 | + run_on_fvp=True, |
| 153 | + use_to_edge_transform_and_lower=True, |
| 154 | + ) |
| 155 | + pipeline.run() |
| 156 | + |
| 157 | + |
| 158 | +@common.parametrize("test_data", MatMul.test_data_generators) |
| 159 | +@common.XfailIfNoCorstone320 |
| 160 | +def test_matmul_u85_BI(test_data: input_t1): |
| 161 | + pipeline = EthosU85PipelineBI[input_t1]( |
| 162 | + MatMul(), |
| 163 | + test_data(), |
| 164 | + aten_op_mm, |
| 165 | + exir_op_mm, |
| 166 | + run_on_fvp=True, |
| 167 | + use_to_edge_transform_and_lower=True, |
| 168 | + ) |
| 169 | + pipeline.run() |
| 170 | + |
| 171 | + |
| 172 | +@common.parametrize("test_data", MatMulSingleInput.test_data_generators) |
| 173 | +@common.XfailIfNoCorstone320 |
| 174 | +def test_matmul_single_input_u85_BI(test_data: input_t1): |
| 175 | + pipeline = EthosU85PipelineBI[input_t1]( |
| 176 | + MatMulSingleInput(), |
| 177 | + test_data(), |
| 178 | + aten_op_mm, |
| 179 | + exir_op_mm, |
| 180 | + run_on_fvp=True, |
| 181 | + use_to_edge_transform_and_lower=True, |
| 182 | + ) |
| 183 | + pipeline.run() |
| 184 | + |
| 185 | + |
| 186 | +@common.parametrize("test_data", MatMulCombo.test_data_generators) |
| 187 | +@common.XfailIfNoCorstone320 |
| 188 | +def test_matmul_combo_u85_BI(test_data: input_t1): |
| 189 | + pipeline = EthosU85PipelineBI[input_t1]( |
| 190 | + MatMulCombo(), |
| 191 | + test_data(), |
| 192 | + aten_op_mm, |
| 193 | + exir_op_mm, |
| 194 | + run_on_fvp=True, |
| 195 | + use_to_edge_transform_and_lower=True, |
| 196 | + ) |
| 197 | + pipeline.run() |
0 commit comments