-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtest_fill.py
More file actions
91 lines (67 loc) · 2.96 KB
/
Copy pathtest_fill.py
File metadata and controls
91 lines (67 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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.full` / `aten.full_like` export + fp64 golden for the WebGPU backend.
Both lower to the `fill` handler, which writes a constant scalar into the
pre-allocated output buffer. `full` and `full_like` are on the training-backward
critical path (gradient seeds). To keep the op in the graph (a constant-shaped
`full` would fold away), the fill size is taken from a live input: `full` uses
`x.shape`, `full_like` uses `x`. Configs span 1D/2D/4D and a non-multiple-of-256
shape that exercises the dispatch bounds guard.
"""
from __future__ import annotations
import unittest
import torch
from executorch.backends.vulkan.partitioner.vulkan_partitioner import VulkanPartitioner
from executorch.exir import to_edge_transform_and_lower
# name -> (kind, shape, fill_value)
CONFIGS = {
"full_1d": ("full", (37,), 0.0), # non-multiple of 256: bounds-guard path
"full_2d": ("full", (4, 8), 3.0),
"full_4d": ("full", (2, 3, 4, 5), -1.5),
"full_like_2d": ("full_like", (16, 16), 7.25),
}
class FullModule(torch.nn.Module):
def __init__(self, val: float) -> None:
super().__init__()
self.val = val
def forward(self, x: torch.Tensor) -> torch.Tensor:
return torch.full(x.shape, self.val)
class FullLikeModule(torch.nn.Module):
def __init__(self, val: float) -> None:
super().__init__()
self.val = val
def forward(self, x: torch.Tensor) -> torch.Tensor:
return torch.full_like(x, self.val)
def _module(kind: str, val: float) -> torch.nn.Module:
return (FullModule(val) if kind == "full" else FullLikeModule(val)).eval()
def _export(m: torch.nn.Module, x: torch.Tensor):
ep = torch.export.export(m, (x,))
return to_edge_transform_and_lower(
ep, partitioner=[VulkanPartitioner()]
).to_executorch()
def _delegates(et) -> bool:
return any(
d.id == "VulkanBackend"
for plan in et.executorch_program.execution_plan
for d in plan.delegates
)
class TestFill(unittest.TestCase):
def test_export_delegates(self) -> None:
for name, (kind, shape, val) in CONFIGS.items():
with self.subTest(name=name):
x = torch.zeros(shape, dtype=torch.float32)
et = _export(_module(kind, val), x)
self.assertTrue(
_delegates(et), f"Expected a VulkanBackend delegate (fill {name})"
)
def test_golden_matches_eager(self) -> None:
for name, (kind, shape, val) in CONFIGS.items():
with self.subTest(name=name):
x = torch.zeros(shape, dtype=torch.float32)
golden = torch.full(shape, val, dtype=torch.float64)
torch.testing.assert_close(_module(kind, val)(x).double(), golden)
if __name__ == "__main__":
unittest.main()