|
| 1 | +# Copyright 2025 Arm Limited and/or its affiliates. |
| 2 | +# |
| 3 | +# This source code is licensed under the BSD-style license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +import unittest |
| 7 | + |
| 8 | +from typing import Callable, NamedTuple, Tuple |
| 9 | + |
| 10 | +import torch |
| 11 | +from executorch.backends.arm.test import common, conftest |
| 12 | +from executorch.backends.arm.test.tester.arm_tester import ArmTester |
| 13 | +from parameterized import parameterized |
| 14 | + |
| 15 | + |
| 16 | +class DataTuple(NamedTuple): |
| 17 | + name: str |
| 18 | + tensor1: torch.Tensor |
| 19 | + tensor2: torch.Tensor |
| 20 | + |
| 21 | + |
| 22 | +class OpTuple(NamedTuple): |
| 23 | + name: str |
| 24 | + operator: torch.nn.Module |
| 25 | + |
| 26 | + |
| 27 | +class And(torch.nn.Module): |
| 28 | + def forward(self, tensor1: torch.Tensor, tensor2: torch.Tensor): |
| 29 | + return tensor1.bitwise_and(tensor2) |
| 30 | + |
| 31 | + |
| 32 | +class Xor(torch.nn.Module): |
| 33 | + def forward(self, tensor1: torch.Tensor, tensor2: torch.Tensor): |
| 34 | + return tensor1.bitwise_xor(tensor2) |
| 35 | + |
| 36 | + |
| 37 | +class Or(torch.nn.Module): |
| 38 | + def forward(self, tensor1: torch.Tensor, tensor2: torch.Tensor): |
| 39 | + return tensor1.bitwise_or(tensor2) |
| 40 | + |
| 41 | + |
| 42 | +test_data_suite: list[DataTuple] = [ |
| 43 | + DataTuple( |
| 44 | + "zeros", |
| 45 | + torch.zeros(1, 10, 10, 10, dtype=torch.int32), |
| 46 | + torch.zeros(1, 10, 10, 10, dtype=torch.int32), |
| 47 | + ), |
| 48 | + DataTuple( |
| 49 | + "ones", |
| 50 | + torch.ones(10, 10, 10, dtype=torch.int8), |
| 51 | + torch.ones(10, 10, 10, dtype=torch.int8), |
| 52 | + ), |
| 53 | + DataTuple( |
| 54 | + "rand_rank2", |
| 55 | + torch.randint(-128, 127, (10, 10), dtype=torch.int8), |
| 56 | + torch.randint(-128, 127, (10, 10), dtype=torch.int8), |
| 57 | + ), |
| 58 | + DataTuple( |
| 59 | + "rand_rank4", |
| 60 | + torch.randint(-128, -127, (1, 10, 10, 10), dtype=torch.int8), |
| 61 | + torch.randint(-128, 127, (1, 10, 10, 10), dtype=torch.int8), |
| 62 | + ), |
| 63 | +] |
| 64 | + |
| 65 | + |
| 66 | +ops: list[OpTuple] = [ |
| 67 | + OpTuple("and", And()), |
| 68 | + OpTuple("or", Or()), |
| 69 | + OpTuple("xor", Xor()), |
| 70 | +] |
| 71 | + |
| 72 | +full_test_suite = [] |
| 73 | +for op in ops: |
| 74 | + for test_data in test_data_suite: |
| 75 | + full_test_suite.append( |
| 76 | + ( |
| 77 | + f"{op.name}_{test_data.name}", |
| 78 | + op.operator, |
| 79 | + test_data.tensor1, |
| 80 | + test_data.tensor2, |
| 81 | + ) |
| 82 | + ) |
| 83 | + |
| 84 | +del test_data |
| 85 | +del ops |
| 86 | + |
| 87 | + |
| 88 | +class TestBitwise(unittest.TestCase): |
| 89 | + |
| 90 | + def _test_bitwise_tosa_MI_pipeline( |
| 91 | + self, module: torch.nn.Module, test_data: Tuple[torch.tensor, torch.tensor] |
| 92 | + ): |
| 93 | + ( |
| 94 | + ArmTester( |
| 95 | + module, |
| 96 | + example_inputs=test_data, |
| 97 | + compile_spec=common.get_tosa_compile_spec("TOSA-0.80+MI"), |
| 98 | + ) |
| 99 | + .export() |
| 100 | + .to_edge_transform_and_lower() |
| 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_bitwise_tosa_BI_pipeline( |
| 107 | + self, module: torch.nn.Module, test_data: Tuple[torch.tensor, torch.tensor] |
| 108 | + ): |
| 109 | + ( |
| 110 | + ArmTester( |
| 111 | + module, |
| 112 | + example_inputs=test_data, |
| 113 | + compile_spec=common.get_tosa_compile_spec( |
| 114 | + "TOSA-0.80+BI", custom_path="local_bin/bitwise" |
| 115 | + ), |
| 116 | + ) |
| 117 | + .export() |
| 118 | + .to_edge_transform_and_lower() |
| 119 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
| 120 | + .to_executorch() |
| 121 | + .run_method_and_compare_outputs(inputs=test_data) |
| 122 | + ) |
| 123 | + |
| 124 | + def _test_bitwise_tosa_u55_BI_pipeline( |
| 125 | + self, module: torch.nn.Module, test_data: Tuple[torch.tensor] |
| 126 | + ): |
| 127 | + # Tests that we don't delegate these ops since they are not supported on U55. |
| 128 | + ( |
| 129 | + ArmTester( |
| 130 | + module, |
| 131 | + example_inputs=test_data, |
| 132 | + compile_spec=common.get_u55_compile_spec(), |
| 133 | + ) |
| 134 | + .export() |
| 135 | + .to_edge_transform_and_lower() |
| 136 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 0}) |
| 137 | + ) |
| 138 | + |
| 139 | + def _test_bitwise_tosa_u85_BI_pipeline( |
| 140 | + self, module: torch.nn.Module, test_data: Tuple[torch.tensor] |
| 141 | + ): |
| 142 | + tester = ( |
| 143 | + ArmTester( |
| 144 | + module, |
| 145 | + example_inputs=test_data, |
| 146 | + compile_spec=common.get_u85_compile_spec(), |
| 147 | + ) |
| 148 | + .export() |
| 149 | + .to_edge_transform_and_lower() |
| 150 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
| 151 | + .to_executorch() |
| 152 | + .serialize() |
| 153 | + ) |
| 154 | + if conftest.is_option_enabled("corstone_fvp"): |
| 155 | + tester.run_method_and_compare_outputs(inputs=test_data) |
| 156 | + |
| 157 | + @parameterized.expand(full_test_suite) |
| 158 | + def test_tosa_MI( |
| 159 | + self, |
| 160 | + test_name: str, |
| 161 | + operator: Callable, |
| 162 | + tensor1: torch.Tensor, |
| 163 | + tensor2: torch.Tensor, |
| 164 | + ): |
| 165 | + self._test_bitwise_tosa_MI_pipeline(operator, (tensor1, tensor2)) |
| 166 | + |
| 167 | + @parameterized.expand(full_test_suite) |
| 168 | + def test_tosa_BI( |
| 169 | + self, |
| 170 | + test_name: str, |
| 171 | + operator: Callable, |
| 172 | + tensor1: torch.Tensor, |
| 173 | + tensor2: torch.Tensor, |
| 174 | + ): |
| 175 | + self._test_bitwise_tosa_BI_pipeline(operator, (tensor1, tensor2)) |
| 176 | + |
| 177 | + @parameterized.expand(full_test_suite) |
| 178 | + def test_tosa_u55_BI( |
| 179 | + self, |
| 180 | + test_name: str, |
| 181 | + operator: Callable, |
| 182 | + tensor1: torch.Tensor, |
| 183 | + tensor2: torch.Tensor, |
| 184 | + ): |
| 185 | + self._test_bitwise_tosa_u55_BI_pipeline(operator, (tensor1, tensor2)) |
| 186 | + |
| 187 | + @parameterized.expand(full_test_suite) |
| 188 | + def test_tosa_u85_BI( |
| 189 | + self, |
| 190 | + test_name: str, |
| 191 | + operator: Callable, |
| 192 | + tensor1: torch.Tensor, |
| 193 | + tensor2: torch.Tensor, |
| 194 | + ): |
| 195 | + self._test_bitwise_tosa_u85_BI_pipeline(operator, (tensor1, tensor2)) |
0 commit comments