xref: /aosp_15_r20/external/executorch/backends/xnnpack/test/ops/negate.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 TestNegate(unittest.TestCase):
14    class Negate(torch.nn.Module):
15        def __init__(self):
16            super().__init__()
17
18        def forward(self, x):
19            z = torch.neg(x)
20            return z
21
22    def _test_negate(self, inputs):
23        (
24            Tester(self.Negate(), inputs)
25            .export()
26            .check_count({"torch.ops.aten.neg.default": 1})
27            .to_edge_transform_and_lower()
28            .check_count({"torch.ops.higher_order.executorch_call_delegate": 1})
29            .check_not(["executorch_exir_dialects_edge__ops_aten_neg_default"])
30            .to_executorch()
31            .serialize()
32            .run_method_and_compare_outputs()
33        )
34
35    def test_fp16_negate(self):
36        inputs = (
37            torch.Tensor(
38                [
39                    [0.0, 0.1, 0.5, 0.499],
40                    [-0.6, -0.4, 100.1, -1000.1],
41                ],
42            ).to(torch.float16),
43        )
44        self._test_negate(inputs)
45
46    def test_fp32_negate(self):
47        inputs = (
48            torch.Tensor(
49                [
50                    [0.0, 0.1, 0.5, 0.499],
51                    [-0.6, -0.4, 100.1, -1000.1],
52                ],
53            ),
54        )
55        self._test_negate(inputs)
56