|
| 1 | +# Copyright 2025 Arm Limited and/or its affiliates. |
| 2 | +# This source code is licensed under the BSD-style license found in the |
| 3 | +# LICENSE file in the root directory of this source tree. |
| 4 | + |
| 5 | +from typing import Tuple |
| 6 | + |
| 7 | +import pytest |
| 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 | +input_tt = Tuple[torch.Tensor, torch.Tensor] |
| 20 | + |
| 21 | + |
| 22 | +def make_float_div_inputs(B: int = 4, T: int = 64) -> input_tt: |
| 23 | + x = torch.randn(B, T) |
| 24 | + # guard against zero in denominator |
| 25 | + y = torch.randn(B, T).abs() + 1e-3 |
| 26 | + return x, y |
| 27 | + |
| 28 | + |
| 29 | +class DivTensorModeFloat(torch.nn.Module): |
| 30 | + """ |
| 31 | + torch.div(x, y, rounding_mode=mode) with |
| 32 | + mode from {None, "floor", "trunc"}. |
| 33 | + """ |
| 34 | + |
| 35 | + aten_ops = ["aten.div.Tensor_mode"] |
| 36 | + aten_ops_int = ["aten.mul.Tensor", "aten.reciprocal.default"] |
| 37 | + |
| 38 | + def __init__(self, mode=None): |
| 39 | + super().__init__() |
| 40 | + assert mode in (None, "floor", "trunc") |
| 41 | + self.mode = mode |
| 42 | + |
| 43 | + def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: |
| 44 | + return torch.div(x, y, rounding_mode=self.mode) |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parametrize("mode", [None, "floor", "trunc"]) |
| 48 | +def test_div_tensor_mode_tosa_FP(mode): |
| 49 | + |
| 50 | + model = DivTensorModeFloat(mode) |
| 51 | + inputs = make_float_div_inputs() |
| 52 | + |
| 53 | + pipeline = TosaPipelineFP[input_tt]( |
| 54 | + model, |
| 55 | + inputs, |
| 56 | + aten_op=model.aten_ops, |
| 57 | + exir_op=[], |
| 58 | + use_to_edge_transform_and_lower=True, |
| 59 | + ) |
| 60 | + pipeline.pop_stage("check_count.exir") |
| 61 | + pipeline.run() |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize("mode", [None, "floor", "trunc"]) |
| 65 | +def test_div_tensor_mode_tosa_INT(mode): |
| 66 | + |
| 67 | + model = DivTensorModeFloat(mode) |
| 68 | + inputs = make_float_div_inputs() |
| 69 | + |
| 70 | + pipeline = TosaPipelineINT[input_tt]( |
| 71 | + model, |
| 72 | + inputs, |
| 73 | + aten_op=model.aten_ops_int, |
| 74 | + exir_op=[], |
| 75 | + use_to_edge_transform_and_lower=True, |
| 76 | + ) |
| 77 | + pipeline.pop_stage("check_count.exir") |
| 78 | + pipeline.run() |
| 79 | + |
| 80 | + |
| 81 | +@common.XfailIfNoCorstone300 |
| 82 | +@pytest.mark.parametrize("mode", [None, "floor"]) |
| 83 | +def test_div_tensor_mode_u55_INT(mode): |
| 84 | + |
| 85 | + model = DivTensorModeFloat(mode) |
| 86 | + inputs = make_float_div_inputs() |
| 87 | + |
| 88 | + pipeline = EthosU55PipelineINT[input_tt]( |
| 89 | + model, |
| 90 | + inputs, |
| 91 | + aten_ops=model.aten_ops_int, |
| 92 | + exir_ops=[], |
| 93 | + use_to_edge_transform_and_lower=True, |
| 94 | + run_on_fvp=True, |
| 95 | + ) |
| 96 | + pipeline.run() |
| 97 | + |
| 98 | + |
| 99 | +@common.XfailIfNoCorstone320 |
| 100 | +@pytest.mark.parametrize("mode", [None, "floor", "trunc"]) |
| 101 | +def test_div_tensor_mode_u85_INT(mode): |
| 102 | + |
| 103 | + model = DivTensorModeFloat(mode) |
| 104 | + inputs = make_float_div_inputs() |
| 105 | + |
| 106 | + pipeline = EthosU85PipelineINT[input_tt]( |
| 107 | + model, |
| 108 | + inputs, |
| 109 | + aten_ops=model.aten_ops_int, |
| 110 | + exir_ops=[], |
| 111 | + use_to_edge_transform_and_lower=True, |
| 112 | + run_on_fvp=True, |
| 113 | + ) |
| 114 | + pipeline.run() |
| 115 | + |
| 116 | + |
| 117 | +@common.SkipIfNoModelConverter |
| 118 | +@pytest.mark.parametrize("mode", [None, "floor", "trunc"]) |
| 119 | +def test_div_tensor_mode_vgf_INT(mode): |
| 120 | + |
| 121 | + model = DivTensorModeFloat(mode) |
| 122 | + inputs = make_float_div_inputs() |
| 123 | + |
| 124 | + pipeline = VgfPipeline[input_tt]( |
| 125 | + model, |
| 126 | + inputs, |
| 127 | + aten_op=model.aten_ops_int, |
| 128 | + exir_op=[], |
| 129 | + tosa_version="TOSA-1.0+INT", |
| 130 | + use_to_edge_transform_and_lower=True, |
| 131 | + ) |
| 132 | + pipeline.pop_stage("check_count.exir") |
| 133 | + pipeline.run() |
| 134 | + |
| 135 | + |
| 136 | +@common.SkipIfNoModelConverter |
| 137 | +@pytest.mark.parametrize("mode", [None, "floor", "trunc"]) |
| 138 | +def test_div_tensor_mode_vgf_FP(mode): |
| 139 | + |
| 140 | + model = DivTensorModeFloat(mode) |
| 141 | + inputs = make_float_div_inputs() |
| 142 | + |
| 143 | + pipeline = VgfPipeline[input_tt]( |
| 144 | + model, |
| 145 | + inputs, |
| 146 | + aten_op=model.aten_ops, |
| 147 | + exir_op=[], |
| 148 | + tosa_version="TOSA-1.0+FP", |
| 149 | + use_to_edge_transform_and_lower=True, |
| 150 | + ) |
| 151 | + pipeline.run() |
0 commit comments