Skip to content

Commit 20b80f7

Browse files
authored
[ExecuTorch][WebGPU] sigmoid op test suite (cases.py op-test framework) (#20391)
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0) (oldest at bottom): * #20465 * #20464 * #20463 * #20435 * #20399 * #20398 * #20397 * #20396 * #20395 * #20394 * #20393 * #20392 * __->__ #20391 * #20390 * #20363 * #20362 * #20361 * #20360 * #20359 Registers `aten.sigmoid.default` in the `cases.py` op-test framework: a `_sigmoid_suite` (hard-coded shapes + a saturation case over a `linspace(-12, 12)` input) that `generate_op_tests` exports and compares to an fp64 torch golden on Dawn. Also adds `test/ops/sigmoid/test_sigmoid.py` (`SigmoidModule` + `N` + `_det_input` + an export-delegation/eager smoke test) and the `aten.sigmoid.default` partitioner-allowlist entry in `tester.py`. @exported-using-ghexport Differential Revision: [D108793159](https://our.internmc.facebook.com/intern/diff/D108793159/) Differential Revision: [D108793159](https://our.internmc.facebook.com/intern/diff/D108793159)
1 parent 57e305d commit 20b80f7

3 files changed

Lines changed: 83 additions & 0 deletions

File tree

backends/webgpu/test/op_tests/cases.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
CONFIGS as _SELECT_CONFIGS,
4545
SelectModule,
4646
)
47+
from executorch.backends.webgpu.test.ops.test_sigmoid import (
48+
_det_input as _sigmoid_det_input,
49+
N as _SIGMOID_N,
50+
SigmoidModule,
51+
)
4752
from executorch.backends.webgpu.test.ops.test_view_copy import (
4853
CONFIGS as _VIEW_CONFIGS,
4954
ViewModule,
@@ -153,3 +158,29 @@ def _view_copy_suite() -> WebGPUTestSuite:
153158
@register_op_test("select")
154159
def _select_suite() -> WebGPUTestSuite:
155160
return _fn_config_suite(SelectModule, _SELECT_CONFIGS)
161+
162+
163+
def _sigmoid_full_range(_shape) -> torch.Tensor:
164+
# Reuses the monolith's saturation-tail input (linspace(-12, 12)).
165+
return _sigmoid_det_input()
166+
167+
168+
@register_op_test("sigmoid")
169+
def _sigmoid_suite() -> WebGPUTestSuite:
170+
# sigmoid has no CONFIGS table; cover unary shapes directly (tol 1e-4).
171+
return WebGPUTestSuite(
172+
module_factory=lambda: SigmoidModule(),
173+
cases=[
174+
Case(name="vec", inputs=((M1,),)),
175+
Case(name="mat", inputs=((M1, M2),)),
176+
Case(name="rank3", inputs=((S1, M1, M2),)),
177+
Case(name="rank4", inputs=((S1, S2, S2, M2),)),
178+
# Saturation tails sigmoid(+-12) (~6e-6 / 0.999994) that randn shapes miss.
179+
Case(
180+
name="saturation",
181+
inputs=(InputSpec(shape=(_SIGMOID_N,), gen=_sigmoid_full_range),),
182+
),
183+
],
184+
atol=1e-4,
185+
rtol=1e-4,
186+
)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.sigmoid.default` module + input for the WebGPU op-test framework.
8+
9+
`SigmoidModule`, `N`, and `_det_input` are imported by `cases.py` to drive the
10+
declarative op-test suite. `SigmoidTest` is the export-delegation
11+
smoke test. Sigmoid is on the Llama critical path (`F.silu` -> `sigmoid` + `mul`); the
12+
deterministic input spans the saturation tails.
13+
"""
14+
15+
import unittest
16+
17+
import torch
18+
19+
from executorch.backends.vulkan.partitioner.vulkan_partitioner import VulkanPartitioner
20+
from executorch.exir import to_edge_transform_and_lower
21+
22+
# Input length; the deterministic input spans the saturation tails.
23+
N = 64
24+
25+
26+
class SigmoidModule(torch.nn.Module):
27+
def forward(self, x: torch.Tensor) -> torch.Tensor:
28+
return torch.sigmoid(x)
29+
30+
31+
def _det_input() -> torch.Tensor:
32+
"""Deterministic fp32 input spanning negatives, zero, and large magnitudes."""
33+
return torch.linspace(-12.0, 12.0, N, dtype=torch.float32)
34+
35+
36+
def _export(m: torch.nn.Module, x: torch.Tensor):
37+
ep = torch.export.export(m, (x,))
38+
return to_edge_transform_and_lower(
39+
ep, partitioner=[VulkanPartitioner()]
40+
).to_executorch()
41+
42+
43+
class SigmoidTest(unittest.TestCase):
44+
def test_export_delegates(self) -> None:
45+
et = _export(SigmoidModule().eval(), _det_input())
46+
found = any(
47+
d.id == "VulkanBackend"
48+
for plan in et.executorch_program.execution_plan
49+
for d in plan.delegates
50+
)
51+
self.assertTrue(found, "Expected a VulkanBackend delegate (sigmoid)")

backends/webgpu/test/tester.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
exir_ops.edge.aten.mul.Tensor,
2525
exir_ops.edge.aten.view_copy.default,
2626
exir_ops.edge.aten.select_copy.int,
27+
exir_ops.edge.aten.sigmoid.default,
2728
]
2829

2930

0 commit comments

Comments
 (0)