|
| 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 torch |
| 7 | +from executorch.backends.arm.arm_partitioner import ArmPartitioner |
| 8 | +from executorch.backends.arm.test import common |
| 9 | +from executorch.backends.arm.test.tester.arm_tester import ArmTester |
| 10 | +from executorch.exir.backend.operator_support import ( |
| 11 | + DontPartition, |
| 12 | + DontPartitionModule, |
| 13 | + DontPartitionName, |
| 14 | +) |
| 15 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 16 | + |
| 17 | + |
| 18 | +class CustomPartitioning(torch.nn.Module): |
| 19 | + inputs = (torch.randn(10, 4, 5), torch.randn(10, 4, 5)) |
| 20 | + |
| 21 | + def forward(self, x: torch.Tensor, y: torch.Tensor): |
| 22 | + z = x + y |
| 23 | + s = torch.sigmoid(z) |
| 24 | + return s * z |
| 25 | + |
| 26 | + |
| 27 | +class NestedModule(torch.nn.Module): |
| 28 | + inputs = (torch.randn(10, 4, 5), torch.randn(10, 4, 5)) |
| 29 | + |
| 30 | + def __init__(self): |
| 31 | + super().__init__() |
| 32 | + self.nested = CustomPartitioning() |
| 33 | + |
| 34 | + def forward(self, x: torch.Tensor, y: torch.Tensor): |
| 35 | + a = x.sigmoid() |
| 36 | + b = a + y |
| 37 | + return self.nested(a, b) |
| 38 | + |
| 39 | + |
| 40 | +def test_single_reject(): |
| 41 | + module = CustomPartitioning() |
| 42 | + inputs = module.inputs |
| 43 | + compile_spec = common.get_tosa_compile_spec("TOSA-0.80+MI") |
| 44 | + check = DontPartition(exir_ops.edge.aten.sigmoid.default) |
| 45 | + partitioner = ArmPartitioner(compile_spec, additional_checks=[check]) |
| 46 | + ( |
| 47 | + ArmTester( |
| 48 | + module, |
| 49 | + example_inputs=inputs, |
| 50 | + compile_spec=compile_spec, |
| 51 | + ) |
| 52 | + .export() |
| 53 | + .to_edge_transform_and_lower(partitioners=[partitioner]) |
| 54 | + .check(["executorch_exir_dialects_edge__ops_aten_sigmoid_default"]) |
| 55 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 2}) |
| 56 | + .to_executorch() |
| 57 | + .run_method_and_compare_outputs(inputs=inputs) |
| 58 | + ) |
| 59 | + assert check.has_rejected_node() |
| 60 | + |
| 61 | + |
| 62 | +def test_multiple_reject(): |
| 63 | + module = CustomPartitioning() |
| 64 | + inputs = module.inputs |
| 65 | + compile_spec = common.get_tosa_compile_spec("TOSA-0.80+MI") |
| 66 | + check = DontPartition( |
| 67 | + exir_ops.edge.aten.sigmoid.default, exir_ops.edge.aten.mul.Tensor |
| 68 | + ) |
| 69 | + partitioner = ArmPartitioner(compile_spec, additional_checks=[check]) |
| 70 | + ( |
| 71 | + ArmTester( |
| 72 | + module, |
| 73 | + example_inputs=inputs, |
| 74 | + compile_spec=compile_spec, |
| 75 | + ) |
| 76 | + .export() |
| 77 | + .to_edge_transform_and_lower(partitioners=[partitioner]) |
| 78 | + .check(["executorch_exir_dialects_edge__ops_aten_sigmoid_default"]) |
| 79 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
| 80 | + .to_executorch() |
| 81 | + .run_method_and_compare_outputs(inputs=inputs) |
| 82 | + ) |
| 83 | + assert check.has_rejected_node() |
| 84 | + |
| 85 | + |
| 86 | +def test_torch_op_reject(): |
| 87 | + module = CustomPartitioning() |
| 88 | + inputs = module.inputs |
| 89 | + compile_spec = common.get_tosa_compile_spec("TOSA-0.80+MI") |
| 90 | + check = DontPartition(torch.ops.aten.sigmoid.default) |
| 91 | + partitioner = ArmPartitioner(compile_spec, additional_checks=[check]) |
| 92 | + ( |
| 93 | + ArmTester( |
| 94 | + module, |
| 95 | + example_inputs=inputs, |
| 96 | + compile_spec=compile_spec, |
| 97 | + ) |
| 98 | + .export() |
| 99 | + .to_edge_transform_and_lower(partitioners=[partitioner]) |
| 100 | + .check(["executorch_exir_dialects_edge__ops_aten_sigmoid_default"]) |
| 101 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 2}) |
| 102 | + .to_executorch() |
| 103 | + .run_method_and_compare_outputs(inputs=inputs) |
| 104 | + ) |
| 105 | + assert check.has_rejected_node() |
| 106 | + |
| 107 | + |
| 108 | +def test_string_op_reject(): |
| 109 | + module = CustomPartitioning() |
| 110 | + inputs = module.inputs |
| 111 | + compile_spec = common.get_tosa_compile_spec("TOSA-0.80+MI") |
| 112 | + check = DontPartition("aten.sigmoid.default") |
| 113 | + partitioner = ArmPartitioner(compile_spec, additional_checks=[check]) |
| 114 | + ( |
| 115 | + ArmTester( |
| 116 | + module, |
| 117 | + example_inputs=inputs, |
| 118 | + compile_spec=compile_spec, |
| 119 | + ) |
| 120 | + .export() |
| 121 | + .to_edge_transform_and_lower(partitioners=[partitioner]) |
| 122 | + .check(["executorch_exir_dialects_edge__ops_aten_sigmoid_default"]) |
| 123 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 2}) |
| 124 | + .to_executorch() |
| 125 | + .run_method_and_compare_outputs(inputs=inputs) |
| 126 | + ) |
| 127 | + |
| 128 | + assert check.has_rejected_node() |
| 129 | + |
| 130 | + |
| 131 | +def test_name_reject(): |
| 132 | + module = CustomPartitioning() |
| 133 | + inputs = module.inputs |
| 134 | + compile_spec = common.get_tosa_compile_spec("TOSA-0.80+MI") |
| 135 | + check = DontPartitionName("mul", "sigmoid", exact=False) |
| 136 | + partitioner = ArmPartitioner(compile_spec, additional_checks=[check]) |
| 137 | + ( |
| 138 | + ArmTester( |
| 139 | + module, |
| 140 | + example_inputs=inputs, |
| 141 | + compile_spec=compile_spec, |
| 142 | + ) |
| 143 | + .export() |
| 144 | + .to_edge_transform_and_lower(partitioners=[partitioner]) |
| 145 | + .check(["executorch_exir_dialects_edge__ops_aten_sigmoid_default"]) |
| 146 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
| 147 | + .to_executorch() |
| 148 | + .run_method_and_compare_outputs(inputs=inputs) |
| 149 | + ) |
| 150 | + assert check.has_rejected_node() |
| 151 | + |
| 152 | + |
| 153 | +def test_module_reject(): |
| 154 | + module = NestedModule() |
| 155 | + inputs = module.inputs |
| 156 | + compile_spec = common.get_tosa_compile_spec("TOSA-0.80+MI") |
| 157 | + check = DontPartitionModule(module_name="CustomPartitioning") |
| 158 | + partitioner = ArmPartitioner(compile_spec, additional_checks=[check]) |
| 159 | + ( |
| 160 | + ArmTester( |
| 161 | + module, |
| 162 | + example_inputs=inputs, |
| 163 | + compile_spec=compile_spec, |
| 164 | + ) |
| 165 | + .export() |
| 166 | + .to_edge_transform_and_lower(partitioners=[partitioner]) |
| 167 | + .check(["executorch_exir_dialects_edge__ops_aten_sigmoid_default"]) |
| 168 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
| 169 | + .to_executorch() |
| 170 | + .run_method_and_compare_outputs(inputs=inputs) |
| 171 | + ) |
| 172 | + assert check.has_rejected_node() |
| 173 | + |
| 174 | + |
| 175 | +def test_inexact_module_reject(): |
| 176 | + module = NestedModule() |
| 177 | + inputs = module.inputs |
| 178 | + compile_spec = common.get_tosa_compile_spec("TOSA-0.80+MI") |
| 179 | + check = DontPartitionModule(module_name="Custom", exact=False) |
| 180 | + partitioner = ArmPartitioner(compile_spec, additional_checks=[check]) |
| 181 | + ( |
| 182 | + ArmTester( |
| 183 | + module, |
| 184 | + example_inputs=inputs, |
| 185 | + compile_spec=compile_spec, |
| 186 | + ) |
| 187 | + .export() |
| 188 | + .to_edge_transform_and_lower(partitioners=[partitioner]) |
| 189 | + .check(["executorch_exir_dialects_edge__ops_aten_sigmoid_default"]) |
| 190 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
| 191 | + .to_executorch() |
| 192 | + .run_method_and_compare_outputs(inputs=inputs) |
| 193 | + ) |
| 194 | + assert check.has_rejected_node() |
| 195 | + |
| 196 | + |
| 197 | +def test_module_instance_reject(): |
| 198 | + module = NestedModule() |
| 199 | + inputs = module.inputs |
| 200 | + compile_spec = common.get_tosa_compile_spec("TOSA-0.80+MI") |
| 201 | + check = DontPartitionModule(instance_name="nested") |
| 202 | + partitioner = ArmPartitioner(compile_spec, additional_checks=[check]) |
| 203 | + ( |
| 204 | + ArmTester( |
| 205 | + module, |
| 206 | + example_inputs=inputs, |
| 207 | + compile_spec=compile_spec, |
| 208 | + ) |
| 209 | + .export() |
| 210 | + .to_edge_transform_and_lower(partitioners=[partitioner]) |
| 211 | + .check(["executorch_exir_dialects_edge__ops_aten_sigmoid_default"]) |
| 212 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
| 213 | + .to_executorch() |
| 214 | + .run_method_and_compare_outputs(inputs=inputs) |
| 215 | + ) |
| 216 | + assert check.has_rejected_node() |
0 commit comments