forked from tile-ai/TileOPs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_fused_add_layer_norm.py
More file actions
141 lines (115 loc) · 5.54 KB
/
Copy pathtest_fused_add_layer_norm.py
File metadata and controls
141 lines (115 loc) · 5.54 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from tileops.utils import get_backend_name
DEVICE = get_backend_name()
import pytest
import torch
import torch.nn.functional as F
from tests.test_base import FixtureBase, TestBase
from tileops.ops.norm.fused_add_layer_norm import FusedAddLayerNormFwdOp
from workloads.fused_add_layer_norm import (
FusedAddLayerNormTest as _FusedAddLayerNormTestWorkload,
)
class FusedAddLayerNormTest(_FusedAddLayerNormTestWorkload, TestBase):
def ref_program(
self,
x: torch.Tensor,
residual: torch.Tensor,
weight: torch.Tensor,
bias: torch.Tensor,
) -> tuple[torch.Tensor, torch.Tensor]:
add_result = (x.float() + residual.float()).to(x.dtype)
y = F.layer_norm(
add_result.float(),
(self.n,),
weight=weight.float(),
bias=bias.float(),
eps=self.eps,
).to(x.dtype)
return y, add_result
class FusedAddLayerNormFixture(FixtureBase):
PARAMS = [
("m, n, dtype, tune", [
# Standard aligned shapes -- fp32
pytest.param(1024, 4096, torch.float32, False, marks=pytest.mark.smoke),
pytest.param(1024, 4096, torch.float16, False, marks=pytest.mark.smoke),
pytest.param(1024, 4096, torch.bfloat16, False, marks=pytest.mark.smoke),
pytest.param(4096, 4096, torch.float32, False, marks=pytest.mark.full),
# Standard aligned shapes -- fp16
pytest.param(4096, 4096, torch.float16, False, marks=pytest.mark.full),
# Standard aligned shapes -- bf16
pytest.param(4096, 4096, torch.bfloat16, False, marks=pytest.mark.full),
# Non-power-of-two hidden dims
pytest.param(1024, 3000, torch.float32, False, marks=pytest.mark.full),
pytest.param(1024, 3000, torch.float16, False, marks=pytest.mark.full),
pytest.param(1024, 3000, torch.bfloat16, False, marks=pytest.mark.full),
# Tail-M: M not divisible by block_m
pytest.param(1025, 4096, torch.float16, False, marks=pytest.mark.full),
pytest.param(1025, 4096, torch.bfloat16, False, marks=pytest.mark.full),
]),
]
def _get_tolerances(dtype: torch.dtype) -> tuple[float, float]:
if dtype == torch.float32:
return 1e-5, 1e-5
elif dtype == torch.float16:
return 1e-3, 1e-3
else: # bfloat16
return 1.6e-2, 1.6e-2
@FusedAddLayerNormFixture
def test_fused_add_layer_norm_op(m: int, n: int, dtype: torch.dtype, tune: bool) -> None:
test = FusedAddLayerNormTest(m, n, dtype)
op = FusedAddLayerNormFwdOp(M=m, N=n, dtype=dtype)
atol, rtol = _get_tolerances(dtype)
test.check(op, *test.gen_inputs(), atol=atol, rtol=rtol)
class FusedAddLayerNormNonContigFixture(FixtureBase):
PARAMS = [
("m, n, dtype", [
pytest.param(1024, 4096, torch.float32, marks=pytest.mark.smoke),
pytest.param(1024, 4096, torch.float16, marks=pytest.mark.smoke),
pytest.param(1024, 4096, torch.bfloat16, marks=pytest.mark.smoke),
]),
]
@FusedAddLayerNormNonContigFixture
def test_fused_add_layer_norm_non_contiguous(m: int, n: int, dtype: torch.dtype) -> None:
"""Test with non-contiguous input (sliced tensor)."""
x_full = torch.randn(m, n * 2, dtype=dtype, device=DEVICE)
r_full = torch.randn(m, n * 2, dtype=dtype, device=DEVICE)
x = x_full[:, :n] # non-contiguous slice
residual = r_full[:, :n]
weight = torch.randn(n, dtype=dtype, device=DEVICE)
bias = torch.randn(n, dtype=dtype, device=DEVICE)
op = FusedAddLayerNormFwdOp(M=m, N=n, dtype=dtype)
# Reference on contiguous copies
test = FusedAddLayerNormTest(m, n, dtype)
y_ref, add_ref = test.ref_program(x.contiguous(), residual.contiguous(), weight, bias)
y, residual_out = op(x, residual, weight, bias)
atol, rtol = _get_tolerances(dtype)
assert torch.allclose(y, y_ref, atol=atol, rtol=rtol), \
f"Non-contiguous y test failed, max err: {(y - y_ref).abs().max()}"
assert torch.allclose(residual_out, add_ref, atol=atol, rtol=rtol), \
f"Non-contiguous residual_out test failed, max err: {(residual_out - add_ref).abs().max()}"
class FusedAddLayerNorm3DFixture(FixtureBase):
PARAMS = [
("batch, seq, hidden, dtype", [
pytest.param(2, 512, 4096, torch.float32, marks=pytest.mark.smoke),
pytest.param(2, 512, 4096, torch.float16, marks=pytest.mark.smoke),
pytest.param(2, 512, 4096, torch.bfloat16, marks=pytest.mark.smoke),
]),
]
@FusedAddLayerNorm3DFixture
def test_fused_add_layer_norm_3d(batch: int, seq: int, hidden: int, dtype: torch.dtype) -> None:
"""Test with 3D input (batch, seq, hidden)."""
x = torch.randn(batch, seq, hidden, dtype=dtype, device=DEVICE)
residual = torch.randn(batch, seq, hidden, dtype=dtype, device=DEVICE)
weight = torch.randn(hidden, dtype=dtype, device=DEVICE)
bias = torch.randn(hidden, dtype=dtype, device=DEVICE)
M = batch * seq
op = FusedAddLayerNormFwdOp(M=M, N=hidden, dtype=dtype)
test = FusedAddLayerNormTest(M, hidden, dtype)
y_ref, add_ref = test.ref_program(x, residual, weight, bias)
y, residual_out = op(x, residual, weight, bias)
atol, rtol = _get_tolerances(dtype)
assert torch.allclose(y, y_ref, atol=atol, rtol=rtol), \
f"3D y test failed, max err: {(y - y_ref).abs().max()}"
assert torch.allclose(residual_out, add_ref, atol=atol, rtol=rtol), \
f"3D residual_out test failed, max err: {(residual_out - add_ref).abs().max()}"
if __name__ == "__main__":
pytest.main([__file__, "-vvs"])