-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransformer_test.py
More file actions
553 lines (443 loc) · 16 KB
/
Copy pathtransformer_test.py
File metadata and controls
553 lines (443 loc) · 16 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
import pytest
import torch
from transformer import (
Decoder,
DecoderLayer,
Encoder,
EncoderLayer,
FeedForward,
MultiHeadAttention,
SinusoidalPositionalEncoding,
TrainablePositionalEncoding,
Transformer,
)
@pytest.mark.parametrize("q_len", [10, 20])
@pytest.mark.parametrize("kv_len", [10, 20])
@pytest.mark.parametrize("num_heads", [1, 4])
def test_mha_output_shape(q_len, kv_len, num_heads):
batch_size, d_model = 4, 64
mha = MultiHeadAttention(d_model=d_model, num_heads=num_heads)
mha.eval()
q = torch.randn(batch_size, q_len, d_model)
k = torch.randn(batch_size, kv_len, d_model)
v = torch.randn(batch_size, kv_len, d_model)
output = mha(q, k, v)
assert output.shape == (batch_size, q_len, d_model)
@pytest.mark.parametrize("d_key", [16, 32])
@pytest.mark.parametrize("d_value", [16, 32])
def test_mha_custom_qkv_dimensions_output_shape(d_key, d_value):
batch_size, d_model = 4, 64
q_len, kv_len = 10, 20
mha = MultiHeadAttention(d_model=d_model, num_heads=8, d_key=d_key, d_value=d_value)
mha.eval()
# Check that custom dimensions are set correctly
assert mha.d_qk == d_key
assert mha.d_v == d_value
q = torch.randn(batch_size, q_len, d_model)
k = torch.randn(batch_size, kv_len, d_model)
v = torch.randn(batch_size, kv_len, d_model)
output = mha(q, k, v)
# Check output shape
assert output.shape == (batch_size, q_len, d_model)
@pytest.mark.parametrize(
"mask",
[
torch.tensor([[False, False, True, True]] * 2, dtype=torch.bool),
torch.tensor([[0.0, 0.0, -torch.inf, -torch.inf]] * 2, dtype=torch.float32),
],
)
def test_mha_padding_mask(mask):
batch_size, seq_len, d_model = 2, 4, 64
num_heads = 8
mha = MultiHeadAttention(d_model=d_model, num_heads=num_heads)
mha.eval()
q = torch.randn(batch_size, seq_len, d_model)
k = torch.randn(batch_size, seq_len, d_model)
v = torch.randn(batch_size, seq_len, d_model)
y = mha(q, k, v, key_padding_mask=mask)
# Tweaking the masked part of the key shouldn't affect the output.
masked_k = k.clone()
masked_k[:, 2:, :] = 0.0
y_masked_k = mha(q, masked_k, v, key_padding_mask=mask)
assert torch.allclose(y, y_masked_k)
# Tweaking the masked part of the value shouldn't affect the output.
masked_v = v.clone()
masked_v[:, 2:, :] = 0.0
y_masked_v = mha(q, k, masked_v, key_padding_mask=mask)
assert torch.allclose(y, y_masked_v)
def test_mha_causal_attention_mask():
batch_size, seq_len, d_model = 4, 10, 64
num_heads = 8
mha = MultiHeadAttention(d_model=d_model, num_heads=num_heads)
mha.eval()
q = torch.randn(batch_size, seq_len, d_model)
k = torch.randn(batch_size, seq_len, d_model)
v = torch.randn(batch_size, seq_len, d_model)
y = mha(q, k, v, is_causal=True)
# Tweaking the second half of the key shouldn't affect the first half of the output.
masked_k = k.clone()
masked_k[:, -3:, :] = 0.0
y_masked_k = mha(q, masked_k, v, is_causal=True)
assert torch.allclose(y[:, :-3, :], y_masked_k[:, :-3, :])
assert not torch.allclose(y[:, -3:, :], y_masked_k[:, -3:, :])
# Tweaking the second half of the value shouldn't affect the first half of the output.
masked_v = v.clone()
masked_v[:, -3:, :] = 0.0
y_masked_v = mha(q, k, masked_v, is_causal=True)
assert torch.allclose(y[:, :-3, :], y_masked_v[:, :-3, :])
assert not torch.allclose(y[:, -3:, :], y_masked_v[:, -3:, :])
def test_mha_gradients():
batch_size, seq_len, d_model = 4, 10, 64
mha = MultiHeadAttention(d_model=d_model, num_heads=8)
query = torch.randn(batch_size, seq_len, d_model, requires_grad=True)
key = torch.randn(batch_size, seq_len, d_model, requires_grad=True)
value = torch.randn(batch_size, seq_len, d_model, requires_grad=True)
y = mha(query, key, value)
# Compute loss and gradients
loss = y.sum()
loss.backward()
# Check gradient shapes
assert query.grad.shape == query.shape
assert key.grad.shape == key.shape
assert value.grad.shape == value.shape
@pytest.mark.parametrize("is_causal", [False, True])
def test_mha_kv_cache_is_updated(is_causal):
batch_size, d_model = 4, 64
mha = MultiHeadAttention(d_model=d_model, num_heads=8, kv_cache_len=10)
mha.eval()
q = torch.randn(batch_size, 1, d_model)
k = torch.randn(batch_size, 1, d_model)
v = torch.randn(batch_size, 1, d_model)
# The first time step updates the first entry in the cache.
mha(q, k, v, is_causal=is_causal, cache_pos=0)
assert not torch.allclose(mha.kv_cache.k_cache[:, :, 0, :], torch.zeros(()))
assert not torch.allclose(mha.kv_cache.v_cache[:, :, 0, :], torch.zeros(()))
assert torch.allclose(mha.kv_cache.k_cache[:, :, 1:, :], torch.zeros(()))
assert torch.allclose(mha.kv_cache.v_cache[:, :, 1:, :], torch.zeros(()))
# The second time step updates the second entry only.
prev_k_cache = mha.kv_cache.k_cache.clone()
prev_v_cache = mha.kv_cache.v_cache.clone()
mha(q, k, v, is_causal=is_causal, cache_pos=1)
assert not torch.allclose(
mha.kv_cache.k_cache[:, :, 1, :], prev_k_cache[:, :, 1, :]
)
assert not torch.allclose(
mha.kv_cache.v_cache[:, :, 1, :], prev_v_cache[:, :, 1, :]
)
assert torch.allclose(mha.kv_cache.k_cache[:, :, 0, :], prev_k_cache[:, :, 0, :])
assert torch.allclose(mha.kv_cache.v_cache[:, :, 0, :], prev_v_cache[:, :, 0, :])
assert torch.allclose(mha.kv_cache.k_cache[:, :, 2:, :], prev_k_cache[:, :, 2:, :])
assert torch.allclose(mha.kv_cache.v_cache[:, :, 2:, :], prev_v_cache[:, :, 2:, :])
@pytest.mark.parametrize("is_causal", [False, True])
def test_mha_kv_cache_affects_output(is_causal):
batch_size, d_model = 4, 64
mha = MultiHeadAttention(d_model=d_model, num_heads=8, kv_cache_len=10)
mha.eval()
q = torch.randn(batch_size, 1, d_model)
k = torch.randn(batch_size, 1, d_model)
v = torch.randn(batch_size, 1, d_model)
mha.kv_cache.k_cache = torch.randn(
batch_size, mha.n_heads, mha.kv_cache.cache_len, mha.d_qk
)
mha.kv_cache.v_cache = torch.randn(
batch_size, mha.n_heads, mha.kv_cache.cache_len, mha.d_v
)
mha.kv_cache.mask_cache = torch.zeros(batch_size, mha.kv_cache.cache_len)
y0 = mha(q, k, v, is_causal=is_causal, cache_pos=1)
mha.kv_cache.k_cache[:, :, 0, :] = 0.0
mha.kv_cache.v_cache[:, :, 0, :] = 0.0
y1 = mha(q, k, v, is_causal=is_causal, cache_pos=1)
assert not torch.allclose(y0, y1)
@pytest.mark.parametrize(
"ctor", [SinusoidalPositionalEncoding, TrainablePositionalEncoding]
)
@pytest.mark.parametrize("seq_length", [1, 4, 8])
def test_positional_encoding_unbatched(ctor, seq_length):
layer = ctor(8, 16)
layer.eval()
x = torch.randn(seq_length, 16)
y = layer(x)
assert y.shape == x.shape
assert torch.norm(y - x) > 0
@pytest.mark.parametrize(
"ctor", [SinusoidalPositionalEncoding, TrainablePositionalEncoding]
)
@pytest.mark.parametrize("seq_length", [1, 4, 8])
def test_positional_encoding_batched(ctor, seq_length):
layer = ctor(8, 16)
layer.eval()
x = torch.randn(4, seq_length, 16)
y = layer(x)
assert y.shape == x.shape
encoding = y - x
print(encoding)
assert torch.norm(encoding) > 0
assert torch.allclose(encoding[0, ...], encoding[1, ...], atol=1e-6)
def test_feed_forward_output_shape():
x = torch.rand((16, 102, 512)) # (batch, length, d_model)
ff = FeedForward(
d_model=512,
d_ff=2048,
)
output = ff(x)
assert output.shape == x.shape
def test_encoder_layer_output_shape():
x = torch.rand((16, 143, 512)) # (batch, length, d_model)
layer = EncoderLayer(
d_model=512,
d_ff=2048,
num_heads=8,
)
output = layer(x)
assert output.shape == x.shape
def test_encoder_output_shape():
x = torch.randint(0, 1000, (16, 143)) # (batch, length)
encoder = Encoder(
vocab_size=1000,
d_model=512,
d_ff=2048,
num_heads=8,
num_layers=6,
max_seq_len=256,
)
output = encoder(x)
assert output.shape == (16, 143, 512)
def test_decoder_layer_output_shape():
x = torch.rand((16, 102, 512)) # (batch, length, d_model)
ctx = torch.rand((16, 143, 512)) # (batch, length, d_model)
layer = DecoderLayer(
d_model=512,
d_ff=2048,
num_heads=8,
)
output = layer(x, ctx)
assert output.shape == x.shape
def test_decoder_output_shape():
x = torch.randint(0, 1000, (16, 102)) # (batch, length)
ctx = torch.rand((16, 143, 512)) # (batch, length, d_model)
decoder = Decoder(
vocab_size=1000,
d_model=512,
d_ff=2048,
num_heads=8,
num_layers=6,
max_seq_len=256,
)
output = decoder(x, ctx)
assert output.shape == (16, 102, 512)
def test_transformer_output_shape():
x = torch.randint(0, 1000, (16, 102)) # (batch, length)
ctx = torch.randint(0, 1000, (16, 143)) # (batch, length)
transformer = Transformer(
vocab_size=1000,
d_model=512,
d_ff=2048,
d_out=1000,
num_heads=8,
num_layers=6,
max_seq_len=256,
)
output = transformer(x, ctx)
assert output.shape == (16, 102, 1000)
def test_transformer_output_shape_no_grad():
x = torch.randint(0, 1000, (16, 102)) # (batch, length)
ctx = torch.randint(0, 1000, (16, 143)) # (batch, length)
transformer = Transformer(
vocab_size=1000,
d_model=512,
d_ff=2048,
d_out=1000,
num_heads=8,
num_layers=6,
max_seq_len=256,
)
with torch.no_grad():
output = transformer(x, ctx)
assert output.shape == (16, 102, 1000)
@pytest.fixture
def padding_test_data():
"""Fixture providing test data for padding mask tests."""
x_tokens = torch.randint(0, 1000, (16, 102)) # (batch, length)
x = torch.rand((16, 102, 512)) # (batch, length, d_model)
mask = torch.ones((16, 102), dtype=torch.int)
mask[:, 80:] = 0 # Last 22 tokens are padding
ctx_tokens = torch.randint(0, 1000, (16, 143)) # (batch, ctx_length)
ctx = torch.rand((16, 143, 512)) # (batch, ctx_length, d_model)
ctx_mask = torch.ones((16, 143), dtype=torch.int)
ctx_mask[:, 120:] = 0 # Last 23 context tokens are padding
return {
"x_tokens": x_tokens,
"x": x,
"mask": mask,
"ctx_tokens": ctx_tokens,
"ctx": ctx,
"ctx_mask": ctx_mask,
}
def test_encoder_layer_with_padding_output_shape(padding_test_data):
data = padding_test_data
layer = EncoderLayer(
d_model=512,
d_ff=2048,
num_heads=8,
)
output = layer(data["x"], data["mask"])
assert output.shape == data["x"].shape
def test_encoder_with_padding_output_shape(padding_test_data):
data = padding_test_data
encoder = Encoder(
vocab_size=1000,
d_model=512,
d_ff=2048,
num_heads=8,
num_layers=6,
max_seq_len=256,
)
output = encoder(data["x_tokens"], data["mask"])
assert output.shape == (16, 102, 512)
def test_decoder_layer_with_padding_output_shape(padding_test_data):
data = padding_test_data
layer = DecoderLayer(
d_model=512,
d_ff=2048,
num_heads=8,
)
output = layer(data["x"], data["ctx"], data["mask"], data["ctx_mask"])
assert output.shape == data["x"].shape
def test_decoder_with_padding_output_shape(padding_test_data):
data = padding_test_data
decoder = Decoder(
vocab_size=1000,
d_model=512,
d_ff=2048,
num_heads=8,
num_layers=6,
max_seq_len=256,
)
output = decoder(data["x_tokens"], data["ctx"], data["mask"], data["ctx_mask"])
assert output.shape == (16, 102, 512)
def test_transformer_with_padding_output_shape(padding_test_data):
data = padding_test_data
transformer = Transformer(
vocab_size=1000,
d_model=512,
d_ff=2048,
d_out=1000,
num_heads=8,
num_layers=6,
max_seq_len=256,
)
output = transformer(
data["x_tokens"],
data["ctx_tokens"],
mask=data["mask"],
ctx_mask=data["ctx_mask"],
)
assert output.shape == (16, 102, 1000)
def test_encoder_layer_padding_mask_works():
# Construct x1, x2 such that they are the same in the first few positions.
x1 = torch.rand((2, 8, 4)) # (batch, length, d_model)
x1[:, 6:, :] = 0.0
x2 = x1.clone()
x2[:, 6:, :] = 1.0
# The remaining positions are marked as padding.
mask = torch.ones((2, 8), dtype=torch.int)
mask[:, 6:] = 0
layer = EncoderLayer(
d_model=4,
d_ff=16,
num_heads=2,
)
# Disable dropout, etc.
layer.eval()
y1 = layer(x1, mask)
y2 = layer(x2, mask)
# The output for non-padded positions should be the same
torch.testing.assert_close(y1[:, :6, :], y2[:, :6, :])
def test_decoder_layer_x_padding_mask_works():
# Construct x1, x2 such that they are the same in the first few positions.
x1 = torch.rand((2, 8, 4)) # (batch, length, d_model)
x1[:, 6:, :] = 0.0
x2 = x1.clone()
x2[:, 6:, :] = 1.0
# The remaining positions are marked as padding.
mask = torch.ones((2, 8), dtype=torch.int)
mask[:, 6:] = 0
ctx = torch.rand((2, 8, 4)) # (batch, ctx_length, d_model)
layer = DecoderLayer(
d_model=4,
d_ff=16,
num_heads=2,
)
# Disable dropout, etc.
layer.eval()
y1 = layer(x1, ctx, mask=mask)
y2 = layer(x2, ctx, mask=mask)
# The output for non-padded positions should be the same.
torch.testing.assert_close(y1[:, :6, :], y2[:, :6, :])
def test_decoder_layer_ctx_padding_mask_works():
x = torch.rand((2, 8, 4)) # (batch, length, d_model)
# Construct ctx1, ctx2 such that they are the same in the first few positions.
ctx1 = torch.rand((2, 8, 4)) # (batch, ctx_length, d_model)
ctx1[:, 6:, :] = 0.0
ctx2 = ctx1.clone()
ctx2[:, 6:, :] = 1.0
# Mark the remaining positions as padding.
ctx_mask = torch.ones((2, 8), dtype=torch.int)
ctx_mask[:, 6:] = 0
layer = DecoderLayer(
d_model=4,
d_ff=16,
num_heads=2,
)
# Disable dropout, etc.
layer.eval()
y1 = layer(x, ctx1, ctx_mask=ctx_mask)
y2 = layer(x, ctx2, ctx_mask=ctx_mask)
# The output should be the same since context padding shouldn't affect non-padded positions.
torch.testing.assert_close(y1, y2)
def test_decoder_layer_causal_mask_works():
# Construct x1, x2 such that they are the same in the first few positions.
x1 = torch.rand((2, 8, 4)) # (batch, length, d_model)
x1[:, 4:, :] = 1.0
x2 = x1.clone()
x2[:, 4:, :] = 0.0
ctx = torch.rand((2, 8, 4)) # (batch, ctx_length, d_model)
layer = DecoderLayer(
d_model=4,
d_ff=16,
num_heads=2,
)
# Disable dropout, etc.
layer.eval()
# Test that causal masking works: changing later positions shouldn't affect earlier outputs
y1 = layer(x1, ctx)
y2 = layer(x2, ctx)
# The first 4 positions should be identical since causal masking prevents later positions
# from affecting earlier ones.
torch.testing.assert_close(y1[:, :4, :], y2[:, :4, :])
def test_inference():
vocab_size, max_len = 1000, 128
ctx = torch.randint(0, vocab_size, (2, max_len)) # (batch, length)
# Last 50 context tokens are padding.
ctx_mask = torch.ones_like(ctx)
ctx_mask[:, 78:] = 0
transformer = Transformer(
vocab_size=vocab_size,
d_model=512,
d_ff=2048,
d_out=1000,
num_heads=8,
num_layers=6,
max_seq_len=max_len,
)
transformer.eval()
# First token.
x0 = torch.randint(0, vocab_size, (2, 1))
# Accumulates input tokens. Shape (batch, length) as length increases.
prev_x = x0
for i in range(max_len):
logits = transformer(prev_x, ctx, ctx_mask=ctx_mask)
y = torch.argmax(logits, dim=-1)
assert y.shape == (2, i + 1)
assert torch.all(y[:, :-1] == prev_x[:, 1:])
prev_x = torch.cat([prev_x, y[:, -1:]], dim=-1)