forked from tile-ai/TileOPs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mamba.py
More file actions
362 lines (293 loc) · 13.1 KB
/
Copy pathtest_mamba.py
File metadata and controls
362 lines (293 loc) · 13.1 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
358
359
360
361
362
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 TestBase, allclose_compare
from tileops.ops.da_cumsum import DaCumsumFwdOp
from tileops.ops.ssd_chunk_scan import SSDChunkScanFwdOp
from tileops.ops.ssd_chunk_state import SSDChunkStateFwdOp
from tileops.ops.ssd_decode import SSDDecodeOp
from tileops.ops.ssd_state_passing import SSDStatePassingFwdOp
from workloads.mamba import (
DaCumsumFwdFixture,
SSDChunkScanFwdFixture,
SSDChunkStateFwdFixture,
SSDDecodeFixture,
SSDStatePassingFwdFixture,
)
from workloads.mamba import DaCumsumFwdTest as _DaCumsumFwdTestWorkload
from workloads.mamba import SSDChunkScanFwdTest as _SSDChunkScanFwdTestWorkload
from workloads.mamba import SSDChunkStateFwdTest as _SSDChunkStateFwdTestWorkload
from workloads.mamba import SSDDecodeTest as _SSDDecodeTestWorkload
from workloads.mamba import (
SSDStatePassingFwdTest as _SSDStatePassingFwdTestWorkload,
)
def da_cumsum_fwd_ref(
dt: torch.Tensor,
A: torch.Tensor,
num_chunks: int,
chunk_len: int,
dt_bias: torch.Tensor | None = None,
dt_softplus: bool = False,
dt_min: float = 0.0,
dt_max: float = float("inf"),
) -> tuple[torch.Tensor, torch.Tensor]:
"""PyTorch reference for da_cumsum_fwd.
Applies the same bias / softplus / clamp pipeline as the kernel, then
computes dt_out and the chunk-local inclusive prefix sum of dA = dt_out * A.
Returns:
dt_out: (batch, n_heads, num_chunks, chunk_len) float32
dA_cumsum: (batch, n_heads, num_chunks, chunk_len) float32
"""
b, S, h = dt.shape
Q = chunk_len
C = num_chunks
dt_val = dt.float()
if dt_bias is not None:
dt_val = dt_val + dt_bias.float()
if dt_softplus:
dt_val = F.softplus(dt_val)
dt_val = torch.clamp(dt_val, min=dt_min, max=dt_max)
dt_chunked = dt_val.reshape(b, C, Q, h) # (b, C, Q, h)
dt_out = dt_chunked.permute(0, 3, 1, 2).contiguous() # (b, h, C, Q)
dA = dt_chunked * A.float() # (b, C, Q, h)
dA_cumsum = dA.cumsum(dim=2).permute(0, 3, 1, 2).contiguous() # (b, h, C, Q)
return dt_out, dA_cumsum
class DaCumsumFwdTest(_DaCumsumFwdTestWorkload, TestBase):
def ref_program(self, dt, A, dt_bias):
return da_cumsum_fwd_ref(
dt, A, self.num_chunks, self.chunk_len,
dt_bias=dt_bias if self.has_dt_bias else None,
dt_softplus=self.dt_softplus,
dt_min=self.dt_min,
dt_max=self.dt_max,
)
@DaCumsumFwdFixture
def test_da_cumsum_fwd(batch, num_chunks, chunk_len, n_heads, has_dt_bias, dt_softplus, tune):
test = DaCumsumFwdTest(
batch, num_chunks, chunk_len, n_heads,
has_dt_bias=has_dt_bias, dt_softplus=dt_softplus,
)
op = DaCumsumFwdOp(
batch, num_chunks, chunk_len, n_heads,
seq_len=num_chunks * chunk_len,
has_dt_bias=has_dt_bias,
dt_softplus=dt_softplus,
tune=tune,
)
inputs = test.gen_inputs()
test.check(op, *inputs, atol=1e-5, rtol=1e-5)
@pytest.mark.smoke
def test_da_cumsum_fwd_missing_bias_raises():
"""DaCumsumFwdKernel must raise when has_dt_bias=True but dt_bias is None."""
from tileops.kernels.mamba import DaCumsumFwdKernel
kernel = DaCumsumFwdKernel(
batch=1, num_chunks=2, chunk_len=64, n_heads=4,
seq_len=128, has_dt_bias=True,
)
dt = torch.randn(1, 128, 4, dtype=torch.float32, device=DEVICE)
A = -torch.rand(4, dtype=torch.float32, device=DEVICE)
with pytest.raises(ValueError, match="dt_bias is required"):
kernel(dt, A, dt_bias=None)
def ssd_chunk_scan_fwd_ref(x, cb, dA_cumsum, C, prev_states, dt, n_groups):
"""Official-aligned PyTorch reference for chunk scan.
Inputs (official layouts):
x: [B, S, H, P] dtype
cb: [B, C, G, L, L] dtype group-owned
dA_cumsum: [B, H, C, L] float32
C: [B, S, G, N] dtype group-owned
prev_states: [B, C, H, P, N] float32 P before N
dt: [B, H, C, L] dtype
Output: [B, S, H, P] float32
"""
b, S, h, p = x.shape
_, _, c, L = dA_cumsum.shape
n = C.shape[-1]
g = n_groups
heads_per_group = h // g
x_chunked = x.float().reshape(b, c, L, h, p) # [B, C, L, H, P]
C_chunked = C.float().reshape(b, c, L, g, n) # [B, C, L, G, N]
# broadcast C from groups to heads: [B, C, L, H, N]
C_heads = C_chunked[:, :, :, torch.arange(h, device=x.device) // heads_per_group, :]
# dA_cumsum: [B, H, C, L] -> [B, C, L, H] for broadcast
dA = dA_cumsum.float().permute(0, 2, 3, 1) # [B, C, L, H]
# --- History path: exp(dA_l) * C[l] @ prev_states[p, n] ---
# prev_states: [B, C, H, P, N]
# C_heads: [B, C, L, H, N] -> einsum over n: [B, C, L, H, P]
y_off = torch.einsum("bclhn,bchpn->bclhp", C_heads, prev_states.float())
y_off = y_off * torch.exp(dA).unsqueeze(-1) # scale by exp(dA_l)
# --- Intra-chunk path: sum_{s<=l} cb[l,s] * exp(dA_l - dA_s) * dt[s] * x[s] ---
# cb: [B, C, G, L, L]; broadcast to heads [B, C, H, L, L]
cb_chunked = cb.float() # [B, C, G, L, L]
cb_heads = cb_chunked[:, :, torch.arange(h, device=x.device) // heads_per_group, :, :]
# decay[b,c,h,l,s] = exp(dA_cumsum[l] - dA_cumsum[s])
dA_l = dA_cumsum.float().unsqueeze(-1) # [B, H, C, L, 1]
dA_s = dA_cumsum.float().unsqueeze(-2) # [B, H, C, 1, L]
decay = torch.exp(dA_l - dA_s) # [B, H, C, L, L]
# causal mask
mask = torch.tril(torch.ones(L, L, device=x.device, dtype=torch.bool))
decay = decay.masked_fill(~mask.unsqueeze(0).unsqueeze(0).unsqueeze(0), 0.0)
decay = decay.permute(0, 2, 1, 3, 4) # [B, C, H, L, L]
# dt: [B, H, C, L] -> [B, C, H, 1, L]
dt_s = dt.float().permute(0, 2, 1, 3).unsqueeze(-2) # [B, C, H, 1, L]
# lcb[b,c,h,l,s] = cb[l,s] * decay[l,s] * dt[s]
lcb = cb_heads * decay * dt_s # [B, C, H, L, L]
# y_diag[b,c,l,h,p] = sum_s lcb[b,c,h,l,s] * x[b,c,s,h,p]
y_diag = torch.einsum("bchls,bcshp->bclhp", lcb, x_chunked)
# combine and reshape to [B, S, H, P]
out = (y_off + y_diag).reshape(b, S, h, p)
return out
class SSDChunkScanFwdTest(_SSDChunkScanFwdTestWorkload, TestBase):
def ref_program(self, x, cb, dA_cumsum, C, prev_states, dt):
return ssd_chunk_scan_fwd_ref(x, cb, dA_cumsum, C, prev_states, dt, self.n_groups)
@SSDChunkScanFwdFixture
def test_ssd_chunk_scan_fwd(batch, num_chunks, chunk_len, n_heads, d_head, d_state, n_groups, dtype, tune):
test = SSDChunkScanFwdTest(batch, num_chunks, chunk_len, n_heads, d_head, d_state, n_groups, dtype)
op = SSDChunkScanFwdOp(batch, num_chunks, chunk_len, n_heads, d_head, d_state, n_groups, dtype, tune=tune)
inputs = test.gen_inputs()
atol = 1e-3 if dtype == torch.float16 else 2e-3
rtol = 1e-5
test.check(op, *inputs, atol=atol, rtol=rtol)
def ssd_chunk_state_fwd_ref(
x: torch.Tensor,
Bmat: torch.Tensor,
dt: torch.Tensor,
dA_cumsum: torch.Tensor,
n_groups: int,
seq_idx=None,
) -> torch.Tensor:
"""PyTorch reference for ssd_chunk_state_fwd."""
b, seq_len, h, p = x.shape
_, _, c, Q = dt.shape
n = Bmat.shape[-1]
heads_per_group = h // n_groups
x_chunked = x.float().reshape(b, c, Q, h, p)
B_chunked = Bmat.float().reshape(b, c, Q, n_groups, n)
B_heads = B_chunked[:, :, :, torch.arange(h) // heads_per_group, :]
dA = dA_cumsum.float().permute(0, 2, 1, 3)
dA_end = dA[:, :, :, -1:]
decay = torch.exp(torch.clamp(dA_end - dA, max=0.0))
dt_chunked = dt.float().permute(0, 2, 1, 3)
weight = decay * dt_chunked
if seq_idx is not None:
seq_chunked = seq_idx.reshape(b, c, Q)
seq_end = seq_chunked[..., -1:]
same = (seq_chunked == seq_end).unsqueeze(3)
weight = weight * same.permute(0, 1, 3, 2)
w = weight.permute(0, 1, 3, 2).unsqueeze(-1).unsqueeze(-1)
contrib = w * B_heads.unsqueeze(-1) * x_chunked.unsqueeze(-2)
out = contrib.sum(dim=2)
return out.permute(0, 1, 2, 4, 3)
class SSDChunkStateFwdTest(_SSDChunkStateFwdTestWorkload, TestBase):
def ref_program(self, x, Bmat, dt, dA_cumsum, seq_idx):
return ssd_chunk_state_fwd_ref(x, Bmat, dt, dA_cumsum, self.n_groups, seq_idx=seq_idx)
@SSDChunkStateFwdFixture
def test_ssd_chunk_state_fwd(
batch, num_chunks, chunk_len, n_heads, d_head, d_state, n_groups, dtype, tune, has_seq_idx,
):
test = SSDChunkStateFwdTest(
batch, num_chunks, chunk_len, n_heads, d_head, d_state, n_groups, dtype, has_seq_idx,
)
op = SSDChunkStateFwdOp(
batch, num_chunks, chunk_len, n_heads, d_head, d_state, n_groups, dtype,
has_seq_idx=has_seq_idx, tune=tune,
)
inputs = test.gen_inputs()
atol = 1e-3 if dtype == torch.float16 else 1.6e-2
rtol = 1e-3
test.check(op, *inputs, atol=atol, rtol=rtol)
def ssd_state_passing_fwd_ref(
states: torch.Tensor,
dA_chunk_cumsum: torch.Tensor,
initial_states: torch.Tensor,
) -> tuple[torch.Tensor, torch.Tensor]:
"""PyTorch reference for the inter-chunk recurrent scan.
Matches mamba convention: out[:,c] = state *before* processing chunk c,
so out[:,0] = initial_states and final_states = state after chunk C-1.
"""
b, c, h, d = states.shape
# out[:,0] = s_{-1} = initial_states (state before chunk 0)
out = [initial_states.float().clone()]
s = initial_states.float()
for ci in range(c):
scale = torch.exp(dA_chunk_cumsum[:, :, ci]).unsqueeze(-1)
u = states[:, ci, :, :].float()
s = scale * s + u
if ci < c - 1:
out.append(s.clone())
return torch.stack(out, dim=1), s
class SSDStatePassingFwdTest(_SSDStatePassingFwdTestWorkload, TestBase):
def ref_program(self, states, dA_chunk_cumsum, initial_states):
return ssd_state_passing_fwd_ref(states, dA_chunk_cumsum, initial_states)
@SSDStatePassingFwdFixture
def test_ssd_state_passing_fwd(batch, num_chunks, n_heads, d_state, dtype, tune):
test = SSDStatePassingFwdTest(batch, num_chunks, n_heads, d_state, dtype)
op = SSDStatePassingFwdOp(batch, num_chunks, n_heads, d_state, dtype=dtype, tune=tune)
inputs = test.gen_inputs()
atol = 1e-3 if dtype == torch.float16 else 1.6e-2
rtol = 1e-3
test.check(op, *inputs, atol=atol, rtol=rtol)
def ssd_decode_ref(
A: torch.Tensor, # (H, P, N) float32
dt: torch.Tensor, # (B, H, P) float32
x: torch.Tensor, # (B, H, P) any dtype
B_in: torch.Tensor, # (B, G, N) any dtype
C_in: torch.Tensor, # (B, G, N) any dtype
state: torch.Tensor, # (B, H, P, N) float32 -- updated in-place
) -> torch.Tensor:
"""PyTorch reference for ssd_decode.
Matches the official Mamba-2 selective_state_update interface:
A: (nheads, headdim, d_state) — repeated from (nheads,) in mamba2.step()
dt: (batch, nheads, headdim) — repeated from (batch, nheads) in mamba2.step()
Returns:
y_out: (B, H, P) float32
Semantics:
g = h // (n_heads // n_groups)
dA[b, h, p, n] = exp(dt[b,h,p] * A[h,p,n])
state[b,h,p,n] <- dA[b,h,p,n] * state[b,h,p,n]
+ dt[b,h,p] * B_in[b,g,n] * x[b,h,p] (in-place)
y_out[b, h, p] = sum_n state[b, h, p, n] * C_in[b, g, n]
"""
B, H, P = dt.shape
G = B_in.shape[1]
heads_per_group = H // G
# Expand B/C from groups to heads: (B, H, N)
head_idx = torch.arange(H, device=B_in.device) // heads_per_group
B_heads = B_in.float()[:, head_idx, :] # (B, H, N)
C_heads = C_in.float()[:, head_idx, :] # (B, H, N)
# dA[b, h, p, n] = exp(dt[b,h,p] * A[h,p,n])
dA = torch.exp(
dt.float()[:, :, :, None] * A.float()[None, :, :, :]
) # (B, H, P, N)
# dBx[b, h, p, n] = dt[b,h,p] * B[b,h,n] * x[b,h,p]
dBx = (
dt.float()[:, :, :, None]
* x.float()[:, :, :, None]
* B_heads[:, :, None, :]
) # (B, H, P, N)
# Update state in-place
new_state = dA * state.float() + dBx
state.copy_(new_state)
# y_out[b, h, p] = sum_n state[b, h, p, n] * C[b, h, n]
y_out = torch.einsum("bhpn,bhn->bhp", state.float(), C_heads)
return y_out
class SSDDecodeTest(_SSDDecodeTestWorkload, TestBase):
def ref_program(self, A, dt, x, B_in, C_in, state):
return ssd_decode_ref(A, dt, x, B_in, C_in, state)
@SSDDecodeFixture
def test_ssd_decode(batch, n_heads, d_head, d_state, n_groups, dtype, tune):
test = SSDDecodeTest(batch, n_heads, d_head, d_state, n_groups, dtype)
op = SSDDecodeOp(batch, n_heads, d_head, d_state, n_groups, dtype, tune=tune)
A, dt, x, B_in, C_in, state = test.gen_inputs()
# Run reference on a clone of state so the two runs start from the same point.
state_ref = state.clone()
y_ref = test.ref_program(A, dt, x, B_in, C_in, state_ref)
# Run kernel; state is updated in-place.
y_op = op(A, dt, x, B_in, C_in, state)
atol = 1e-3
rtol = 1e-3
allclose_compare(y_op, y_ref, atol=atol, rtol=rtol)
allclose_compare(state, state_ref, atol=atol, rtol=rtol)
if __name__ == "__main__":
pytest.main([__file__, "-vvs"])