|
| 1 | +# Copyright 2024 Arm Limited and/or its affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import unittest |
| 8 | + |
| 9 | +import torch |
| 10 | +from executorch.backends.arm.test import common |
| 11 | +from executorch.backends.arm.test.tester.arm_tester import ArmTester |
| 12 | +from parameterized import parameterized |
| 13 | + |
| 14 | + |
| 15 | +class TestRshift(unittest.TestCase): |
| 16 | + """ |
| 17 | + Tests arithmetic right shift |
| 18 | + """ |
| 19 | + |
| 20 | + class Rshift(torch.nn.Module): |
| 21 | + test_data = [ |
| 22 | + ((torch.IntTensor(5, 5), 2),), |
| 23 | + ((torch.IntTensor(1, 2, 3, 4), 3),), |
| 24 | + ((torch.ShortTensor(1, 5, 3, 4), 5),), |
| 25 | + ((torch.CharTensor(10, 12, 3, 4), 1),), |
| 26 | + ] |
| 27 | + |
| 28 | + def forward(self, x: torch.Tensor, shift: int): |
| 29 | + return x >> shift |
| 30 | + |
| 31 | + def _test_rshift_tosa_MI(self, test_data): |
| 32 | + ( |
| 33 | + ArmTester( |
| 34 | + self.Rshift(), |
| 35 | + example_inputs=test_data, |
| 36 | + compile_spec=common.get_tosa_compile_spec("TOSA-0.80.0+MI"), |
| 37 | + ) |
| 38 | + .export() |
| 39 | + .to_edge_transform_and_lower() |
| 40 | + .to_executorch() |
| 41 | + .run_method_and_compare_outputs(inputs=test_data) |
| 42 | + ) |
| 43 | + |
| 44 | + def _test_rshift_tosa_BI(self, test_data): |
| 45 | + ( |
| 46 | + ArmTester( |
| 47 | + self.Rshift(), |
| 48 | + example_inputs=test_data, |
| 49 | + compile_spec=common.get_tosa_compile_spec("TOSA-0.80.0+BI"), |
| 50 | + ) |
| 51 | + .quantize() |
| 52 | + .export() |
| 53 | + .to_edge_transform_and_lower() |
| 54 | + .to_executorch() |
| 55 | + # TODO MLETORCH-250 Increase flexibility of ArmTester to handle int IO |
| 56 | + # .run_method_and_compare_outputs(inputs=test_data) |
| 57 | + ) |
| 58 | + |
| 59 | + def _test_rshift_ethosu_BI(self, test_data, compile_spec): |
| 60 | + return ( |
| 61 | + ArmTester( |
| 62 | + self.Rshift(), |
| 63 | + example_inputs=test_data, |
| 64 | + compile_spec=compile_spec, |
| 65 | + ) |
| 66 | + .quantize() |
| 67 | + .export() |
| 68 | + .to_edge_transform_and_lower() |
| 69 | + .to_executorch() |
| 70 | + ) |
| 71 | + |
| 72 | + @parameterized.expand(Rshift.test_data) |
| 73 | + def test_rshift_tosa_MI(self, test_data): |
| 74 | + self._test_rshift_tosa_MI(test_data) |
| 75 | + |
| 76 | + @parameterized.expand(Rshift.test_data) |
| 77 | + def test_rshift_tosa_BI(self, test_data): |
| 78 | + self._test_rshift_tosa_BI(test_data) |
| 79 | + |
| 80 | + # TODO Enable FVP testing |
| 81 | + @parameterized.expand(Rshift.test_data) |
| 82 | + def test_rshift_u55_BI(self, test_data): |
| 83 | + compile_spec = common.get_u55_compile_spec() |
| 84 | + self._test_rshift_ethosu_BI(test_data, compile_spec) |
| 85 | + |
| 86 | + # TODO Enable FVP testing |
| 87 | + @parameterized.expand(Rshift.test_data) |
| 88 | + def test_rshift_u85_BI(self, test_data): |
| 89 | + compile_spec = common.get_u85_compile_spec() |
| 90 | + self._test_rshift_ethosu_BI(test_data, compile_spec) |
0 commit comments