|
1 | 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. |
2 | | -# Copyright 2025 Arm Limited and/or its affiliates. |
3 | 2 | # All rights reserved. |
| 3 | +# Copyright 2025 Arm Limited and/or its affiliates. |
4 | 4 | # |
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 | | - |
14 | 11 | import torch |
15 | | -from executorch.backends.arm.test import common, conftest |
16 | | -from executorch.backends.arm.test.tester.arm_tester import ArmTester |
17 | | -from executorch.exir.backend.compile_spec_schema import CompileSpec |
18 | | -from parameterized import parameterized |
19 | | - |
20 | | - |
21 | | -class TestAbs(unittest.TestCase): |
22 | | - class Abs(torch.nn.Module): |
23 | | - test_parameters = [ |
24 | | - (torch.zeros(5),), |
25 | | - (torch.full((5,), -1, dtype=torch.float32),), |
26 | | - (torch.ones(5) * -1,), |
27 | | - (torch.randn(8),), |
28 | | - (torch.randn(2, 3, 4),), |
29 | | - (torch.randn(1, 2, 3, 4),), |
30 | | - (torch.normal(mean=0, std=10, size=(2, 3, 4)),), |
31 | | - ] |
32 | | - |
33 | | - def forward(self, x): |
34 | | - return torch.abs(x) |
35 | | - |
36 | | - def _test_abs_tosa_MI_pipeline( |
37 | | - self, module: torch.nn.Module, test_data: Tuple[torch.Tensor] |
38 | | - ): |
39 | | - ( |
40 | | - ArmTester( |
41 | | - module, |
42 | | - example_inputs=test_data, |
43 | | - compile_spec=common.get_tosa_compile_spec("TOSA-0.80+MI"), |
44 | | - ) |
45 | | - .export() |
46 | | - .check_count({"torch.ops.aten.abs.default": 1}) |
47 | | - .check_not(["torch.ops.quantized_decomposed"]) |
48 | | - .to_edge() |
49 | | - .partition() |
50 | | - .check_not(["torch.ops.aten.abs.default"]) |
51 | | - .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
52 | | - .to_executorch() |
53 | | - .run_method_and_compare_outputs(inputs=test_data) |
54 | | - ) |
55 | | - |
56 | | - def _test_abs_tosa_BI_pipeline( |
57 | | - self, module: torch.nn.Module, test_data: Tuple[torch.Tensor] |
58 | | - ): |
59 | | - ( |
60 | | - ArmTester( |
61 | | - module, |
62 | | - example_inputs=test_data, |
63 | | - compile_spec=common.get_tosa_compile_spec("TOSA-0.80+BI"), |
64 | | - ) |
65 | | - .quantize() |
66 | | - .export() |
67 | | - .check_count({"torch.ops.aten.abs.default": 1}) |
68 | | - .check(["torch.ops.quantized_decomposed"]) |
69 | | - .to_edge() |
70 | | - .partition() |
71 | | - .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
72 | | - .to_executorch() |
73 | | - .run_method_and_compare_outputs(inputs=test_data, qtol=1) |
74 | | - ) |
75 | | - |
76 | | - def _test_abs_ethosu_BI_pipeline( |
77 | | - self, |
78 | | - compile_spec: list[CompileSpec], |
79 | | - module: torch.nn.Module, |
80 | | - test_data: Tuple[torch.Tensor], |
81 | | - ): |
82 | | - tester = ( |
83 | | - ArmTester( |
84 | | - module, |
85 | | - example_inputs=test_data, |
86 | | - compile_spec=compile_spec, |
87 | | - ) |
88 | | - .quantize() |
89 | | - .export() |
90 | | - .check_count({"torch.ops.aten.abs.default": 1}) |
91 | | - .check(["torch.ops.quantized_decomposed"]) |
92 | | - .to_edge() |
93 | | - .partition() |
94 | | - .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
95 | | - .to_executorch() |
96 | | - .serialize() |
97 | | - ) |
98 | | - if conftest.is_option_enabled("corstone_fvp"): |
99 | | - tester.run_method_and_compare_outputs(qtol=1, inputs=test_data) |
100 | | - |
101 | | - @parameterized.expand(Abs.test_parameters) |
102 | | - def test_abs_tosa_MI(self, test_data: torch.Tensor): |
103 | | - test_data = (test_data,) |
104 | | - self._test_abs_tosa_MI_pipeline(self.Abs(), test_data) |
105 | | - |
106 | | - @parameterized.expand(Abs.test_parameters) |
107 | | - def test_abs_tosa_BI(self, test_data: torch.Tensor): |
108 | | - test_data = (test_data,) |
109 | | - self._test_abs_tosa_BI_pipeline(self.Abs(), test_data) |
110 | | - |
111 | | - @parameterized.expand(Abs.test_parameters) |
112 | | - @pytest.mark.corstone_fvp |
113 | | - def test_abs_u55_BI(self, test_data: torch.Tensor): |
114 | | - test_data = (test_data,) |
115 | | - self._test_abs_ethosu_BI_pipeline( |
116 | | - common.get_u55_compile_spec(), self.Abs(), test_data |
117 | | - ) |
118 | | - |
119 | | - @parameterized.expand(Abs.test_parameters) |
120 | | - @pytest.mark.corstone_fvp |
121 | | - def test_abs_u85_BI(self, test_data: torch.Tensor): |
122 | | - test_data = (test_data,) |
123 | | - self._test_abs_ethosu_BI_pipeline( |
124 | | - common.get_u85_compile_spec(), self.Abs(), test_data |
125 | | - ) |
| 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.abs.default" |
| 21 | +exir_op = "executorch_exir_dialects_edge__ops_aten_abs_default" |
| 22 | + |
| 23 | +input_t1 = Tuple[torch.Tensor] # Input x |
| 24 | + |
| 25 | + |
| 26 | +class Abs(torch.nn.Module): |
| 27 | + test_parameters = { |
| 28 | + "zeros": lambda: (torch.zeros(5),), |
| 29 | + "full": lambda: (torch.full((5,), -1, dtype=torch.float32),), |
| 30 | + "ones": lambda: (torch.ones(5) * -1,), |
| 31 | + "randn_1d": lambda: (torch.randn(8),), |
| 32 | + "randn_3d": lambda: (torch.randn(2, 3, 4),), |
| 33 | + "randn_4d": lambda: (torch.randn(1, 2, 3, 4),), |
| 34 | + "torch_normal": lambda: (torch.normal(mean=0, std=10, size=(2, 3, 4)),), |
| 35 | + } |
| 36 | + |
| 37 | + def forward(self, x): |
| 38 | + return torch.abs(x) |
| 39 | + |
| 40 | + |
| 41 | +@common.parametrize("test_data", Abs.test_parameters) |
| 42 | +def test_abs_tosa_MI(test_data: torch.Tensor): |
| 43 | + pipeline = TosaPipelineMI[input_t1](Abs(), test_data(), aten_op, exir_op) |
| 44 | + pipeline.run() |
| 45 | + |
| 46 | + |
| 47 | +@common.parametrize("test_data", Abs.test_parameters) |
| 48 | +def test_abs_tosa_BI(test_data: torch.Tensor): |
| 49 | + pipeline = TosaPipelineBI[input_t1](Abs(), test_data(), aten_op, exir_op) |
| 50 | + pipeline.run() |
| 51 | + |
| 52 | + |
| 53 | +@common.parametrize("test_data", Abs.test_parameters) |
| 54 | +@common.XfailIfNoCorstone300 |
| 55 | +def test_abs_u55_BI(test_data: torch.Tensor): |
| 56 | + pipeline = EthosU55PipelineBI[input_t1]( |
| 57 | + Abs(), test_data(), aten_op, exir_op, run_on_fvp=True |
| 58 | + ) |
| 59 | + pipeline.run() |
| 60 | + |
| 61 | + |
| 62 | +@common.parametrize("test_data", Abs.test_parameters) |
| 63 | +@common.XfailIfNoCorstone320 |
| 64 | +def test_abs_u85_BI(test_data: torch.Tensor): |
| 65 | + pipeline = EthosU85PipelineBI[input_t1]( |
| 66 | + Abs(), test_data(), aten_op, exir_op, run_on_fvp=True |
| 67 | + ) |
| 68 | + pipeline.run() |
0 commit comments