|
| 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 | +from typing import Tuple |
| 7 | + |
| 8 | +import torch |
| 9 | +from executorch.backends.arm.test import common |
| 10 | + |
| 11 | +from executorch.backends.arm.test.tester.test_pipeline import ( |
| 12 | + EthosU85PipelineBI, |
| 13 | + OpNotSupportedPipeline, |
| 14 | + TosaPipelineBI, |
| 15 | + TosaPipelineMI, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +input_t = Tuple[torch.Tensor] |
| 20 | + |
| 21 | + |
| 22 | +class NotEqual(torch.nn.Module): |
| 23 | + aten_op_Tensor = "torch.ops.aten.ne.Tensor" |
| 24 | + aten_op_Scalar = "torch.ops.aten.ne.Scalar" |
| 25 | + decomposed_ops = ["torch.ops.aten.eq.Tensor", "torch.ops.aten.logical_not.default"] |
| 26 | + decomposed_exir_ops = [ |
| 27 | + "executorch_exir_dialects_edge__ops_aten_eq_Tensor", |
| 28 | + "executorch_exir_dialects_edge__ops_aten_logical_not_default", |
| 29 | + ] |
| 30 | + exir_op = "executorch_exir_dialects_edge__ops_aten_ne_Tensor" |
| 31 | + |
| 32 | + def __init__(self, input, other): |
| 33 | + super().__init__() |
| 34 | + self.input_ = input |
| 35 | + self.other_ = other |
| 36 | + |
| 37 | + def forward( |
| 38 | + self, |
| 39 | + input_: torch.Tensor, |
| 40 | + other_: torch.Tensor | int | float, |
| 41 | + ): |
| 42 | + return input_ != other_ |
| 43 | + |
| 44 | + def get_inputs(self): |
| 45 | + return (self.input_, self.other_) |
| 46 | + |
| 47 | + |
| 48 | +op_ne_tensor_rank1_ones = NotEqual( |
| 49 | + torch.ones(5), |
| 50 | + torch.ones(5), |
| 51 | +) |
| 52 | +op_ne_tensor_rank2_rand = NotEqual( |
| 53 | + torch.rand(4, 5), |
| 54 | + torch.rand(1, 5), |
| 55 | +) |
| 56 | +op_ne_tensor_rank3_randn = NotEqual( |
| 57 | + torch.randn(10, 5, 2), |
| 58 | + torch.randn(10, 5, 2), |
| 59 | +) |
| 60 | +op_ne_tensor_rank4_randn = NotEqual( |
| 61 | + torch.randn(3, 2, 2, 2), |
| 62 | + torch.randn(3, 2, 2, 2), |
| 63 | +) |
| 64 | + |
| 65 | +op_ne_scalar_rank1_ones = NotEqual(torch.ones(5), 1.0) |
| 66 | +op_ne_scalar_rank2_rand = NotEqual(torch.rand(4, 5), 0.2) |
| 67 | +op_ne_scalar_rank3_randn = NotEqual(torch.randn(10, 5, 2), -0.1) |
| 68 | +op_ne_scalar_rank4_randn = NotEqual(torch.randn(3, 2, 2, 2), 0.3) |
| 69 | +op_ne_scalar_rank4_randn_1batch = NotEqual(torch.randn(1, 2, 2, 2), 0.3) |
| 70 | + |
| 71 | +test_data_tensor = { |
| 72 | + "ne_tensor_rank1_ones": op_ne_tensor_rank1_ones, |
| 73 | + "ne_tensor_rank2_rand": op_ne_tensor_rank2_rand, |
| 74 | + "ne_tensor_rank3_randn": op_ne_tensor_rank3_randn, |
| 75 | + "ne_tensor_rank4_randn": op_ne_tensor_rank4_randn, |
| 76 | +} |
| 77 | + |
| 78 | +test_data_scalar = { |
| 79 | + "ne_scalar_rank1_ones": op_ne_scalar_rank1_ones, |
| 80 | + "ne_scalar_rank2_rand": op_ne_scalar_rank2_rand, |
| 81 | + "ne_scalar_rank3_randn": op_ne_scalar_rank3_randn, |
| 82 | + "ne_scalar_rank4_randn": op_ne_scalar_rank4_randn, |
| 83 | + "ne_scalar_rank4_randn_1batch": op_ne_scalar_rank4_randn_1batch, |
| 84 | +} |
| 85 | + |
| 86 | + |
| 87 | +@common.parametrize("test_module", test_data_tensor) |
| 88 | +def test_ne_tensor_tosa_MI(test_module): |
| 89 | + pipeline = TosaPipelineMI[input_t]( |
| 90 | + test_module, test_module.get_inputs(), NotEqual.aten_op_Tensor, NotEqual.exir_op |
| 91 | + ) |
| 92 | + pipeline.run() |
| 93 | + |
| 94 | + |
| 95 | +@common.parametrize("test_module", test_data_scalar) |
| 96 | +def test_ne_scalar_tosa_MI(test_module): |
| 97 | + pipeline = TosaPipelineMI[input_t]( |
| 98 | + test_module, |
| 99 | + test_module.get_inputs(), |
| 100 | + NotEqual.aten_op_Scalar, |
| 101 | + NotEqual.exir_op, |
| 102 | + ) |
| 103 | + pipeline.run() |
| 104 | + |
| 105 | + |
| 106 | +@common.parametrize("test_module", test_data_tensor) |
| 107 | +def test_ne_tensor_tosa_BI(test_module): |
| 108 | + pipeline = TosaPipelineBI[input_t]( |
| 109 | + test_module, test_module.get_inputs(), NotEqual.decomposed_ops, NotEqual.exir_op |
| 110 | + ) |
| 111 | + pipeline.run() |
| 112 | + |
| 113 | + |
| 114 | +@common.parametrize("test_module", test_data_scalar) |
| 115 | +def test_ne_scalar_tosa_BI(test_module): |
| 116 | + pipeline = TosaPipelineBI[input_t]( |
| 117 | + test_module, test_module.get_inputs(), NotEqual.decomposed_ops, NotEqual.exir_op |
| 118 | + ) |
| 119 | + pipeline.run() |
| 120 | + |
| 121 | + |
| 122 | +@common.parametrize("test_module", test_data_tensor) |
| 123 | +@common.XfailIfNoCorstone300 |
| 124 | +def test_ne_tensor_u55_BI(test_module): |
| 125 | + # EQUAL is not supported on U55. |
| 126 | + pipeline = OpNotSupportedPipeline[input_t]( |
| 127 | + test_module, |
| 128 | + test_module.get_inputs(), |
| 129 | + "TOSA-0.80+BI+u55", |
| 130 | + { |
| 131 | + NotEqual.decomposed_exir_ops[0]: 1, |
| 132 | + NotEqual.decomposed_exir_ops[1]: 1, |
| 133 | + }, |
| 134 | + ) |
| 135 | + pipeline.run() |
| 136 | + |
| 137 | + |
| 138 | +@common.parametrize("test_module", test_data_scalar) |
| 139 | +@common.XfailIfNoCorstone300 |
| 140 | +def test_ne_scalar_u55_BI(test_module): |
| 141 | + # Not equal (ne) is decomposed into the TOSA ops EQUAL and LOGICAL_NOT, both of |
| 142 | + # which are unsupported on U55. |
| 143 | + pipeline = OpNotSupportedPipeline[input_t]( |
| 144 | + test_module, |
| 145 | + test_module.get_inputs(), |
| 146 | + "TOSA-0.80+BI+u55", |
| 147 | + { |
| 148 | + NotEqual.decomposed_exir_ops[0]: 1, |
| 149 | + NotEqual.decomposed_exir_ops[1]: 1, |
| 150 | + }, |
| 151 | + n_expected_delegates=1, |
| 152 | + ) |
| 153 | + pipeline.run() |
| 154 | + |
| 155 | + |
| 156 | +@common.parametrize( |
| 157 | + "test_module", |
| 158 | + test_data_tensor, |
| 159 | + xfails={ |
| 160 | + "ne_tensor_rank4_randn": "MLETORCH-517: Batch size > 1 not fully supported", |
| 161 | + }, |
| 162 | + strict=False, |
| 163 | +) |
| 164 | +@common.XfailIfNoCorstone320 |
| 165 | +def test_ne_tensor_u85_BI(test_module): |
| 166 | + pipeline = EthosU85PipelineBI[input_t]( |
| 167 | + test_module, |
| 168 | + test_module.get_inputs(), |
| 169 | + NotEqual.decomposed_ops, |
| 170 | + NotEqual.decomposed_exir_ops, |
| 171 | + run_on_fvp=True, |
| 172 | + ) |
| 173 | + pipeline.run() |
| 174 | + |
| 175 | + |
| 176 | +@common.parametrize( |
| 177 | + "test_module", |
| 178 | + test_data_scalar, |
| 179 | + xfails={ |
| 180 | + "ne_scalar_rank4_randn": "MLETORCH-517: Batch size > 1 not fully supported", |
| 181 | + "ne_scalar_rank4_randn_1batch": "MLETORCH-847: Boolean ne result unstable on U85", |
| 182 | + }, |
| 183 | + strict=False, |
| 184 | +) |
| 185 | +@common.XfailIfNoCorstone320 |
| 186 | +def test_ne_scalar_u85_BI(test_module): |
| 187 | + pipeline = EthosU85PipelineBI[input_t]( |
| 188 | + test_module, |
| 189 | + test_module.get_inputs(), |
| 190 | + NotEqual.decomposed_ops, |
| 191 | + NotEqual.decomposed_exir_ops, |
| 192 | + run_on_fvp=True, |
| 193 | + ) |
| 194 | + pipeline.run() |
0 commit comments