-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathperceiver_ar_pytorch_full.py
450 lines (326 loc) · 14.5 KB
/
perceiver_ar_pytorch_full.py
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
#===================================================================================================================
# Perceiver-AR Trasformer Module
# With useful modifications
#
# Version 1.0
#
# Original source code courtesy of lucidrains
# https://github.com/lucidrains/perceiver-ar-pytorch
#
# Original source code retrieved on 10/04/2022
#
# Project Los Angeles
# Tegridy Code 2023
#===================================================================================================================
# Critical dependencies
#
# !pip install torch
# !pip install einops
#===================================================================================================================
import torch
import torch.nn.functional as F
from torch import nn, einsum
from einops import rearrange, repeat
#===================================================================================================================
# helper functions
def exists(val):
return val is not None
# feedforward
def FeedForward(dim, mult = 4, dropout = 0.):
hidden_dim = int(dim * mult)
return nn.Sequential(
nn.LayerNorm(dim),
nn.Linear(dim, hidden_dim, bias = False),
nn.GELU(),
nn.Dropout(dropout),
nn.Linear(hidden_dim, dim, bias = False)
)
# rotary positional embedding
# https://arxiv.org/abs/2104.09864
class RotaryEmbedding(nn.Module):
def __init__(self, dim):
super().__init__()
inv_freq = 1.0 / (10000 ** (torch.arange(0, dim, 2).float() / dim))
self.register_buffer("inv_freq", inv_freq)
def forward(self, max_seq_len, *, device):
seq = torch.arange(max_seq_len, device = device, dtype = self.inv_freq.dtype)
freqs = einsum("i , j -> i j", seq, self.inv_freq)
return torch.cat((freqs, freqs), dim = -1)
def rotate_half(x):
x = rearrange(x, "... (j d) -> ... j d", j = 2)
x1, x2 = x.unbind(dim = -2)
return torch.cat((-x2, x1), dim = -1)
def apply_rotary_pos_emb(pos, t):
seq_len, rotate_dim = t.shape[-2], pos.shape[-1]
pos = pos[..., -seq_len:, :]
t, t_pass = t[..., :rotate_dim], t[..., rotate_dim:]
t = (t * pos.cos()) + (rotate_half(t) * pos.sin())
return torch.cat((t, t_pass), dim = -1)
# attention
class CausalAttention(nn.Module):
def __init__(
self,
*,
dim,
dim_head = 64,
heads = 8,
dropout = 0.
):
super().__init__()
self.scale = dim_head ** -0.5
self.heads = heads
inner_dim = heads * dim_head
self.norm = nn.LayerNorm(dim)
self.dropout = nn.Dropout(dropout)
self.to_qkv = nn.Linear(dim, inner_dim * 3, bias = False)
self.to_out = nn.Linear(inner_dim, dim, bias = False)
def forward(self, x, rotary_pos_emb = None):
x = self.norm(x)
q, k, v = self.to_qkv(x).chunk(3, dim = -1)
q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h = self.heads), (q, k, v))
q = q * self.scale
if exists(rotary_pos_emb):
q = apply_rotary_pos_emb(rotary_pos_emb, q)
k = apply_rotary_pos_emb(rotary_pos_emb, k)
sim = einsum('b h i d, b h j d -> b h i j', q, k)
i, j = sim.shape[-2:]
causal_mask = torch.ones((i, j), device = x.device, dtype = torch.bool).triu(j - i + 1)
sim = sim.masked_fill(causal_mask, -torch.finfo(sim.dtype).max)
attn = sim.softmax(dim = -1)
attn = self.dropout(attn)
out = einsum('b h i j, b h j d -> b h i d', attn, v)
out = rearrange(out, 'b h n d -> b n (h d)')
return self.to_out(out)
class CausalPrefixAttention(nn.Module):
def __init__(
self,
*,
dim,
dim_head = 64,
heads = 8,
max_heads_process = 2,
dropout = 0.,
cross_attn_dropout = 0.
):
super().__init__()
self.scale = dim_head ** -0.5
self.heads = heads
self.max_heads_process = max_heads_process
inner_dim = heads * dim_head
self.norm = nn.LayerNorm(dim)
self.context_norm = nn.LayerNorm(dim)
self.dropout = nn.Dropout(dropout)
self.cross_attn_dropout = cross_attn_dropout # they drop out a percentage of the prefix during training, shown to help prevent overfitting
self.to_q = nn.Linear(dim, inner_dim, bias = False)
self.to_kv = nn.Linear(dim, inner_dim * 2, bias = False)
self.to_out = nn.Linear(inner_dim, dim)
def forward(self, x, context, context_mask = None, rotary_pos_emb = None):
batch, context_len, device = x.shape[0], context.shape[-2], x.device
q_rotary_pos_emb = rotary_pos_emb
k_rotary_pos_emb = rotary_pos_emb
# take care of cross attention dropout
if self.training and self.cross_attn_dropout > 0.:
rand = torch.zeros((batch, context_len), device = device).uniform_()
keep_context_len = context_len - int(context_len * self.cross_attn_dropout)
keep_indices = rand.topk(keep_context_len, dim = -1).indices
keep_mask = torch.zeros_like(rand).scatter_(1, keep_indices, 1).bool()
context = rearrange(context[keep_mask], '(b n) d -> b n d', b = batch)
if exists(context_mask):
context_mask = rearrange(context_mask[keep_mask], '(b n) -> b n', b = batch)
# operate on rotary position embeddings for keys
k_rotary_pos_emb = repeat(k_rotary_pos_emb, '... -> b ...', b = batch)
k_rotary_pos_emb_context, k_rotary_pos_emb_seq = k_rotary_pos_emb[:, :context_len], k_rotary_pos_emb[:, context_len:]
k_rotary_pos_emb_context = rearrange(k_rotary_pos_emb_context[keep_mask], '(b n) d -> b n d', b = batch)
k_rotary_pos_emb = torch.cat((k_rotary_pos_emb_context, k_rotary_pos_emb_seq), dim = 1)
k_rotary_pos_emb = rearrange(k_rotary_pos_emb, 'b n d -> b 1 n d')
# normalization
x = self.norm(x)
context = self.context_norm(context)
# derive queries, keys, values
q = self.to_q(x)
k_input, v_input = self.to_kv(x).chunk(2, dim = -1)
k_context, v_context = self.to_kv(context).chunk(2, dim = -1)
k = torch.cat((k_context, k_input), dim = 1)
v = torch.cat((v_context, v_input), dim = 1)
q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h = self.heads), (q, k, v))
q = q * self.scale
# rotate queries and keys with rotary embeddings
if exists(rotary_pos_emb):
q = apply_rotary_pos_emb(q_rotary_pos_emb, q)
k = apply_rotary_pos_emb(k_rotary_pos_emb, k)
# take care of masking
i, j = q.shape[-2], k.shape[-2]
mask_value = -torch.finfo(q.dtype).max
if exists(context_mask):
mask_len = context_mask.shape[-1]
context_mask = F.pad(context_mask, (0, max(j - mask_len, 0)), value = True)
context_mask = rearrange(context_mask, 'b j -> b 1 1 j')
causal_mask = torch.ones((i, j), device = x.device, dtype = torch.bool).triu(j - i + 1)
# process in chunks of heads
out = []
max_heads = self.max_heads_process
for q_chunk, k_chunk, v_chunk in zip(q.split(max_heads, dim = 1), k.split(max_heads, dim = 1), v.split(max_heads, dim = 1)):
sim = einsum('b h i d, b h j d -> b h i j', q_chunk, k_chunk)
if exists(context_mask):
sim = sim.masked_fill(~context_mask, mask_value)
sim = sim.masked_fill(causal_mask, mask_value)
attn = sim.softmax(dim = -1)
attn = self.dropout(attn)
out_chunk = einsum('b h i j, b h j d -> b h i d', attn, v_chunk)
out.append(out_chunk)
# concat all the heads together
out = torch.cat(out, dim = 1)
# merge heads and then combine with linear
out = rearrange(out, 'b h n d -> b n (h d)')
return self.to_out(out)
class PerceiverAR(nn.Module):
def __init__(
self,
*,
num_tokens,
dim,
depth,
max_seq_len,
cross_attn_seq_len,
dim_head = 64,
heads = 8,
dropout = 0.,
cross_attn_dropout = 0.,
ff_mult = 4,
perceive_depth = 1,
perceive_max_heads_process = 2 # processes the heads in the perceiver layer in chunks to lower peak memory, in the case the prefix is really long
):
super().__init__()
assert max_seq_len > cross_attn_seq_len, 'max_seq_len must be greater than cross_attn_seq_len, the length of the sequence for which to cross attend to "perceiver" style'
self.max_seq_len = max_seq_len
self.cross_attn_seq_len = cross_attn_seq_len
self.token_emb = nn.Embedding(num_tokens, dim)
self.pos_emb = nn.Embedding(max_seq_len, dim)
self.rotary_pos_emb = RotaryEmbedding(dim = max(32, dim_head // 2))
self.perceive_layers = nn.ModuleList([])
self.token_pad = num_tokens
for _ in range(perceive_depth):
self.perceive_layers.append(nn.ModuleList([
CausalPrefixAttention(dim = dim, dim_head = dim_head, heads = heads, max_heads_process = perceive_max_heads_process, dropout = dropout, cross_attn_dropout = cross_attn_dropout),
FeedForward(dim, mult = ff_mult, dropout = dropout)
]))
self.layers = nn.ModuleList([])
for _ in range(depth):
self.layers.append(nn.ModuleList([
CausalAttention(dim = dim, dim_head = dim_head, heads = heads),
FeedForward(dim, mult = ff_mult, dropout = dropout),
]))
self.to_logits = nn.Linear(dim, num_tokens, bias = False)
def compute_accuracy(self, logits, labels):
out = torch.argmax(logits, dim=-1)
out = out.flatten()
labels = labels.flatten()
mask = (labels != self.token_pad)
out = out[mask]
labels = labels[mask]
num_right = (out == labels)
num_right = torch.sum(num_right).type(torch.float32)
acc = num_right / len(labels)
return acc
def forward(
self,
x,
prefix_mask = None,
labels = None
):
seq_len, device = x.shape[1], x.device
assert self.cross_attn_seq_len < seq_len <= self.max_seq_len
x = self.token_emb(x)
x = x + self.pos_emb(torch.arange(seq_len, device = device))
# rotary positional embedding
rotary_pos_emb = self.rotary_pos_emb(seq_len, device = device)
# divide into prefix to cross attend to and sequence to self attend to
prefix, x = x[:, :self.cross_attn_seq_len], x[:, self.cross_attn_seq_len:]
# initial perceiver attention and feedforward (one cross attention)
for cross_attn, ff in self.perceive_layers:
x = cross_attn(x, prefix, context_mask = prefix_mask, rotary_pos_emb = rotary_pos_emb) + x
x = ff(x) + x
# layers
for attn, ff in self.layers:
x = attn(x, rotary_pos_emb = rotary_pos_emb) + x
x = ff(x) + x
# to logits
logits = self.to_logits(x)
# take care of cross entropy loss if labels are provided
if not exists(labels):
return logits
labels = labels[:, self.cross_attn_seq_len:]
loss = F.cross_entropy(rearrange(logits, 'b n c -> b c n'), labels, ignore_index = 0)
acc = self.compute_accuracy(logits, labels)
return (loss, acc)
#===================================================================================================================
# helper function
def eval_decorator(fn):
def inner(model, *args, **kwargs):
was_training = model.training
model.eval()
out = fn(model, *args, **kwargs)
model.train(was_training)
return out
return inner
# top k filtering
def top_k(logits, thres=0.9):
k = int((1 - thres) * logits.shape[-1])
val, ind = torch.topk(logits, k)
probs = torch.full_like(logits, float("-inf"))
probs.scatter_(1, ind, val)
return probs
class AutoregressiveWrapper(nn.Module):
def __init__(self, net, pad_value=0):
super().__init__()
self.max_seq_len = net.max_seq_len
self.pad_value = pad_value
self.net = net
@torch.no_grad()
@eval_decorator
def generate(
self,
start_tokens,
seq_len,
eos_token=None,
temperature=1.0,
filter_thres=0.9,
verbose=True,
return_prime=True,
**kwargs
):
b, n, device = *start_tokens.shape, start_tokens.device
out = start_tokens
if verbose:
print("Generating sequence of max length:", seq_len)
for s in range(seq_len):
logits = self.net(
out[:, -self.max_seq_len:],
**kwargs
)[:, -1]
filtered_logits = top_k(logits, thres = filter_thres)
probs = F.softmax(filtered_logits / temperature, dim=-1)
sample = torch.multinomial(probs, 1)
out = torch.cat((out, sample), dim=-1)
if verbose:
if s % 32 == 0:
print(s, '/', seq_len)
if exists(eos_token):
is_eos_token = out == eos_token
if is_eos_token.any(dim=-1).all():
# mask out everything after the eos tokens
shifted_is_eos_tokens = F.pad(is_eos_token, (1, -1))
mask = shifted_is_eos_tokens.float().cumsum(dim=-1) >= 1
out = out.masked_fill(mask, self.pad_value)
if verbose:
print('Model called the end of sequence at:', s, '/', seq_len)
break
if return_prime:
return out[:, :]
else:
return out[:, n:]
return out
def forward(self, x, **kwargs):
x_inp, x_labels = x[:, :-1], x[:, 1:]
return self.net(x_inp, labels = x_labels, **kwargs)
#===================================================================================================================