|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# Copyright 2024 Arm Limited and/or its affiliates. |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | + |
| 8 | +import unittest |
| 9 | + |
| 10 | +import torch |
| 11 | +from executorch.backends.arm.test import common |
| 12 | +from executorch.backends.arm.test.tester.arm_tester import ArmTester |
| 13 | +from parameterized import parameterized |
| 14 | + |
| 15 | +test_data_sute = [ |
| 16 | + # (test_name, input, other,) See torch.mul() for info |
| 17 | + ( |
| 18 | + "op_mul_rank1_ones", |
| 19 | + torch.ones(5), |
| 20 | + torch.ones(5), |
| 21 | + ), |
| 22 | + ( |
| 23 | + "op_mul_rank2_rand", |
| 24 | + torch.rand(4, 5), |
| 25 | + torch.rand(1, 5), |
| 26 | + ), |
| 27 | + ( |
| 28 | + "op_mul_rank3_randn", |
| 29 | + torch.randn(10, 5, 2), |
| 30 | + torch.randn(10, 5, 2), |
| 31 | + ), |
| 32 | + ( |
| 33 | + "op_mul_rank4_randn", |
| 34 | + torch.randn(5, 10, 25, 20), |
| 35 | + torch.randn(5, 10, 25, 20), |
| 36 | + ), |
| 37 | + ( |
| 38 | + "op_mul_rank4_ones_mul_negative", |
| 39 | + torch.ones(1, 10, 25, 20), |
| 40 | + (-1) * torch.ones(5, 10, 25, 20), |
| 41 | + ), |
| 42 | + ( |
| 43 | + "op_mul_rank4_negative_large_rand", |
| 44 | + (-200) * torch.rand(5, 10, 25, 20), |
| 45 | + torch.rand(5, 1, 1, 20), |
| 46 | + ), |
| 47 | + ( |
| 48 | + "op_mul_rank4_large_randn", |
| 49 | + 200 * torch.randn(5, 10, 25, 20), |
| 50 | + torch.rand(5, 10, 25, 1), |
| 51 | + ), |
| 52 | +] |
| 53 | + |
| 54 | + |
| 55 | +class TestMul(unittest.TestCase): |
| 56 | + class Mul(torch.nn.Module): |
| 57 | + |
| 58 | + def forward( |
| 59 | + self, |
| 60 | + input_: torch.Tensor, |
| 61 | + other_: torch.Tensor, |
| 62 | + ): |
| 63 | + return input_ * other_ |
| 64 | + |
| 65 | + def _test_mul_tosa_MI_pipeline( |
| 66 | + self, module: torch.nn.Module, test_data: tuple[torch.Tensor, torch.Tensor] |
| 67 | + ): |
| 68 | + ( |
| 69 | + ArmTester( |
| 70 | + module, |
| 71 | + example_inputs=test_data, |
| 72 | + compile_spec=common.get_tosa_compile_spec(permute_memory_to_nhwc=True), |
| 73 | + ) |
| 74 | + .export() |
| 75 | + .check_count({"torch.ops.aten.mul.Tensor": 1}) |
| 76 | + .check_not(["torch.ops.quantized_decomposed"]) |
| 77 | + .to_edge() |
| 78 | + .partition() |
| 79 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
| 80 | + .to_executorch() |
| 81 | + .run_method_and_compare_outputs(inputs=test_data) |
| 82 | + ) |
| 83 | + |
| 84 | + def _test_mul_tosa_BI_pipeline( |
| 85 | + self, module: torch.nn.Module, test_data: tuple[torch.Tensor, torch.Tensor] |
| 86 | + ): |
| 87 | + ( |
| 88 | + ArmTester( |
| 89 | + module, |
| 90 | + example_inputs=test_data, |
| 91 | + compile_spec=common.get_tosa_compile_spec(permute_memory_to_nhwc=True), |
| 92 | + ) |
| 93 | + .quantize() |
| 94 | + .export() |
| 95 | + .check_count({"torch.ops.aten.mul.Tensor": 1}) |
| 96 | + .check(["torch.ops.quantized_decomposed"]) |
| 97 | + .to_edge() |
| 98 | + .partition() |
| 99 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
| 100 | + .to_executorch() |
| 101 | + .run_method_and_compare_outputs(inputs=test_data, qtol=1.0) |
| 102 | + ) |
| 103 | + |
| 104 | + def _test_mul_u55_BI_pipeline( |
| 105 | + self, module: torch.nn.Module, test_data: tuple[torch.Tensor, torch.Tensor] |
| 106 | + ): |
| 107 | + ( |
| 108 | + ArmTester( |
| 109 | + module, |
| 110 | + example_inputs=test_data, |
| 111 | + compile_spec=common.get_u55_compile_spec(permute_memory_to_nhwc=True), |
| 112 | + ) |
| 113 | + .quantize() |
| 114 | + .export() |
| 115 | + .check_count({"torch.ops.aten.mul.Tensor": 1}) |
| 116 | + .check(["torch.ops.quantized_decomposed"]) |
| 117 | + .to_edge() |
| 118 | + .partition() |
| 119 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
| 120 | + .to_executorch() |
| 121 | + ) |
| 122 | + |
| 123 | + @parameterized.expand(test_data_sute) |
| 124 | + def test_mul_tosa_MI( |
| 125 | + self, |
| 126 | + test_name: str, |
| 127 | + input_: torch.Tensor, |
| 128 | + other_: torch.Tensor, |
| 129 | + ): |
| 130 | + test_data = (input_, other_) |
| 131 | + self._test_mul_tosa_MI_pipeline(self.Mul(), test_data) |
| 132 | + |
| 133 | + @parameterized.expand(test_data_sute) |
| 134 | + def test_mul_tosa_BI( |
| 135 | + self, |
| 136 | + test_name: str, |
| 137 | + input_: torch.Tensor, |
| 138 | + other_: torch.Tensor, |
| 139 | + ): |
| 140 | + |
| 141 | + test_data = (input_, other_) |
| 142 | + self._test_mul_tosa_BI_pipeline(self.Mul(), test_data) |
| 143 | + |
| 144 | + # Expected to fail since RESCALE cannot be fused with MUL in Vela. |
| 145 | + @parameterized.expand(test_data_sute) |
| 146 | + @unittest.expectedFailure |
| 147 | + def test_mul_u55_BI( |
| 148 | + self, |
| 149 | + test_name: str, |
| 150 | + input_: torch.Tensor, |
| 151 | + other_: torch.Tensor, |
| 152 | + ): |
| 153 | + test_data = (input_, other_) |
| 154 | + self._test_mul_u55_BI_pipeline(self.Mul(), test_data) |
0 commit comments