xref: /aosp_15_r20/external/executorch/backends/arm/test/ops/test_sigmoid.py (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1# Copyright (c) Meta Platforms, Inc. and affiliates.
2# Copyright 2024 Arm Limited and/or its affiliates.
3# All rights reserved.
4#
5# This source code is licensed under the BSD-style license found in the
6# LICENSE file in the root directory of this source tree.
7
8import logging
9import unittest
10
11from typing import Tuple
12
13import torch
14from executorch.backends.arm.test import common
15from executorch.backends.arm.test.tester.arm_tester import ArmTester
16from executorch.exir.backend.compile_spec_schema import CompileSpec
17from parameterized import parameterized
18
19logger = logging.getLogger(__name__)
20logger.setLevel(logging.INFO)
21
22
23test_data_suite = [
24    # (test_name, test_data)
25    ("zeros", torch.zeros(10, 10, 10, 10)),
26    ("ones", torch.ones(10, 10, 10)),
27    ("rand", torch.rand(10, 10) - 0.5),
28    ("randn_pos", torch.randn(10) + 10),
29    ("randn_neg", torch.randn(10) - 10),
30    ("ramp", torch.arange(-16, 16, 0.2)),
31]
32
33
34class TestSigmoid(unittest.TestCase):
35    class Sigmoid(torch.nn.Module):
36        def __init__(self):
37            super().__init__()
38            self.sigmoid = torch.nn.Sigmoid()
39
40        def forward(self, x):
41            return self.sigmoid(x)
42
43    class AddSigmoid(torch.nn.Module):
44        def __init__(self):
45            super().__init__()
46            self.sigmoid = torch.nn.Sigmoid()
47
48        def forward(self, x):
49            return self.sigmoid(x + x)
50
51    class SigmoidAdd(torch.nn.Module):
52        def __init__(self):
53            super().__init__()
54            self.sigmoid = torch.nn.Sigmoid()
55
56        def forward(self, x):
57            return x + self.sigmoid(x)
58
59    class SigmoidAddSigmoid(torch.nn.Module):
60        def __init__(self):
61            super().__init__()
62            self.sigmoid = torch.nn.Sigmoid()
63
64        def forward(self, x, y):
65            return self.sigmoid((self.sigmoid(y) + self.sigmoid(x)))
66
67    def _test_sigmoid_tosa_MI_pipeline(
68        self, module: torch.nn.Module, test_data: Tuple[torch.tensor]
69    ):
70        (
71            ArmTester(
72                module,
73                example_inputs=test_data,
74                compile_spec=common.get_tosa_compile_spec("TOSA-0.80.0+MI"),
75            )
76            .export()
77            .check(["torch.ops.aten.sigmoid.default"])
78            .check_not(["torch.ops.quantized_decomposed"])
79            .to_edge()
80            .partition()
81            .check_not(["executorch_exir_dialects_edge__ops_aten_sigmoid_default"])
82            .check_count({"torch.ops.higher_order.executorch_call_delegate": 1})
83            .to_executorch()
84            .run_method_and_compare_outputs(inputs=test_data)
85        )
86
87    def _test_sigmoid_tosa_BI_pipeline(self, module: torch.nn.Module, test_data: Tuple):
88        (
89            ArmTester(
90                module,
91                example_inputs=test_data,
92                compile_spec=common.get_tosa_compile_spec("TOSA-0.80.0+BI"),
93            )
94            .quantize()
95            .export()
96            .check(["torch.ops.aten.sigmoid.default"])
97            .check(["torch.ops.quantized_decomposed"])
98            .to_edge()
99            .partition()
100            .check_not(["executorch_exir_dialects_edge__ops_aten_sigmoid_default"])
101            .check_count({"torch.ops.higher_order.executorch_call_delegate": 1})
102            .to_executorch()
103            .run_method_and_compare_outputs(inputs=test_data)
104        )
105
106    def _test_sigmoid_tosa_ethos_BI_pipeline(
107        self,
108        compile_spec: list[CompileSpec],
109        module: torch.nn.Module,
110        test_data: Tuple[torch.tensor],
111    ):
112        (
113            ArmTester(
114                module,
115                example_inputs=test_data,
116                compile_spec=compile_spec,
117            )
118            .quantize()
119            .export()
120            .check_count({"torch.ops.aten.sigmoid.default": 1})
121            .check(["torch.ops.quantized_decomposed"])
122            .to_edge()
123            .partition()
124            .check_not(["executorch_exir_dialects_edge__ops_aten_sigmoid_default"])
125            .check_count({"torch.ops.higher_order.executorch_call_delegate": 1})
126            .to_executorch()
127        )
128
129    def _test_sigmoid_tosa_u55_BI_pipeline(
130        self, module: torch.nn.Module, test_data: Tuple[torch.tensor]
131    ):
132        self._test_sigmoid_tosa_ethos_BI_pipeline(
133            common.get_u55_compile_spec(), module, test_data
134        )
135
136    def _test_sigmoid_tosa_u85_BI_pipeline(
137        self, module: torch.nn.Module, test_data: Tuple[torch.tensor]
138    ):
139        self._test_sigmoid_tosa_ethos_BI_pipeline(
140            common.get_u85_compile_spec(), module, test_data
141        )
142
143    @parameterized.expand(test_data_suite)
144    def test_sigmoid_tosa_MI(
145        self,
146        test_name: str,
147        test_data: torch.Tensor,
148    ):
149        self._test_sigmoid_tosa_MI_pipeline(self.Sigmoid(), (test_data,))
150
151    @parameterized.expand(test_data_suite)
152    def test_sigmoid_tosa_BI(self, test_name: str, test_data: torch.Tensor):
153        self._test_sigmoid_tosa_BI_pipeline(self.Sigmoid(), (test_data,))
154
155    def test_add_sigmoid_tosa_MI(self):
156        self._test_sigmoid_tosa_MI_pipeline(self.AddSigmoid(), (test_data_suite[0][1],))
157
158    def test_add_sigmoid_tosa_BI(self):
159        self._test_sigmoid_tosa_BI_pipeline(self.AddSigmoid(), (test_data_suite[5][1],))
160
161    def test_sigmoid_add_tosa_MI(self):
162        self._test_sigmoid_tosa_MI_pipeline(self.SigmoidAdd(), (test_data_suite[0][1],))
163
164    def test_sigmoid_add_tosa_BI(self):
165        self._test_sigmoid_tosa_BI_pipeline(self.SigmoidAdd(), (test_data_suite[0][1],))
166
167    def test_sigmoid_add_sigmoid_tosa_MI(self):
168        self._test_sigmoid_tosa_MI_pipeline(
169            self.SigmoidAddSigmoid(), (test_data_suite[4][1], test_data_suite[3][1])
170        )
171
172    def test_sigmoid_add_sigmoid_tosa_BI(self):
173        self._test_sigmoid_tosa_BI_pipeline(
174            self.SigmoidAddSigmoid(), (test_data_suite[4][1], test_data_suite[3][1])
175        )
176
177    @parameterized.expand(test_data_suite)
178    def test_sigmoid_tosa_u55_BI(self, test_name: str, test_data: torch.Tensor):
179        self._test_sigmoid_tosa_u55_BI_pipeline(self.Sigmoid(), (test_data,))
180
181    @parameterized.expand(test_data_suite)
182    def test_sigmoid_tosa_u85_BI(self, test_name: str, test_data: torch.Tensor):
183        self._test_sigmoid_tosa_u85_BI_pipeline(self.Sigmoid(), (test_data,))
184