-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathtest_fused_deltanet_decode.py
More file actions
357 lines (302 loc) · 10.8 KB
/
test_fused_deltanet_decode.py
File metadata and controls
357 lines (302 loc) · 10.8 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# 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.
"""
Correctness test: fully-fused Triton GatedDeltaNet decode kernel vs PyTorch reference.
Verifies that torch.ops.triton.fused_deltanet_decode produces the same output
and state as the original GatedDeltaNet T=1 recurrence with manual Q/K/V split,
L2 norm, head repeat, and gating.
"""
import unittest
import torch
import torch.nn.functional as F
def _skip_if_no_cuda():
if not torch.cuda.is_available():
raise unittest.SkipTest("CUDA not available")
if not torch.cuda.is_bf16_supported():
raise unittest.SkipTest("BF16 not supported on this GPU")
def _import_fused_deltanet_decode():
from executorch.backends.cuda.triton.kernels.fused_deltanet_decode import (
fused_deltanet_decode, # noqa: F401 — registers torch.ops.triton.*
)
return fused_deltanet_decode
def _max_abs_error(a, b):
return (a.float() - b.float()).abs().max().item()
# bf16 kernel vs fp32 reference tolerance.
MAX_ABS_TOL = 0.05
MULTISTEP_TOL = 0.1
def _reference_deltanet_decode(
qkv_conv,
alpha,
beta_raw,
neg_A_exp,
dt_bias,
state,
num_k_heads,
num_v_heads,
head_k_dim,
head_v_dim,
):
"""Reference PyTorch implementation matching model.py's original T=1 path.
Does Q/K/V split, L2 norm, head repeat, gating, then recurrent update.
"""
B = qkv_conv.shape[0]
key_dim = num_k_heads * head_k_dim
q = qkv_conv[:, :key_dim].reshape(B, num_k_heads, head_k_dim)
k = qkv_conv[:, key_dim : 2 * key_dim].reshape(B, num_k_heads, head_k_dim)
v = qkv_conv[:, 2 * key_dim :].reshape(B, num_v_heads, head_v_dim)
q = F.normalize(q.float(), p=2, dim=-1)
k = F.normalize(k.float(), p=2, dim=-1)
v = v.float()
head_repeat = num_v_heads // num_k_heads
if head_repeat > 1:
q = q.repeat_interleave(head_repeat, dim=1)
k = k.repeat_interleave(head_repeat, dim=1)
beta = torch.sigmoid(beta_raw.float())
g = neg_A_exp.float() * F.softplus(alpha.float() + dt_bias.float())
scale = head_k_dim**-0.5
state_f32 = state.float()
decay = torch.exp(g).unsqueeze(-1).unsqueeze(-1)
state_f32 = state_f32 * decay
Sk = torch.einsum("bhkv,bhk->bhv", state_f32, k)
delta = beta.unsqueeze(-1) * (v - Sk)
state_f32 = state_f32 + torch.einsum("bhk,bhv->bhkv", k, delta)
output = torch.einsum("bhkv,bhk->bhv", state_f32, q) * scale
new_state = state_f32.to(state.dtype)
return output, new_state
# Qwen3.5 MoE dimensions (used across tests)
NUM_K_HEADS = 16
NUM_V_HEADS = 32
HEAD_K_DIM = 128
HEAD_V_DIM = 128
KEY_DIM = NUM_K_HEADS * HEAD_K_DIM # 2048
VALUE_DIM = NUM_V_HEADS * HEAD_V_DIM # 4096
CONV_DIM = 2 * KEY_DIM + VALUE_DIM # 8192
class TestFusedDeltanetDecode(unittest.TestCase):
"""Test fused GatedDeltaNet decode kernel correctness against PyTorch reference."""
@classmethod
def setUpClass(cls):
_skip_if_no_cuda()
cls.fused_fn = _import_fused_deltanet_decode()
torch.manual_seed(42)
cls.A_log = torch.log(torch.empty(NUM_V_HEADS, device="cuda").uniform_(0.5, 8))
cls.neg_A_exp = -torch.exp(cls.A_log).float()
cls.dt_bias = torch.ones(NUM_V_HEADS, device="cuda", dtype=torch.float32)
def _run_fused(self, qkv, alpha, beta_raw, state):
"""Run fused kernel and return (output, new_state)."""
output, new_state = torch.ops.triton.fused_deltanet_decode(
qkv,
alpha,
beta_raw,
self.A_log,
self.dt_bias,
state,
)
return output, new_state
def _run_reference(self, qkv, alpha, beta_raw, state):
"""Run reference and return (output, new_state)."""
return _reference_deltanet_decode(
qkv,
alpha,
beta_raw,
self.neg_A_exp,
self.dt_bias,
state,
NUM_K_HEADS,
NUM_V_HEADS,
HEAD_K_DIM,
HEAD_V_DIM,
)
# ------------------------------------------------------------------
# Correctness
# ------------------------------------------------------------------
def test_basic(self):
"""Single batch, Qwen3.5 MoE dimensions."""
B = 1
torch.manual_seed(42)
qkv = torch.randn(B, CONV_DIM, device="cuda", dtype=torch.bfloat16) * 0.1
alpha = torch.randn(B, NUM_V_HEADS, device="cuda", dtype=torch.float32)
beta_raw = torch.randn(B, NUM_V_HEADS, device="cuda", dtype=torch.float32)
state = (
torch.randn(
B,
NUM_V_HEADS,
HEAD_K_DIM,
HEAD_V_DIM,
device="cuda",
dtype=torch.bfloat16,
)
* 0.1
)
ref_out, ref_state = self._run_reference(
qkv.clone(),
alpha.clone(),
beta_raw.clone(),
state.clone(),
)
fused_out, fused_state = self._run_fused(
qkv.clone(),
alpha.clone(),
beta_raw.clone(),
state.clone(),
)
self.assertLess(
_max_abs_error(fused_out, ref_out), MAX_ABS_TOL, "output mismatch"
)
self.assertLess(
_max_abs_error(fused_state, ref_state), MAX_ABS_TOL, "state mismatch"
)
def test_batch(self):
"""Batch size > 1."""
for B in [2, 4]:
with self.subTest(B=B):
torch.manual_seed(42)
qkv = (
torch.randn(B, CONV_DIM, device="cuda", dtype=torch.bfloat16) * 0.1
)
alpha = torch.randn(B, NUM_V_HEADS, device="cuda", dtype=torch.float32)
beta_raw = torch.randn(
B, NUM_V_HEADS, device="cuda", dtype=torch.float32
)
state = (
torch.randn(
B,
NUM_V_HEADS,
HEAD_K_DIM,
HEAD_V_DIM,
device="cuda",
dtype=torch.bfloat16,
)
* 0.1
)
ref_out, ref_state = self._run_reference(
qkv.clone(),
alpha.clone(),
beta_raw.clone(),
state.clone(),
)
fused_out, fused_state = self._run_fused(
qkv.clone(),
alpha.clone(),
beta_raw.clone(),
state.clone(),
)
self.assertLess(
_max_abs_error(fused_out, ref_out),
MAX_ABS_TOL,
f"B={B} output mismatch",
)
self.assertLess(
_max_abs_error(fused_state, ref_state),
MAX_ABS_TOL,
f"B={B} state mismatch",
)
def test_multistep(self):
"""10-step sequential decode checks accumulation drift."""
torch.manual_seed(42)
state_ref = (
torch.randn(
1,
NUM_V_HEADS,
HEAD_K_DIM,
HEAD_V_DIM,
device="cuda",
dtype=torch.bfloat16,
)
* 0.01
)
state_fused = state_ref.clone()
for _ in range(10):
qkv = torch.randn(1, CONV_DIM, device="cuda", dtype=torch.bfloat16) * 0.1
alpha = torch.randn(1, NUM_V_HEADS, device="cuda", dtype=torch.float32)
beta_raw = torch.randn(1, NUM_V_HEADS, device="cuda", dtype=torch.float32)
ref_out, state_ref = self._run_reference(
qkv.clone(),
alpha.clone(),
beta_raw.clone(),
state_ref,
)
fused_out, state_fused = self._run_fused(
qkv.clone(),
alpha.clone(),
beta_raw.clone(),
state_fused,
)
self.assertLess(
_max_abs_error(fused_out, ref_out),
MULTISTEP_TOL,
"multi-step output drift",
)
self.assertLess(
_max_abs_error(state_fused, state_ref),
MULTISTEP_TOL,
"multi-step state drift",
)
def test_state_not_mutated(self):
"""Kernel must not mutate the input state tensor."""
B = 1
torch.manual_seed(42)
qkv = torch.randn(B, CONV_DIM, device="cuda", dtype=torch.bfloat16) * 0.1
alpha = torch.randn(B, NUM_V_HEADS, device="cuda", dtype=torch.float32)
beta_raw = torch.randn(B, NUM_V_HEADS, device="cuda", dtype=torch.float32)
state = (
torch.randn(
B,
NUM_V_HEADS,
HEAD_K_DIM,
HEAD_V_DIM,
device="cuda",
dtype=torch.bfloat16,
)
* 0.1
)
state_copy = state.clone()
_, _ = self._run_fused(qkv, alpha, beta_raw, state)
self.assertTrue(torch.equal(state, state_copy), "input state was mutated")
# ------------------------------------------------------------------
# CUDA Graph compatibility
# ------------------------------------------------------------------
def test_cuda_graph(self):
"""Kernel must be capturable in a CUDA graph."""
B = 1
torch.manual_seed(42)
qkv = torch.randn(B, CONV_DIM, device="cuda", dtype=torch.bfloat16) * 0.1
alpha = torch.randn(B, NUM_V_HEADS, device="cuda", dtype=torch.float32)
beta_raw = torch.randn(B, NUM_V_HEADS, device="cuda", dtype=torch.float32)
state = (
torch.randn(
B,
NUM_V_HEADS,
HEAD_K_DIM,
HEAD_V_DIM,
device="cuda",
dtype=torch.bfloat16,
)
* 0.1
)
# Warmup
for _ in range(3):
_ = self._run_fused(qkv, alpha, beta_raw, state)
# Capture
graph = torch.cuda.CUDAGraph()
with torch.cuda.graph(graph):
out_cg, state_cg = self._run_fused(qkv, alpha, beta_raw, state)
# Replay
graph.replay()
# Compare with reference
ref_out, _ = self._run_reference(
qkv.clone(),
alpha.clone(),
beta_raw.clone(),
state.clone(),
)
self.assertFalse(torch.isnan(out_cg).any(), "NaN in CUDA graph output")
self.assertLess(
_max_abs_error(out_cg, ref_out),
MAX_ABS_TOL,
"CUDA graph output mismatch",
)
if __name__ == "__main__":
unittest.main()