Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions backends/webgpu/test/op_tests/cases.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
Expand Down Expand Up @@ -39,6 +39,11 @@
compare_gen_a,
compare_gen_b,
)
from executorch.backends.webgpu.test.ops.test_logical_and import (
la_gen_a,
la_gen_b,
LogicalAndModule,
)
from executorch.backends.webgpu.test.ops.test_floor_divide import FloorDivideModule
from executorch.backends.webgpu.test.ops.test_argmax import (
argmax_tie_gen,
Expand Down Expand Up @@ -307,6 +312,29 @@
return _compare_suite("ge")


@register_op_test("logical_and")
def _logical_and_suite() -> WebGPUTestSuite:
# out = (a>0) && (b>0): two bool masks derived on-GPU from float inputs via
# gt.Tensor (baked zeros), AND'd -> bool. Distinct a/b seeds so the masks
# differ (AND ~25% True, a real mix an OR mutant fails); all shapes numel %
# 4 == 0 (bool packs 4/word). float32 oracle (byte-exact bool golden).
def case(name, shape):
return Case(
name=name,
construct={"shape": shape},
inputs=(
InputSpec(shape=shape, gen=la_gen_a),
InputSpec(shape=shape, gen=la_gen_b),
),
)

return WebGPUTestSuite(
module_factory=lambda shape: LogicalAndModule(shape),
cases=[case("2d", (4, 8)), case("3d", (2, 3, 8)), case("sq", (16, 16))],
golden_dtype="float32",
)


@register_op_test("pow")
def _pow_suite() -> WebGPUTestSuite:
# Positive base avoids pow(neg, frac)=NaN; exponent spans negative+positive.
Expand Down
76 changes: 76 additions & 0 deletions backends/webgpu/test/ops/test_logical_and.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

"""`aten.logical_and.default` module + configs for the WebGPU op-test framework.

`LogicalAndModule` derives its two bool operands on-GPU from float inputs
(`a > 0`, `b > 0` via the delegated `gt.Tensor` against a baked zero buffer), so
the only runtime inputs are the two float tensors (the op-test framework is
float-input-only). `a`/`b` use distinct seeds so the two bool masks differ (each
~50% True, independent -> AND ~25% True), a real mix that a wrong op (e.g. OR)
would fail. Output is bool (byte-exact golden). `LogicalAndTest` is the
export-delegation smoke test.
"""

import unittest

import torch

from executorch.backends.vulkan.partitioner.vulkan_partitioner import VulkanPartitioner
from executorch.exir import to_edge_transform_and_lower


class LogicalAndModule(torch.nn.Module):
def __init__(self, shape) -> None:
super().__init__()
self.register_buffer("z", torch.zeros(shape))

def forward(self, a: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
return torch.logical_and(a > self.z, b > self.z)


def _la_gen(seed):
# Distinct per-input seed so the two derived bool masks differ.
def g(shape):
gen = torch.Generator().manual_seed(seed)
return torch.randn(*shape, generator=gen, dtype=torch.float32)

return g


la_gen_a = _la_gen(0)
la_gen_b = _la_gen(1)


# All shapes have numel % 4 == 0 (bool tensors pack 4 bytes/word).
SHAPES = [(4, 8), (2, 3, 8), (16, 16)]


class LogicalAndTest(unittest.TestCase):
def test_export_delegates(self) -> None:
for shape in SHAPES:
with self.subTest(shape=shape):
a = la_gen_a(shape)
b = la_gen_b(shape)
ep = torch.export.export(LogicalAndModule(shape).eval(), (a, b))
edge = to_edge_transform_and_lower(
ep, partitioner=[VulkanPartitioner()]
)
et = edge.to_executorch()
deleg = any(
d.id == "VulkanBackend"
for plan in et.executorch_program.execution_plan
for d in plan.delegates
)
self.assertTrue(deleg, f"Expected VulkanBackend delegate ({shape})")
gm = edge.exported_program().graph_module
self.assertTrue(
all(
"logical_and" not in str(getattr(n, "target", ""))
for n in gm.graph.nodes
),
f"logical_and fell back to CPU for {shape}",
)
Loading