|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# Copyright 2024-2025 Arm Limited and/or its affiliates. |
| 4 | +# |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | + |
| 8 | +from typing import Tuple, Union |
| 9 | + |
| 10 | +import torch |
| 11 | +from executorch.backends.arm.test import common |
| 12 | + |
| 13 | +from executorch.backends.arm.test.tester.test_pipeline import ( |
| 14 | + EthosU55PipelineINT, |
| 15 | + EthosU85PipelineINT, |
| 16 | + TosaPipelineFP, |
| 17 | + TosaPipelineINT, |
| 18 | + VgfPipeline, |
| 19 | +) |
| 20 | + |
| 21 | +test_data_suite = { |
| 22 | + # (test_name, input, other) |
| 23 | + "op_floor_div_rank1_ones": lambda: ( |
| 24 | + torch.ones(5), |
| 25 | + torch.ones(5), |
| 26 | + ), |
| 27 | + "op_floor_div_rank1_rand": lambda: ( |
| 28 | + torch.rand(5) * 5, |
| 29 | + torch.rand(5) * 5, |
| 30 | + ), |
| 31 | + "op_floor_div_rank4_negative_ones": lambda: ( |
| 32 | + (-1) * torch.ones(5, 10, 25, 20), |
| 33 | + torch.ones(5, 10, 25, 20), |
| 34 | + ), |
| 35 | + "op_floor_div_rank4_ones_div_negative": lambda: ( |
| 36 | + torch.ones(5, 10, 25, 20), |
| 37 | + (-1) * torch.ones(5, 10, 25, 20), |
| 38 | + ), |
| 39 | + "op_floor_div_rank4_randn_mutltiple_broadcasts": lambda: ( |
| 40 | + torch.randn(1, 4, 4, 1), |
| 41 | + torch.randn(1, 1, 4, 4), |
| 42 | + ), |
| 43 | + "op_floor_div_rank4_randn_scalar": lambda: ( |
| 44 | + torch.randn(1, 4, 4, 1), |
| 45 | + 2, |
| 46 | + ), |
| 47 | + "op_floor_div_rank4_large_rand": lambda: ( |
| 48 | + 200 * torch.rand(5, 10, 25, 20), |
| 49 | + torch.rand(5, 10, 25, 20), |
| 50 | + ), |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +class FloorDivide(torch.nn.Module): |
| 55 | + aten_op = "torch.ops.aten.floor_divide.default" |
| 56 | + aten_ops_int = ["aten.mul.Tensor", "aten.reciprocal.default", "aten.floor.default"] |
| 57 | + exir_op = "executorch_exir_dialects_edge__ops_aten_div_Tensor_mode" |
| 58 | + exir_ops_int = [ |
| 59 | + "executorch_exir_dialects_edge__ops_aten_reciprocal_default", |
| 60 | + "executorch_exir_dialects_edge__ops_aten_mul_Tensor", |
| 61 | + "executorch_exir_dialects_edge__ops_aten_floor_default", |
| 62 | + ] |
| 63 | + |
| 64 | + def forward( |
| 65 | + self, |
| 66 | + input_: Union[torch.Tensor, torch.types.Number], |
| 67 | + other_: Union[torch.Tensor, torch.types.Number], |
| 68 | + ): |
| 69 | + return torch.floor_divide(input=input_, other=other_) |
| 70 | + |
| 71 | + |
| 72 | +input_t1 = Tuple[torch.Tensor, Union[torch.Tensor, int]] |
| 73 | + |
| 74 | + |
| 75 | +@common.parametrize("test_data", test_data_suite) |
| 76 | +def test_floor_divide_tosa_FP(test_data: input_t1): |
| 77 | + pipeline = TosaPipelineFP[input_t1]( |
| 78 | + FloorDivide(), |
| 79 | + test_data(), |
| 80 | + FloorDivide.aten_op, |
| 81 | + FloorDivide.exir_op, |
| 82 | + use_to_edge_transform_and_lower=False, |
| 83 | + ) |
| 84 | + pipeline.run() |
| 85 | + |
| 86 | + |
| 87 | +@common.parametrize("test_data", test_data_suite) |
| 88 | +def test_floor_divide_tosa_INT(test_data: input_t1): |
| 89 | + pipeline = TosaPipelineINT[input_t1]( |
| 90 | + FloorDivide(), |
| 91 | + test_data(), |
| 92 | + aten_op=FloorDivide.aten_ops_int, |
| 93 | + exir_op=FloorDivide.exir_ops_int, |
| 94 | + use_to_edge_transform_and_lower=False, |
| 95 | + ) |
| 96 | + pipeline.run() |
| 97 | + |
| 98 | + |
| 99 | +@common.parametrize("test_data", test_data_suite) |
| 100 | +@common.XfailIfNoCorstone300 |
| 101 | +def test_floor_divide_u55_INT(test_data: input_t1): |
| 102 | + pipeline = EthosU55PipelineINT[input_t1]( |
| 103 | + FloorDivide(), |
| 104 | + test_data(), |
| 105 | + aten_ops=FloorDivide.aten_ops_int, |
| 106 | + exir_ops=[], |
| 107 | + run_on_fvp=True, |
| 108 | + use_to_edge_transform_and_lower=False, |
| 109 | + ) |
| 110 | + pipeline.pop_stage("check_not.exir") |
| 111 | + pipeline.pop_stage("check_count.exir") |
| 112 | + pipeline.run() |
| 113 | + |
| 114 | + |
| 115 | +@common.parametrize("test_data", test_data_suite) |
| 116 | +@common.XfailIfNoCorstone320 |
| 117 | +def test_floor_divide_u85_INT(test_data: input_t1): |
| 118 | + pipeline = EthosU85PipelineINT[input_t1]( |
| 119 | + FloorDivide(), |
| 120 | + test_data(), |
| 121 | + aten_ops=FloorDivide.aten_ops_int, |
| 122 | + exir_ops=FloorDivide.exir_ops_int, |
| 123 | + run_on_fvp=True, |
| 124 | + use_to_edge_transform_and_lower=False, |
| 125 | + ) |
| 126 | + pipeline.run() |
| 127 | + |
| 128 | + |
| 129 | +@common.parametrize("test_data", test_data_suite) |
| 130 | +@common.SkipIfNoModelConverter |
| 131 | +def test_floor_divide_vgf_FP(test_data: input_t1): |
| 132 | + pipeline = VgfPipeline[input_t1]( |
| 133 | + FloorDivide(), |
| 134 | + test_data(), |
| 135 | + FloorDivide.aten_op, |
| 136 | + FloorDivide.exir_op, |
| 137 | + tosa_version="TOSA-1.0+FP", |
| 138 | + use_to_edge_transform_and_lower=False, |
| 139 | + ) |
| 140 | + pipeline.run() |
| 141 | + |
| 142 | + |
| 143 | +@common.parametrize("test_data", test_data_suite) |
| 144 | +@common.SkipIfNoModelConverter |
| 145 | +def test_floor_divide_vgf_INT(test_data: input_t1): |
| 146 | + pipeline = VgfPipeline[input_t1]( |
| 147 | + FloorDivide(), |
| 148 | + test_data(), |
| 149 | + aten_op=FloorDivide.aten_ops_int, |
| 150 | + exir_op=FloorDivide.exir_ops_int, |
| 151 | + tosa_version="TOSA-1.0+INT", |
| 152 | + use_to_edge_transform_and_lower=False, |
| 153 | + ) |
| 154 | + pipeline.run() |
0 commit comments