|
| 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 | + |
| 10 | +from executorch.backends.arm.test import common |
| 11 | + |
| 12 | +from executorch.backends.arm.test.tester.test_pipeline import ( |
| 13 | + EthosU85PipelineBI, |
| 14 | + OpNotSupportedPipeline, |
| 15 | + TosaPipelineBI, |
| 16 | + TosaPipelineMI, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +aten_op = "torch.aten.ops.masked_fill.Scalar" |
| 21 | +exir_op = "executorch_exir_dialects_edge__ops_aten_masked_fill_scalar" |
| 22 | + |
| 23 | +input_t = Tuple[torch.Tensor, torch.Tensor, float] |
| 24 | + |
| 25 | + |
| 26 | +class MaskedFill(torch.nn.Module): |
| 27 | + def forward( |
| 28 | + self, x: torch.Tensor, mask: torch.Tensor, value: float |
| 29 | + ) -> torch.Tensor: |
| 30 | + return torch.masked_fill(x, mask, value) |
| 31 | + |
| 32 | + |
| 33 | +test_modules = { |
| 34 | + "masked_fill_1": lambda: ( |
| 35 | + MaskedFill(), |
| 36 | + ( |
| 37 | + torch.rand(1, 3, 4, 5), |
| 38 | + (torch.rand(1, 3, 4, 5) < 0.5), # boolean mask |
| 39 | + -1.0, |
| 40 | + ), |
| 41 | + ), |
| 42 | + "masked_fill_2": lambda: ( |
| 43 | + MaskedFill(), |
| 44 | + ( |
| 45 | + torch.rand(1, 10, 10, 10), |
| 46 | + (torch.rand(1, 10, 10, 10) > 0.75), |
| 47 | + 3.14, |
| 48 | + ), |
| 49 | + ), |
| 50 | + "masked_fill_3_zero_fill": lambda: ( |
| 51 | + MaskedFill(), |
| 52 | + ( |
| 53 | + torch.rand(1, 3, 4, 5), |
| 54 | + torch.rand(1, 3, 4, 5) < 0.2, |
| 55 | + 0.0, |
| 56 | + ), |
| 57 | + ), |
| 58 | + "masked_fill_4_full_mask": lambda: ( |
| 59 | + MaskedFill(), |
| 60 | + ( |
| 61 | + torch.rand(1, 3, 4, 5), |
| 62 | + torch.ones(1, 3, 4, 5, dtype=torch.bool), |
| 63 | + 7.0, |
| 64 | + ), |
| 65 | + ), |
| 66 | + "masked_fill_5_no_mask": lambda: ( |
| 67 | + MaskedFill(), |
| 68 | + ( |
| 69 | + torch.rand(1, 3, 4, 5), |
| 70 | + torch.zeros(1, 3, 4, 5, dtype=torch.bool), |
| 71 | + -3.0, |
| 72 | + ), |
| 73 | + ), |
| 74 | + "masked_fill_6_scalar_broadcast": lambda: ( |
| 75 | + MaskedFill(), |
| 76 | + ( |
| 77 | + torch.rand(1, 1, 1, 1), |
| 78 | + torch.tensor([[[[True]]]]), |
| 79 | + 42.0, |
| 80 | + ), |
| 81 | + ), |
| 82 | + "masked_fill_7_large_tensor": lambda: ( |
| 83 | + MaskedFill(), |
| 84 | + ( |
| 85 | + torch.rand(1, 8, 8, 8), |
| 86 | + torch.rand(1, 8, 8, 8) > 0.5, |
| 87 | + -127.0, |
| 88 | + ), |
| 89 | + ), |
| 90 | + "masked_fill_8_extreme_scalar_inf": lambda: ( |
| 91 | + MaskedFill(), |
| 92 | + ( |
| 93 | + torch.rand(1, 3, 7, 5), |
| 94 | + torch.rand(1, 3, 7, 5) > 0.5, |
| 95 | + float("inf"), |
| 96 | + ), |
| 97 | + ), |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +@common.parametrize("test_module", test_modules) |
| 102 | +def test_masked_fill_scalar_tosa_MI(test_module): |
| 103 | + module, inputs = test_module() |
| 104 | + pipeline = TosaPipelineMI[input_t](module, inputs, aten_op=[]) |
| 105 | + pipeline.run() |
| 106 | + |
| 107 | + |
| 108 | +@common.parametrize("test_module", test_modules) |
| 109 | +def test_masked_fill_scalar_tosa_BI(test_module): |
| 110 | + module, inputs = test_module() |
| 111 | + pipeline = TosaPipelineBI[input_t]( |
| 112 | + module, |
| 113 | + inputs, |
| 114 | + aten_op=[], |
| 115 | + ) |
| 116 | + pipeline.run() |
| 117 | + |
| 118 | + |
| 119 | +@common.parametrize("test_module", test_modules) |
| 120 | +@common.XfailIfNoCorstone300 |
| 121 | +def test_masked_fill_scalar_u55_BI(test_module): |
| 122 | + module, inputs = test_module() |
| 123 | + pipeline = OpNotSupportedPipeline[input_t]( |
| 124 | + module, |
| 125 | + inputs, |
| 126 | + {exir_op: 0, "executorch_exir_dialects_edge__ops_aten_where_self": 1}, |
| 127 | + n_expected_delegates=0, |
| 128 | + quantize=True, |
| 129 | + u55_subset=True, |
| 130 | + ) |
| 131 | + pipeline.run() |
| 132 | + |
| 133 | + |
| 134 | +@common.parametrize("test_module", test_modules) |
| 135 | +@common.XfailIfNoCorstone320 |
| 136 | +def test_masked_fill_scalar_u85_BI(test_module): |
| 137 | + module, inputs = test_module() |
| 138 | + pipeline = EthosU85PipelineBI[input_t]( |
| 139 | + module, |
| 140 | + inputs, |
| 141 | + aten_ops=[], |
| 142 | + exir_ops=exir_op, |
| 143 | + ) |
| 144 | + pipeline.run() |
0 commit comments