|
| 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 ClassVar, Dict, Tuple |
| 7 | + |
| 8 | +import torch |
| 9 | +from executorch.backends.arm._passes import PromoteBoolOperandsPass |
| 10 | + |
| 11 | +from executorch.backends.arm.test import common |
| 12 | +from executorch.backends.arm.test.tester.test_pipeline import PassPipeline |
| 13 | +from executorch.backends.test.harness.stages import StageType |
| 14 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 15 | + |
| 16 | +tensor_pair_t = Tuple[torch.Tensor, torch.Tensor] |
| 17 | + |
| 18 | + |
| 19 | +def _collect_cast_dtypes(pipeline: PassPipeline[tensor_pair_t]) -> list[torch.dtype]: |
| 20 | + exported_program = pipeline.tester.get_artifact( |
| 21 | + StageType.RUN_PASSES |
| 22 | + ).exported_program() |
| 23 | + graph_module = exported_program.graph_module |
| 24 | + cast_dtypes: list[torch.dtype] = [] |
| 25 | + for node in graph_module.graph.nodes: |
| 26 | + if ( |
| 27 | + node.op == "call_function" |
| 28 | + and node.target == exir_ops.edge.dim_order_ops._to_dim_order_copy.default |
| 29 | + and "dtype" in node.kwargs |
| 30 | + ): |
| 31 | + cast_dtypes.append(node.kwargs["dtype"]) |
| 32 | + return cast_dtypes |
| 33 | + |
| 34 | + |
| 35 | +class BoolBitwiseAndModule(torch.nn.Module): |
| 36 | + test_data: ClassVar[Dict[str, tensor_pair_t]] = { |
| 37 | + "bool_tensors": ( |
| 38 | + torch.tensor([[True, False], [False, True]], dtype=torch.bool), |
| 39 | + torch.tensor([[False, True], [True, False]], dtype=torch.bool), |
| 40 | + ) |
| 41 | + } |
| 42 | + |
| 43 | + def forward(self, lhs: torch.Tensor, rhs: torch.Tensor) -> torch.Tensor: |
| 44 | + return torch.bitwise_and(lhs, rhs) |
| 45 | + |
| 46 | + |
| 47 | +class MixedMulModule(torch.nn.Module): |
| 48 | + test_data: ClassVar[Dict[str, tensor_pair_t]] = { |
| 49 | + "mixed_tensors": ( |
| 50 | + torch.tensor([True, False, True, False], dtype=torch.bool), |
| 51 | + torch.tensor([1, 2, 3, 4], dtype=torch.int32), |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + def forward(self, lhs: torch.Tensor, rhs: torch.Tensor) -> torch.Tensor: |
| 56 | + return torch.mul(lhs, rhs) |
| 57 | + |
| 58 | + |
| 59 | +@common.parametrize("test_data", BoolBitwiseAndModule.test_data) |
| 60 | +def test_promote_bool_operands_all_bool(test_data: tensor_pair_t) -> None: |
| 61 | + module = BoolBitwiseAndModule() |
| 62 | + ops_before_pass = { |
| 63 | + "executorch_exir_dialects_edge__ops_aten_bitwise_and_Tensor": 1, |
| 64 | + } |
| 65 | + ops_after_pass = { |
| 66 | + "executorch_exir_dialects_edge__ops_aten_bitwise_and_Tensor": 1, |
| 67 | + "executorch_exir_dialects_edge__ops_dim_order_ops__to_dim_order_copy_default": 3, |
| 68 | + } |
| 69 | + pipeline = PassPipeline[tensor_pair_t]( |
| 70 | + module, |
| 71 | + test_data, |
| 72 | + quantize=False, |
| 73 | + ops_before_pass=ops_before_pass, |
| 74 | + ops_after_pass=ops_after_pass, |
| 75 | + pass_list=[PromoteBoolOperandsPass], |
| 76 | + ) |
| 77 | + pipeline.run() |
| 78 | + cast_dtypes = _collect_cast_dtypes(pipeline) |
| 79 | + assert cast_dtypes.count(torch.int8) == 2 |
| 80 | + assert cast_dtypes.count(torch.bool) == 1 |
| 81 | + |
| 82 | + |
| 83 | +@common.parametrize("test_data", MixedMulModule.test_data) |
| 84 | +def test_promote_bool_operands_mixed_types(test_data: tensor_pair_t) -> None: |
| 85 | + module = MixedMulModule() |
| 86 | + ops_before_pass = { |
| 87 | + "executorch_exir_dialects_edge__ops_aten_mul_Tensor": 1, |
| 88 | + } |
| 89 | + ops_after_pass = { |
| 90 | + "executorch_exir_dialects_edge__ops_aten_mul_Tensor": 1, |
| 91 | + "executorch_exir_dialects_edge__ops_dim_order_ops__to_dim_order_copy_default": 1, |
| 92 | + } |
| 93 | + pipeline = PassPipeline[tensor_pair_t]( |
| 94 | + module, |
| 95 | + test_data, |
| 96 | + quantize=False, |
| 97 | + ops_before_pass=ops_before_pass, |
| 98 | + ops_after_pass=ops_after_pass, |
| 99 | + pass_list=[PromoteBoolOperandsPass], |
| 100 | + ) |
| 101 | + pipeline.run() |
| 102 | + cast_dtypes = _collect_cast_dtypes(pipeline) |
| 103 | + assert cast_dtypes.count(torch.int32) == 1 |
0 commit comments