|
| 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._softmax.default` export + golden for the WebGPU backend. |
| 8 | +
|
| 9 | +Exports single-op softmax graphs through VulkanPartitioner and writes a |
| 10 | +torch-computed golden (the native binary has no ATen) + the raw fp32 input the |
| 11 | +native test loads and compares. Softmax is on the training critical path: the |
| 12 | +decomposed attention lowers to `matmul -> softmax(dim=-1) -> matmul`, whereas the |
| 13 | +fused inference `sdpa` computes softmax internally, so a standalone `_softmax` |
| 14 | +op is only exercised by the decomposed backward. `dim=-1` gives inner=1 (the |
| 15 | +attention case); a middle dim exercises the inner>1 reduction path. |
| 16 | +""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 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 -> (shape, dim). dim=-1 => inner=1 (attention); a middle dim => inner>1. |
| 28 | +CONFIGS = { |
| 29 | + "last_dim_3d": ((4, 8, 16), -1), |
| 30 | + "middle_dim_3d": ((4, 8, 16), 1), |
| 31 | + "last_dim_2d": ((32, 64), -1), |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +class SoftmaxModule(torch.nn.Module): |
| 36 | + def __init__(self, dim: int) -> None: |
| 37 | + super().__init__() |
| 38 | + self.dim = dim |
| 39 | + |
| 40 | + def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 41 | + return torch.softmax(x, dim=self.dim) |
| 42 | + |
| 43 | + |
| 44 | +def _det_input(shape) -> torch.Tensor: |
| 45 | + """Deterministic fp32 spanning large +/- magnitudes (exercises the |
| 46 | + max-subtraction: a naive exp(x) would overflow on the +40 entries).""" |
| 47 | + numel = 1 |
| 48 | + for d in shape: |
| 49 | + numel *= d |
| 50 | + return torch.linspace(-40.0, 40.0, numel, dtype=torch.float32).reshape(shape) |
| 51 | + |
| 52 | + |
| 53 | +def _fp64_golden(x: torch.Tensor, dim: int) -> torch.Tensor: |
| 54 | + """Numerically-stable softmax in fp64, independent of torch.softmax: |
| 55 | + exp(x - max) / sum(exp(x - max)) along `dim`.""" |
| 56 | + xd = x.double() |
| 57 | + e = torch.exp(xd - xd.amax(dim=dim, keepdim=True)) |
| 58 | + return (e / e.sum(dim=dim, keepdim=True)).to(torch.float32) |
| 59 | + |
| 60 | + |
| 61 | +def _export(m: torch.nn.Module, x: torch.Tensor): |
| 62 | + ep = torch.export.export(m, (x,)) |
| 63 | + return to_edge_transform_and_lower( |
| 64 | + ep, partitioner=[VulkanPartitioner()] |
| 65 | + ).to_executorch() |
| 66 | + |
| 67 | + |
| 68 | +def _delegates(et) -> bool: |
| 69 | + return any( |
| 70 | + d.id == "VulkanBackend" |
| 71 | + for plan in et.executorch_program.execution_plan |
| 72 | + for d in plan.delegates |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | +class TestSoftmax(unittest.TestCase): |
| 77 | + def test_export_delegates(self) -> None: |
| 78 | + for name, (shape, dim) in CONFIGS.items(): |
| 79 | + with self.subTest(config=name): |
| 80 | + et = _export(SoftmaxModule(dim).eval(), _det_input(shape)) |
| 81 | + self.assertTrue( |
| 82 | + _delegates(et), |
| 83 | + f"Expected a VulkanBackend delegate (softmax {name})", |
| 84 | + ) |
| 85 | + |
| 86 | + def test_golden_matches_fp64(self) -> None: |
| 87 | + for name, (shape, dim) in CONFIGS.items(): |
| 88 | + with self.subTest(config=name): |
| 89 | + x = _det_input(shape) |
| 90 | + torch.testing.assert_close( |
| 91 | + torch.softmax(x, dim=dim), |
| 92 | + _fp64_golden(x, dim), |
| 93 | + atol=1e-6, |
| 94 | + rtol=1e-5, |
| 95 | + ) |
| 96 | + |
| 97 | + |
| 98 | +def export_softmax_model(pte_path: str, golden_path: str, input_path: str) -> None: |
| 99 | + """Write the softmax(dim=-1) .pte + fp64 golden (raw LE fp32) + raw LE fp32 input.""" |
| 100 | + shape = (4, 8, 16) |
| 101 | + m = SoftmaxModule(-1).eval() |
| 102 | + x = _det_input(shape) |
| 103 | + golden = _fp64_golden(x, -1).numpy().astype("<f4") |
| 104 | + et = _export(m, x) |
| 105 | + with open(pte_path, "wb") as f: |
| 106 | + f.write(et.buffer) |
| 107 | + golden.tofile(golden_path) |
| 108 | + x.numpy().astype("<f4").tofile(input_path) |
| 109 | + print( |
| 110 | + f"Exported {pte_path}; golden {golden_path} ({golden.size} floats); " |
| 111 | + f"input {input_path} ({x.numel()} floats)" |
| 112 | + ) |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + unittest.main() |
0 commit comments