|
| 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 | +import torch.nn as nn |
| 10 | + |
| 11 | +from executorch.backends.arm.test import common |
| 12 | +from executorch.backends.arm.test.tester.test_pipeline import ( |
| 13 | + EthosU55PipelineINT, |
| 14 | + EthosU85PipelineINT, |
| 15 | + TosaPipelineFP, |
| 16 | + TosaPipelineINT, |
| 17 | + VgfPipeline, |
| 18 | +) |
| 19 | + |
| 20 | +test_data_suite = { |
| 21 | + # (test_name, test_data) |
| 22 | + "zeros_default": lambda: (1.0, torch.zeros(1, 10, 10, 10)), |
| 23 | + "ones_default": lambda: (1.0, torch.ones(10, 10, 10)), |
| 24 | + "rand_default": lambda: (1.0, torch.rand(10, 10) - 0.5), |
| 25 | + "randn_pos_default": lambda: (1.0, torch.randn(1, 2, 3, 3) + 10), |
| 26 | + "randn_neg_default": lambda: (1.0, torch.randn(2, 4, 3) - 10), |
| 27 | + "ramp_default": lambda: (1.0, torch.arange(-16, 16, 0.2)), |
| 28 | + "large_pos_default": lambda: (1.0, torch.randn(3, 3) * 1e6 + 1e7), |
| 29 | + "large_neg_default": lambda: (1.0, -torch.empty(5).uniform_(1e5, 1e8)), |
| 30 | + "small_pos_default": lambda: (1.0, torch.empty(5).uniform_(1e-8, 1e-5)), |
| 31 | + "small_neg_default": lambda: (1.0, -torch.empty(5).uniform_(1e-8, 1e-5)), |
| 32 | + "zeros_custom": lambda: (2.0, torch.zeros(1, 10, 10, 10)), |
| 33 | + "ones_custom": lambda: (2.0, torch.ones(10, 10, 10)), |
| 34 | + "rand_custom": lambda: (2.0, torch.rand(10, 10) - 0.5), |
| 35 | + "randn_pos_custom": lambda: (2.0, torch.randn(1, 3, 3) + 10), |
| 36 | + "randn_neg_custom": lambda: (2.0, torch.randn(1, 2, 4, 3) - 10), |
| 37 | + "ramp_custom": lambda: (2.0, torch.arange(-16, 16, 0.2)), |
| 38 | + "large_pos_custom": lambda: (2.0, torch.randn(3, 3) * 1e6 + 1e7), |
| 39 | + "large_neg_custom": lambda: (2.0, -torch.empty(5).uniform_(1e5, 1e8)), |
| 40 | + "small_pos_custom": lambda: (2.0, torch.empty(5).uniform_(1e-8, 1e-5)), |
| 41 | + "small_neg_custom": lambda: (2.0, -torch.empty(5).uniform_(1e-8, 1e-5)), |
| 42 | + "zeros_zero": lambda: (0.0, torch.zeros(1, 10, 10, 10)), |
| 43 | + "ones_zero": lambda: (0.0, torch.ones(10, 10, 10)), |
| 44 | + "rand_zero": lambda: (0.0, torch.rand(10, 10) - 0.5), |
| 45 | + "randn_pos_zero": lambda: (0.0, torch.randn(1, 3, 3) + 10), |
| 46 | + "randn_neg_zero": lambda: (0.0, torch.randn(1, 2, 4, 3) - 10), |
| 47 | + "ramp_zero": lambda: (0.0, torch.arange(-16, 16, 0.2)), |
| 48 | + "large_pos_zero": lambda: (0.0, torch.randn(3, 3) * 1e6 + 1e7), |
| 49 | + "large_neg_zero": lambda: (0.0, -torch.empty(5).uniform_(1e5, 1e8)), |
| 50 | + "small_pos_zero": lambda: (0.0, torch.empty(5).uniform_(1e-8, 1e-5)), |
| 51 | + "small_neg_zero": lambda: (0.0, -torch.empty(5).uniform_(1e-8, 1e-5)), |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +class Elu(nn.Module): |
| 56 | + aten_op = "torch.ops.aten.elu.default" |
| 57 | + exir_op = "executorch_exir_dialects_edge__ops_aten__elu_default" |
| 58 | + |
| 59 | + def __init__(self, input_alpha: float = 1.0): |
| 60 | + super().__init__() |
| 61 | + self.elu = torch.nn.ELU(alpha=input_alpha) |
| 62 | + |
| 63 | + def forward(self, input_: torch.Tensor): |
| 64 | + return self.elu(input_) |
| 65 | + |
| 66 | + |
| 67 | +input_t1 = Tuple[torch.Tensor] |
| 68 | + |
| 69 | + |
| 70 | +@common.parametrize("test_module", test_data_suite) |
| 71 | +def test_elu_tosa_FP(test_module: input_t1): |
| 72 | + alpha, test_data = test_module() |
| 73 | + pipeline = TosaPipelineFP[input_t1]( |
| 74 | + Elu(alpha), (test_data,), aten_op=Elu.aten_op, exir_op=Elu.exir_op |
| 75 | + ) |
| 76 | + pipeline.run() |
| 77 | + |
| 78 | + |
| 79 | +@common.parametrize("test_module", test_data_suite) |
| 80 | +def test_elu_tosa_INT(test_module: input_t1): |
| 81 | + alpha, test_data = test_module() |
| 82 | + pipeline = TosaPipelineINT[input_t1]( |
| 83 | + Elu(alpha), (test_data,), aten_op=Elu.aten_op, exir_op=Elu.exir_op |
| 84 | + ) |
| 85 | + pipeline.run() |
| 86 | + |
| 87 | + |
| 88 | +@common.XfailIfNoCorstone300 |
| 89 | +@common.parametrize("test_module", test_data_suite) |
| 90 | +def test_elu_u55_INT(test_module: input_t1): |
| 91 | + alpha, test_data = test_module() |
| 92 | + pipeline = EthosU55PipelineINT[input_t1]( |
| 93 | + Elu(alpha), (test_data,), aten_ops=Elu.aten_op, exir_ops=Elu.exir_op |
| 94 | + ) |
| 95 | + pipeline.run() |
| 96 | + |
| 97 | + |
| 98 | +@common.XfailIfNoCorstone320 |
| 99 | +@common.parametrize("test_module", test_data_suite) |
| 100 | +def test_elu_u85_INT(test_module: input_t1): |
| 101 | + alpha, test_data = test_module() |
| 102 | + pipeline = EthosU85PipelineINT[input_t1]( |
| 103 | + Elu(alpha), (test_data,), aten_ops=Elu.aten_op, exir_ops=Elu.exir_op |
| 104 | + ) |
| 105 | + pipeline.run() |
| 106 | + |
| 107 | + |
| 108 | +@common.SkipIfNoModelConverter |
| 109 | +@common.parametrize("test_module", test_data_suite) |
| 110 | +def test_elu_vgf_FP(test_module: input_t1): |
| 111 | + alpha, test_data = test_module() |
| 112 | + pipeline = VgfPipeline[input_t1]( |
| 113 | + Elu(alpha), |
| 114 | + (test_data,), |
| 115 | + aten_op=Elu.aten_op, |
| 116 | + exir_op=Elu.exir_op, |
| 117 | + tosa_version="TOSA-1.0+FP", |
| 118 | + ) |
| 119 | + pipeline.run() |
| 120 | + |
| 121 | + |
| 122 | +@common.SkipIfNoModelConverter |
| 123 | +@common.parametrize("test_module", test_data_suite) |
| 124 | +def test_elu_vgf_INT(test_module: input_t1): |
| 125 | + alpha, test_data = test_module() |
| 126 | + pipeline = VgfPipeline[input_t1]( |
| 127 | + Elu(alpha), |
| 128 | + (test_data,), |
| 129 | + aten_op=Elu.aten_op, |
| 130 | + exir_op=Elu.exir_op, |
| 131 | + tosa_version="TOSA-1.0+INT", |
| 132 | + ) |
| 133 | + pipeline.run() |
0 commit comments