xref: /aosp_15_r20/external/executorch/backends/xnnpack/test/ops/sigmoid.py (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1# Copyright (c) Meta Platforms, Inc. and 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
7import unittest
8
9import torch
10from executorch.backends.xnnpack.test.tester import Tester
11
12
13class TestSigmoid(unittest.TestCase):
14    class Sigmoid(torch.nn.Module):
15        def __init__(self):
16            super().__init__()
17            self.sigmoid = torch.nn.Sigmoid()
18
19        def forward(self, x):
20            z = self.sigmoid(x)
21            return z
22
23    def _test_sigmoid(self, inputs):
24        (
25            Tester(self.Sigmoid(), inputs)
26            .export()
27            .check_count({"torch.ops.aten.sigmoid.default": 1})
28            .to_edge_transform_and_lower()
29            .check_count({"torch.ops.higher_order.executorch_call_delegate": 1})
30            .check_not(["executorch_exir_dialects_edge__ops_aten_sigmoid_default"])
31            .to_executorch()
32            .serialize()
33            .run_method_and_compare_outputs()
34        )
35
36    def test_fp16_sigmoid(self):
37        inputs = (torch.randn(4).to(torch.float16),)
38        self._test_sigmoid(inputs)
39
40    def test_fp32_sigmoid(self):
41        inputs = (torch.ones(4),)
42        self._test_sigmoid(inputs)
43