|
| 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 | + EthosU55PipelineBI, |
| 14 | + EthosU85PipelineBI, |
| 15 | + TosaPipelineBI, |
| 16 | + TosaPipelineMI, |
| 17 | +) |
| 18 | + |
| 19 | +test_data_suite = { |
| 20 | + # (test_name, test_data) |
| 21 | + "zeros_default": lambda: (1.0, torch.zeros(1, 10, 10, 10)), |
| 22 | + "ones_default": lambda: (1.0, torch.ones(10, 10, 10)), |
| 23 | + "rand_default": lambda: (1.0, torch.rand(10, 10) - 0.5), |
| 24 | + "randn_pos_default": lambda: (1.0, torch.randn(1, 2, 3, 3) + 10), |
| 25 | + "randn_neg_default": lambda: (1.0, torch.randn(2, 4, 3) - 10), |
| 26 | + "ramp_default": lambda: (1.0, torch.arange(-16, 16, 0.2)), |
| 27 | + "large_pos_default": lambda: (1.0, torch.randn(3, 3) * 1e6 + 1e7), |
| 28 | + "large_neg_default": lambda: (1.0, -torch.empty(5).uniform_(1e5, 1e8)), |
| 29 | + "small_pos_default": lambda: (1.0, torch.empty(5).uniform_(1e-8, 1e-5)), |
| 30 | + "small_neg_default": lambda: (1.0, -torch.empty(5).uniform_(1e-8, 1e-5)), |
| 31 | + "zeros_custom": lambda: (2.0, torch.zeros(1, 10, 10, 10)), |
| 32 | + "ones_custom": lambda: (2.0, torch.ones(10, 10, 10)), |
| 33 | + "rand_custom": lambda: (2.0, torch.rand(10, 10) - 0.5), |
| 34 | + "randn_pos_custom": lambda: (2.0, torch.randn(1, 3, 3) + 10), |
| 35 | + "randn_neg_custom": lambda: (2.0, torch.randn(1, 2, 4, 3) - 10), |
| 36 | + "ramp_custom": lambda: (2.0, torch.arange(-16, 16, 0.2)), |
| 37 | + "large_pos_custom": lambda: (2.0, torch.randn(3, 3) * 1e6 + 1e7), |
| 38 | + "large_neg_custom": lambda: (2, -torch.empty(5).uniform_(1e5, 1e8)), |
| 39 | + "small_pos_custom": lambda: (2.0, torch.empty(5).uniform_(1e-8, 1e-5)), |
| 40 | + "small_neg_custom": lambda: (2.0, -torch.empty(5).uniform_(1e-8, 1e-5)), |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +class Elu(nn.Module): |
| 45 | + aten_op = "torch.ops.aten.elu.default" |
| 46 | + exir_op = "executorch_exir_dialects_edge__ops_aten__elu_default" |
| 47 | + |
| 48 | + def __init__(self, input_alpha: float = 1.0): |
| 49 | + super().__init__() |
| 50 | + self.elu = torch.nn.ELU(alpha=input_alpha) |
| 51 | + |
| 52 | + def forward(self, input_: torch.Tensor): |
| 53 | + return self.elu(input_) |
| 54 | + |
| 55 | + |
| 56 | +input_t1 = Tuple[torch.Tensor] |
| 57 | + |
| 58 | + |
| 59 | +@common.parametrize("test_module", test_data_suite) |
| 60 | +def test_elu_tosa_MI(test_module: input_t1): |
| 61 | + alpha, test_data = test_module() |
| 62 | + pipeline = TosaPipelineMI[input_t1]( |
| 63 | + Elu(alpha), (test_data,), aten_op=Elu.aten_op, exir_op=Elu.exir_op |
| 64 | + ) |
| 65 | + pipeline.run() |
| 66 | + |
| 67 | + |
| 68 | +@common.parametrize("test_module", test_data_suite) |
| 69 | +def test_elu_tosa_BI(test_module: input_t1): |
| 70 | + alpha, test_data = test_module() |
| 71 | + pipeline = TosaPipelineBI[input_t1]( |
| 72 | + Elu(alpha), (test_data,), aten_op=Elu.aten_op, exir_op=Elu.exir_op |
| 73 | + ) |
| 74 | + pipeline.run() |
| 75 | + |
| 76 | + |
| 77 | +@common.XfailIfNoCorstone300 |
| 78 | +@common.parametrize("test_module", test_data_suite) |
| 79 | +def test_elu_u55_BI(test_module: input_t1): |
| 80 | + alpha, test_data = test_module() |
| 81 | + pipeline = EthosU55PipelineBI[input_t1]( |
| 82 | + Elu(alpha), (test_data,), aten_ops=Elu.aten_op, exir_ops=Elu.exir_op |
| 83 | + ) |
| 84 | + pipeline.run() |
| 85 | + |
| 86 | + |
| 87 | +@common.XfailIfNoCorstone320 |
| 88 | +@common.parametrize("test_module", test_data_suite) |
| 89 | +def test_elu_u85_BI(test_module: input_t1): |
| 90 | + alpha, test_data = test_module() |
| 91 | + pipeline = EthosU85PipelineBI[input_t1]( |
| 92 | + Elu(alpha), (test_data,), aten_ops=Elu.aten_op, exir_ops=Elu.exir_op |
| 93 | + ) |
| 94 | + pipeline.run() |
0 commit comments