|
| 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 | +"""`aten.sub.Tensor` export + golden for the WebGPU backend. |
| 8 | +
|
| 9 | +Exports single-op subtraction graphs through VulkanPartitioner (the WebGPU runtime |
| 10 | +consumes the Vulkan VK00 delegate directly) and checks an fp64 torch golden |
| 11 | +(`out = in1 - alpha * in2`). The native/etvk numeric oracle compares the GPU kernel |
| 12 | +against this same reference; this suite locks delegation + the reference math. |
| 13 | +Configs span same-shape 2D/3D, trailing- and leading-dim broadcast, and alpha != 1. |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import unittest |
| 19 | + |
| 20 | +import torch |
| 21 | + |
| 22 | +from executorch.backends.vulkan.partitioner.vulkan_partitioner import VulkanPartitioner |
| 23 | +from executorch.exir import to_edge_transform_and_lower |
| 24 | + |
| 25 | + |
| 26 | +# name -> (shape_a, shape_b); b broadcasts into a. |
| 27 | +CONFIGS = { |
| 28 | + "2d": ((4, 4), (4, 4)), |
| 29 | + "3d": ((2, 3, 4), (2, 3, 4)), |
| 30 | + "bcast_last": ((4, 4), (4, 1)), |
| 31 | + "bcast_row": ((4, 4), (1, 4)), |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +class SubModule(torch.nn.Module): |
| 36 | + def forward(self, a: torch.Tensor, b: torch.Tensor) -> torch.Tensor: |
| 37 | + return torch.sub(a, b) |
| 38 | + |
| 39 | + |
| 40 | +class SubAlphaModule(torch.nn.Module): |
| 41 | + def __init__(self, alpha: float) -> None: |
| 42 | + super().__init__() |
| 43 | + self.alpha = alpha |
| 44 | + |
| 45 | + def forward(self, a: torch.Tensor, b: torch.Tensor) -> torch.Tensor: |
| 46 | + return torch.sub(a, b, alpha=self.alpha) |
| 47 | + |
| 48 | + |
| 49 | +def _det_inputs(shape_a, shape_b): |
| 50 | + """Deterministic fp32 inputs (fixed seed) for a config.""" |
| 51 | + g = torch.Generator().manual_seed(0) |
| 52 | + a = torch.randn(*shape_a, generator=g, dtype=torch.float32) |
| 53 | + b = torch.randn(*shape_b, generator=g, dtype=torch.float32) |
| 54 | + return a, b |
| 55 | + |
| 56 | + |
| 57 | +def _export(m: torch.nn.Module, a: torch.Tensor, b: torch.Tensor): |
| 58 | + ep = torch.export.export(m.eval(), (a, b)) |
| 59 | + return to_edge_transform_and_lower( |
| 60 | + ep, partitioner=[VulkanPartitioner()] |
| 61 | + ).to_executorch() |
| 62 | + |
| 63 | + |
| 64 | +def _delegates(et) -> bool: |
| 65 | + return any( |
| 66 | + d.id == "VulkanBackend" |
| 67 | + for plan in et.executorch_program.execution_plan |
| 68 | + for d in plan.delegates |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +class TestSub(unittest.TestCase): |
| 73 | + def test_export_delegates(self) -> None: |
| 74 | + # Delegation => no aten.sub.Tensor left in the top-level portable graph. |
| 75 | + for name, (sa, sb) in CONFIGS.items(): |
| 76 | + with self.subTest(name=name): |
| 77 | + a, b = _det_inputs(sa, sb) |
| 78 | + et = _export(SubModule(), a, b) |
| 79 | + self.assertTrue( |
| 80 | + _delegates(et), |
| 81 | + f"Expected a VulkanBackend delegate (sub {name})", |
| 82 | + ) |
| 83 | + |
| 84 | + def test_golden_matches_fp64(self) -> None: |
| 85 | + for name, (sa, sb) in CONFIGS.items(): |
| 86 | + with self.subTest(name=name): |
| 87 | + a, b = _det_inputs(sa, sb) |
| 88 | + ref = (a.double() - b.double()).to(torch.float32) |
| 89 | + torch.testing.assert_close(SubModule()(a, b), ref) |
| 90 | + |
| 91 | + def test_golden_matches_fp64_alpha(self) -> None: |
| 92 | + # Locks the handler's out = in1 - alpha * in2 path (alpha != 1). |
| 93 | + alpha = 2.5 |
| 94 | + a, b = _det_inputs((4, 4), (4, 4)) |
| 95 | + ref = (a.double() - alpha * b.double()).to(torch.float32) |
| 96 | + torch.testing.assert_close(SubAlphaModule(alpha)(a, b), ref) |
| 97 | + |
| 98 | + |
| 99 | +def export_sub_model(pte_path: str, golden_path: str, input_path: str) -> None: |
| 100 | + """Write sub(a, b) .pte + fp64-computed torch golden + raw LE fp32 inputs (in1, in2).""" |
| 101 | + a, b = _det_inputs((1024, 1024), (1024, 1024)) |
| 102 | + golden = (a.double() - b.double()).to(torch.float32).numpy().astype("<f4") |
| 103 | + et = _export(SubModule(), a, b) |
| 104 | + with open(pte_path, "wb") as f: |
| 105 | + f.write(et.buffer) |
| 106 | + golden.tofile(golden_path) |
| 107 | + with open(input_path, "wb") as f: |
| 108 | + a.numpy().astype("<f4").tofile(f) |
| 109 | + b.numpy().astype("<f4").tofile(f) |
| 110 | + print(f"Exported {pte_path}; golden {golden_path} ({golden.size} floats)") |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + unittest.main() |
0 commit comments