|
| 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.expand_copy.default` export + golden for the WebGPU backend. |
| 8 | +
|
| 9 | +Exports single-op expand-copy graphs through VulkanPartitioner and checks an fp64 |
| 10 | +torch golden. expand_copy materializes a broadcasted view (size-1 input dims, and |
| 11 | +rank-increasing leading dims) into the target shape via a pure gather -- no |
| 12 | +arithmetic, so fp64 and fp32 agree exactly. Configs cover a broadcast leading |
| 13 | +dim, a broadcast middle dim, and a rank increase (input rank < output rank) that |
| 14 | +exercises the kernel's right-alignment of input dims into the output rank. |
| 15 | +""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import math |
| 20 | +import unittest |
| 21 | + |
| 22 | +import torch |
| 23 | + |
| 24 | +from executorch.backends.vulkan.partitioner.vulkan_partitioner import VulkanPartitioner |
| 25 | +from executorch.exir import to_edge_transform_and_lower |
| 26 | + |
| 27 | +# name -> (input shape, expanded shape). |
| 28 | +CONFIGS = { |
| 29 | + "broadcast_leading": ((1, 4), (3, 4)), |
| 30 | + "broadcast_middle": ((2, 1, 5), (2, 4, 5)), |
| 31 | + "rank_increase": ((4,), (3, 4)), |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +class ExpandCopyModule(torch.nn.Module): |
| 36 | + def __init__(self, shape: tuple[int, ...]) -> None: |
| 37 | + super().__init__() |
| 38 | + self.shape = shape |
| 39 | + |
| 40 | + def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 41 | + return x.expand(self.shape).clone() |
| 42 | + |
| 43 | + |
| 44 | +def _det_input(shape: tuple[int, ...]) -> torch.Tensor: |
| 45 | + """Deterministic fp32 ramp; a broadcast dim repeats it so the copy is visible.""" |
| 46 | + return torch.arange(math.prod(shape), dtype=torch.float32).reshape(shape) |
| 47 | + |
| 48 | + |
| 49 | +def _export(m: torch.nn.Module, x: torch.Tensor): |
| 50 | + ep = torch.export.export(m, (x,)) |
| 51 | + return to_edge_transform_and_lower( |
| 52 | + ep, partitioner=[VulkanPartitioner()] |
| 53 | + ).to_executorch() |
| 54 | + |
| 55 | + |
| 56 | +def _delegates(et) -> bool: |
| 57 | + return any( |
| 58 | + d.id == "VulkanBackend" |
| 59 | + for plan in et.executorch_program.execution_plan |
| 60 | + for d in plan.delegates |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +def _top_level_op_names(et) -> set[str]: |
| 65 | + return { |
| 66 | + op.name |
| 67 | + for plan in et.executorch_program.execution_plan |
| 68 | + for op in plan.operators |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | +class TestExpandCopy(unittest.TestCase): |
| 73 | + def test_export_delegates(self) -> None: |
| 74 | + for name, (in_shape, out_shape) in CONFIGS.items(): |
| 75 | + with self.subTest(name=name): |
| 76 | + x = _det_input(in_shape) |
| 77 | + et = _export(ExpandCopyModule(out_shape).eval(), x) |
| 78 | + self.assertTrue( |
| 79 | + _delegates(et), |
| 80 | + f"Expected a VulkanBackend delegate (expand_copy {name})", |
| 81 | + ) |
| 82 | + # Delegated => expand_copy absent from top-level portable ops. |
| 83 | + self.assertFalse( |
| 84 | + any("expand_copy" in n for n in _top_level_op_names(et)), |
| 85 | + f"expand_copy leaked into top-level ops ({name})", |
| 86 | + ) |
| 87 | + |
| 88 | + def test_golden_matches_torch(self) -> None: |
| 89 | + for name, (in_shape, out_shape) in CONFIGS.items(): |
| 90 | + with self.subTest(name=name): |
| 91 | + x = _det_input(in_shape) |
| 92 | + golden = x.double().expand(out_shape).clone() |
| 93 | + got = ExpandCopyModule(out_shape)(x) |
| 94 | + torch.testing.assert_close(got.double(), golden) |
| 95 | + |
| 96 | + |
| 97 | +def export_expand_copy_model( |
| 98 | + out_shape: tuple[int, ...], |
| 99 | + in_shape: tuple[int, ...], |
| 100 | + pte_path: str, |
| 101 | + golden_path: str, |
| 102 | + input_path: str, |
| 103 | +) -> None: |
| 104 | + """Write an expand_copy .pte + torch golden (raw LE fp32) + raw LE fp32 input.""" |
| 105 | + m = ExpandCopyModule(out_shape).eval() |
| 106 | + x = _det_input(in_shape) |
| 107 | + golden = m(x).detach().numpy().astype("<f4") |
| 108 | + et = _export(m, x) |
| 109 | + with open(pte_path, "wb") as f: |
| 110 | + f.write(et.buffer) |
| 111 | + golden.tofile(golden_path) |
| 112 | + x.numpy().astype("<f4").tofile(input_path) |
| 113 | + print( |
| 114 | + f"Exported {pte_path}; golden {golden_path} ({golden.size} floats); " |
| 115 | + f"input {input_path} ({x.numel()} floats)" |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + unittest.main() |
0 commit comments