File size: 6,964 Bytes
ae9e4fe | 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 | import numpy as np
from mla.backend import xp
from mla.tensor import Tensor
from mla.gradcheck import gradcheck
from mla.model import (Config, TiedEmbedding, RoPE, RMSNorm, repeat_kv,
Attention, SwiGLU, Block, Model)
def _tiny_cfg():
return Config(vocab_size=7, d_model=4, n_layers=1, n_heads=2,
n_kv_heads=1, head_dim=2, swiglu_hidden=8, seq_len=3)
def test_embed_shape():
xp.random.seed(0)
emb = TiedEmbedding(_tiny_cfg())
ids = xp.asarray([[1, 3, 3], [0, 2, 5]])
out = emb.embed(ids)
assert out.shape == (2, 3, 4)
def test_project_shape():
xp.random.seed(0)
emb = TiedEmbedding(_tiny_cfg())
h = Tensor(xp.random.randn(2, 3, 4))
logits = emb.project(h)
assert logits.shape == (2, 3, 7)
def test_tied_weight_gradcheck():
xp.random.seed(0)
ids = xp.asarray([[1, 3, 3], [0, 2, 5]])
def f(w):
emb = w.gather(ids)
return emb.matmul(w.transpose())
w = Tensor(xp.random.randn(7, 4))
ok, rel = gradcheck(f, w)
assert ok, f"tied-embedding gradcheck failed, max_rel={rel}"
def test_tied_row_asymmetry():
xp.random.seed(0)
ids = xp.asarray([[2]])
def f(w):
emb = w.gather(ids)
return emb.matmul(w.transpose())
w = Tensor(xp.random.randn(3, 2))
f(w).backward()
g = np.asarray(w.grad)
assert np.abs(g[2]).sum() > np.abs(g[0]).sum()
def _rope_cfg():
return Config(vocab_size=7, d_model=8, n_layers=1, n_heads=2,
n_kv_heads=1, head_dim=4, swiglu_hidden=8, seq_len=8)
def test_rope_shape():
rope = RoPE(_rope_cfg())
x = Tensor(xp.random.randn(2, 2, 3, 4))
out = rope(x)
assert out.shape == (2, 2, 3, 4)
def test_rope_pos0_identity():
rope = RoPE(_rope_cfg())
x = Tensor(xp.random.randn(1, 1, 1, 4))
out = rope(x, offset=0)
assert np.allclose(np.asarray(out.data), np.asarray(x.data))
def test_rope_gradcheck():
xp.random.seed(0)
rope = RoPE(_rope_cfg())
def f(x):
return rope(x, offset=2)
x = Tensor(xp.random.randn(1, 2, 3, 4))
ok, rel = gradcheck(f, x)
assert ok, f"rope gradcheck failed, max_rel={rel}"
def test_rope_relative_position():
xp.random.seed(0)
rope = RoPE(_rope_cfg())
q = xp.random.randn(1, 1, 1, 4)
k = xp.random.randn(1, 1, 1, 4)
def score(m, n):
qm = rope(Tensor(q), offset=m)
kn = rope(Tensor(k), offset=n)
return float((qm.data * kn.data).sum())
assert abs(score(1, 4) - score(3, 6)) < 1e-9
assert abs(score(1, 4) - score(1, 5)) > 1e-6
def test_rmsnorm_shape():
norm = RMSNorm(8)
x = Tensor(xp.random.randn(2, 3, 8))
assert norm(x).shape == (2, 3, 8)
def test_rmsnorm_unit_rms():
norm = RMSNorm(8, eps=1e-12)
x = Tensor(xp.random.randn(4, 8))
y = np.asarray(norm(x).data)
rms = np.sqrt((y ** 2).mean(axis=-1))
assert np.allclose(rms, 1.0, atol=1e-5)
def test_rmsnorm_gradcheck_x():
xp.random.seed(0)
norm = RMSNorm(8)
def f(x):
return norm(x)
x = Tensor(xp.random.randn(2, 3, 8))
ok, rel = gradcheck(f, x)
assert ok, f"rmsnorm dx gradcheck failed, max_rel={rel}"
def test_rmsnorm_gradcheck_gamma():
xp.random.seed(0)
norm = RMSNorm(8)
x = Tensor(xp.random.randn(2, 3, 8))
def f(w):
norm.weight = w
return norm(x)
g = Tensor(xp.random.randn(8))
ok, rel = gradcheck(f, g)
assert ok, f"rmsnorm dgamma gradcheck failed, max_rel={rel}"
def _attn_cfg():
return Config(vocab_size=7, d_model=8, n_layers=1, n_heads=2,
n_kv_heads=1, head_dim=4, swiglu_hidden=8, seq_len=8)
def test_repeat_kv():
x = Tensor(xp.arange(2 * 1 * 2 * 3).reshape(2, 1, 2, 3).astype(float))
y = repeat_kv(x, 3)
assert y.shape == (2, 3, 2, 3)
yd = np.asarray(y.data)
assert np.allclose(yd[:, 0], yd[:, 1])
assert np.allclose(yd[:, 1], yd[:, 2])
def test_repeat_kv_gradcheck():
xp.random.seed(0)
def f(x):
return repeat_kv(x, 3)
x = Tensor(xp.random.randn(2, 1, 2, 3))
ok, rel = gradcheck(f, x)
assert ok, f"repeat_kv gradcheck failed, max_rel={rel}"
def test_attention_shape():
xp.random.seed(0)
cfg = _attn_cfg()
attn = Attention(cfg)
x = Tensor(xp.random.randn(2, 4, cfg.d_model))
assert attn(x).shape == (2, 4, cfg.d_model)
def test_attention_causal():
xp.random.seed(0)
cfg = _attn_cfg()
attn = Attention(cfg)
x = xp.random.randn(1, 4, cfg.d_model)
o1 = np.asarray(attn(Tensor(x)).data)
x2 = x.copy()
x2[0, 3] += 5.0
o2 = np.asarray(attn(Tensor(x2)).data)
assert np.allclose(o1[0, :3], o2[0, :3])
assert not np.allclose(o1[0, 3], o2[0, 3])
def test_attention_gradcheck_x():
xp.random.seed(0)
cfg = _attn_cfg()
attn = Attention(cfg)
def f(x):
return attn(x)
x = Tensor(xp.random.randn(1, 3, cfg.d_model))
ok, rel = gradcheck(f, x)
assert ok, f"attention dx gradcheck failed, max_rel={rel}"
def test_attention_gradcheck_wq():
xp.random.seed(0)
cfg = _attn_cfg()
attn = Attention(cfg)
x = Tensor(xp.random.randn(1, 3, cfg.d_model))
def f(w):
attn.wq = w
return attn(x)
w = Tensor(xp.random.randn(cfg.d_model, cfg.n_heads * cfg.head_dim))
ok, rel = gradcheck(f, w)
assert ok, f"attention dwq gradcheck failed, max_rel={rel}"
def test_swiglu_shape():
xp.random.seed(0)
cfg = _attn_cfg()
mlp = SwiGLU(cfg)
x = Tensor(xp.random.randn(2, 3, cfg.d_model))
assert mlp(x).shape == (2, 3, cfg.d_model)
def test_swiglu_gradcheck():
xp.random.seed(0)
cfg = _attn_cfg()
mlp = SwiGLU(cfg)
def f(x):
return mlp(x)
x = Tensor(xp.random.randn(2, 3, cfg.d_model))
ok, rel = gradcheck(f, x)
assert ok, f"swiglu gradcheck failed, max_rel={rel}"
def test_block_gradcheck():
xp.random.seed(0)
cfg = _attn_cfg()
block = Block(cfg)
def f(x):
return block(x)
x = Tensor(xp.random.randn(1, 3, cfg.d_model))
ok, rel = gradcheck(f, x)
assert ok, f"block gradcheck failed, max_rel={rel}"
def _tiny_model_cfg():
return Config(vocab_size=7, d_model=8, n_layers=2, n_heads=2,
n_kv_heads=1, head_dim=4, swiglu_hidden=8, seq_len=8)
def test_model_forward_shape():
xp.random.seed(0)
cfg = _tiny_model_cfg()
m = Model(cfg)
ids = xp.asarray([[1, 3, 5, 0], [2, 2, 4, 6]])
out = m(ids)
assert out.shape == (2, 4, cfg.vocab_size)
def test_model_gradcheck():
xp.random.seed(0)
cfg = _tiny_model_cfg()
m = Model(cfg)
ids = xp.asarray([[1, 3, 5], [2, 4, 6]])
def f(w):
m.embed.weight = w
return m(ids)
w = Tensor(xp.random.randn(cfg.vocab_size, cfg.d_model) * 0.1)
ok, rel = gradcheck(f, w)
assert ok, f"full-model gradcheck failed, max_rel={rel}"
def test_param_count():
m = Model(Config())
assert m.n_params() == 3_869_184
|