|
| 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 | + |
| 7 | +import torch |
| 8 | +from executorch.backends.arm.test import common |
| 9 | +from executorch.backends.arm.test.tester.test_pipeline import ( |
| 10 | + EthosU55PipelineBI, |
| 11 | + EthosU85PipelineBI, |
| 12 | + OpNotSupportedPipeline, |
| 13 | + TosaPipelineBI, |
| 14 | + TosaPipelineMI, |
| 15 | +) |
| 16 | + |
| 17 | +input_t = tuple[torch.Tensor] |
| 18 | +test_data_t = tuple[int, torch.dtype] |
| 19 | + |
| 20 | + |
| 21 | +class OnesAdd(torch.nn.Module): |
| 22 | + aten_op: str = "torch.ops.aten.ones.default" |
| 23 | + |
| 24 | + def __init__(self, n: int, dtype: torch.dtype): |
| 25 | + super().__init__() |
| 26 | + self.args = (n,) |
| 27 | + self.dtype = dtype |
| 28 | + |
| 29 | + def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 30 | + return torch.ones(*self.args, dtype=self.dtype) + x |
| 31 | + |
| 32 | + test_data: dict[str, test_data_t] = { |
| 33 | + "10x3x3": (lambda: (torch.randn(10, 3, 3),), (3, torch.float32)), |
| 34 | + "10x1": (lambda: (torch.randn(10, 1),), (10, torch.float32)), |
| 35 | + "int32_int32": ( |
| 36 | + lambda: (torch.randint(0, 10, [10], dtype=torch.int32),), |
| 37 | + (10, torch.int32), |
| 38 | + ), |
| 39 | + } |
| 40 | + |
| 41 | + test_data_not_delegated: dict[str, test_data_t] = { |
| 42 | + "fp32_int64": (lambda: (torch.randn(10),), (10, torch.int64)), |
| 43 | + "fp32_int32": (lambda: (torch.randn(10),), (10, torch.int32)), |
| 44 | + "int32_int64": ( |
| 45 | + lambda: (torch.randint(0, 10, [10], dtype=torch.int32),), |
| 46 | + (10, torch.int64), |
| 47 | + ), |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | +@common.parametrize("test_data", OnesAdd.test_data) |
| 52 | +def test_ones_tosa_MI(test_data: test_data_t): |
| 53 | + input_data, init_data = test_data |
| 54 | + pipeline = TosaPipelineMI[input_t]( |
| 55 | + OnesAdd(*init_data), |
| 56 | + input_data(), |
| 57 | + OnesAdd.aten_op, |
| 58 | + ) |
| 59 | + pipeline.run() |
| 60 | + |
| 61 | + |
| 62 | +@common.parametrize("test_data", OnesAdd.test_data) |
| 63 | +def test_ones_tosa_BI(test_data: test_data_t): |
| 64 | + input_data, init_data = test_data |
| 65 | + pipeline = TosaPipelineBI[input_t]( |
| 66 | + OnesAdd(*init_data), |
| 67 | + input_data(), |
| 68 | + OnesAdd.aten_op, |
| 69 | + ) |
| 70 | + pipeline.pop_stage("check.quant_nodes") |
| 71 | + pipeline.run() |
| 72 | + |
| 73 | + |
| 74 | +@common.parametrize("test_data", OnesAdd.test_data) |
| 75 | +def test_ones_u55_BI(test_data: test_data_t): |
| 76 | + input_data, init_data = test_data |
| 77 | + pipeline = EthosU55PipelineBI[input_t]( |
| 78 | + OnesAdd(*init_data), |
| 79 | + input_data(), |
| 80 | + OnesAdd.aten_op, |
| 81 | + use_to_edge_transform_and_lower=True, |
| 82 | + ) |
| 83 | + pipeline.pop_stage("check.quant_nodes") |
| 84 | + pipeline.run() |
| 85 | + |
| 86 | + |
| 87 | +@common.parametrize("test_data", OnesAdd.test_data) |
| 88 | +def test_ones_u85_BI(test_data: test_data_t): |
| 89 | + input_data, init_data = test_data |
| 90 | + pipeline = EthosU85PipelineBI[input_t]( |
| 91 | + OnesAdd(*init_data), |
| 92 | + input_data(), |
| 93 | + OnesAdd.aten_op, |
| 94 | + use_to_edge_transform_and_lower=True, |
| 95 | + ).dump_artifact("to_edge_transform_and_lower") |
| 96 | + pipeline.pop_stage("check.quant_nodes") |
| 97 | + pipeline.run() |
| 98 | + |
| 99 | + |
| 100 | +@common.parametrize( |
| 101 | + "test_data", |
| 102 | + OnesAdd.test_data_not_delegated, |
| 103 | + xfails={ |
| 104 | + "fp32_int32": "MLETORCG-716: Do not delegate empty networks to vela", |
| 105 | + "fp32_int64": "MLETORCG-716: Do not delegate empty networks to vela", |
| 106 | + "int32_int64": "MLETORCG-716: Do not delegate empty networks to vela", |
| 107 | + }, |
| 108 | +) |
| 109 | +def test_ones_tosa_BI_not_delegated(test_data: test_data_t): |
| 110 | + input_data, init_data = test_data |
| 111 | + pipeline = OpNotSupportedPipeline[input_t]( |
| 112 | + OnesAdd(*init_data), input_data(), non_delegated_ops={}, quantize=True |
| 113 | + ) |
| 114 | + pipeline.run() |
0 commit comments