|
| 1 | +# Copyright 2024-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 | +import torch |
| 7 | +from executorch.backends.arm.test import common |
| 8 | + |
| 9 | +from executorch.backends.arm.test.tester.test_pipeline import ( |
| 10 | + EthosU55PipelineBI, |
| 11 | + EthosU85PipelineBI, |
| 12 | + TosaPipelineBI, |
| 13 | + TosaPipelineMI, |
| 14 | +) |
| 15 | + |
| 16 | +float_test_data_suite = { |
| 17 | + "scalar_tensor_float_1": (3.7, torch.float32, torch.rand((1, 2, 3, 4))), |
| 18 | + "scalar_tensor_float_2": (66, torch.float32, torch.rand((1, 2, 3))), |
| 19 | +} |
| 20 | + |
| 21 | +int_test_data_suite = { |
| 22 | + "scalar_tensor_int32": ( |
| 23 | + 33, |
| 24 | + torch.int32, |
| 25 | + torch.randint(0, 10, (1, 2), dtype=torch.int32), |
| 26 | + ), |
| 27 | + "scalar_tensor_int8": ( |
| 28 | + 8, |
| 29 | + torch.int8, |
| 30 | + torch.rand(1, 2, 3), |
| 31 | + ), |
| 32 | + "scalar_tensor_int16": ( |
| 33 | + 16 * 16 * 16, |
| 34 | + torch.int16, |
| 35 | + torch.rand((1,)).unsqueeze(0), # Rank 0 inputs not supported |
| 36 | + ), |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +class ScalarTensor(torch.nn.Module): |
| 41 | + aten_op = "torch.ops.aten.scalar_tensor.default" |
| 42 | + |
| 43 | + def __init__(self, scalar, dtype=torch.float32): |
| 44 | + super().__init__() |
| 45 | + self.scalar = scalar |
| 46 | + self.dtype = dtype |
| 47 | + |
| 48 | + def forward(self, x: torch.Tensor): |
| 49 | + return torch.scalar_tensor(self.scalar, dtype=self.dtype) + x |
| 50 | + |
| 51 | + |
| 52 | +@common.parametrize("test_data", int_test_data_suite | float_test_data_suite) |
| 53 | +def test_scalar_tensor_tosa_MI(test_data): # Note TOSA MI supports all types |
| 54 | + scalar, dtype, data = test_data |
| 55 | + TosaPipelineMI(ScalarTensor(scalar, dtype), tuple(data), ScalarTensor.aten_op).run() |
| 56 | + |
| 57 | + |
| 58 | +@common.parametrize("test_data", int_test_data_suite | float_test_data_suite) |
| 59 | +def test_scalar_tensor_tosa_BI(test_data): |
| 60 | + scalar, dtype, data = test_data |
| 61 | + pipeline: TosaPipelineBI = TosaPipelineBI( |
| 62 | + ScalarTensor(scalar, dtype), tuple(data), ScalarTensor.aten_op |
| 63 | + ) |
| 64 | + pipeline.pop_stage("check.quant_nodes") |
| 65 | + pipeline.run() |
| 66 | + |
| 67 | + |
| 68 | +@common.parametrize("test_data", float_test_data_suite) |
| 69 | +@common.XfailIfNoCorstone300 |
| 70 | +def test_scalar_tensor_tosa_u55(test_data): |
| 71 | + scalar, dtype, data = test_data |
| 72 | + EthosU55PipelineBI( |
| 73 | + ScalarTensor(scalar, dtype), |
| 74 | + tuple(data), |
| 75 | + ScalarTensor.aten_op, |
| 76 | + symmetric_io_quantization=True, |
| 77 | + run_on_fvp=True, |
| 78 | + ).run() |
| 79 | + |
| 80 | + |
| 81 | +@common.parametrize("test_data", float_test_data_suite) |
| 82 | +@common.XfailIfNoCorstone320 |
| 83 | +def test_scalar_tensor_tosa_u85(test_data): |
| 84 | + scalar, dtype, data = test_data |
| 85 | + EthosU85PipelineBI( |
| 86 | + ScalarTensor(scalar, dtype), |
| 87 | + tuple(data), |
| 88 | + ScalarTensor.aten_op, |
| 89 | + symmetric_io_quantization=True, |
| 90 | + run_on_fvp=True, |
| 91 | + ).run() |
0 commit comments