|
| 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 | +from executorch.backends.arm.test.tester.test_pipeline import ( |
| 12 | + EthosU55PipelineINT, |
| 13 | + EthosU85PipelineINT, |
| 14 | + TosaPipelineFP, |
| 15 | + TosaPipelineINT, |
| 16 | + VgfPipeline, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +def _nonzero_float_tensor(*shape: int) -> torch.Tensor: |
| 21 | + return torch.rand(*shape, dtype=torch.float32) * 5 + 0.1 |
| 22 | + |
| 23 | + |
| 24 | +class Remainder(torch.nn.Module): |
| 25 | + input_t = Tuple[torch.Tensor | float, torch.Tensor | float] |
| 26 | + |
| 27 | + test_cases = { |
| 28 | + "rank2_tensors": lambda: ( |
| 29 | + torch.randn(2, 3) * 7, |
| 30 | + _nonzero_float_tensor(2, 3), |
| 31 | + ), |
| 32 | + "rank4_tensors": lambda: ( |
| 33 | + torch.randn(1, 4, 2, 3) * 7, |
| 34 | + _nonzero_float_tensor(1, 4, 2, 3), |
| 35 | + ), |
| 36 | + "broadcast": lambda: ( |
| 37 | + torch.randn(4, 5, 1), |
| 38 | + _nonzero_float_tensor(1, 5, 6), |
| 39 | + ), |
| 40 | + "scalar_rhs": lambda: ( |
| 41 | + torch.randn(1, 2, 3, 4), |
| 42 | + 0.25, |
| 43 | + ), |
| 44 | + } |
| 45 | + |
| 46 | + def forward(self, x: torch.Tensor | float, y: torch.Tensor | float) -> torch.Tensor: |
| 47 | + return torch.remainder(x, y) |
| 48 | + |
| 49 | + |
| 50 | +def _get_aten_op(test_data: Remainder.input_t): |
| 51 | + if any(isinstance(x, float) for x in test_data): |
| 52 | + return "torch.ops.aten.remainder.Scalar" |
| 53 | + else: |
| 54 | + return "torch.ops.aten.remainder.Tensor" |
| 55 | + |
| 56 | + |
| 57 | +def _get_exir_op(test_data: Remainder.input_t): |
| 58 | + if isinstance(test_data[1], float): |
| 59 | + return "executorch_exir_dialects_edge__ops_aten_remainder_Scalar" |
| 60 | + else: |
| 61 | + return "executorch_exir_dialects_edge__ops_aten_remainder_Tensor" |
| 62 | + |
| 63 | + |
| 64 | +@common.parametrize("test_data", Remainder.test_cases) |
| 65 | +def test_remainder_tosa_FP(test_data): |
| 66 | + data = test_data() |
| 67 | + pipeline = TosaPipelineFP[Remainder.input_t]( |
| 68 | + Remainder(), |
| 69 | + data, |
| 70 | + _get_aten_op(data), |
| 71 | + _get_exir_op(data), |
| 72 | + ) |
| 73 | + pipeline.run() |
| 74 | + |
| 75 | + |
| 76 | +@common.parametrize("test_data", Remainder.test_cases) |
| 77 | +def test_remainder_tosa_INT(test_data): |
| 78 | + pipeline = TosaPipelineINT[Remainder.input_t]( |
| 79 | + Remainder(), |
| 80 | + test_data(), |
| 81 | + [], |
| 82 | + ) |
| 83 | + pipeline.run() |
| 84 | + |
| 85 | + |
| 86 | +@common.parametrize("test_data", Remainder.test_cases) |
| 87 | +@common.XfailIfNoCorstone300 |
| 88 | +def test_remainder_u55_INT(test_data): |
| 89 | + pipeline = EthosU55PipelineINT[Remainder.input_t]( |
| 90 | + Remainder(), |
| 91 | + test_data(), |
| 92 | + [], |
| 93 | + ) |
| 94 | + pipeline.run() |
| 95 | + |
| 96 | + |
| 97 | +@common.parametrize("test_data", Remainder.test_cases) |
| 98 | +@common.XfailIfNoCorstone320 |
| 99 | +def test_remainder_u85_INT(test_data): |
| 100 | + pipeline = EthosU85PipelineINT[Remainder.input_t]( |
| 101 | + Remainder(), |
| 102 | + test_data(), |
| 103 | + [], |
| 104 | + ) |
| 105 | + pipeline.run() |
| 106 | + |
| 107 | + |
| 108 | +@common.parametrize("test_data", Remainder.test_cases) |
| 109 | +@common.SkipIfNoModelConverter |
| 110 | +def test_remainder_vgf_FP(test_data): |
| 111 | + data = test_data() |
| 112 | + pipeline = VgfPipeline[Remainder.input_t]( |
| 113 | + Remainder(), |
| 114 | + data, |
| 115 | + _get_aten_op(data), |
| 116 | + _get_exir_op(data), |
| 117 | + tosa_version="TOSA-1.0+FP", |
| 118 | + ) |
| 119 | + pipeline.run() |
| 120 | + |
| 121 | + |
| 122 | +@common.parametrize("test_data", Remainder.test_cases) |
| 123 | +@common.SkipIfNoModelConverter |
| 124 | +def test_remainder_vgf_INT(test_data): |
| 125 | + pipeline = VgfPipeline[Remainder.input_t]( |
| 126 | + Remainder(), |
| 127 | + test_data(), |
| 128 | + [], |
| 129 | + tosa_version="TOSA-1.0+INT", |
| 130 | + ) |
| 131 | + pipeline.run() |
0 commit comments