forked from tile-ai/TileOPs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_engram.py
More file actions
309 lines (242 loc) · 10.9 KB
/
Copy pathtest_engram.py
File metadata and controls
309 lines (242 loc) · 10.9 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
import pytest
import torch
import torch.nn.functional as F
from tests.test_base import FixtureBase, TestBase
from tileops.ops.engram import EngramGateConvBwdOp, EngramGateConvFwdOp
from tileops.ops.engram_decode import EngramDecodeOp
from tileops.utils import get_backend_name
from workloads.engram import (
CONV_KERNEL_SIZE,
)
from workloads.engram import (
EngramDecodeTest as _EngramDecodeTestWorkload,
)
from workloads.engram import (
EngramGateConvBwdTest as _EngramGateConvBwdTestWorkload,
)
from workloads.engram import (
EngramGateConvFwdTest as _EngramGateConvFwdTestWorkload,
)
DEVICE = get_backend_name()
def _rmsnorm(x, w, eps=1e-6):
"""Returns (normed, rrms)."""
x_f = x.float()
rrms = (x_f ** 2).mean(dim=-1, keepdim=True).add(eps).rsqrt()
normed = x_f * rrms * w.float()
return normed, rrms.squeeze(-1)
def engram_gate_conv_fwd_torch(H, k, v, rms_w_h, rms_w_v, conv_w, eps=1e-6):
"""PyTorch reference for Engram GateConv forward."""
M, T, d = H.shape
h_norm, rrms_h = _rmsnorm(H, rms_w_h, eps)
k_norm, rrms_k = _rmsnorm(k, rms_w_h, eps)
dot = (h_norm * k_norm).sum(dim=-1, keepdim=True)
alpha = torch.sigmoid(dot / (d ** 0.5))
v_hat = alpha * v.float()
v_hat_norm, rrms_v = _rmsnorm(v_hat.to(H.dtype), rms_w_v, eps)
v_perm = v_hat_norm.float().permute(0, 2, 1)
v_padded = F.pad(v_perm, (CONV_KERNEL_SIZE - 1, 0))
conv_w_expanded = conv_w.float().T.unsqueeze(1)
conv_out = F.conv1d(v_padded, conv_w_expanded, groups=d).permute(0, 2, 1)
Y = F.silu(conv_out) + v_hat.float()
return (
Y.to(H.dtype),
v_hat.to(H.dtype),
alpha.squeeze(-1).float(),
rrms_h.float(),
rrms_k.float(),
rrms_v.float(),
)
class EngramGateConvFwdTest(_EngramGateConvFwdTestWorkload, TestBase):
def ref_program(self, H, k, v, rms_w_h, rms_w_v, conv_w):
return engram_gate_conv_fwd_torch(H, k, v, rms_w_h, rms_w_v, conv_w, self.eps)
class EngramGateConvFwdFixture(FixtureBase):
PARAMS = [
("M, seq_len, d, dtype, tune", [
pytest.param(1, 32, 256, torch.float16, False, marks=pytest.mark.smoke),
pytest.param(1, 32, 256, torch.bfloat16, False, marks=pytest.mark.smoke),
pytest.param(2, 64, 512, torch.float16, False, marks=pytest.mark.full),
pytest.param(4, 128, 512, torch.float16, False, marks=pytest.mark.full),
pytest.param(2, 16, 256, torch.bfloat16, False, marks=pytest.mark.full),
]),
]
@EngramGateConvFwdFixture
def test_engram_gate_conv_fwd(M, seq_len, d, dtype, tune):
test = EngramGateConvFwdTest(M, seq_len, d, dtype)
op = EngramGateConvFwdOp(M, seq_len, d, dtype, tune=tune)
inputs = test.gen_inputs()
atol = 1e-1 if dtype == torch.float16 else 2e-1
rtol = 1e-1
test.check(op, *inputs, atol=atol, rtol=rtol)
def ref_engram_gate_conv_bwd(dY, H, k, v, rms_w_h, rms_w_v, conv_w,
vhat, alpha, rrms_h, rrms_k, rrms_v, eps=1e-6):
"""PyTorch reference backward via autograd."""
M, T, d = H.shape
H_ag = H.float().detach().requires_grad_(True)
k_ag = k.float().detach().requires_grad_(True)
v_ag = v.float().detach().requires_grad_(True)
w_h_ag = rms_w_h.float().detach().requires_grad_(True)
w_v_ag = rms_w_v.float().detach().requires_grad_(True)
cw_ag = conv_w.float().detach().requires_grad_(True)
def _rmsnorm(x, w):
return x * (x ** 2).mean(dim=-1, keepdim=True).add(eps).rsqrt() * w
h_norm = _rmsnorm(H_ag, w_h_ag)
k_norm = _rmsnorm(k_ag, w_h_ag)
dot = (h_norm * k_norm).sum(dim=-1, keepdim=True)
alpha_ag = torch.sigmoid(dot / (d ** 0.5))
v_hat_ag = alpha_ag * v_ag
v_hat_norm = _rmsnorm(v_hat_ag, w_v_ag)
v_perm = v_hat_norm.permute(0, 2, 1)
v_padded = F.pad(v_perm, (CONV_KERNEL_SIZE - 1, 0))
cw_expanded = cw_ag.T.unsqueeze(1)
conv_out = F.conv1d(v_padded, cw_expanded, groups=d).permute(0, 2, 1)
Y_ag = F.silu(conv_out) + v_hat_ag
Y_ag.backward(dY.float())
return (
H_ag.grad.to(H.dtype),
k_ag.grad.to(H.dtype),
v_ag.grad.to(H.dtype),
w_h_ag.grad,
w_v_ag.grad,
cw_ag.grad,
)
class EngramGateConvBwdTest(_EngramGateConvBwdTestWorkload, TestBase):
def ref_program(self, dY, H, k, v, rms_w_h, rms_w_v, conv_w,
vhat, alpha, rrms_h, rrms_k, rrms_v):
return ref_engram_gate_conv_bwd(
dY, H, k, v, rms_w_h, rms_w_v, conv_w,
vhat, alpha, rrms_h, rrms_k, rrms_v, self.eps,
)
def _ref_rmsnorm(x, w, eps=1e-6):
x_f = x.float()
rrms = (x_f ** 2).mean(dim=-1, keepdim=True).add(eps).rsqrt()
normed = x_f * rrms * w.float()
return normed, rrms.squeeze(-1)
class EngramGateConvBwdFixture(FixtureBase):
PARAMS = [
("M, seq_len, d, dtype, tune", [
pytest.param(1, 32, 256, torch.float16, False, marks=pytest.mark.smoke),
pytest.param(1, 32, 256, torch.bfloat16, False, marks=pytest.mark.smoke),
pytest.param(2, 64, 512, torch.float16, False, marks=pytest.mark.full),
pytest.param(4, 128, 512, torch.float16, False, marks=pytest.mark.full),
pytest.param(2, 16, 256, torch.bfloat16, False, marks=pytest.mark.full),
]),
]
@EngramGateConvBwdFixture
def test_engram_gate_conv_bwd(M, seq_len, d, dtype, tune):
test = EngramGateConvBwdTest(M, seq_len, d, dtype)
op = EngramGateConvBwdOp(M, seq_len, d, dtype, tune=tune)
inputs = test.gen_inputs()
atol = 2e-1 if dtype == torch.float16 else 3e-1
rtol = 2e-1
test.check(op, *inputs, atol=atol, rtol=rtol)
def _rmsnorm_decode(x, w, eps=1e-6):
x_f = x.float()
rrms = (x_f ** 2).mean(dim=-1, keepdim=True).add(eps).rsqrt()
return (x_f * rrms * w.float()), rrms
def engram_decode_step_torch(
e_t, h_t, conv_state, W_K, W_V, rms_w_h, rms_w_v, conv_w,
max_conv_len, dilation, eps=1e-6,
):
"""PyTorch reference for a single decode step with dilated causal conv."""
B, d = h_t.shape
w = conv_w.shape[0]
L = conv_state.shape[1]
k = e_t.float() @ W_K.float()
v = e_t.float() @ W_V.float()
h_norm, _ = _rmsnorm_decode(h_t.unsqueeze(1), rms_w_h)
k_norm, _ = _rmsnorm_decode(k.unsqueeze(1).to(h_t.dtype), rms_w_h)
h_norm = h_norm.squeeze(1)
k_norm = k_norm.squeeze(1)
dot = (h_norm * k_norm).sum(dim=-1, keepdim=True)
alpha = torch.sigmoid(dot / (d ** 0.5))
v_hat = alpha * v
v_hat_norm, _ = _rmsnorm_decode(v_hat.unsqueeze(1).to(h_t.dtype), rms_w_v)
v_hat_norm = v_hat_norm.squeeze(1)
if max_conv_len > L:
padded_state = F.pad(conv_state.float(), (0, 0, max_conv_len - L, 0))
else:
padded_state = conv_state.float()
conv_out = torch.zeros(B, d, device=h_t.device)
for p in range(w - 1):
state_idx = max_conv_len - (w - 1 - p) * dilation
if 0 <= state_idx < max_conv_len:
conv_out += conv_w[p].float().unsqueeze(0) * padded_state[:, state_idx, :]
conv_out += conv_w[w - 1].float().unsqueeze(0) * v_hat_norm
if max_conv_len > L:
new_conv_state = torch.cat([
conv_state,
v_hat_norm.unsqueeze(1).to(conv_state.dtype),
], dim=1)
else:
new_conv_state = torch.cat([
conv_state[:, 1:, :],
v_hat_norm.unsqueeze(1).to(conv_state.dtype),
], dim=1)
y_t = F.silu(conv_out) + v_hat
return y_t.to(h_t.dtype), new_conv_state
class EngramDecodeTest(_EngramDecodeTestWorkload, TestBase):
def ref_program(self, e_t, h_t, conv_state, W_K, W_V, rms_w_h, rms_w_v, conv_w):
y_ref, state_ref = engram_decode_step_torch(
e_t, h_t, conv_state, W_K, W_V, rms_w_h, rms_w_v, conv_w,
self.max_conv_len, self.dilation, self.eps,
)
return y_ref, state_ref
class EngramDecodeFixture(FixtureBase):
PARAMS = [
# (batch, d_mem, d, max_conv_len, conv_kernel_size, dilation, dtype, tune)
("batch, d_mem, d, max_conv_len, conv_kernel_size, dilation, dtype, tune", [
pytest.param(1, 512, 256, 12, 4, 3, torch.float16, False, marks=pytest.mark.smoke),
pytest.param(1, 512, 256, 12, 4, 3, torch.bfloat16, False, marks=pytest.mark.smoke),
pytest.param(4, 1024, 512, 20, 4, 5, torch.float16, False, marks=pytest.mark.full),
pytest.param(8, 2048, 512, 32, 4, 5, torch.float16, False, marks=pytest.mark.full),
pytest.param(8, 512, 256, 18, 4, 3, torch.bfloat16, False, marks=pytest.mark.full),
]),
]
@EngramDecodeFixture
def test_engram_decode(batch, d_mem, d, max_conv_len, conv_kernel_size, dilation, dtype, tune):
test = EngramDecodeTest(batch, d_mem, d, max_conv_len, conv_kernel_size, dilation, dtype)
op = EngramDecodeOp(
batch, d_mem, d, max_conv_len, conv_kernel_size, dilation, dtype, tune=tune,
)
inputs = test.gen_inputs()
atol = 5e-2 if dtype == torch.float16 else 1e-1
rtol = 5e-2
test.check(op, *inputs, atol=atol, rtol=rtol)
@pytest.mark.smoke
def test_engram_decode_multi_step():
"""Verify multi-step decode with growing conv_state and dilated conv."""
B, d_mem, d = 2, 256, 256
conv_kernel_size = 4
dilation = 3
max_conv_len = dilation * (conv_kernel_size - 1) # = 9, minimum required
dtype = torch.float16
eps = 1e-6
torch.manual_seed(123)
W_K = torch.randn(d_mem, d, dtype=dtype, device=DEVICE) * 0.02
W_V = torch.randn(d_mem, d, dtype=dtype, device=DEVICE) * 0.02
rms_w_h = torch.ones(d, dtype=dtype, device=DEVICE)
rms_w_v = torch.ones(d, dtype=dtype, device=DEVICE)
conv_w = torch.randn(conv_kernel_size, d, dtype=dtype, device=DEVICE) * 0.02
op = EngramDecodeOp(B, d_mem, d, max_conv_len, conv_kernel_size, dilation, dtype)
# Start with empty conv_state (like empty KV cache)
conv_state = torch.zeros(B, 0, d, dtype=dtype, device=DEVICE)
conv_state_ref = conv_state.clone()
num_steps = max_conv_len + 8 # go past growing phase into steady state
for step in range(num_steps):
e_t = torch.randn(B, d_mem, dtype=dtype, device=DEVICE) * 0.1
h_t = torch.randn(B, d, dtype=dtype, device=DEVICE)
y_op, conv_state = op(e_t, h_t, conv_state, W_K, W_V, rms_w_h, rms_w_v, conv_w)
y_ref, conv_state_ref = engram_decode_step_torch(
e_t, h_t, conv_state_ref, W_K, W_V, rms_w_h, rms_w_v, conv_w,
max_conv_len, dilation, eps,
)
y_err = (y_op.float() - y_ref.float()).abs().max().item()
# Compare valid portion of conv_state
ref_len = conv_state_ref.shape[1]
op_state_valid = conv_state[:, -ref_len:, :]
s_err = (op_state_valid.float() - conv_state_ref.float()).abs().max().item()
assert y_err < 0.1, f"Step {step}: y max_err={y_err:.6f}"
assert s_err < 0.05, f"Step {step}: state max_err={s_err:.6f}"
print(f"Multi-step decode test passed ({num_steps} steps, w={conv_kernel_size}, δ={dilation}).")
if __name__ == "__main__":
pytest.main([__file__, "-vvs"])