|
| 1 | +# Copyright (c) Samsung Electronics Co. LTD |
| 2 | +# All rights reserved |
| 3 | +# |
| 4 | +# Licensed under the BSD License (the "License"); you may not use this file |
| 5 | +# except in compliance with the License. See the license file in the root |
| 6 | +# directory of this source tree for more details. |
| 7 | + |
| 8 | + |
| 9 | +import unittest |
| 10 | + |
| 11 | +import torch |
| 12 | + |
| 13 | +from executorch.backends.samsung.serialization.compile_options import ( |
| 14 | + gen_samsung_backend_compile_spec, |
| 15 | +) |
| 16 | +from executorch.backends.samsung.test.tester import SamsungTester |
| 17 | + |
| 18 | + |
| 19 | +class Add(torch.nn.Module): |
| 20 | + def __init__(self) -> None: |
| 21 | + super().__init__() |
| 22 | + |
| 23 | + def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: |
| 24 | + return x + y |
| 25 | + |
| 26 | + |
| 27 | +class AddConstant(torch.nn.Module): |
| 28 | + def __init__(self, constant) -> None: |
| 29 | + super().__init__() |
| 30 | + self.constant = constant |
| 31 | + |
| 32 | + def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 33 | + return x + self.constant |
| 34 | + |
| 35 | + |
| 36 | +class TestAdd(unittest.TestCase): |
| 37 | + def _test(self, module: torch.nn.Module, inputs): |
| 38 | + tester = SamsungTester( |
| 39 | + module, |
| 40 | + inputs, |
| 41 | + [gen_samsung_backend_compile_spec("E9955")], |
| 42 | + ) |
| 43 | + ( |
| 44 | + tester.export() |
| 45 | + .check_count({"torch.ops.aten.add.Tensor": 1}) |
| 46 | + .to_edge_transform_and_lower() |
| 47 | + .check_not(["executorch_exir_dialects_edge__ops_aten_add_Tensor"]) |
| 48 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
| 49 | + .to_executorch() |
| 50 | + ) |
| 51 | + |
| 52 | + def test_fp32_simple_add(self): |
| 53 | + inputs = (torch.randn(1, 3, 8, 8), torch.randn(1, 3, 8, 8)) |
| 54 | + self._test(Add(), inputs) |
| 55 | + |
| 56 | + def test_fp32_const_add(self): |
| 57 | + inputs = (torch.randn(1, 3, 8, 8),) |
| 58 | + self._test(AddConstant(torch.randn(1, 3, 8, 8)), inputs) |
| 59 | + |
| 60 | + def test_fp32_add_broadcast(self): |
| 61 | + inputs = (torch.randn(1, 1, 8, 8), torch.randn(1, 3, 8, 8)) |
| 62 | + self._test(Add(), inputs) |
0 commit comments