|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# Copyright 2025 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 | + |
| 8 | +import unittest |
| 9 | + |
| 10 | +from typing import Tuple |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +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 | + ) |
0 commit comments