|
| 1 | +# (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary. |
| 2 | + |
| 3 | +import unittest |
| 4 | +from typing import Tuple |
| 5 | + |
| 6 | +from parameterized import parameterized |
| 7 | + |
| 8 | +from executorch.backends.cadence.aot.ops_registrations import * # noqa |
| 9 | + |
| 10 | +import torch |
| 11 | +import torch.nn as nn |
| 12 | +from executorch.backends.cadence.aot.export_example import export_model |
| 13 | + |
| 14 | + |
| 15 | +class ATenOpTestCases(unittest.TestCase): |
| 16 | + @parameterized.expand( |
| 17 | + [ |
| 18 | + [(7, 5, 6), (7, 5, 6)], |
| 19 | + [(7, 5, 6), (1)], |
| 20 | + [(1), (7, 5, 6)], |
| 21 | + [(1), (7, 5, 6), 2.23], |
| 22 | + [(1), (7, 5, 6), -1.0], |
| 23 | + [(1), (7, 5, 6), -2.23], |
| 24 | + [(7, 5, 6), (7, 5, 6), 1.23], |
| 25 | + [(6, 7), (6, 7)], |
| 26 | + [(6, 7), (6, 7), 2], |
| 27 | + # Broadcast tests (should be optimized on G3) |
| 28 | + [(1, 32, 64), (1, 1, 64)], |
| 29 | + [(1, 32, 64), (64)], |
| 30 | + [(1, 1, 32), (32)], |
| 31 | + [(16, 1, 16), (1, 1, 16)], |
| 32 | + [(16, 1, 16), (16)], |
| 33 | + [(1, 4, 8, 8), (1, 1, 8, 8)], |
| 34 | + [(1, 4, 8, 8), (8, 8)], |
| 35 | + # Broadcast tests (should go to portable ops) |
| 36 | + [(1, 10, 1, 8), (4, 1, 4, 1)], |
| 37 | + [(1, 1, 16), (1, 8, 1), 2.5], |
| 38 | + # # aten.upsample_nearest2d tests |
| 39 | + [(5, 6, 6, 8), (5, 6, 6, 8)], |
| 40 | + [(1, 1, 12, 16), (1, 1, 12, 16)], |
| 41 | + ] |
| 42 | + ) |
| 43 | + def test_aten_add_out( |
| 44 | + self, Xshape: Tuple[int], Yshape: Tuple[int], alpha: float = 1 |
| 45 | + ) -> None: |
| 46 | + class AddTensor(nn.Module): |
| 47 | + def __init__(self, alpha: float): |
| 48 | + super().__init__() |
| 49 | + self.alpha = alpha |
| 50 | + |
| 51 | + def forward(self, x: torch.Tensor, y: torch.Tensor): |
| 52 | + return torch.add(x, y, alpha=self.alpha) |
| 53 | + |
| 54 | + model = AddTensor(alpha) |
| 55 | + |
| 56 | + X = torch.randn(Xshape) |
| 57 | + Y = torch.randn(Yshape) |
| 58 | + |
| 59 | + model.eval() |
| 60 | + export_model( |
| 61 | + model, (X, Y), file_name=self._testMethodName, run_and_compare=False |
| 62 | + ) |
| 63 | + |
| 64 | + @parameterized.expand( |
| 65 | + [ |
| 66 | + [(7, 5, 6), (7, 5, 6)], |
| 67 | + [(7, 5, 6), (1)], |
| 68 | + [(1), (7, 5, 6)], |
| 69 | + [(1), (7, 5, 6), 2.23], |
| 70 | + [(1), (7, 5, 6), -1.0], |
| 71 | + [(1), (7, 5, 6), -2.23], |
| 72 | + [(7, 5, 6), (7, 5, 6), 1.23], |
| 73 | + [(6, 7), (6, 7)], |
| 74 | + [(6, 7), (6, 7), 2], |
| 75 | + # Broadcast tests (should be optimized on G3) |
| 76 | + [(1, 32, 64), (1, 1, 64)], |
| 77 | + [(1, 32, 64), (64)], |
| 78 | + [(1, 1, 32), (32)], |
| 79 | + [(16, 1, 16), (1, 1, 16)], |
| 80 | + [(16, 1, 16), (16)], |
| 81 | + [(1, 4, 8, 8), (1, 1, 8, 8)], |
| 82 | + [(1, 4, 8, 8), (8, 8)], |
| 83 | + # Broadcast tests (should go to portable ops) |
| 84 | + [(1, 10, 1, 8), (4, 1, 4, 1)], |
| 85 | + [(1, 1, 16), (1, 8, 1), 2.5], |
| 86 | + # # aten.upsample_nearest2d tests |
| 87 | + [(5, 6, 6, 8), (5, 6, 6, 8)], |
| 88 | + [(1, 1, 12, 16), (1, 1, 12, 16)], |
| 89 | + ] |
| 90 | + ) |
| 91 | + def test_aten_add_scalar_out( |
| 92 | + self, Xshape: Tuple[int], Yshape: Tuple[int], alpha: float = 1 |
| 93 | + ) -> None: |
| 94 | + # Tensor-Scalar addition |
| 95 | + class AddScalar(nn.Module): |
| 96 | + def __init__(self, alpha: float): |
| 97 | + super().__init__() |
| 98 | + self.alpha = alpha |
| 99 | + |
| 100 | + def forward(self, x: torch.Tensor, y: float): |
| 101 | + return torch.add(x, y, alpha=self.alpha) |
| 102 | + |
| 103 | + model = AddScalar(alpha) |
| 104 | + |
| 105 | + X = torch.randn(Xshape) |
| 106 | + Y = 2.34 |
| 107 | + |
| 108 | + model.eval() |
| 109 | + export_model( |
| 110 | + model, (X, Y), file_name=self._testMethodName, run_and_compare=False |
| 111 | + ) |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + unittest.main() |
0 commit comments