|
5 | 5 | # This source code is licensed under the BSD-style license found in the |
6 | 6 | # LICENSE file in the root directory of this source tree. |
7 | 7 |
|
8 | | -import unittest |
9 | 8 |
|
10 | 9 | from typing import Tuple |
11 | 10 |
|
12 | | -import pytest |
13 | 11 | import torch |
14 | | -from executorch.backends.arm.test import common, conftest |
15 | | -from executorch.backends.arm.test.tester.arm_tester import ArmTester |
16 | | -from executorch.exir.backend.compile_spec_schema import CompileSpec |
17 | | -from parameterized import parameterized # type: ignore[import-untyped] |
18 | | - |
19 | | - |
20 | | -class TestSimpleAdd(unittest.TestCase): |
21 | | - """Tests a single add op, x+x and x+y.""" |
22 | | - |
23 | | - class Add(torch.nn.Module): |
24 | | - test_parameters = [ |
25 | | - (torch.FloatTensor([1, 2, 3, 5, 7]),), |
26 | | - (3 * torch.ones(8),), |
27 | | - (10 * torch.randn(8),), |
28 | | - (torch.ones(1, 1, 4, 4),), |
29 | | - (torch.ones(1, 3, 4, 2),), |
30 | | - ] |
31 | | - |
32 | | - def forward(self, x): |
33 | | - return x + x |
34 | | - |
35 | | - class Add2(torch.nn.Module): |
36 | | - test_parameters = [ |
37 | | - ( |
38 | | - torch.FloatTensor([1, 2, 3, 5, 7]), |
39 | | - (torch.FloatTensor([2, 1, 2, 1, 10])), |
40 | | - ), |
41 | | - (torch.ones(1, 10, 4, 6), torch.ones(1, 10, 4, 6)), |
42 | | - (torch.randn(1, 1, 4, 4), torch.ones(1, 1, 4, 1)), |
43 | | - (torch.randn(1, 3, 4, 4), torch.randn(1, 3, 4, 4)), |
44 | | - (10000 * torch.randn(1, 1, 4, 4), torch.randn(1, 1, 4, 1)), |
45 | | - ] |
46 | | - |
47 | | - def __init__(self): |
48 | | - super().__init__() |
49 | | - |
50 | | - def forward(self, x, y): |
51 | | - return x + y |
52 | | - |
53 | | - def _test_add_tosa_MI_pipeline( |
54 | | - self, module: torch.nn.Module, test_data: Tuple[torch.Tensor] |
55 | | - ): |
56 | | - ( |
57 | | - ArmTester( |
58 | | - module, |
59 | | - example_inputs=test_data, |
60 | | - compile_spec=common.get_tosa_compile_spec("TOSA-0.80+MI"), |
61 | | - ) |
62 | | - .export() |
63 | | - .check_count({"torch.ops.aten.add.Tensor": 1}) |
64 | | - .check_not(["torch.ops.quantized_decomposed"]) |
65 | | - .to_edge() |
66 | | - .partition() |
67 | | - .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
68 | | - .to_executorch() |
69 | | - .run_method_and_compare_outputs(inputs=test_data) |
70 | | - ) |
71 | | - |
72 | | - def _test_add_tosa_BI_pipeline( |
73 | | - self, module: torch.nn.Module, test_data: Tuple[torch.Tensor] |
74 | | - ): |
75 | | - ( |
76 | | - ArmTester( |
77 | | - module, |
78 | | - example_inputs=test_data, |
79 | | - compile_spec=common.get_tosa_compile_spec("TOSA-0.80+BI"), |
80 | | - ) |
81 | | - .quantize() |
82 | | - .export() |
83 | | - .check_count({"torch.ops.aten.add.Tensor": 1}) |
84 | | - .check(["torch.ops.quantized_decomposed"]) |
85 | | - .to_edge() |
86 | | - .partition() |
87 | | - .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
88 | | - .to_executorch() |
89 | | - .run_method_and_compare_outputs(inputs=test_data, qtol=1) |
90 | | - ) |
91 | | - |
92 | | - def _test_add_ethos_BI_pipeline( |
93 | | - self, |
94 | | - module: torch.nn.Module, |
95 | | - compile_spec: CompileSpec, |
96 | | - test_data: Tuple[torch.Tensor], |
97 | | - ): |
98 | | - tester = ( |
99 | | - ArmTester( |
100 | | - module, |
101 | | - example_inputs=test_data, |
102 | | - compile_spec=compile_spec, |
103 | | - ) |
104 | | - .quantize() |
105 | | - .export() |
106 | | - .check_count({"torch.ops.aten.add.Tensor": 1}) |
107 | | - .check(["torch.ops.quantized_decomposed"]) |
108 | | - .to_edge() |
109 | | - .partition() |
110 | | - .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
111 | | - .to_executorch() |
112 | | - .serialize() |
113 | | - ) |
114 | | - if conftest.is_option_enabled("corstone_fvp"): |
115 | | - tester.run_method_and_compare_outputs(qtol=1, inputs=test_data) |
116 | | - |
117 | | - return tester |
118 | | - |
119 | | - @parameterized.expand(Add.test_parameters) |
120 | | - def test_add_tosa_MI(self, test_data: torch.Tensor): |
121 | | - test_data = (test_data,) |
122 | | - self._test_add_tosa_MI_pipeline(self.Add(), test_data) |
123 | | - |
124 | | - @parameterized.expand(Add.test_parameters) |
125 | | - def test_add_tosa_BI(self, test_data: torch.Tensor): |
126 | | - test_data = (test_data,) |
127 | | - self._test_add_tosa_BI_pipeline(self.Add(), test_data) |
128 | | - |
129 | | - @parameterized.expand(Add.test_parameters) |
130 | | - @pytest.mark.corstone_fvp |
131 | | - def test_add_u55_BI(self, test_data: torch.Tensor): |
132 | | - test_data = (test_data,) |
133 | | - self._test_add_ethos_BI_pipeline( |
134 | | - self.Add(), |
135 | | - common.get_u55_compile_spec(), |
136 | | - test_data, |
137 | | - ) |
138 | | - |
139 | | - @parameterized.expand(Add.test_parameters) |
140 | | - @pytest.mark.corstone_fvp |
141 | | - def test_add_u85_BI(self, test_data: torch.Tensor): |
142 | | - test_data = (test_data,) |
143 | | - self._test_add_ethos_BI_pipeline( |
144 | | - self.Add(), |
145 | | - common.get_u85_compile_spec(), |
146 | | - test_data, |
147 | | - ) |
148 | | - |
149 | | - @parameterized.expand(Add2.test_parameters) |
150 | | - def test_add2_tosa_MI(self, operand1: torch.Tensor, operand2: torch.Tensor): |
151 | | - test_data = (operand1, operand2) |
152 | | - self._test_add_tosa_MI_pipeline(self.Add2(), test_data) |
153 | | - |
154 | | - @parameterized.expand(Add2.test_parameters) |
155 | | - def test_add2_tosa_BI(self, operand1: torch.Tensor, operand2: torch.Tensor): |
156 | | - test_data = (operand1, operand2) |
157 | | - self._test_add_tosa_BI_pipeline(self.Add2(), test_data) |
158 | | - |
159 | | - @parameterized.expand(Add2.test_parameters) |
160 | | - @pytest.mark.corstone_fvp |
161 | | - def test_add2_u55_BI(self, operand1: torch.Tensor, operand2: torch.Tensor): |
162 | | - test_data = (operand1, operand2) |
163 | | - self._test_add_ethos_BI_pipeline( |
164 | | - self.Add2(), common.get_u55_compile_spec(), test_data |
165 | | - ) |
166 | | - |
167 | | - @parameterized.expand(Add2.test_parameters) |
168 | | - @pytest.mark.corstone_fvp |
169 | | - def test_add2_u85_BI(self, operand1: torch.Tensor, operand2: torch.Tensor): |
170 | | - test_data = (operand1, operand2) |
171 | | - self._test_add_ethos_BI_pipeline( |
172 | | - self.Add2(), common.get_u85_compile_spec(), test_data |
173 | | - ) |
| 12 | +from executorch.backends.arm.test import common |
| 13 | +from executorch.backends.arm.test.tester.test_pipeline import ( |
| 14 | + EthosU55PipelineBI, |
| 15 | + EthosU85PipelineBI, |
| 16 | + TosaPipelineBI, |
| 17 | + TosaPipelineMI, |
| 18 | +) |
| 19 | + |
| 20 | +aten_op = "torch.ops.aten.add.Tensor" |
| 21 | +exir_op = "executorch_exir_dialects_edge__ops_aten_add_Tensor" |
| 22 | + |
| 23 | +input_t1 = Tuple[torch.Tensor] # Input x |
| 24 | + |
| 25 | + |
| 26 | +class Add(torch.nn.Module): |
| 27 | + def forward(self, x: torch.Tensor): |
| 28 | + return x + x |
| 29 | + |
| 30 | + test_data: list[input_t1] = { |
| 31 | + "5d_float": (torch.FloatTensor([1, 2, 3, 5, 7]),), |
| 32 | + "1d_ones": ((3 * torch.ones(8),)), |
| 33 | + "1d_randn": (10 * torch.randn(8),), |
| 34 | + "4d_ones_1": (torch.ones(1, 1, 4, 4),), |
| 35 | + "4d_ones_2": (torch.ones(1, 3, 4, 2),), |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | +input_t2 = Tuple[torch.Tensor, torch.Tensor] # Input x, y |
| 40 | + |
| 41 | + |
| 42 | +class Add2(torch.nn.Module): |
| 43 | + def forward(self, x: torch.Tensor, y: torch.Tensor): |
| 44 | + return x + y |
| 45 | + |
| 46 | + test_data: list[input_t2] = { |
| 47 | + "5d_float": ( |
| 48 | + torch.FloatTensor([1, 2, 3, 5, 7]), |
| 49 | + (torch.FloatTensor([2, 1, 2, 1, 10])), |
| 50 | + ), |
| 51 | + "4d_ones": (torch.ones(1, 10, 4, 6), torch.ones(1, 10, 4, 6)), |
| 52 | + "4d_randn_1": (torch.randn(1, 1, 4, 4), torch.ones(1, 1, 4, 1)), |
| 53 | + "4d_randn_2": (torch.randn(1, 3, 4, 4), torch.randn(1, 3, 4, 4)), |
| 54 | + "4d_randn_big": (10000 * torch.randn(1, 1, 4, 4), torch.randn(1, 1, 4, 1)), |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | +@common.parametrize("test_data", Add.test_data) |
| 59 | +def test_add_tosa_MI(test_data: input_t1): |
| 60 | + pipeline = TosaPipelineMI[input_t1](Add(), test_data, aten_op, exir_op) |
| 61 | + pipeline.run() |
| 62 | + |
| 63 | + |
| 64 | +@common.parametrize("test_data", Add.test_data) |
| 65 | +def test_add_tosa_BI(test_data: input_t1): |
| 66 | + pipeline = TosaPipelineBI[input_t1](Add(), test_data, aten_op, exir_op) |
| 67 | + pipeline.run() |
| 68 | + |
| 69 | + |
| 70 | +@common.parametrize("test_data", Add.test_data) |
| 71 | +def test_add_u55_BI(test_data: input_t1): |
| 72 | + pipeline = EthosU55PipelineBI[input_t1]( |
| 73 | + Add(), test_data, aten_op, exir_op, run_on_fvp=False |
| 74 | + ) |
| 75 | + pipeline.run() |
| 76 | + |
| 77 | + |
| 78 | +@common.parametrize("test_data", Add.test_data) |
| 79 | +def test_add_u85_BI(test_data: input_t1): |
| 80 | + pipeline = EthosU85PipelineBI[input_t1]( |
| 81 | + Add(), test_data, aten_op, exir_op, run_on_fvp=False |
| 82 | + ) |
| 83 | + pipeline.run() |
| 84 | + |
| 85 | + |
| 86 | +@common.parametrize("test_data", Add.test_data) |
| 87 | +@common.SkipIfNoCorstone300 |
| 88 | +def test_add_u55_BI_on_fvp(test_data: input_t1): |
| 89 | + pipeline = EthosU55PipelineBI[input_t1]( |
| 90 | + Add(), test_data, aten_op, exir_op, run_on_fvp=True |
| 91 | + ) |
| 92 | + pipeline.run() |
| 93 | + |
| 94 | + |
| 95 | +@common.parametrize("test_data", Add.test_data) |
| 96 | +@common.SkipIfNoCorstone320 |
| 97 | +def test_add_u85_BI_on_fvp(test_data: input_t1): |
| 98 | + pipeline = EthosU85PipelineBI[input_t1]( |
| 99 | + Add(), test_data, aten_op, exir_op, run_on_fvp=True |
| 100 | + ) |
| 101 | + pipeline.run() |
| 102 | + |
| 103 | + |
| 104 | +@common.parametrize("test_data", Add2.test_data) |
| 105 | +def test_add2_tosa_MI(test_data: input_t2): |
| 106 | + pipeline = TosaPipelineMI[input_t2](Add2(), test_data, aten_op, exir_op) |
| 107 | + pipeline.run() |
| 108 | + |
| 109 | + |
| 110 | +@common.parametrize("test_data", Add2.test_data) |
| 111 | +def test_add2_tosa_BI(test_data: input_t2): |
| 112 | + pipeline = TosaPipelineBI[input_t2](Add2(), test_data, aten_op, exir_op) |
| 113 | + pipeline.run() |
| 114 | + |
| 115 | + |
| 116 | +@common.parametrize("test_data", Add2.test_data) |
| 117 | +def test_add2_u55_BI(test_data: input_t2): |
| 118 | + pipeline = EthosU55PipelineBI[input_t2]( |
| 119 | + Add2(), test_data, aten_op, exir_op, run_on_fvp=False |
| 120 | + ) |
| 121 | + pipeline.run() |
| 122 | + |
| 123 | + |
| 124 | +@common.parametrize("test_data", Add2.test_data) |
| 125 | +@common.SkipIfNoCorstone300 |
| 126 | +def test_add2_u55_BI_on_fvp(test_data: input_t2): |
| 127 | + pipeline = EthosU55PipelineBI[input_t2]( |
| 128 | + Add2(), test_data, aten_op, exir_op, run_on_fvp=True |
| 129 | + ) |
| 130 | + pipeline.run() |
| 131 | + |
| 132 | + |
| 133 | +@common.parametrize("test_data", Add2.test_data) |
| 134 | +def test_add2_u85_BI(test_data: input_t2): |
| 135 | + pipeline = EthosU85PipelineBI[input_t2]( |
| 136 | + Add2(), test_data, aten_op, exir_op, run_on_fvp=False |
| 137 | + ) |
| 138 | + pipeline.run() |
| 139 | + |
| 140 | + |
| 141 | +@common.parametrize("test_data", Add2.test_data) |
| 142 | +@common.SkipIfNoCorstone320 |
| 143 | +def test_add2_u85_BI_on_fvp(test_data: input_t2): |
| 144 | + pipeline = EthosU85PipelineBI[input_t2]( |
| 145 | + Add2(), test_data, aten_op, exir_op, run_on_fvp=True |
| 146 | + ) |
| 147 | + pipeline.run() |
0 commit comments