|
| 1 | +# Copyright (c) Meta Platforms, Inc. and 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.vulkan import VulkanPartitioner |
| 11 | +from executorch.exir import to_edge_transform_and_lower |
| 12 | + |
| 13 | + |
| 14 | +class AddModule(torch.nn.Module): |
| 15 | + def forward(self, a: torch.Tensor, b: torch.Tensor) -> torch.Tensor: |
| 16 | + return a + b |
| 17 | + |
| 18 | + |
| 19 | +class AddSelfModule(torch.nn.Module): |
| 20 | + def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 21 | + return x + x |
| 22 | + |
| 23 | + |
| 24 | +class AddScalarModule(torch.nn.Module): |
| 25 | + def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 26 | + return x + 3.0 |
| 27 | + |
| 28 | + |
| 29 | +class AddChainedModule(torch.nn.Module): |
| 30 | + def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: |
| 31 | + z = x + y |
| 32 | + z = z + x |
| 33 | + z = z + y |
| 34 | + return z |
| 35 | + |
| 36 | + |
| 37 | +class TestAdd(unittest.TestCase): |
| 38 | + """fp32 torch.add export tests — uses VulkanPartitioner since the WebGPU |
| 39 | + runtime directly consumes the Vulkan delegate (VK00 FlatBuffer).""" |
| 40 | + |
| 41 | + def _export_and_check(self, model, example_inputs) -> None: |
| 42 | + ep = torch.export.export(model, example_inputs) |
| 43 | + et_program = to_edge_transform_and_lower( |
| 44 | + ep, partitioner=[VulkanPartitioner()] |
| 45 | + ).to_executorch() |
| 46 | + |
| 47 | + found_vulkan = False |
| 48 | + for plan in et_program.executorch_program.execution_plan: |
| 49 | + for delegate in plan.delegates: |
| 50 | + if delegate.id == "VulkanBackend": |
| 51 | + found_vulkan = True |
| 52 | + break |
| 53 | + self.assertTrue(found_vulkan, "Expected VulkanBackend delegate in .pte") |
| 54 | + self.assertGreater(len(et_program.buffer), 100) |
| 55 | + |
| 56 | + def test_add_2d(self) -> None: |
| 57 | + self._export_and_check(AddModule(), (torch.randn(4, 4), torch.randn(4, 4))) |
| 58 | + |
| 59 | + def test_add_3d(self) -> None: |
| 60 | + self._export_and_check(AddModule(), (torch.randn(2, 3, 4), torch.randn(2, 3, 4))) |
| 61 | + |
| 62 | + def test_add_4d(self) -> None: |
| 63 | + self._export_and_check( |
| 64 | + AddModule(), (torch.randn(1, 2, 3, 4), torch.randn(1, 2, 3, 4)) |
| 65 | + ) |
| 66 | + |
| 67 | + def test_add_broadcast_last_dim(self) -> None: |
| 68 | + self._export_and_check(AddModule(), (torch.randn(4, 4), torch.randn(4, 1))) |
| 69 | + |
| 70 | + def test_add_broadcast_first_dim(self) -> None: |
| 71 | + self._export_and_check(AddModule(), (torch.randn(4, 4), torch.randn(1, 4))) |
| 72 | + |
| 73 | + def test_add_self(self) -> None: |
| 74 | + self._export_and_check(AddSelfModule(), (torch.randn(4, 4),)) |
| 75 | + |
| 76 | + def test_add_scalar(self) -> None: |
| 77 | + self._export_and_check(AddScalarModule(), (torch.randn(4, 4),)) |
| 78 | + |
| 79 | + def test_add_chained(self) -> None: |
| 80 | + self._export_and_check( |
| 81 | + AddChainedModule(), (torch.randn(4, 4), torch.randn(4, 4)) |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +def export_add_model(output_path: str) -> None: |
| 86 | + """Export a simple add model to .pte for native runtime testing.""" |
| 87 | + model = AddModule() |
| 88 | + example_inputs = (torch.randn(1024, 1024), torch.randn(1024, 1024)) |
| 89 | + ep = torch.export.export(model, example_inputs) |
| 90 | + et_program = to_edge_transform_and_lower( |
| 91 | + ep, partitioner=[VulkanPartitioner()] |
| 92 | + ).to_executorch() |
| 93 | + with open(output_path, "wb") as f: |
| 94 | + f.write(et_program.buffer) |
| 95 | + print(f"Exported {output_path}") |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + unittest.main() |
0 commit comments