forked from tile-ai/TileOPs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_gla_recurrence.py
More file actions
242 lines (202 loc) · 7.27 KB
/
Copy pathtest_gla_recurrence.py
File metadata and controls
242 lines (202 loc) · 7.27 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
from tileops.utils import get_backend_name
DEVICE = get_backend_name()
import pytest
import torch
from tests.test_base import FixtureBase, TestBase
from tileops.ops import GLADecodeOp
from workloads.gla import GLADecodeTest as _GLADecodeTestWorkload
def gla_decode_torch(
q: torch.Tensor,
k: torch.Tensor,
v: torch.Tensor,
gk: torch.Tensor,
state: torch.Tensor,
scale: float = -1.0,
) -> tuple[torch.Tensor, torch.Tensor]:
"""Pure-PyTorch reference for single-step GLA recurrence."""
DK = q.shape[-1]
if scale <= 0:
scale = DK ** -0.5
q, k, v = q.float(), k.float(), v.float()
gk = gk.float()
state = state.float()
alpha = torch.exp(gk)
new_state = alpha.unsqueeze(-1) * state + k.unsqueeze(-1) * v.unsqueeze(-2)
o = scale * torch.einsum("bhk,bhkv->bhv", q, new_state)
return o, new_state
class GLADecodeTest(_GLADecodeTestWorkload, TestBase):
def ref_program(
self,
q: torch.Tensor,
k: torch.Tensor,
v: torch.Tensor,
gk: torch.Tensor,
state: torch.Tensor,
) -> tuple[torch.Tensor, torch.Tensor]:
o, new_state = gla_decode_torch(q, k, v, gk, state, self.scale)
return o.to(self.dtype), new_state.to(self.dtype)
try:
from fla.ops.gla import fused_recurrent_gla
except ImportError:
fused_recurrent_gla = None
# =============================================================================
# Torch reference implementation (test-only)
# =============================================================================
# =============================================================================
# Correctness tests
# =============================================================================
def _get_tolerances(dtype: torch.dtype) -> dict:
if dtype == torch.float32:
return {"atol": 5e-4, "rtol": 5e-4}
elif dtype == torch.float16:
return {"atol": 1e-2, "rtol": 1e-2}
else: # bfloat16
return {"atol": 2e-2, "rtol": 2e-2}
class GLADecodeFixture(FixtureBase):
PARAMS = [
("batch, heads, dim_k, dim_v, dtype, tune", [
pytest.param(1, 4, 64, 64, torch.float32, False, marks=pytest.mark.smoke),
pytest.param(
1,
4,
64,
64,
torch.float16,
False,
marks=[
pytest.mark.smoke,
pytest.mark.skip(
reason="Low-precision GLA decode fails under tilelang 0.1.9 due to numerics regression; re-enable when decode produces correct results."
),
],
),
pytest.param(
1,
4,
64,
64,
torch.bfloat16,
False,
marks=[
pytest.mark.smoke,
pytest.mark.skip(
reason="Low-precision GLA decode fails under tilelang 0.1.9 due to numerics regression; re-enable when decode produces correct results."
),
],
),
pytest.param(2, 8, 64, 64, torch.float32, False, marks=pytest.mark.full),
pytest.param(2, 4, 128, 128, torch.float32, False, marks=pytest.mark.full),
pytest.param(
2,
8,
64,
64,
torch.float16,
False,
marks=[
pytest.mark.full,
pytest.mark.skip(
reason="Low-precision GLA decode fails under tilelang 0.1.9 due to numerics regression; re-enable when decode produces correct results."
),
],
),
pytest.param(
2,
8,
64,
64,
torch.bfloat16,
False,
marks=[
pytest.mark.full,
pytest.mark.skip(
reason="Low-precision GLA decode fails under tilelang 0.1.9 due to numerics regression; re-enable when decode produces correct results."
),
],
),
]),
]
@GLADecodeFixture
def test_gla_decode(
batch: int,
heads: int,
dim_k: int,
dim_v: int,
dtype: torch.dtype,
tune: bool,
) -> None:
torch.manual_seed(42)
test = GLADecodeTest(batch, heads, dim_k, dim_v, dtype)
op = GLADecodeOp(batch, heads, dim_k, dim_v, dtype=dtype, tune=tune)
tols = _get_tolerances(dtype)
test.check(op, *test.gen_inputs(), **tols)
@GLADecodeFixture
def test_gla_decode_multi_step(
batch: int,
heads: int,
dim_k: int,
dim_v: int,
dtype: torch.dtype,
tune: bool,
) -> None:
"""Test multiple sequential decode steps to verify state propagation."""
torch.manual_seed(42)
num_steps = 8
B, H, DK, DV = batch, heads, dim_k, dim_v
op = GLADecodeOp(B, H, DK, DV, dtype=dtype, tune=tune)
tols = _get_tolerances(dtype)
state_op = torch.zeros(B, H, DK, DV, device=DEVICE, dtype=dtype)
state_ref = torch.zeros(B, H, DK, DV, device=DEVICE, dtype=dtype)
for _ in range(num_steps):
q = torch.randn(B, H, DK, device=DEVICE, dtype=dtype) * 0.1
k = torch.randn(B, H, DK, device=DEVICE, dtype=dtype) * 0.1
v = torch.randn(B, H, DV, device=DEVICE, dtype=dtype) * 0.1
gk = -torch.rand(B, H, DK, device=DEVICE, dtype=dtype)
o_ref, state_ref = gla_decode_torch(q, k, v, gk, state_ref)
o_ref = o_ref.to(dtype)
state_ref = state_ref.to(dtype)
with torch.no_grad():
o_op, state_op = op(q, k, v, gk, state_op)
torch.testing.assert_close(o_op, o_ref, **tols)
torch.testing.assert_close(state_op, state_ref, **tols)
@GLADecodeFixture
def test_gla_decode_vs_fla(
batch: int,
heads: int,
dim_k: int,
dim_v: int,
dtype: torch.dtype,
tune: bool,
) -> None:
"""Compare TileOPs GLA decode against FLA fused_recurrent_gla with T=1."""
if fused_recurrent_gla is None:
pytest.skip("FLA not installed")
torch.manual_seed(42)
B, H, DK, DV = batch, heads, dim_k, dim_v
scale = DK ** -0.5
q = torch.randn(B, H, DK, device=DEVICE, dtype=dtype) * 0.1
k = torch.randn(B, H, DK, device=DEVICE, dtype=dtype) * 0.1
v = torch.randn(B, H, DV, device=DEVICE, dtype=dtype) * 0.1
gk = -torch.rand(B, H, DK, device=DEVICE, dtype=dtype)
state = torch.randn(B, H, DK, DV, device=DEVICE, dtype=dtype) * 0.1
# TileOPs
op = GLADecodeOp(B, H, DK, DV, scale=scale, dtype=dtype, tune=tune)
with torch.no_grad():
o_tile, s_tile = op(q, k, v, gk, state)
# FLA: needs BTHD layout with T=1
# q [B,H,DK] -> [B,1,H,DK]
q_fla = q.unsqueeze(1)
k_fla = k.unsqueeze(1)
v_fla = v.unsqueeze(1)
gk_fla = gk.unsqueeze(1)
o_fla, s_fla = fused_recurrent_gla(
q_fla, k_fla, v_fla, gk=gk_fla,
scale=scale, initial_state=state.contiguous(),
output_final_state=True,
)
o_fla = o_fla.squeeze(1).to(dtype)
tols = _get_tolerances(dtype)
torch.testing.assert_close(o_tile, o_fla, **tols)
torch.testing.assert_close(s_tile, s_fla.to(dtype), **tols)
if __name__ == "__main__":
pytest.main([__file__, "-vvs"])