x-transformers 1.44.8__py3-none-any.whl → 2.0.1__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2419 @@
1
+ Metadata-Version: 2.4
2
+ Name: x-transformers
3
+ Version: 2.0.1
4
+ Summary: X-Transformers
5
+ Project-URL: Homepage, https://pypi.org/project/x-transformers/
6
+ Project-URL: Repository, https://github.com/lucidrains/x-transformers
7
+ Author-email: Phil Wang <lucidrains@gmail.com>
8
+ License: MIT License
9
+
10
+ Copyright (c) 2020 Phil Wang
11
+
12
+ Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ of this software and associated documentation files (the "Software"), to deal
14
+ in the Software without restriction, including without limitation the rights
15
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
+ copies of the Software, and to permit persons to whom the Software is
17
+ furnished to do so, subject to the following conditions:
18
+
19
+ The above copyright notice and this permission notice shall be included in all
20
+ copies or substantial portions of the Software.
21
+
22
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
+ SOFTWARE.
29
+ License-File: LICENSE
30
+ Keywords: artificial intelligence,attention mechanism,transformers
31
+ Classifier: Development Status :: 4 - Beta
32
+ Classifier: Intended Audience :: Developers
33
+ Classifier: License :: OSI Approved :: MIT License
34
+ Classifier: Programming Language :: Python :: 3.6
35
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
36
+ Requires-Python: >=3.9
37
+ Requires-Dist: einops>=0.8.0
38
+ Requires-Dist: einx>=0.3.0
39
+ Requires-Dist: loguru
40
+ Requires-Dist: packaging>=21.0
41
+ Requires-Dist: torch>=2.0
42
+ Provides-Extra: examples
43
+ Requires-Dist: torchvision; extra == 'examples'
44
+ Requires-Dist: tqdm; extra == 'examples'
45
+ Provides-Extra: test
46
+ Requires-Dist: pytest; extra == 'test'
47
+ Description-Content-Type: text/markdown
48
+
49
+ ## x-transformers
50
+
51
+ [![PyPI version](https://badge.fury.io/py/x-transformers.svg)](https://badge.fury.io/py/x-transformers)
52
+
53
+ A concise but fully-featured transformer, complete with a set of promising e**x**perimental features from various papers.
54
+
55
+ ## Install
56
+
57
+ ```bash
58
+ $ pip install x-transformers
59
+ ```
60
+
61
+ ## Usage
62
+
63
+ Full encoder / decoder
64
+
65
+ ```python
66
+ import torch
67
+ from x_transformers import XTransformer
68
+
69
+ model = XTransformer(
70
+ dim = 512,
71
+ enc_num_tokens = 256,
72
+ enc_depth = 6,
73
+ enc_heads = 8,
74
+ enc_max_seq_len = 1024,
75
+ dec_num_tokens = 256,
76
+ dec_depth = 6,
77
+ dec_heads = 8,
78
+ dec_max_seq_len = 1024,
79
+ tie_token_emb = True # tie embeddings of encoder and decoder
80
+ )
81
+
82
+ src = torch.randint(0, 256, (1, 1024))
83
+ src_mask = torch.ones_like(src).bool()
84
+ tgt = torch.randint(0, 256, (1, 1024))
85
+
86
+ loss = model(src, tgt, mask = src_mask) # (1, 1024, 512)
87
+ loss.backward()
88
+ ```
89
+
90
+ Decoder-only (GPT-like)
91
+
92
+ ```python
93
+ import torch
94
+ from x_transformers import TransformerWrapper, Decoder
95
+
96
+ model = TransformerWrapper(
97
+ num_tokens = 20000,
98
+ max_seq_len = 1024,
99
+ attn_layers = Decoder(
100
+ dim = 512,
101
+ depth = 12,
102
+ heads = 8
103
+ )
104
+ ).cuda()
105
+
106
+ x = torch.randint(0, 256, (1, 1024)).cuda()
107
+
108
+ model(x) # (1, 1024, 20000)
109
+ ```
110
+
111
+ GPT3 would be approximately the following (but you wouldn't be able to run it anyways)
112
+
113
+ ```python
114
+
115
+ gpt3 = TransformerWrapper(
116
+ num_tokens = 50000,
117
+ max_seq_len = 2048,
118
+ attn_layers = Decoder(
119
+ dim = 12288,
120
+ depth = 96,
121
+ heads = 96,
122
+ attn_dim_head = 128
123
+ )
124
+ ).cuda()
125
+ ```
126
+
127
+ Encoder-only (BERT-like)
128
+
129
+ ```python
130
+ import torch
131
+ from x_transformers import TransformerWrapper, Encoder
132
+
133
+ model = TransformerWrapper(
134
+ num_tokens = 20000,
135
+ max_seq_len = 1024,
136
+ attn_layers = Encoder(
137
+ dim = 512,
138
+ depth = 12,
139
+ heads = 8
140
+ )
141
+ ).cuda()
142
+
143
+ x = torch.randint(0, 256, (1, 1024)).cuda()
144
+ mask = torch.ones_like(x).bool()
145
+
146
+ model(x, mask = mask) # (1, 1024, 20000)
147
+ ```
148
+
149
+ State of the art image classification (<a href="https://arxiv.org/abs/2205.01580">SimpleViT</a>)
150
+
151
+ ```python
152
+ import torch
153
+ from x_transformers import ViTransformerWrapper, Encoder
154
+
155
+ model = ViTransformerWrapper(
156
+ image_size = 256,
157
+ patch_size = 32,
158
+ num_classes = 1000,
159
+ attn_layers = Encoder(
160
+ dim = 512,
161
+ depth = 6,
162
+ heads = 8,
163
+ )
164
+ )
165
+
166
+ img = torch.randn(1, 3, 256, 256)
167
+ model(img) # (1, 1000)
168
+ ```
169
+
170
+ Image -> caption
171
+
172
+ ```python
173
+ import torch
174
+ from x_transformers import ViTransformerWrapper, TransformerWrapper, Encoder, Decoder
175
+
176
+ encoder = ViTransformerWrapper(
177
+ image_size = 256,
178
+ patch_size = 32,
179
+ attn_layers = Encoder(
180
+ dim = 512,
181
+ depth = 6,
182
+ heads = 8
183
+ )
184
+ )
185
+
186
+ decoder = TransformerWrapper(
187
+ num_tokens = 20000,
188
+ max_seq_len = 1024,
189
+ attn_layers = Decoder(
190
+ dim = 512,
191
+ depth = 6,
192
+ heads = 8,
193
+ cross_attend = True
194
+ )
195
+ )
196
+
197
+ img = torch.randn(1, 3, 256, 256)
198
+ caption = torch.randint(0, 20000, (1, 1024))
199
+
200
+ encoded = encoder(img, return_embeddings = True)
201
+ decoder(caption, context = encoded) # (1, 1024, 20000)
202
+ ```
203
+
204
+ <a href="https://arxiv.org/abs/2209.06794">PaLI</a>, state of the art language-vision model
205
+
206
+ ```python
207
+ import torch
208
+ from x_transformers import ViTransformerWrapper, XTransformer, Encoder
209
+
210
+ # PaLI composes of
211
+ # 1. vision transformer (ViTransformerWrapper) +
212
+ # 2. encoder-decoder transformer (XTransformer)
213
+
214
+ vit = ViTransformerWrapper(
215
+ image_size = 256,
216
+ patch_size = 32,
217
+ attn_layers = Encoder(
218
+ dim = 512,
219
+ depth = 6,
220
+ heads = 8
221
+ )
222
+ )
223
+
224
+ pali = XTransformer(
225
+ dim = 512,
226
+ enc_num_tokens = 256,
227
+ enc_depth = 6,
228
+ enc_heads = 8,
229
+ enc_max_seq_len = 1024,
230
+ dec_num_tokens = 256,
231
+ dec_depth = 6,
232
+ dec_heads = 8,
233
+ dec_max_seq_len = 1024
234
+ )
235
+
236
+ # training data
237
+
238
+ img = torch.randn(1, 3, 256, 256) # images
239
+ prompt = torch.randint(0, 256, (1, 1024)) # prompt
240
+ prompt_mask = torch.ones(1, 1024).bool() # prompt text mask
241
+ output_text = torch.randint(0, 256, (1, 1024)) # target output text
242
+
243
+ # train
244
+
245
+ img_embeds = vit(
246
+ img,
247
+ return_embeddings = True
248
+ )
249
+
250
+ loss = pali(
251
+ prompt,
252
+ output_text,
253
+ mask = prompt_mask,
254
+ src_prepend_embeds = img_embeds # will preprend image embeddings to encoder text embeddings before attention
255
+ )
256
+
257
+ loss.backward()
258
+
259
+ # do the above for many steps on a 17B parameter model
260
+ # attention is all you need
261
+ ```
262
+
263
+ ## Dropouts
264
+
265
+ ```python
266
+ import torch
267
+ from x_transformers import TransformerWrapper, Decoder, Encoder
268
+
269
+ model = TransformerWrapper(
270
+ num_tokens = 20000,
271
+ max_seq_len = 1024,
272
+ emb_dropout = 0.1, # dropout after embedding
273
+ attn_layers = Decoder(
274
+ dim = 512,
275
+ depth = 6,
276
+ heads = 8,
277
+ layer_dropout = 0.1, # stochastic depth - dropout entire layer
278
+ attn_dropout = 0.1, # dropout post-attention
279
+ ff_dropout = 0.1 # feedforward dropout
280
+ )
281
+ )
282
+
283
+ x = torch.randint(0, 20000, (1, 1024))
284
+ model(x)
285
+ ```
286
+
287
+ ## Features
288
+
289
+ ### Flash Attention
290
+
291
+ <img src="./images/flash-attention.png" width="500px"></img>
292
+
293
+ What originally started off as <a href="https://arxiv.org/abs/2112.05682">a short paper</a> from Markus Rabe culminated as a practical fused attention CUDA kernel, named <a href="https://arxiv.org/abs/2205.14135">Flash Attention</a> by <a href="https://tridao.me/">Tri Dao</a>.
294
+
295
+ The technique processes the attention matrix in tiles, only keeping track of the running softmax and exponentiated weighted sums. By recomputing on the backwards pass in a tiled fashion, one is able to keep the memory linear with respect to sequence length. This allows a lot of recent models to be able to reach for longer context lengths without worrying about the memory bottleneck.
296
+
297
+ Other engineering decisions made by Tri Dao led to its enormous success, namely minimizing HBM accesses so that both the forwards and backwards outperform naive attention. In other words, flash attention is not only more memory efficient, but faster as well, making it a necessity for training transformers.
298
+
299
+ MetaAI has recently added the ability to use <a href="https://github.com/hazyresearch/flash-attention">Tri Dao's CUDA kernel</a> through the <a href="https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html">scaled_dot_product_attention</a> function in Pytorch 2.0. (They also have a `mem_efficient` attention, which is identical to flash attention design, just that the tiles are traversed differently)
300
+
301
+ <a href="https://ai.facebook.com/blog/large-language-model-llama-meta-ai/">Llama</a> was trained using Flash Attention. The only reason to avoid it is if you require operating on the attention matrix (dynamic positional bias, talking heads, residual attention).
302
+
303
+ You can use it in this repository by setting `attn_flash` to `True` and enjoy the immediate memory savings and increase in speed.
304
+
305
+ ex.
306
+
307
+ ```python
308
+ import torch
309
+ from x_transformers import TransformerWrapper, Decoder, Encoder
310
+
311
+ model = TransformerWrapper(
312
+ num_tokens = 20000,
313
+ max_seq_len = 1024,
314
+ attn_layers = Decoder(
315
+ dim = 512,
316
+ depth = 6,
317
+ heads = 8,
318
+ attn_flash = True # just set this to True if you have pytorch 2.0 installed
319
+ )
320
+ )
321
+ ```
322
+
323
+ ### Augmenting Self-attention with Persistent Memory
324
+
325
+ <img src="./images/all-attention.png" width="500px"></img>
326
+
327
+ https://arxiv.org/abs/1907.01470
328
+
329
+ Proposes adding learned memory key / values prior to attention. They were able to remove feedforwards altogether and attain similar performance to the original transformers. I have found that keeping the feedforwards and adding the memory key / values leads to even better performance.
330
+
331
+ ```python
332
+ from x_transformers import Decoder, Encoder
333
+
334
+ enc = Encoder(
335
+ dim = 512,
336
+ depth = 6,
337
+ heads = 8,
338
+ attn_num_mem_kv = 16 # 16 memory key / values
339
+ )
340
+ ```
341
+
342
+ ### Memory Transformers
343
+
344
+ <img src="./images/memory-transformer.png" width="500px"></img>
345
+
346
+ https://arxiv.org/abs/2006.11527
347
+
348
+ Proposes adding learned tokens, akin to CLS tokens, named memory tokens, that is passed through the attention layers alongside the input tokens. This setting is compatible with both encoder and decoder training.
349
+
350
+ ```python
351
+ import torch
352
+ from x_transformers import TransformerWrapper, Decoder, Encoder
353
+
354
+ model = TransformerWrapper(
355
+ num_tokens = 20000,
356
+ max_seq_len = 1024,
357
+ num_memory_tokens = 20, # 20 memory tokens
358
+ attn_layers = Encoder(
359
+ dim = 512,
360
+ depth = 6,
361
+ heads = 8
362
+ )
363
+ )
364
+ ```
365
+
366
+ Update: MetaAI researchers <a href="https://arxiv.org/abs/2309.16588">have found</a> that adding memory tokens (they call them register tokens), alleviates outliers (which is suspected now to be a pathology of attention networks unable to <a href="https://arxiv.org/abs/2306.12929">attend to nothing</a>).
367
+
368
+ Update 2: a hybrid architecture out of Nvidia named <a href="https://openreview.net/forum?id=A1ztozypga">Hymba</a> used memory tokens successfully in the autoregressive case, termed meta tokens in their paper.
369
+
370
+ Update 3: further corroborated by <a href="https://arxiv.org/abs/2501.00663">a paper</a> trying to extend memory in attention networks, termed persistent memory
371
+
372
+ ### Transformers Without Tears
373
+
374
+ <img src="./images/scalenorm.png"></img>
375
+
376
+ https://arxiv.org/abs/1910.05895
377
+
378
+ They experiment with alternatives to Layer normalization and found one that is both effective and simpler. Researchers have shared with me this leads to faster convergence.
379
+
380
+ ```python
381
+ import torch
382
+ from x_transformers import TransformerWrapper, Decoder, Encoder
383
+
384
+ model = TransformerWrapper(
385
+ num_tokens = 20000,
386
+ max_seq_len = 1024,
387
+ attn_layers = Decoder(
388
+ dim = 512,
389
+ depth = 6,
390
+ heads = 8,
391
+ use_scalenorm = True # set to True to use for all layers
392
+ )
393
+ )
394
+ ```
395
+
396
+ You can also use the l2 normalized embeddings proposed as part of `fixnorm`. I have found it leads to improved convergence, when paired with small initialization (proposed by <a href="https://github.com/BlinkDL">BlinkDL</a>). The small initialization will be taken care of as long as `l2norm_embed` is set to `True`
397
+
398
+ ```python
399
+ import torch
400
+ from x_transformers import TransformerWrapper, Decoder, Encoder
401
+
402
+ model = TransformerWrapper(
403
+ num_tokens = 20000,
404
+ max_seq_len = 1024,
405
+ l2norm_embed = True, # set this to True for l2 normalized embedding + small init
406
+ attn_layers = Decoder(
407
+ dim = 512,
408
+ depth = 6,
409
+ heads = 8
410
+ )
411
+ )
412
+ ```
413
+
414
+ Along the same lines of l2 normalized embeddings, Huggingface's <a href="https://huggingface.co/bigscience/bloom">175B parameter BLOOM</a> also places a layernorm right after the embeddings and just before the tokens enter the attention layers. This was corroborated by Yandex's <a href="https://github.com/yandex/YaLM-100B">100B parameter YaLM</a> to stabilize training.
415
+
416
+ It is recommended you either have either `l2norm_embed` or `post_emb_norm` set to `True` but not both, as they probably serve the same purpose.
417
+
418
+ ```python
419
+ import torch
420
+ from x_transformers import TransformerWrapper, Decoder, Encoder
421
+
422
+ model = TransformerWrapper(
423
+ num_tokens = 20000,
424
+ max_seq_len = 1024,
425
+ post_emb_norm = True, # set this to True to layernorm summed token + pos embeddings
426
+ attn_layers = Decoder(
427
+ dim = 512,
428
+ depth = 6,
429
+ heads = 8
430
+ )
431
+ )
432
+ ```
433
+
434
+ ### Root Mean Square Layer Normalization
435
+
436
+ https://arxiv.org/abs/1910.07467
437
+
438
+ The authors propose to replace layer normalization with a simpler alternative, without mean centering and the learned bias. An investigative paper found this to be the <a href="https://arxiv.org/abs/2102.11972">best performing normalization variant</a>. It was also used in Deepmind's latest large language models, <a href="https://deepmind.com/research/publications/2021/improving-language-models-by-retrieving-from-trillions-of-tokens">Retro</a> and <a href="https://arxiv.org/abs/2112.11446">Gopher</a>.
439
+
440
+ ```python
441
+ import torch
442
+ from x_transformers import TransformerWrapper, Decoder, Encoder
443
+
444
+ model = TransformerWrapper(
445
+ num_tokens = 20000,
446
+ max_seq_len = 1024,
447
+ attn_layers = Decoder(
448
+ dim = 512,
449
+ depth = 6,
450
+ heads = 8,
451
+ use_rmsnorm = True # set to true to use for all layers
452
+ )
453
+ )
454
+ ```
455
+
456
+ *July 2023* <a href="https://arxiv.org/abs/2307.14995">A linear attention paper</a> has experiments to show that removing the learned multiplicative gamma led to no performance degradation. This simplifies the RMS normalization to a satisfying `l2norm(x) * sqrt(dim)`.
457
+
458
+ ```python
459
+ import torch
460
+ from x_transformers import TransformerWrapper, Decoder, Encoder
461
+
462
+ model = TransformerWrapper(
463
+ num_tokens = 20000,
464
+ max_seq_len = 1024,
465
+ attn_layers = Decoder(
466
+ dim = 512,
467
+ depth = 6,
468
+ heads = 8,
469
+ use_simple_rmsnorm = True # set to true to use for all layers
470
+ )
471
+ )
472
+ ```
473
+
474
+ ### GLU Variants Improve Transformer
475
+
476
+ <img src="./images/ffglu.png"></img>
477
+
478
+ https://arxiv.org/abs/2002.05202
479
+
480
+ Noam Shazeer paper that explores gating in the feedforward, finding that simple gating with GELU leads to significant improvements. This variant also showed up in the latest mT5 architecture. You should always turn this on (I may eventually turn it on by default).
481
+
482
+ ```python
483
+ import torch
484
+ from x_transformers import TransformerWrapper, Decoder, Encoder
485
+
486
+ model = TransformerWrapper(
487
+ num_tokens = 20000,
488
+ max_seq_len = 1024,
489
+ attn_layers = Decoder(
490
+ dim = 512,
491
+ depth = 6,
492
+ heads = 8,
493
+ ff_glu = True # set to true to use for all feedforwards
494
+ )
495
+ )
496
+ ```
497
+
498
+ The <a href="https://ai.googleblog.com/2022/04/pathways-language-model-palm-scaling-to.html">PaLM</a> language model also chose to use the Swish GLU variant. You can turn this on by setting two flags
499
+
500
+ ```python
501
+ import torch
502
+ from x_transformers import TransformerWrapper, Decoder, Encoder
503
+
504
+ model = TransformerWrapper(
505
+ num_tokens = 20000,
506
+ max_seq_len = 1024,
507
+ attn_layers = Decoder(
508
+ dim = 512,
509
+ depth = 6,
510
+ heads = 8,
511
+ ff_swish = True, # set this to True
512
+ ff_glu = True # set to true to use for all feedforwards
513
+ )
514
+ )
515
+ ``````
516
+
517
+ ### No Bias in Feedforward
518
+
519
+ Starting with <a href="https://ai.googleblog.com/2022/04/pathways-language-model-palm-scaling-to.html">PaLM</a>, there begun a trend to remove biases from the transformer all together. <a href="https://github.com/borisdayma">Boris Dayma</a> has run a number of experiments that showed removing biases from feedforwards led to increased throughput without any loss of accuracy. This was corroborated by <a href="https://arxiv.org/abs/2212.14034">yet another paper</a> investigating transformer architecture variants.
520
+
521
+ You can turn off the feedforward bias as follows
522
+
523
+ ```python
524
+ import torch
525
+ from x_transformers import TransformerWrapper, Decoder, Encoder
526
+
527
+ model = TransformerWrapper(
528
+ num_tokens = 20000,
529
+ max_seq_len = 1024,
530
+ attn_layers = Decoder(
531
+ dim = 512,
532
+ depth = 6,
533
+ heads = 8,
534
+ ff_no_bias = True # set this to True
535
+ )
536
+ )
537
+ ```
538
+
539
+ ### ReLU²
540
+
541
+ https://arxiv.org/abs/2109.08668
542
+
543
+ This paper used neural architecture search and found an activation, Relu Squared, that is both simpler and performs better than GELU, in the autoregressive language model setting. I have confirmed this in my independent experiments. However, if one were using the GLU variant from above, GELU still performs better. Pending further corroboration.
544
+
545
+ ```python
546
+ import torch
547
+ from x_transformers import TransformerWrapper, Decoder, Encoder
548
+
549
+ model = TransformerWrapper(
550
+ num_tokens = 20000,
551
+ max_seq_len = 1024,
552
+ attn_layers = Decoder(
553
+ dim = 512,
554
+ depth = 6,
555
+ heads = 8,
556
+ ff_relu_squared = True
557
+ )
558
+ )
559
+ ```
560
+
561
+ ### Explicit Sparse Transformer: Concentrated Attention Through Explicit Selection
562
+
563
+ <img src="./images/topk-attention.png" width="500px"></img>
564
+
565
+ https://arxiv.org/abs/1912.11637
566
+
567
+ This paper proposes an efficient way to sparsify attention by zeroing all dot-product query/key values not within the top k values. The show that this cheap method was as effective as other more expensive operations like sparsemax or entmax15. This technique comes with the cost of an extra hyperparameter (the top k values to keep). The paper recommends a value of `k = 8`
568
+
569
+ ```python
570
+ import torch
571
+ from x_transformers import TransformerWrapper, Decoder
572
+
573
+ model = TransformerWrapper(
574
+ num_tokens = 20000,
575
+ max_seq_len = 1024,
576
+ attn_layers = Decoder(
577
+ dim = 512,
578
+ depth = 6,
579
+ heads = 8,
580
+ attn_sparse_topk = 8, # keep only the top 8 values before attention (softmax)
581
+ attn_sparse_topk_straight_through = True # straight through the original gradients
582
+ )
583
+ )
584
+ ```
585
+
586
+ An extreme case of `topk` value of `1`, you can use the following
587
+
588
+ ```python
589
+ model = TransformerWrapper(
590
+ num_tokens = 20000,
591
+ max_seq_len = 1024,
592
+ attn_layers = Decoder(
593
+ dim = 512,
594
+ depth = 6,
595
+ heads = 8,
596
+ attn_hard = True # will only propagate the single value of the argmax of qk logit. offered in the case it addresses https://arxiv.org/abs/2410.01104
597
+ )
598
+ )
599
+ ```
600
+
601
+ ### Talking-Heads Attention
602
+
603
+ <img src="./images/talking-heads.png" width="500px"></img>
604
+
605
+ https://arxiv.org/abs/2003.02436
606
+
607
+ A Noam Shazeer paper that proposes mixing information between heads pre and post attention (softmax). This comes with the cost of extra memory and compute.
608
+
609
+ ```python
610
+ import torch
611
+ from x_transformers import TransformerWrapper, Decoder
612
+
613
+ model = TransformerWrapper(
614
+ num_tokens = 20000,
615
+ max_seq_len = 1024,
616
+ attn_layers = Decoder(
617
+ dim = 512,
618
+ depth = 6,
619
+ heads = 8,
620
+ attn_pre_talking_heads = True, # linear combination across pre-softmax attn logits across heads
621
+ attn_post_talking_heads = True # linear combination across post-softmax attn across heads
622
+ )
623
+ )
624
+ ```
625
+
626
+ ### One Write-Head Is All You Need
627
+
628
+ https://arxiv.org/abs/1911.02150
629
+
630
+ Yet another Noam Shazeer paper (he's a legend) that proposes to only have one head for the key / values, but multi-headed queries. This paper was largely ignored for a while, but recently validated at scale in <a href="https://arxiv.org/abs/2203.07814">AlphaCode</a> as well as <a href="https://arxiv.org/abs/2204.02311">PaLM</a>. It has the property of being memory efficient when decoding extremely large language models. You can use it with one keyword argument as shown below.
631
+
632
+ ```python
633
+ import torch
634
+ from x_transformers import TransformerWrapper, Decoder
635
+
636
+ model = TransformerWrapper(
637
+ num_tokens = 20000,
638
+ max_seq_len = 1024,
639
+ attn_layers = Decoder(
640
+ dim = 512,
641
+ depth = 6,
642
+ heads = 8,
643
+ attn_one_kv_head = True
644
+ )
645
+ )
646
+ ```
647
+
648
+ This has been further generalized in <a href="https://arxiv.org/abs/2305.13245">a recent paper</a> to allow for groups of query heads to attend to a single key / value head. You can use this by specifying the `attn_kv_heads`
649
+
650
+ ```python
651
+ import torch
652
+ from x_transformers import TransformerWrapper, Decoder
653
+
654
+ model = TransformerWrapper(
655
+ num_tokens = 20000,
656
+ max_seq_len = 1024,
657
+ attn_layers = Decoder(
658
+ dim = 512,
659
+ depth = 12,
660
+ heads = 8,
661
+ attn_kv_heads = 2 # say you want 4 query heads to attend to 1 key / value head
662
+ )
663
+ )
664
+ ```
665
+
666
+ ### Attention on Attention for Image Captioning
667
+
668
+ <img src="./images/attention-on-attention.png"></img>
669
+
670
+ https://arxiv.org/abs/1908.06954
671
+
672
+ This paper proposes to add a gated linear unit at the end of the attention layer, further gated by the original queries. Although this is not widely used outside of visual question / answering, I suspect it should lead to improvements after seeing the success of the feedforward GLU variant.
673
+
674
+ Update: After some experimentation, I found this variant actually performs worse, but if it were to be modified to not concatenate the queries before gating, it performs much better. That is what we will be using in this repository.
675
+
676
+ ```python
677
+ import torch
678
+ from x_transformers import TransformerWrapper, Encoder
679
+
680
+ model = TransformerWrapper(
681
+ num_tokens = 20000,
682
+ max_seq_len = 1024,
683
+ attn_layers = Encoder(
684
+ dim = 512,
685
+ depth = 6,
686
+ heads = 8,
687
+ attn_on_attn = True # gate output of attention layer, by queries
688
+ )
689
+ )
690
+ ```
691
+
692
+ ### Intra-attention Gating on Values
693
+
694
+ <img src="./images/gate_values.png" width="400px"></img>
695
+
696
+ <a href="https://github.com/deepmind/alphafold">Alphafold2</a> had a peculiar variant of attention where they gate the aggregated values with the input, presumably to have the block have more control over the update.
697
+
698
+ A quick test shows a small but noticeable improvement, on about the same order as attention on attention.
699
+
700
+ ```python
701
+ import torch
702
+ from x_transformers import TransformerWrapper, Encoder
703
+
704
+ model = TransformerWrapper(
705
+ num_tokens = 20000,
706
+ max_seq_len = 1024,
707
+ attn_layers = Encoder(
708
+ dim = 512,
709
+ depth = 6,
710
+ heads = 8,
711
+ attn_gate_values = True # gate aggregated values with the input
712
+ )
713
+ )
714
+ ```
715
+
716
+ ### Improving Transformer Models by Reordering their Sublayers
717
+
718
+ <img src="./images/sandwich.png"></img>
719
+
720
+ <img src="./images/sandwich-2.png"></img>
721
+
722
+ https://arxiv.org/abs/1911.03864
723
+
724
+ This paper proposes to break from the normal fixed pattern of alternating attention and feedforwards, but to have blocks of only attention at the beginning followed by blocks of feedforwards at the end. This was further corroborated by a paper by Nvidia that reduces the number of attention layers to be 1/3rd of the feedforwards without loss in performance.
725
+
726
+ The amount of interleaving is controlled by a "sandwich coefficient", which they found to be optimal at a value of `6`.
727
+
728
+ You can experiment with this feature as shown below
729
+
730
+ ```python
731
+ import torch
732
+ from x_transformers import TransformerWrapper, Encoder
733
+
734
+ model = TransformerWrapper(
735
+ num_tokens = 20000,
736
+ max_seq_len = 1024,
737
+ attn_layers = Encoder(
738
+ dim = 512,
739
+ depth = 6,
740
+ heads = 8,
741
+ sandwich_coef = 6 # interleave attention and feedforwards with sandwich coefficient of 6
742
+ )
743
+ )
744
+ ```
745
+
746
+ ### Weight-tied Layers
747
+
748
+ In the early days of the cambrian explosion of BERT, a paper explored weight tying all the layers, the model named <a href="https://arxiv.org/abs/1909.11942">ALBERT</a>. You can use it by setting `weight_tie_layers = True`
749
+
750
+ ```python
751
+ import torch
752
+ from x_transformers import TransformerWrapper, Encoder
753
+
754
+ model = TransformerWrapper(
755
+ num_tokens = 20000,
756
+ max_seq_len = 1024,
757
+ attn_layers = Encoder(
758
+ dim = 512,
759
+ depth = 12,
760
+ weight_tie_layers = True # set this to True to weight tie all the layers
761
+ )
762
+ )
763
+ ```
764
+
765
+ If you wish to do something more sophisticated, say 3 layers, with each layer recurrent 4 times before onto the next (similar to <a href="https://arxiv.org/abs/2405.15071">this paper</a>), that is possible as well. Be aware the `layers_execute_order` is 0-indexed
766
+
767
+ ```python
768
+ import torch
769
+ from x_transformers import TransformerWrapper, Decoder
770
+
771
+ model = TransformerWrapper(
772
+ num_tokens = 20000,
773
+ max_seq_len = 1024,
774
+ attn_layers = Decoder(
775
+ dim = 512,
776
+ custom_layers = (
777
+ 'a', 'f', # 3 sets of attention and feedforward
778
+ 'a', 'f',
779
+ 'a', 'f'
780
+ ),
781
+ layers_execute_order = (
782
+ *((0, 1) * 4), # each done 4 times before sequentially passed forward, but you can probably imagine some more interesting configurations...
783
+ *((2, 3) * 4),
784
+ *((4, 5) * 4),
785
+ )
786
+ )
787
+ )
788
+ ```
789
+
790
+ ### Understanding and Improving Transformer From a Multi-Particle Dynamic System Point of View
791
+
792
+ <img src="./images/macaron-1.png"></img>
793
+
794
+ <img src="./images/macaron-2.png"></img>
795
+
796
+ https://arxiv.org/abs/1906.02762
797
+
798
+ The authors propose to view the success of transformers from a dynamical systems point of view, and then proposes an improvement based on mathematics of that POV. Specifically, they propose to place the attention layer in between two feedforward layers. This was adopted by a paper using transformers for speech recognition, the <a href="https://arxiv.org/abs/2005.08100">Conformer</a>.
799
+
800
+ ```python
801
+ import torch
802
+ from x_transformers import TransformerWrapper, Encoder
803
+
804
+ model = TransformerWrapper(
805
+ num_tokens = 20000,
806
+ max_seq_len = 1024,
807
+ attn_layers = Encoder(
808
+ dim = 512,
809
+ depth = 6,
810
+ heads = 8,
811
+ macaron = True # use macaron configuration
812
+ )
813
+ )
814
+ ```
815
+
816
+ ### T5's Simplified Relative Positional Encoding
817
+
818
+ https://arxiv.org/abs/1910.10683
819
+
820
+ T5 is one of the most successful encoder / decoder transformer architectures trained to date. They invented a new simplified relative positional encoding based on learned bias values that are added to the attention matrix pre-softmax. This bias is shared and injected into each attention layer. I have decided to include this because it offers a cheap way to have relative positional encoding (superior to absolute positional), and I have read papers that suggest having positional encoding added to each layer (vs only before the first) is beneficial.
821
+
822
+ ```python
823
+ import torch
824
+ from x_transformers import TransformerWrapper, Decoder
825
+
826
+ model = TransformerWrapper(
827
+ num_tokens = 20000,
828
+ max_seq_len = 1024,
829
+ attn_layers = Decoder(
830
+ dim = 512,
831
+ depth = 6,
832
+ heads = 8,
833
+ rel_pos_bias = True # adds relative positional bias to all attention layers, a la T5
834
+ )
835
+ )
836
+ ```
837
+
838
+ ### Residual Attention
839
+
840
+ <img src="./images/residual_attn.png" width="500px"></img>
841
+
842
+ https://arxiv.org/abs/2012.11747
843
+
844
+ This paper from Google proposes residualizing the pre-attention scores across all layers. At the cost of no extra parameters, they show improvement on top of regular attention networks. If you turn on this setting, be aware that the best results in the paper used post-normalization, in which case a learning warmup will be needed. The authors also reported that they could use a higher learning rate and get even better gains in the same amount of steps. (In the paper they use `2e-4` vs `1e-4` for vanilla transformer)
845
+
846
+ ```python
847
+ import torch
848
+ from x_transformers import TransformerWrapper, Encoder
849
+
850
+ model = TransformerWrapper(
851
+ num_tokens = 20000,
852
+ max_seq_len = 1024,
853
+ attn_layers = Encoder(
854
+ dim = 512,
855
+ depth = 6,
856
+ heads = 8,
857
+ pre_norm = False, # in the paper, residual attention had best results with post-layernorm
858
+ residual_attn = True # add residual attention
859
+ )
860
+ )
861
+ ```
862
+
863
+ I also tried residualizing cross attention and may have noticed an improvement in convergence. You can try it by setting the `cross_residual_attn` keyword to `True`
864
+
865
+ ```python
866
+ import torch
867
+ from x_transformers import XTransformer
868
+
869
+ model = XTransformer(
870
+ dim = 512,
871
+ enc_num_tokens = 256,
872
+ enc_depth = 6,
873
+ enc_heads = 8,
874
+ enc_max_seq_len = 1024,
875
+ dec_num_tokens = 256,
876
+ dec_depth = 6,
877
+ dec_heads = 8,
878
+ dec_max_seq_len = 1024,
879
+ dec_cross_residual_attn = True # residualize cross attention
880
+ )
881
+ ```
882
+
883
+ ### Transformer-XL recurrence
884
+
885
+ You can also do Transformer-XL recurrence, by simply passing in a `max_mem_len` in the `TransformerWrapper` class, and then making sure your `Decoder` has `rel_pos_bias` (or `rotary_pos_emb`) set to `True`.
886
+
887
+ Then, you can retrieve the memories at each step with the `return_mems` keyword and pass it to the next iteration.
888
+
889
+ ```python
890
+ import torch
891
+ from x_transformers import TransformerWrapper, Decoder
892
+
893
+ model_xl = TransformerWrapper(
894
+ num_tokens = 20000,
895
+ max_seq_len = 512,
896
+ max_mem_len = 2048,
897
+ attn_layers = Decoder(
898
+ dim = 512,
899
+ depth = 6,
900
+ heads = 8,
901
+ rel_pos_bias = True
902
+ )
903
+ )
904
+
905
+ seg1 = torch.randint(0, 20000, (1, 512))
906
+ seg2 = torch.randint(0, 20000, (1, 512))
907
+ seg3 = torch.randint(0, 20000, (1, 512))
908
+
909
+ logits1, mems1 = model_xl(seg1, return_mems = True)
910
+ logits2, mems2 = model_xl(seg2, mems = mems1, return_mems = True)
911
+ logits3, mems3 = model_xl(seg3, mems = mems2, return_mems = True)
912
+ ```
913
+
914
+ Setting up the logic for training and sampling from transformer xl can be a bit overwhelming. This repository offers a simple wrapper that should make this easy, with the `XLAutoregressiveWrapper`.
915
+
916
+ ```python
917
+ # pass in the above model_xl
918
+
919
+ xl_wrapper = XLAutoregressiveWrapper(model_xl)
920
+
921
+ seg = torch.randint(0, 20000, (1, 4096)).cuda() # sequence exceeding max length, automatically segmented and memory managed
922
+
923
+ loss = xl_wrapper(seg)
924
+ loss.backward()
925
+
926
+ # then, after much training
927
+
928
+ prime = seg[:, :1024] # if prime exceeds max length, memory will be caught up before generating
929
+
930
+ generated = xl_wrapper.generate(prime, 4096) # (1, 4096)
931
+ ```
932
+
933
+ ### Enhanced recurrence
934
+
935
+ <img src="./images/enhanced-recurrence.png" width="400px"/>
936
+
937
+ <a href="https://arxiv.org/abs/2012.15688">This paper</a> proposes a simple technique to enhance the range of Transformer-XL. They simply route the memory segment of a layer to the layer below it, for the next recurrent step. You can enable this by setting `shift_mem_down = 1`. You can also shift down arbitrary number of layers by setting this value to `> 1`.
938
+
939
+ ```python
940
+ import torch
941
+ from x_transformers import TransformerWrapper, Decoder
942
+
943
+ model_xl = TransformerWrapper(
944
+ num_tokens = 20000,
945
+ max_seq_len = 512,
946
+ max_mem_len = 2048,
947
+ shift_mem_down = 1,
948
+ attn_layers = Decoder(
949
+ dim = 512,
950
+ depth = 6,
951
+ heads = 8,
952
+ rotary_pos_emb = True
953
+ )
954
+ )
955
+
956
+ seg1 = torch.randint(0, 20000, (1, 512))
957
+ seg2 = torch.randint(0, 20000, (1, 512))
958
+ seg3 = torch.randint(0, 20000, (1, 512))
959
+
960
+ logits1, mems1 = model_xl(seg1, return_mems = True)
961
+ logits2, mems2 = model_xl(seg2, mems = mems1, return_mems = True) # mems1 of layer N are automatically routed to the layer N-1
962
+ ```
963
+
964
+ ### Gated residual
965
+
966
+ <img src="./images/gating.png" width="500px"></img>
967
+
968
+ https://arxiv.org/abs/1910.06764
969
+
970
+ The authors propose gating the residual connections in the transformer network and demonstrate increased stability and performance for Transformer-XL in a variety of reinforcement learning tasks.
971
+
972
+ ```python
973
+ import torch
974
+ from x_transformers import TransformerWrapper, Decoder
975
+
976
+ model = TransformerWrapper(
977
+ num_tokens = 20000,
978
+ max_seq_len = 1024,
979
+ max_mem_len = 2048,
980
+ attn_layers = Decoder(
981
+ dim = 512,
982
+ depth = 6,
983
+ heads = 16,
984
+ gate_residual = True
985
+ )
986
+ )
987
+ ```
988
+
989
+ ### Rotary Positional Embeddings
990
+
991
+ <img src="./images/rotary.png" width="500px"></img>
992
+
993
+ Developed in Beijing, this new technique quickly gained interest in the NLP circles. In short, it allows you to endow the transformer with relative positional embeddings at the cost of no learned parameters. You apply a rotary operation to the queries and keys prior to their dot product in attention. The big idea is injecting positions through rotations.
994
+
995
+ Highly recommend that you have this turned on whenever you are working on an ordered sequence.
996
+
997
+ ```python
998
+ import torch
999
+ from x_transformers import TransformerWrapper, Decoder
1000
+
1001
+ model = TransformerWrapper(
1002
+ num_tokens = 20000,
1003
+ max_seq_len = 1024,
1004
+ attn_layers = Decoder(
1005
+ dim = 512,
1006
+ depth = 6,
1007
+ heads = 8,
1008
+ rotary_pos_emb = True # turns on rotary positional embeddings
1009
+ )
1010
+ )
1011
+ ```
1012
+
1013
+ Update (12/2022): Rotary embedding has since been hugely successful, widely adopted in many large language models, including the largest in the world, PaLM. However, it has been uncovered in the ALiBi paper that rotary embeddings cannot length extrapolate well. This was recently addressed in <a href="https://arxiv.org/abs/2212.10554v1">a Microsoft research paper</a>. They propose a way to unobtrusively add the same decay as in ALiBi, and found that this resolves the extrapolation problem. You can use it in this repository by setting `rotary_xpos = True`. Like ALiBi, it would enforce the attention to be local. You can set the receptive field with `rotary_xpos_scale_base` value, which defaults to `512`
1014
+
1015
+ ```python
1016
+ import torch
1017
+ from x_transformers import TransformerWrapper, Decoder
1018
+
1019
+ model = TransformerWrapper(
1020
+ num_tokens = 20000,
1021
+ max_seq_len = 1024,
1022
+ attn_layers = Decoder(
1023
+ dim = 512,
1024
+ depth = 6,
1025
+ heads = 8,
1026
+ rotary_xpos = True # modified rotary to extrapolate well beyond length at which it was trained
1027
+ )
1028
+ )
1029
+ ```
1030
+
1031
+ ### Dynamic Positional Bias
1032
+
1033
+ <img src="./images/dynamic-pos-bias.png" width="150px"></img>
1034
+
1035
+ This technique bears roots from the field of vision transformers, where researchers are trying to have relative positions generalize to larger resolutions (without having to retrain the entire network). It was used in two recent papers, <a href="https://arxiv.org/abs/2108.00154">CrossFormer</a>, as well as <a href="https://arxiv.org/abs/2111.09883">SwinV2</a>.
1036
+
1037
+ <a href="https://github.com/cfoster0">Charles Foster</a> first tried this for a language model, and found that it works. Later on <a href="https://github.com/bob80333">Eric Engelhart</a> produced experimental results that show the same type of extrapolation holds, even for 1d sequences.
1038
+
1039
+ Eric trained at sequence lengths of 128, and showed that it generalized well to 1024. In addition, he showed that linear positions was better than log (used in SwinV2), for language.
1040
+
1041
+ Linear distances
1042
+
1043
+ <img src="./images/dynamic-pos-bias-linear.png" width="600px"></img>
1044
+
1045
+ Log distances
1046
+
1047
+ <img src="./images/dynamic-pos-bias-log.png" width="600px"></img>
1048
+
1049
+ Negative control - Sinusoidal
1050
+
1051
+ <img src="./images/dynamic-pos-bias-sinusoidal.png" width="600px"></img>
1052
+
1053
+ More of Eric's experimental results can be found <a href="https://github.com/bob80333/investigating_extrapolation">here</a>
1054
+
1055
+ You can use this type of relative position if you wish to train at smaller sequence lengths and have it generalize to longer ones, for both autoregressive and bidirectional models.
1056
+
1057
+ Update: <a href="https://www.kaggle.com/competitions/stanford-ribonanza-rna-folding/discussion/460121">First place RNA folding using dynamic positional bias</a>
1058
+
1059
+ ```python
1060
+ import torch
1061
+ from x_transformers import TransformerWrapper, Decoder
1062
+
1063
+ model = TransformerWrapper(
1064
+ num_tokens = 256,
1065
+ max_seq_len = 1024,
1066
+ attn_layers = Decoder(
1067
+ dim = 512,
1068
+ depth = 6,
1069
+ heads = 8,
1070
+ dynamic_pos_bias = True, # set this to True
1071
+ dynamic_pos_bias_log_distance = False # whether to use log distance, as in SwinV2
1072
+ )
1073
+ )
1074
+ ```
1075
+
1076
+ ### ALiBi Positional Embedding
1077
+
1078
+ <a href="https://ofir.io/train_short_test_long.pdf">This paper</a> proposes to simply apply a static linear bias to the attention matrix. The authors show this is not only effective as a relative positional encoding, but also allows the attention net to extrapolate to greater sequences length than what it was trained on, for autoregressive language models.
1079
+
1080
+ This repository also offers a bidirectional variant (nonsymmetric), proposed by the authors <a href="https://github.com/ofirpress/attention_with_linear_biases/issues/5">here</a>. However, this is untested. If you need bidirectional length extrapolation, the safest option would be Dynamic Position Bias
1081
+
1082
+ Update: It may be that ALiBi enforces a strong local attention across the heads, and may hinder it from attending at distances greater than 1k. To avoid any issues with global message passing, I've decided to introduce another hyperparameter `alibi_num_heads`, so one can specify less heads for the ALiBi bias
1083
+
1084
+ Update: There are reports that ALiBi outperform Rotary embeddings for pretraining and downstream fine-tuning.
1085
+
1086
+ Update: <a href="https://arxiv.org/abs/2305.19466">New paper</a> shows that no positional embedding can length extrapolate even than explicit ones
1087
+
1088
+ ```python
1089
+ import torch
1090
+ from x_transformers import TransformerWrapper, Decoder
1091
+
1092
+ model = TransformerWrapper(
1093
+ num_tokens = 20000,
1094
+ max_seq_len = 1024,
1095
+ attn_layers = Decoder(
1096
+ dim = 512,
1097
+ depth = 6,
1098
+ heads = 8,
1099
+ alibi_pos_bias = True, # turns on ALiBi positional embedding
1100
+ alibi_num_heads = 4 # only use ALiBi for 4 out of the 8 heads, so other 4 heads can still attend far distances
1101
+ )
1102
+ )
1103
+ ```
1104
+
1105
+ ### Shifted Tokens
1106
+
1107
+ An <a href="https://github.com/BlinkDL">independent researcher</a> has found that shifting a subset of the feature dimension along the sequence dimension by 1 token helps with convergence (<a href="https://zhuanlan.zhihu.com/p/191393788">Time-mixing</a>). I have tested this for the autoregressive case and can confirm that it leads to greatly improved convergence. This also lines up with <a href="https://arxiv.org/abs/2106.07477">the results</a> of some papers in the vision domain.
1108
+
1109
+ To use it, simply set `shift_tokens = 1` (or to whatever number of shifts you desire). The feature dimension will be divided by `shift_tokens + 1` and then each chunk will be shifted `[0, shift_tokens]` respectively
1110
+
1111
+ Update: new experiments by @sdtblck suggests this may only work for character-level training
1112
+
1113
+ Update: after more experiments, it seems that in the context of BPE encoding, with rotary turned on, there is no benefit to shifting. for character-level training, shifting may still improve a tiny bit
1114
+
1115
+ Update: When doing BPE encoded tokens, it seems that shift of 2 will bottleneck the dimensions (divided by 5). It is recommended you always do a shift of 1, unless if you are working with character level.
1116
+
1117
+ ```python
1118
+ import torch
1119
+ from x_transformers import TransformerWrapper, Decoder
1120
+
1121
+ model = TransformerWrapper(
1122
+ num_tokens = 20000,
1123
+ max_seq_len = 1024,
1124
+ attn_layers = Decoder(
1125
+ dim = 512,
1126
+ depth = 6,
1127
+ heads = 8,
1128
+ shift_tokens = 1
1129
+ )
1130
+ )
1131
+ ```
1132
+
1133
+ If you want finer control over how much is shifted per block (whether attention or feedforward), simply pass in a tuple of size that is equal to the number of layers.
1134
+
1135
+ ```python
1136
+ import torch
1137
+ from x_transformers import TransformerWrapper, Decoder
1138
+
1139
+ model = TransformerWrapper(
1140
+ num_tokens = 20000,
1141
+ max_seq_len = 1024,
1142
+ attn_layers = Decoder(
1143
+ dim = 512,
1144
+ depth = 6,
1145
+ heads = 8,
1146
+ shift_tokens = (1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0) # 12 blocks, attention and feedforward alternating, with progressively less shifting
1147
+ )
1148
+ )
1149
+ ```
1150
+
1151
+ ### Sandwich Norm
1152
+
1153
+ <img src="./images/sandwich_norm.png" width="400px"/>
1154
+
1155
+ This technique first made an appearance in <a href="https://arxiv.org/abs/2105.13290">the CoqView paper</a>, a Chinese version of the famous text-to-image transformer DALL-E. They propose, when using pre-layernorm, to add an extra layernorm to all the branch outputs. I have found this to be very effective for a number of projects, when facing instability during training.
1156
+
1157
+ ```python
1158
+ import torch
1159
+ from x_transformers import TransformerWrapper, Decoder
1160
+
1161
+ model = TransformerWrapper(
1162
+ num_tokens = 20000,
1163
+ max_seq_len = 1024,
1164
+ attn_layers = Decoder(
1165
+ dim = 512,
1166
+ depth = 6,
1167
+ heads = 8,
1168
+ sandwich_norm = True # set this to True
1169
+ )
1170
+ )
1171
+
1172
+ x = torch.randint(0, 20000, (1, 1024))
1173
+ model(x)
1174
+ ```
1175
+
1176
+ ### ResiDual
1177
+
1178
+ <img src="./images/resi_dual.png" width="400px"/>
1179
+
1180
+ <a href="https://arxiv.org/abs/2304.14802">This Microsoft paper</a> proposes yet another normalization configuration, combining both pre and post layernorm. They claim this hybridization reduces representation collapse (known to be an issue with pre-layernorm with increasing depth), while maintaining stability and reducing vanishing gradients (issues with post-layernorm). Initial experiments on my end show it to work no worse than pre-layernorm or sandwich norm. More study needed by the public to see if this is actually a winning technique.
1181
+
1182
+ ```python
1183
+ import torch
1184
+ from x_transformers import TransformerWrapper, Decoder
1185
+
1186
+ model = TransformerWrapper(
1187
+ num_tokens = 20000,
1188
+ max_seq_len = 1024,
1189
+ attn_layers = Decoder(
1190
+ dim = 512,
1191
+ depth = 6,
1192
+ heads = 8,
1193
+ resi_dual = True, # set this to True
1194
+ resi_dual_scale = 0.1 # in appendix, they said on fp16 the prenorm residual is prone to overflow. they claim by scaling it at each layer by a factor, it would prevent the overflow, and keep results the same (as layernorms are invariant to scaling of the input)
1195
+ )
1196
+ )
1197
+
1198
+ x = torch.randint(0, 20000, (1, 1024))
1199
+ model(x)
1200
+ ```
1201
+
1202
+ ### Normformer
1203
+
1204
+ <img src="./images/normformer.png" width="400px"/>
1205
+
1206
+ This <a href="https://openreview.net/forum?id=GMYWzWztDx5">paper</a> uncovers an issue with pre-norm transformers where gradients are mismatched between the early and later layers. They propose 4 changes, of which I will be offering 3.
1207
+
1208
+ The first change is to offer per head scaling after aggregating the values in attention. My experiments show a slight improvement in convergence.
1209
+
1210
+ ```python
1211
+ import torch
1212
+ from x_transformers import TransformerWrapper, Decoder
1213
+
1214
+ model = TransformerWrapper(
1215
+ num_tokens = 20000,
1216
+ max_seq_len = 1024,
1217
+ attn_layers = Decoder(
1218
+ dim = 512,
1219
+ depth = 6,
1220
+ heads = 8,
1221
+ attn_head_scale = True # set this to True
1222
+ )
1223
+ )
1224
+
1225
+ x = torch.randint(0, 20000, (1, 1024))
1226
+ model(x)
1227
+ ```
1228
+
1229
+ The second change is an extra layernorm right after the activation in the feedforward. I have also verified a slight improvement, at the cost of extra compute.
1230
+
1231
+ ```python
1232
+ import torch
1233
+ from x_transformers import TransformerWrapper, Decoder
1234
+
1235
+ model = TransformerWrapper(
1236
+ num_tokens = 20000,
1237
+ max_seq_len = 1024,
1238
+ attn_layers = Decoder(
1239
+ dim = 512,
1240
+ depth = 6,
1241
+ heads = 8,
1242
+ ff_post_act_ln = True # set this to True
1243
+ )
1244
+ )
1245
+
1246
+ x = torch.randint(0, 20000, (1, 1024))
1247
+ model(x)
1248
+ ```
1249
+
1250
+ For the residual scaling, you simply have to set `scale_residual = True`. I have noticed slight improvements, but occasional instability as well, so use with caution.
1251
+
1252
+ ```python
1253
+ import torch
1254
+ from x_transformers import TransformerWrapper, Decoder
1255
+
1256
+ model = TransformerWrapper(
1257
+ num_tokens = 20000,
1258
+ max_seq_len = 1024,
1259
+ attn_layers = Decoder(
1260
+ dim = 512,
1261
+ depth = 6,
1262
+ heads = 8,
1263
+ scale_residual = True # set this to True
1264
+ )
1265
+ )
1266
+
1267
+ x = torch.randint(0, 20000, (1, 1024))
1268
+ model(x)
1269
+ ```
1270
+
1271
+ The last change is a layernorm right after the outwards projection in attention. This is actually identical to the sandwich norm proposed by the Coqview paper, so you can use this by simply setting `sandwich_norm = True`, although it would also add it to the feedforward layer.
1272
+
1273
+ ### Cosine Sim Attention
1274
+
1275
+ <img src="./images/cosine-sim-attention.png" width="400px"></img>
1276
+
1277
+ This <a href="https://arxiv.org/abs/2010.04245">paper</a> proposes to l2 normalize the queries and keys along the head dimension before the dot product (cosine similarity), with the additional change of the scale being learned rather than static. The normalization prevents the attention operation from overflowing, and removes any need for numerical stability measures prior to softmax. Both are perennial problems when training transformers.
1278
+
1279
+ This was validated at scale recently by the training of <a href="https://arxiv.org/abs/2111.09883">a 3B parameter vision transformer</a>. The SwinV2 paper also proposes to change the pre-layernorm to a post-layernorm for further stability.
1280
+
1281
+ I have validated that this works just as well as dot product attention in an autoregressive setting, if one were to initialize the temperature as proposed in the QK-norm paper (as a function of the sequence length).
1282
+
1283
+ This flavor of attention also has <a href="https://arxiv.org/abs/2111.05498">a connection</a> to sparse distributed memory. <a href="https://www.youtube.com/watch?v=THIIk7LR9_8">[youtube talk]</a>
1284
+
1285
+ Update: I have discovered a way to remove the learned temperature altogether, by grouping the feature dimension and doing l2-normalization on each group. This allows the queries and keys to have a similarity that is upper bounded by the number of groups. A group size of 8 or 16 was sufficient in my tests. Decided to name this technique "Grouped QK Normalization". The drawback is that I believe an attention head dimension 32 is too small to use this tactic (a dimension often used in vision)
1286
+
1287
+ Update 2: Tero Karras has successfully used cosine sim attention in <a href="https://arxiv.org/abs/2312.02696">a new paper</a>.
1288
+
1289
+ You can use it as follows
1290
+
1291
+ ```python
1292
+ import torch
1293
+ from x_transformers import TransformerWrapper, Decoder
1294
+
1295
+ model = TransformerWrapper(
1296
+ num_tokens = 20000,
1297
+ max_seq_len = 1024,
1298
+ attn_layers = Decoder(
1299
+ dim = 512,
1300
+ depth = 6,
1301
+ heads = 8,
1302
+ attn_qk_norm = True, # set this to True
1303
+ attn_qk_norm_groups = 8 # number of groups in the feature dimension for l2norm, similarity scores will be bounded between [-group, group]. determines how sharp the attention can be
1304
+ )
1305
+ )
1306
+
1307
+ x = torch.randint(0, 20000, (1, 1024))
1308
+ model(x)
1309
+ ```
1310
+
1311
+ Another update: Simply scaling the cosine similarity (group of 1) with a fixed constant (10) may work too
1312
+
1313
+ ```python
1314
+ import torch
1315
+ from x_transformers import TransformerWrapper, Decoder
1316
+
1317
+ model = TransformerWrapper(
1318
+ num_tokens = 20000,
1319
+ max_seq_len = 1024,
1320
+ attn_layers = Decoder(
1321
+ dim = 512,
1322
+ depth = 6,
1323
+ heads = 8,
1324
+ attn_qk_norm = True, # set to True
1325
+ attn_qk_norm_scale = 10 # new scale on the similarity, with groups of 1
1326
+ )
1327
+ )
1328
+
1329
+ x = torch.randint(0, 20000, (1, 1024))
1330
+ model(x)
1331
+ ```
1332
+
1333
+ ### QK RMSNorm
1334
+
1335
+ <img src="./images/qknorm-analysis.png" width="450px"></img>
1336
+
1337
+ Update: Google Brain has proven out something similar to cosine sim attention in <a href="https://arxiv.org/abs/2302.05442">a 22B parameter model</a>. In their papers, they have analysis showing that the normalization resulted in not only extra stability, but also better results in the end (due to less need to adjust learning rate when increasing parameter count).
1338
+
1339
+ We are nearing the point of wiping out a source of transformer training instability with one simple intervention, in my opinion. The only slight difference in the paper is that they still have a learned scale across the feature dimension (per use of rmsnorm). Not sure how critical this is, but just to make sure we don't miss anything, I will include this here. You can use this by setting `qk_norm_dim_scale = True`
1340
+
1341
+ Update: <a href="https://twitter.com/Tim_Dettmers/status/1625531080513306627">Counterpoint from Tim Dettmers</a>
1342
+
1343
+ Update 2: <a href="https://arxiv.org/abs/2305.19268">Counter</a> to Tim's assertion that outliers are needed, and potentially even <a href="https://arxiv.org/abs/2306.12929">some solutions</a>
1344
+
1345
+ Update 3: Used by <a href="https://www.adept.ai/blog/persimmon-8b">8B parameter LLM</a> successfully
1346
+
1347
+ Update 4: a MetaAI group found that they can <a href="https://arxiv.org/abs/2309.16588">alleviate outliers</a> by adding `register tokens`, also known as `memory tokens` from earlier literature (Burtsev et al). Perhaps what should be tried next is see if qk norm can be improved in the presence of memory tokens.
1348
+
1349
+ ```python
1350
+ import torch
1351
+ from x_transformers import TransformerWrapper, Decoder
1352
+
1353
+ model = TransformerWrapper(
1354
+ num_tokens = 20000,
1355
+ max_seq_len = 1024,
1356
+ attn_layers = Decoder(
1357
+ dim = 512,
1358
+ depth = 12,
1359
+ heads = 8,
1360
+ attn_qk_norm = True,
1361
+ attn_qk_norm_dim_scale = True # set this to True, in addition to `attn_qk_norm = True`
1362
+ )
1363
+ )
1364
+
1365
+ x = torch.randint(0, 256, (1, 1024))
1366
+ model(x)
1367
+ ```
1368
+
1369
+ ### Turning off absolute positional embedding
1370
+
1371
+ A number of papers have hinted that causal transformers (`Decoder`) can learn absolute positions in the absence of added embeddings of any sort. This was recently thoroughly investigated <a href="https://arxiv.org/abs/2203.16634">here</a>. You can turn off the absolute positional embedding by setting `use_abs_pos_emb = False` in the `TransformerWrapper`
1372
+
1373
+ Given <a href="https://ai.googleblog.com/2022/04/pathways-language-model-palm-scaling-to.html">PaLM</a>, the trend going forward may be to forgo absolute positional embedding (again, for causal transformers only), and add relative positional embeddings with RoPE, ALiBi, etc.
1374
+
1375
+ Update: <a href="https://arxiv.org/abs/2305.19466">This paper</a> shows that in the absence of any engineered absolute or relative positional embeddings, decoders can generate implicit positions, and even length generalize better than solutions of the past. They were unaware of dynamic positional bias, however.
1376
+
1377
+ ```python
1378
+ import torch
1379
+ from x_transformers import TransformerWrapper, Decoder
1380
+
1381
+ model = TransformerWrapper(
1382
+ num_tokens = 20000,
1383
+ max_seq_len = 1024,
1384
+ use_abs_pos_emb = False, # set this to False
1385
+ attn_layers = Decoder(
1386
+ dim = 512,
1387
+ depth = 6,
1388
+ heads = 8,
1389
+ )
1390
+ )
1391
+
1392
+ x = torch.randint(0, 20000, (1, 1024))
1393
+ model(x)
1394
+ ```
1395
+
1396
+ ### Forgetful Causal Mask
1397
+
1398
+ <img src="./images/fcm.png" width="450px"></img>
1399
+
1400
+ <a href="https://arxiv.org/abs/2210.13432">This paper</a> shows convincing results that one can combine masking (from masked language modeling) with autoregressive training, leading to significantly better results.
1401
+
1402
+ You can use this by setting the `mask_prob` on the `AutoregressiveWrapper` class
1403
+
1404
+
1405
+ ```python
1406
+ import torch
1407
+ from x_transformers import TransformerWrapper, Decoder, AutoregressiveWrapper
1408
+
1409
+ model = TransformerWrapper(
1410
+ num_tokens = 20000,
1411
+ max_seq_len = 1024,
1412
+ attn_layers = Decoder(
1413
+ dim = 512,
1414
+ depth = 12,
1415
+ heads = 8
1416
+ )
1417
+ )
1418
+
1419
+ model = AutoregressiveWrapper(
1420
+ model,
1421
+ mask_prob = 0.15 # in paper, they use 15%, same as BERT
1422
+ ).cuda()
1423
+
1424
+ # mock data
1425
+
1426
+ x = torch.randint(0, 20000, (1, 1024)).cuda()
1427
+
1428
+ # derive cross entropy loss, masking all taken care of
1429
+
1430
+ loss = model(x)
1431
+ loss.backward()
1432
+ ```
1433
+
1434
+
1435
+ ## Miscellaneous
1436
+
1437
+ ### Cross Attention
1438
+
1439
+ ```python
1440
+ import torch
1441
+ from x_transformers import Encoder, CrossAttender
1442
+
1443
+ enc = Encoder(dim = 512, depth = 6)
1444
+ model = CrossAttender(dim = 512, depth = 6)
1445
+
1446
+ nodes = torch.randn(1, 1, 512)
1447
+ node_masks = torch.ones(1, 1).bool()
1448
+
1449
+ neighbors = torch.randn(1, 5, 512)
1450
+ neighbor_masks = torch.ones(1, 5).bool()
1451
+
1452
+ encoded_neighbors = enc(neighbors, mask = neighbor_masks)
1453
+ model(nodes, context = encoded_neighbors, mask = node_masks, context_mask = neighbor_masks) # (1, 1, 512)
1454
+
1455
+ ```
1456
+
1457
+ ### Continuous Embeddings
1458
+
1459
+ ```python
1460
+ import torch
1461
+ from x_transformers import ContinuousTransformerWrapper, Decoder
1462
+
1463
+ model = ContinuousTransformerWrapper(
1464
+ dim_in = 32,
1465
+ dim_out = 100,
1466
+ max_seq_len = 1024,
1467
+ attn_layers = Decoder(
1468
+ dim = 512,
1469
+ depth = 12,
1470
+ heads = 8
1471
+ )
1472
+ )
1473
+
1474
+ x = torch.randn((1, 1024, 32))
1475
+ mask = torch.ones(1, 1024).bool()
1476
+
1477
+ model(x, mask = mask) # (1, 1024, 100)
1478
+ ```
1479
+
1480
+ You can also train a transformer that accepts continuous values autoregressively easily, in the same scheme as done successfully in <a href="https://arxiv.org/abs/2112.05329">this paper</a>
1481
+
1482
+ ```python
1483
+ import torch
1484
+ from x_transformers import ContinuousTransformerWrapper, Decoder
1485
+ from x_transformers import ContinuousAutoregressiveWrapper
1486
+
1487
+ model = ContinuousTransformerWrapper(
1488
+ dim_in = 777,
1489
+ dim_out = 777,
1490
+ max_seq_len = 1024,
1491
+ attn_layers = Decoder(
1492
+ dim = 512,
1493
+ depth = 12,
1494
+ heads = 8
1495
+ )
1496
+ )
1497
+
1498
+ # wrap it with the continuous autoregressive wrapper
1499
+
1500
+ model = ContinuousAutoregressiveWrapper(model)
1501
+
1502
+ # mock data
1503
+
1504
+ x = torch.randn((1, 1024, 777))
1505
+ mask = torch.ones(1, 1024).bool()
1506
+
1507
+ # train on a lot of data above
1508
+
1509
+ loss = model(x, mask = mask)
1510
+ loss.backward
1511
+
1512
+ # then generate
1513
+
1514
+ start_emb = torch.randn(1, 777)
1515
+ generated = model.generate(start_emb, 17) # (17, 777)
1516
+ ```
1517
+
1518
+ ### xVal - Continuous and Discrete
1519
+
1520
+ <img src="./images/xval.png" width="400px"></img>
1521
+
1522
+ This is promising work that resulted from the collaboration across many institutes (collectively known as Polymathic AI). They found that by offering a continuously scaled number token to the transformer, the transformer was able to generalize arithmetic and forecasting tasks better than the alternative encoding schemes.
1523
+
1524
+ This is corroborated by some [prior work](https://github.com/lucidrains/tab-transformer-pytorch#ft-transformer)
1525
+
1526
+ ```python
1527
+ import torch
1528
+
1529
+ from x_transformers import (
1530
+ Decoder,
1531
+ XValTransformerWrapper,
1532
+ XValAutoregressiveWrapper
1533
+ )
1534
+
1535
+ model = XValTransformerWrapper(
1536
+ num_tokens = 4,
1537
+ numerical_token_id = 3,
1538
+ max_seq_len = 1024,
1539
+ attn_layers = Decoder(
1540
+ dim = 512,
1541
+ depth = 12,
1542
+ heads = 8
1543
+ )
1544
+ )
1545
+
1546
+ # wrap it with the xval autoregressive wrapper
1547
+
1548
+ model = XValAutoregressiveWrapper(model)
1549
+
1550
+ # mock data
1551
+
1552
+ ids = torch.randint(0, 4, (1, 777))
1553
+ nums = torch.randn(1, 777)
1554
+
1555
+ # train on a lot of data above
1556
+
1557
+ loss = model(ids, nums)
1558
+ loss.backward()
1559
+
1560
+ # then generate
1561
+
1562
+ start_ids = torch.randint(0, 4, (1, 1))
1563
+ start_nums = torch.randn(1, 1)
1564
+
1565
+ ids_out, num_out, is_number_mask = model.generate(start_ids, start_nums, 17)
1566
+
1567
+ # (1, 17), (1, 17), (1, 17)
1568
+
1569
+ # discrete, continuous, mask for discrete / continuous
1570
+ ```
1571
+
1572
+ ## Citations
1573
+
1574
+ ```bibtex
1575
+ @misc{vaswani2017attention,
1576
+ title = {Attention Is All You Need},
1577
+ author = {Ashish Vaswani and Noam Shazeer and Niki Parmar and Jakob Uszkoreit and Llion Jones and Aidan N. Gomez and Lukasz Kaiser and Illia Polosukhin},
1578
+ year = {2017},
1579
+ eprint = {1706.03762},
1580
+ archivePrefix = {arXiv},
1581
+ primaryClass = {cs.CL}
1582
+ }
1583
+ ```
1584
+
1585
+ ```bibtex
1586
+ @article{DBLP:journals/corr/abs-1907-01470,
1587
+ author = {Sainbayar Sukhbaatar and Edouard Grave and Guillaume Lample and Herv{\'{e}} J{\'{e}}gou and Armand Joulin},
1588
+ title = {Augmenting Self-attention with Persistent Memory},
1589
+ journal = {CoRR},
1590
+ volume = {abs/1907.01470},
1591
+ year = {2019},
1592
+ url = {http://arxiv.org/abs/1907.01470}
1593
+ }
1594
+ ```
1595
+
1596
+ ```bibtex
1597
+ @article{1910.05895,
1598
+ author = {Toan Q. Nguyen and Julian Salazar},
1599
+ title = {Transformers without Tears: Improving the Normalization of Self-Attention},
1600
+ year = {2019},
1601
+ eprint = {arXiv:1910.05895},
1602
+ doi = {10.5281/zenodo.3525484},
1603
+ }
1604
+ ```
1605
+
1606
+ ```bibtex
1607
+ @misc{shazeer2020glu,
1608
+ title = {GLU Variants Improve Transformer},
1609
+ author = {Noam Shazeer},
1610
+ year = {2020},
1611
+ url = {https://arxiv.org/abs/2002.05202}
1612
+ }
1613
+ ```
1614
+
1615
+ ```bibtex
1616
+ @inproceedings{Zoph2022STMoEDS,
1617
+ title = {ST-MoE: Designing Stable and Transferable Sparse Expert Models},
1618
+ author = {Barret Zoph and Irwan Bello and Sameer Kumar and Nan Du and Yanping Huang and Jeff Dean and Noam M. Shazeer and William Fedus},
1619
+ year = {2022}
1620
+ }
1621
+ ```
1622
+
1623
+ ```bibtex
1624
+ @misc{bhojanapalli2020lowrank,
1625
+ title = {Low-Rank Bottleneck in Multi-head Attention Models},
1626
+ author = {Srinadh Bhojanapalli and Chulhee Yun and Ankit Singh Rawat and Sashank J. Reddi and Sanjiv Kumar},
1627
+ year = {2020},
1628
+ eprint = {2002.07028}
1629
+ }
1630
+ ```
1631
+
1632
+ ```bibtex
1633
+ @misc{burtsev2020memory,
1634
+ title = {Memory Transformer},
1635
+ author = {Mikhail S. Burtsev and Grigory V. Sapunov},
1636
+ year = {2020},
1637
+ eprint = {2006.11527},
1638
+ archivePrefix = {arXiv},
1639
+ primaryClass = {cs.CL}
1640
+ }
1641
+ ```
1642
+
1643
+ ```bibtex
1644
+ @misc{zhao2019explicit,
1645
+ title = {Explicit Sparse Transformer: Concentrated Attention Through Explicit Selection},
1646
+ author = {Guangxiang Zhao and Junyang Lin and Zhiyuan Zhang and Xuancheng Ren and Qi Su and Xu Sun},
1647
+ year = {2019},
1648
+ eprint = {1912.11637},
1649
+ archivePrefix = {arXiv},
1650
+ primaryClass = {cs.CL}
1651
+ }
1652
+ ```
1653
+
1654
+ ```bibtex
1655
+ @misc{correia2019adaptively,
1656
+ title = {Adaptively Sparse Transformers},
1657
+ author = {Gonçalo M. Correia and Vlad Niculae and André F. T. Martins},
1658
+ year = {2019},
1659
+ eprint = {1909.00015},
1660
+ archivePrefix = {arXiv},
1661
+ primaryClass = {cs.CL}
1662
+ }
1663
+ ```
1664
+
1665
+ ```bibtex
1666
+ @misc{shazeer2020talkingheads,
1667
+ title = {Talking-Heads Attention},
1668
+ author = {Noam Shazeer and Zhenzhong Lan and Youlong Cheng and Nan Ding and Le Hou},
1669
+ year = {2020},
1670
+ eprint = {2003.02436},
1671
+ archivePrefix = {arXiv},
1672
+ primaryClass = {cs.LG}
1673
+ }
1674
+ ```
1675
+
1676
+ ```bibtex
1677
+ @misc{press2020improving,
1678
+ title = {Improving Transformer Models by Reordering their Sublayers},
1679
+ author = {Ofir Press and Noah A. Smith and Omer Levy},
1680
+ year = {2020},
1681
+ eprint = {1911.03864},
1682
+ archivePrefix = {arXiv},
1683
+ primaryClass = {cs.CL}
1684
+ }
1685
+ ```
1686
+
1687
+ ```bibtex
1688
+ @misc{lu2019understanding,
1689
+ title = {Understanding and Improving Transformer From a Multi-Particle Dynamic System Point of View},
1690
+ author = {Yiping Lu and Zhuohan Li and Di He and Zhiqing Sun and Bin Dong and Tao Qin and Liwei Wang and Tie-Yan Liu},
1691
+ year = {2019},
1692
+ eprint = {1906.02762},
1693
+ archivePrefix = {arXiv},
1694
+ primaryClass = {cs.LG}
1695
+ }
1696
+ ```
1697
+
1698
+ ```bibtex
1699
+ @misc{ke2020rethinking,
1700
+ title = {Rethinking Positional Encoding in Language Pre-training},
1701
+ author = {Guolin Ke and Di He and Tie-Yan Liu},
1702
+ year = {2020},
1703
+ eprint = {2006.15595},
1704
+ archivePrefix = {arXiv},
1705
+ primaryClass = {cs.CL}
1706
+ }
1707
+ ```
1708
+
1709
+ ```bibtex
1710
+ @misc{dosovitskiy2020image,
1711
+ title = {An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale},
1712
+ author = {Alexey Dosovitskiy and Lucas Beyer and Alexander Kolesnikov and Dirk Weissenborn and Xiaohua Zhai and Thomas Unterthiner and Mostafa Dehghani and Matthias Minderer and Georg Heigold and Sylvain Gelly and Jakob Uszkoreit and Neil Houlsby},
1713
+ year = {2020},
1714
+ eprint = {2010.11929},
1715
+ archivePrefix = {arXiv},
1716
+ primaryClass = {cs.CV}
1717
+ }
1718
+ ```
1719
+
1720
+ ```bibtex
1721
+ @misc{huang2019attention,
1722
+ title = {Attention on Attention for Image Captioning},
1723
+ author = {Lun Huang and Wenmin Wang and Jie Chen and Xiao-Yong Wei},
1724
+ year = {2019},
1725
+ eprint = {1908.06954},
1726
+ archivePrefix = {arXiv},
1727
+ primaryClass = {cs.CV}
1728
+ }
1729
+ ```
1730
+
1731
+ ```bibtex
1732
+ @misc{raffel2020exploring,
1733
+ title = {Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer},
1734
+ author = {Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu},
1735
+ year = {2020},
1736
+ eprint = {1910.10683},
1737
+ archivePrefix = {arXiv},
1738
+ primaryClass = {cs.LG}
1739
+ }
1740
+ ```
1741
+
1742
+ ```bibtex
1743
+ @inproceedings{martins-etal-2020-sparse,
1744
+ title = "Sparse Text Generation",
1745
+ author = "Martins, Pedro Henrique and
1746
+ Marinho, Zita and
1747
+ Martins, Andr{\'e} F. T.",
1748
+ booktitle = "Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP)",
1749
+ month = nov,
1750
+ year = "2020",
1751
+ address = "Online",
1752
+ publisher = "Association for Computational Linguistics",
1753
+ url = "https://www.aclweb.org/anthology/2020.emnlp-main.348"
1754
+ }
1755
+ ```
1756
+
1757
+ ```bibtex
1758
+ @misc{he2020realformer,
1759
+ title = {RealFormer: Transformer Likes Residual Attention},
1760
+ author = {Ruining He and Anirudh Ravula and Bhargav Kanagal and Joshua Ainslie},
1761
+ year = {2020},
1762
+ eprint = {2012.11747},
1763
+ archivePrefix = {arXiv},
1764
+ primaryClass = {cs.LG}
1765
+ }
1766
+ ```
1767
+
1768
+ ```bibtex
1769
+ @misc{carion2020endtoend,
1770
+ title = {End-to-End Object Detection with Transformers},
1771
+ author = {Nicolas Carion and Francisco Massa and Gabriel Synnaeve and Nicolas Usunier and Alexander Kirillov and Sergey Zagoruyko},
1772
+ year = {2020},
1773
+ eprint = {2005.12872},
1774
+ archivePrefix = {arXiv},
1775
+ primaryClass = {cs.CV}
1776
+ }
1777
+ ```
1778
+
1779
+ ```bibtex
1780
+ @misc{press2021ALiBi,
1781
+ title = {Train Short, Test Long: Attention with Linear Biases Enable Input Length Extrapolation},
1782
+ author = {Ofir Press and Noah A. Smith and Mike Lewis},
1783
+ year = {2021},
1784
+ url = {https://ofir.io/train_short_test_long.pdf}
1785
+ }
1786
+ ```
1787
+
1788
+ ```bibtex
1789
+ @misc{parisotto2019stabilizing,
1790
+ title = {Stabilizing Transformers for Reinforcement Learning},
1791
+ author = {Emilio Parisotto and H. Francis Song and Jack W. Rae and Razvan Pascanu and Caglar Gulcehre and Siddhant M. Jayakumar and Max Jaderberg and Raphael Lopez Kaufman and Aidan Clark and Seb Noury and Matthew M. Botvinick and Nicolas Heess and Raia Hadsell},
1792
+ year = {2019},
1793
+ eprint = {1910.06764},
1794
+ archivePrefix = {arXiv},
1795
+ primaryClass = {cs.LG}
1796
+ }
1797
+ ```
1798
+
1799
+ ```bibtex
1800
+ @misc{narang2021transformer,
1801
+ title = {Do Transformer Modifications Transfer Across Implementations and Applications?},
1802
+ author = {Sharan Narang and Hyung Won Chung and Yi Tay and William Fedus and Thibault Fevry and Michael Matena and Karishma Malkan and Noah Fiedel and Noam Shazeer and Zhenzhong Lan and Yanqi Zhou and Wei Li and Nan Ding and Jake Marcus and Adam Roberts and Colin Raffel},
1803
+ year = {2021},
1804
+ eprint = {2102.11972},
1805
+ archivePrefix = {arXiv},
1806
+ primaryClass = {cs.LG}
1807
+ }
1808
+ ```
1809
+
1810
+ ```bibtex
1811
+ @misc{zhang2019root,
1812
+ title = {Root Mean Square Layer Normalization},
1813
+ author = {Biao Zhang and Rico Sennrich},
1814
+ year = {2019},
1815
+ eprint = {1910.07467},
1816
+ archivePrefix = {arXiv},
1817
+ primaryClass = {cs.LG}
1818
+ }
1819
+ ```
1820
+
1821
+ ```bibtex
1822
+ @inproceedings{Qin2023ScalingTT,
1823
+ title = {Scaling TransNormer to 175 Billion Parameters},
1824
+ author = {Zhen Qin and Dong Li and Weigao Sun and Weixuan Sun and Xuyang Shen and Xiaodong Han and Yunshen Wei and Baohong Lv and Fei Yuan and Xiao Luo and Y. Qiao and Yiran Zhong},
1825
+ year = {2023},
1826
+ url = {https://api.semanticscholar.org/CorpusID:260203124}
1827
+ }
1828
+ ```
1829
+
1830
+ ```bibtex
1831
+ @misc{su2021roformer,
1832
+ title = {RoFormer: Enhanced Transformer with Rotary Position Embedding},
1833
+ author = {Jianlin Su and Yu Lu and Shengfeng Pan and Bo Wen and Yunfeng Liu},
1834
+ year = {2021},
1835
+ eprint = {2104.09864},
1836
+ archivePrefix = {arXiv},
1837
+ primaryClass = {cs.CL}
1838
+ }
1839
+ ```
1840
+
1841
+ ```bibtex
1842
+ @inproceedings{Chen2023ExtendingCW,
1843
+ title = {Extending Context Window of Large Language Models via Positional Interpolation},
1844
+ author = {Shouyuan Chen and Sherman Wong and Liangjian Chen and Yuandong Tian},
1845
+ year = {2023}
1846
+ }
1847
+ ```
1848
+
1849
+ ```bibtex
1850
+ @inproceedings{Sun2022ALT,
1851
+ title = {A Length-Extrapolatable Transformer},
1852
+ author = {Yutao Sun and Li Dong and Barun Patra and Shuming Ma and Shaohan Huang and Alon Benhaim and Vishrav Chaudhary and Xia Song and Furu Wei},
1853
+ year = {2022}
1854
+ }
1855
+ ```
1856
+
1857
+ ```bibtex
1858
+ @Article{AlphaFold2021,
1859
+ author = {Jumper, John and Evans, Richard and Pritzel, Alexander and Green, Tim and Figurnov, Michael and Ronneberger, Olaf and Tunyasuvunakool, Kathryn and Bates, Russ and {\v{Z}}{\'\i}dek, Augustin and Potapenko, Anna and Bridgland, Alex and Meyer, Clemens and Kohl, Simon A A and Ballard, Andrew J and Cowie, Andrew and Romera-Paredes, Bernardino and Nikolov, Stanislav and Jain, Rishub and Adler, Jonas and Back, Trevor and Petersen, Stig and Reiman, David and Clancy, Ellen and Zielinski, Michal and Steinegger, Martin and Pacholska, Michalina and Berghammer, Tamas and Bodenstein, Sebastian and Silver, David and Vinyals, Oriol and Senior, Andrew W and Kavukcuoglu, Koray and Kohli, Pushmeet and Hassabis, Demis},
1860
+ journal = {Nature},
1861
+ title = {Highly accurate protein structure prediction with {AlphaFold}},
1862
+ year = {2021},
1863
+ doi = {10.1038/s41586-021-03819-2},
1864
+ note = {(Accelerated article preview)},
1865
+ }
1866
+ ```
1867
+
1868
+ ```bibtex
1869
+ @software{peng_bo_2021_5196578,
1870
+ author = {PENG Bo},
1871
+ title = {BlinkDL/RWKV-LM: 0.01},
1872
+ month = {aug},
1873
+ year = {2021},
1874
+ publisher = {Zenodo},
1875
+ version = {0.01},
1876
+ doi = {10.5281/zenodo.5196578},
1877
+ url = {https://doi.org/10.5281/zenodo.5196578}
1878
+ }
1879
+ ```
1880
+
1881
+ ```bibtex
1882
+ @misc{csordás2021devil,
1883
+ title = {The Devil is in the Detail: Simple Tricks Improve Systematic Generalization of Transformers},
1884
+ author = {Róbert Csordás and Kazuki Irie and Jürgen Schmidhuber},
1885
+ year = {2021},
1886
+ eprint = {2108.12284},
1887
+ archivePrefix = {arXiv},
1888
+ primaryClass = {cs.LG}
1889
+ }
1890
+ ```
1891
+
1892
+ ```bibtex
1893
+ @misc{so2021primer,
1894
+ title = {Primer: Searching for Efficient Transformers for Language Modeling},
1895
+ author = {David R. So and Wojciech Mańke and Hanxiao Liu and Zihang Dai and Noam Shazeer and Quoc V. Le},
1896
+ year = {2021},
1897
+ eprint = {2109.08668},
1898
+ archivePrefix = {arXiv},
1899
+ primaryClass = {cs.LG}
1900
+ }
1901
+ ```
1902
+
1903
+ ```bibtex
1904
+ @misc{ding2021erniedoc,
1905
+ title = {ERNIE-Doc: A Retrospective Long-Document Modeling Transformer},
1906
+ author = {Siyu Ding and Junyuan Shang and Shuohuan Wang and Yu Sun and Hao Tian and Hua Wu and Haifeng Wang},
1907
+ year = {2021},
1908
+ eprint = {2012.15688},
1909
+ archivePrefix = {arXiv},
1910
+ primaryClass = {cs.CL}
1911
+ }
1912
+ ```
1913
+
1914
+ ```bibtex
1915
+ @misc{ding2021cogview,
1916
+ title = {CogView: Mastering Text-to-Image Generation via Transformers},
1917
+ author = {Ming Ding and Zhuoyi Yang and Wenyi Hong and Wendi Zheng and Chang Zhou and Da Yin and Junyang Lin and Xu Zou and Zhou Shao and Hongxia Yang and Jie Tang},
1918
+ year = {2021},
1919
+ eprint = {2105.13290},
1920
+ archivePrefix = {arXiv},
1921
+ primaryClass = {cs.CV}
1922
+ }
1923
+ ```
1924
+
1925
+ ```bibtex
1926
+ @inproceedings{anonymous2022normformer,
1927
+ title = {NormFormer: Improved Transformer Pretraining with Extra Normalization},
1928
+ author = {Anonymous},
1929
+ booktitle = {Submitted to The Tenth International Conference on Learning Representations },
1930
+ year = {2022},
1931
+ url = {https://openreview.net/forum?id=GMYWzWztDx5},
1932
+ note = {under review}
1933
+ }
1934
+ ```
1935
+
1936
+ ```bibtex
1937
+ @misc{henry2020querykey,
1938
+ title = {Query-Key Normalization for Transformers},
1939
+ author = {Alex Henry and Prudhvi Raj Dachapally and Shubham Pawar and Yuxuan Chen},
1940
+ year = {2020},
1941
+ eprint = {2010.04245},
1942
+ archivePrefix = {arXiv},
1943
+ primaryClass = {cs.CL}
1944
+ }
1945
+ ```
1946
+
1947
+ ```bibtex
1948
+ @misc{liu2021swin,
1949
+ title = {Swin Transformer V2: Scaling Up Capacity and Resolution},
1950
+ author = {Ze Liu and Han Hu and Yutong Lin and Zhuliang Yao and Zhenda Xie and Yixuan Wei and Jia Ning and Yue Cao and Zheng Zhang and Li Dong and Furu Wei and Baining Guo},
1951
+ year = {2021},
1952
+ eprint = {2111.09883},
1953
+ archivePrefix = {arXiv},
1954
+ primaryClass = {cs.CV}
1955
+ }
1956
+ ```
1957
+
1958
+ ```bibtex
1959
+ @article{Haviv2022TransformerLM,
1960
+ title = {Transformer Language Models without Positional Encodings Still Learn Positional Information},
1961
+ author = {Adi Haviv and Ori Ram and Ofir Press and Peter Izsak and Omer Levy},
1962
+ journal = {ArXiv},
1963
+ year = {2022},
1964
+ volume = {abs/2203.16634}
1965
+ }
1966
+ ```
1967
+
1968
+ ```bibtex
1969
+ @article{chowdhery2022PaLM,
1970
+ title = {PaLM: Scaling Language Modeling with Pathways},
1971
+ author = {Chowdhery, Aakanksha et al},
1972
+ year = {2022}
1973
+ }
1974
+ ```
1975
+
1976
+ ```bibtex
1977
+ @article{Shazeer2019FastTD,
1978
+ title = {Fast Transformer Decoding: One Write-Head is All You Need},
1979
+ author = {Noam M. Shazeer},
1980
+ journal = {ArXiv},
1981
+ year = {2019},
1982
+ volume = {abs/1911.02150}
1983
+ }
1984
+ ```
1985
+
1986
+ ```bibtex
1987
+ @article{Ainslie2023GQATG,
1988
+ title = {GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints},
1989
+ author = {Joshua Ainslie and James Lee-Thorp and Michiel de Jong and Yury Zemlyanskiy and Federico Lebr'on and Sumit K. Sanghai},
1990
+ journal = {ArXiv},
1991
+ year = {2023},
1992
+ volume = {abs/2305.13245},
1993
+ url = {https://api.semanticscholar.org/CorpusID:258833177}
1994
+ }
1995
+ ```
1996
+
1997
+ ```bibtex
1998
+ @article{Liu2022FCMFC,
1999
+ title = {FCM: Forgetful Causal Masking Makes Causal Language Models Better Zero-Shot Learners},
2000
+ author = {Hao Liu and Xinyang Geng and Lisa Lee and Igor Mordatch and Sergey Levine and Sharan Narang and P. Abbeel},
2001
+ journal = {ArXiv},
2002
+ year = {2022},
2003
+ volume = {abs/2210.13432}
2004
+ }
2005
+ ```
2006
+
2007
+ ```bibtex
2008
+ @inproceedings{Huang2016DeepNW,
2009
+ title = {Deep Networks with Stochastic Depth},
2010
+ author = {Gao Huang and Yu Sun and Zhuang Liu and Daniel Sedra and Kilian Q. Weinberger},
2011
+ booktitle = {European Conference on Computer Vision},
2012
+ year = {2016}
2013
+ }
2014
+ ```
2015
+
2016
+ ```bibtex
2017
+ @inproceedings{Hua2022TransformerQI,
2018
+ title = {Transformer Quality in Linear Time},
2019
+ author = {Weizhe Hua and Zihang Dai and Hanxiao Liu and Quoc V. Le},
2020
+ booktitle = {International Conference on Machine Learning},
2021
+ year = {2022}
2022
+ }
2023
+ ```
2024
+
2025
+ ```bibtex
2026
+ @article{Chang2022MaskGITMG,
2027
+ title = {MaskGIT: Masked Generative Image Transformer},
2028
+ author = {Huiwen Chang and Han Zhang and Lu Jiang and Ce Liu and William T. Freeman},
2029
+ journal = {2022 IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},
2030
+ year = {2022},
2031
+ pages = {11305-11315}
2032
+ }
2033
+ ```
2034
+
2035
+ ```bibtex
2036
+ @article{Lezama2022ImprovedMI,
2037
+ title = {Improved Masked Image Generation with Token-Critic},
2038
+ author = {Jos{\'e} Lezama and Huiwen Chang and Lu Jiang and Irfan Essa},
2039
+ journal = {ArXiv},
2040
+ year = {2022},
2041
+ volume = {abs/2209.04439}
2042
+ }
2043
+ ```
2044
+
2045
+ ```bibtex
2046
+ @misc{https://doi.org/10.48550/arxiv.2302.01327,
2047
+ doi = {10.48550/ARXIV.2302.01327},
2048
+ url = {https://arxiv.org/abs/2302.01327},
2049
+ author = {Kumar, Manoj and Dehghani, Mostafa and Houlsby, Neil},
2050
+ title = {Dual PatchNorm},
2051
+ publisher = {arXiv},
2052
+ year = {2023},
2053
+ copyright = {Creative Commons Attribution 4.0 International}
2054
+ }
2055
+ ```
2056
+
2057
+ ```bibtex
2058
+ @inproceedings{dao2022flashattention,
2059
+ title = {Flash{A}ttention: Fast and Memory-Efficient Exact Attention with {IO}-Awareness},
2060
+ author = {Dao, Tri and Fu, Daniel Y. and Ermon, Stefano and Rudra, Atri and R{\'e}, Christopher},
2061
+ booktitle = {Advances in Neural Information Processing Systems},
2062
+ year = {2022}
2063
+ }
2064
+ ```
2065
+
2066
+ ```bibtex
2067
+ @inproceedings{Dehghani2023ScalingVT,
2068
+ title = {Scaling Vision Transformers to 22 Billion Parameters},
2069
+ author = {Mostafa Dehghani and Josip Djolonga and Basil Mustafa and Piotr Padlewski and Jonathan Heek and Justin Gilmer and Andreas Steiner and Mathilde Caron and Robert Geirhos and Ibrahim M. Alabdulmohsin and Rodolphe Jenatton and Lucas Beyer and Michael Tschannen and Anurag Arnab and Xiao Wang and Carlos Riquelme and Matthias Minderer and Joan Puigcerver and Utku Evci and Manoj Kumar and Sjoerd van Steenkiste and Gamaleldin F. Elsayed and Aravindh Mahendran and Fisher Yu and Avital Oliver and Fantine Huot and Jasmijn Bastings and Mark Collier and Alexey A. Gritsenko and Vighnesh Birodkar and Cristina Nader Vasconcelos and Yi Tay and Thomas Mensink and Alexander Kolesnikov and Filip Paveti'c and Dustin Tran and Thomas Kipf and Mario Luvci'c and Xiaohua Zhai and Daniel Keysers and Jeremiah Harmsen and Neil Houlsby},
2070
+ year = {2023}
2071
+ }
2072
+ ```
2073
+
2074
+ ```bibtex
2075
+ @article{Beyer2022BetterPV,
2076
+ title = {Better plain ViT baselines for ImageNet-1k},
2077
+ author = {Lucas Beyer and Xiaohua Zhai and Alexander Kolesnikov},
2078
+ journal = {ArXiv},
2079
+ year = {2022},
2080
+ volume = {abs/2205.01580}
2081
+ }
2082
+ ```
2083
+
2084
+ ```bibtex
2085
+ @article{Kazemnejad2023TheIO,
2086
+ title = {The Impact of Positional Encoding on Length Generalization in Transformers},
2087
+ author = {Amirhossein Kazemnejad and Inkit Padhi and Karthikeyan Natesan Ramamurthy and Payel Das and Siva Reddy},
2088
+ journal = {ArXiv},
2089
+ year = {2023},
2090
+ volume = {abs/2305.19466}
2091
+ }
2092
+ ```
2093
+
2094
+ ```bibtex
2095
+ @misc{bloc97-2023
2096
+ title = {NTK-Aware Scaled RoPE allows LLaMA models to have extended (8k+) context size without any fine-tuning and minimal perplexity degradation.},
2097
+ author = {/u/bloc97},
2098
+ url = {https://www.reddit.com/r/LocalLLaMA/comments/14lz7j5/ntkaware_scaled_rope_allows_llama_models_to_have/}
2099
+ }
2100
+ ```
2101
+
2102
+ ```bibtex
2103
+ @inproceedings{Zoph2022STMoEDS,
2104
+ title = {ST-MoE: Designing Stable and Transferable Sparse Expert Models},
2105
+ author = {Barret Zoph and Irwan Bello and Sameer Kumar and Nan Du and Yanping Huang and Jeff Dean and Noam M. Shazeer and William Fedus},
2106
+ year = {2022}
2107
+ }
2108
+ ```
2109
+
2110
+ ```bibtex
2111
+ @article{Lan2019ALBERTAL,
2112
+ title = {ALBERT: A Lite BERT for Self-supervised Learning of Language Representations},
2113
+ author = {Zhenzhong Lan and Mingda Chen and Sebastian Goodman and Kevin Gimpel and Piyush Sharma and Radu Soricut},
2114
+ journal = {ArXiv},
2115
+ year = {2019},
2116
+ volume = {abs/1909.11942},
2117
+ url = {https://api.semanticscholar.org/CorpusID:202888986}
2118
+ }
2119
+ ```
2120
+
2121
+ ```bibtex
2122
+ @inproceedings{Li2022ContrastiveDO,
2123
+ title = {Contrastive Decoding: Open-ended Text Generation as Optimization},
2124
+ author = {Xiang Lisa Li and Ari Holtzman and Daniel Fried and Percy Liang and Jason Eisner and Tatsunori Hashimoto and Luke Zettlemoyer and Mike Lewis},
2125
+ booktitle = {Annual Meeting of the Association for Computational Linguistics},
2126
+ year = {2022},
2127
+ url = {https://api.semanticscholar.org/CorpusID:253157949}
2128
+ }
2129
+ ```
2130
+
2131
+ ```bibtex
2132
+ @inproceedings{OBrien2023ContrastiveDI,
2133
+ title = {Contrastive Decoding Improves Reasoning in Large Language Models},
2134
+ author = {Sean O'Brien and Mike Lewis},
2135
+ year = {2023},
2136
+ url = {https://api.semanticscholar.org/CorpusID:261884427}
2137
+ }
2138
+ ```
2139
+
2140
+ ```bibtex
2141
+ @inproceedings{Darcet2023VisionTN,
2142
+ title = {Vision Transformers Need Registers},
2143
+ author = {Timoth'ee Darcet and Maxime Oquab and Julien Mairal and Piotr Bojanowski},
2144
+ year = {2023},
2145
+ url = {https://api.semanticscholar.org/CorpusID:263134283}
2146
+ }
2147
+ ```
2148
+
2149
+ ```bibtex
2150
+ @article{Bondarenko2023QuantizableTR,
2151
+ title = {Quantizable Transformers: Removing Outliers by Helping Attention Heads Do Nothing},
2152
+ author = {Yelysei Bondarenko and Markus Nagel and Tijmen Blankevoort},
2153
+ journal = {ArXiv},
2154
+ year = {2023},
2155
+ volume = {abs/2306.12929},
2156
+ url = {https://api.semanticscholar.org/CorpusID:259224568}
2157
+ }
2158
+ ```
2159
+
2160
+ ```bibtex
2161
+ @inproceedings{Golkar2023xValAC,
2162
+ title = {xVal: A Continuous Number Encoding for Large Language Models},
2163
+ author = {Siavash Golkar and Mariel Pettee and Michael Eickenberg and Alberto Bietti and M. Cranmer and G{\'e}raud Krawezik and Francois Lanusse and Michael McCabe and Ruben Ohana and Liam Parker and Bruno R{\'e}galdo-Saint Blancard and Tiberiu Teşileanu and Kyunghyun Cho and Shirley Ho},
2164
+ year = {2023},
2165
+ url = {https://api.semanticscholar.org/CorpusID:263622222}
2166
+ }
2167
+ ```
2168
+
2169
+ ```bibtex
2170
+ @article{Wang2022DeepNetST,
2171
+ title = {DeepNet: Scaling Transformers to 1, 000 Layers},
2172
+ author = {Hongyu Wang and Shuming Ma and Li Dong and Shaohan Huang and Dongdong Zhang and Furu Wei},
2173
+ journal = {ArXiv},
2174
+ year = {2022},
2175
+ volume = {abs/2203.00555},
2176
+ url = {https://api.semanticscholar.org/CorpusID:247187905}
2177
+ }
2178
+ ```
2179
+
2180
+ ```bibtex
2181
+ @article{Rafailov2023DirectPO,
2182
+ title = {Direct Preference Optimization: Your Language Model is Secretly a Reward Model},
2183
+ author = {Rafael Rafailov and Archit Sharma and Eric Mitchell and Stefano Ermon and Christopher D. Manning and Chelsea Finn},
2184
+ journal = {ArXiv},
2185
+ year = {2023},
2186
+ volume = {abs/2305.18290},
2187
+ url = {https://api.semanticscholar.org/CorpusID:258959321}
2188
+ }
2189
+ ```
2190
+
2191
+ ```bibtex
2192
+ @misc{xAI2024Grok,
2193
+ author = {xAI},
2194
+ title = {Grok},
2195
+ year = {2024},
2196
+ publisher = {GitHub},
2197
+ journal = {GitHub repository},
2198
+ howpublished = {\url{https://github.com/xai-org/grok-1}},
2199
+ }
2200
+ ```
2201
+
2202
+ ```bibtex
2203
+ @inproceedings{Golovneva2024ContextualPE,
2204
+ title = {Contextual Position Encoding: Learning to Count What's Important},
2205
+ author = {Olga Golovneva and Tianlu Wang and Jason Weston and Sainbayar Sukhbaatar},
2206
+ year = {2024},
2207
+ url = {https://api.semanticscholar.org/CorpusID:270094992}
2208
+ }
2209
+ ```
2210
+
2211
+ ```bibtex
2212
+ @article{Peebles2022ScalableDM,
2213
+ title = {Scalable Diffusion Models with Transformers},
2214
+ author = {William S. Peebles and Saining Xie},
2215
+ journal = {2023 IEEE/CVF International Conference on Computer Vision (ICCV)},
2216
+ year = {2022},
2217
+ pages = {4172-4182},
2218
+ url = {https://api.semanticscholar.org/CorpusID:254854389}
2219
+ }
2220
+ ```
2221
+
2222
+ ```bibtex
2223
+ @misc{Rubin2024,
2224
+ author = {Ohad Rubin},
2225
+ url = {https://medium.com/@ohadrubin/exploring-weight-decay-in-layer-normalization-challenges-and-a-reparameterization-solution-ad4d12c24950}
2226
+ }
2227
+ ```
2228
+
2229
+ ```bibtex
2230
+ @article{Mesnard2024GemmaOM,
2231
+ title = {Gemma: Open Models Based on Gemini Research and Technology},
2232
+ author = {Gemma Team Thomas Mesnard and Cassidy Hardin and Robert Dadashi and Surya Bhupatiraju and Shreya Pathak and L. Sifre and Morgane Riviere and Mihir Kale and J Christopher Love and Pouya Dehghani Tafti and L'eonard Hussenot and Aakanksha Chowdhery and Adam Roberts and Aditya Barua and Alex Botev and Alex Castro-Ros and Ambrose Slone and Am'elie H'eliou and Andrea Tacchetti and Anna Bulanova and Antonia Paterson and Beth Tsai and Bobak Shahriari and Charline Le Lan and Christopher A. Choquette-Choo and Cl'ement Crepy and Daniel Cer and Daphne Ippolito and David Reid and Elena Buchatskaya and Eric Ni and Eric Noland and Geng Yan and George Tucker and George-Christian Muraru and Grigory Rozhdestvenskiy and Henryk Michalewski and Ian Tenney and Ivan Grishchenko and Jacob Austin and James Keeling and Jane Labanowski and Jean-Baptiste Lespiau and Jeff Stanway and Jenny Brennan and Jeremy Chen and Johan Ferret and Justin Chiu and Justin Mao-Jones and Katherine Lee and Kathy Yu and Katie Millican and Lars Lowe Sjoesund and Lisa Lee and Lucas Dixon and Machel Reid and Maciej Mikula and Mateo Wirth and Michael Sharman and Nikolai Chinaev and Nithum Thain and Olivier Bachem and Oscar Chang and Oscar Wahltinez and Paige Bailey and Paul Michel and Petko Yotov and Pier Giuseppe Sessa and Rahma Chaabouni and Ramona Comanescu and Reena Jana and Rohan Anil and Ross McIlroy and Ruibo Liu and Ryan Mullins and Samuel L Smith and Sebastian Borgeaud and Sertan Girgin and Sholto Douglas and Shree Pandya and Siamak Shakeri and Soham De and Ted Klimenko and Tom Hennigan and Vladimir Feinberg and Wojciech Stokowiec and Yu-hui Chen and Zafarali Ahmed and Zhitao Gong and Tris Brian Warkentin and Ludovic Peran and Minh Giang and Cl'ement Farabet and Oriol Vinyals and Jeffrey Dean and Koray Kavukcuoglu and Demis Hassabis and Zoubin Ghahramani and Douglas Eck and Joelle Barral and Fernando Pereira and Eli Collins and Armand Joulin and Noah Fiedel and Evan Senter and Alek Andreev and Kathleen Kenealy},
2233
+ journal = {ArXiv},
2234
+ year = {2024},
2235
+ volume = {abs/2403.08295},
2236
+ url = {https://api.semanticscholar.org/CorpusID:268379206}
2237
+ }
2238
+ ```
2239
+
2240
+ ```bibtex
2241
+ @article{Nguyen2024MinPS,
2242
+ title = {Min P Sampling: Balancing Creativity and Coherence at High Temperature},
2243
+ author = {Minh Nguyen and Andrew Baker and Andreas Kirsch and Clement Neo},
2244
+ journal = {ArXiv},
2245
+ year = {2024},
2246
+ volume = {abs/2407.01082},
2247
+ url = {https://api.semanticscholar.org/CorpusID:270870613}
2248
+ }
2249
+ ```
2250
+
2251
+ ```bibtex
2252
+ @article{Bao2022AllAW,
2253
+ title = {All are Worth Words: A ViT Backbone for Diffusion Models},
2254
+ author = {Fan Bao and Shen Nie and Kaiwen Xue and Yue Cao and Chongxuan Li and Hang Su and Jun Zhu},
2255
+ journal = {2023 IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},
2256
+ year = {2022},
2257
+ pages = {22669-22679},
2258
+ url = {https://api.semanticscholar.org/CorpusID:253581703}
2259
+ }
2260
+ ```
2261
+
2262
+ ```bibtex
2263
+ @article{Jumper2021HighlyAP,
2264
+ title = {Highly accurate protein structure prediction with AlphaFold},
2265
+ author = {John M. Jumper and Richard Evans and Alexander Pritzel and Tim Green and Michael Figurnov and Olaf Ronneberger and Kathryn Tunyasuvunakool and Russ Bates and Augustin Ž{\'i}dek and Anna Potapenko and Alex Bridgland and Clemens Meyer and Simon A A Kohl and Andy Ballard and Andrew Cowie and Bernardino Romera-Paredes and Stanislav Nikolov and Rishub Jain and Jonas Adler and Trevor Back and Stig Petersen and David Reiman and Ellen Clancy and Michal Zielinski and Martin Steinegger and Michalina Pacholska and Tamas Berghammer and Sebastian Bodenstein and David Silver and Oriol Vinyals and Andrew W. Senior and Koray Kavukcuoglu and Pushmeet Kohli and Demis Hassabis},
2266
+ journal = {Nature},
2267
+ year = {2021},
2268
+ volume = {596},
2269
+ pages = {583 - 589},
2270
+ url = {https://api.semanticscholar.org/CorpusID:235959867}
2271
+ }
2272
+ ```
2273
+
2274
+ ```bibtex
2275
+ @article{Yang2017BreakingTS,
2276
+ title = {Breaking the Softmax Bottleneck: A High-Rank RNN Language Model},
2277
+ author = {Zhilin Yang and Zihang Dai and Ruslan Salakhutdinov and William W. Cohen},
2278
+ journal = {ArXiv},
2279
+ year = {2017},
2280
+ volume = {abs/1711.03953},
2281
+ url = {https://api.semanticscholar.org/CorpusID:26238954}
2282
+ }
2283
+ ```
2284
+
2285
+ ```bibtex
2286
+ @inproceedings{Kanai2018SigsoftmaxRO,
2287
+ title = {Sigsoftmax: Reanalysis of the Softmax Bottleneck},
2288
+ author = {Sekitoshi Kanai and Yasuhiro Fujiwara and Yuki Yamanaka and Shuichi Adachi},
2289
+ booktitle = {Neural Information Processing Systems},
2290
+ year = {2018},
2291
+ url = {https://api.semanticscholar.org/CorpusID:44064935}
2292
+ ```
2293
+
2294
+ ```bibtex
2295
+ @article{Kim2020TheLC,
2296
+ title = {The Lipschitz Constant of Self-Attention},
2297
+ author = {Hyunjik Kim and George Papamakarios and Andriy Mnih},
2298
+ journal = {ArXiv},
2299
+ year = {2020},
2300
+ volume = {abs/2006.04710},
2301
+ url = {https://api.semanticscholar.org/CorpusID:219530837}
2302
+ }
2303
+ ```
2304
+
2305
+ ```bibtex
2306
+ @inproceedings{Ramapuram2024TheoryAA,
2307
+ title = {Theory, Analysis, and Best Practices for Sigmoid Self-Attention},
2308
+ author = {Jason Ramapuram and Federico Danieli and Eeshan Gunesh Dhekane and Floris Weers and Dan Busbridge and Pierre Ablin and Tatiana Likhomanenko and Jagrit Digani and Zijin Gu and Amitis Shidani and Russ Webb},
2309
+ year = {2024},
2310
+ url = {https://api.semanticscholar.org/CorpusID:272463580}
2311
+ }
2312
+ ```
2313
+
2314
+ ```bibtex
2315
+ @inproceedings{Leviathan2024SelectiveAI,
2316
+ title = {Selective Attention Improves Transformer},
2317
+ author = {Yaniv Leviathan and Matan Kalman and Yossi Matias},
2318
+ year = {2024},
2319
+ url = {https://api.semanticscholar.org/CorpusID:273098114}
2320
+ }
2321
+ ```
2322
+
2323
+ ```bibtex
2324
+ @article{Bai2019DeepEM,
2325
+ title = {Deep Equilibrium Models},
2326
+ author = {Shaojie Bai and J. Zico Kolter and Vladlen Koltun},
2327
+ journal = {ArXiv},
2328
+ year = {2019},
2329
+ volume = {abs/1909.01377},
2330
+ url = {https://api.semanticscholar.org/CorpusID:202539738}
2331
+ }
2332
+ ```
2333
+
2334
+ ```bibtex
2335
+ @article{Wu2021MuseMorphoseFA,
2336
+ title = {MuseMorphose: Full-Song and Fine-Grained Piano Music Style Transfer With One Transformer VAE},
2337
+ author = {Shih-Lun Wu and Yi-Hsuan Yang},
2338
+ journal = {IEEE/ACM Transactions on Audio, Speech, and Language Processing},
2339
+ year = {2021},
2340
+ volume = {31},
2341
+ pages = {1953-1967},
2342
+ url = {https://api.semanticscholar.org/CorpusID:234338162}
2343
+ }
2344
+ ```
2345
+
2346
+ ```bibtex
2347
+ @inproceedings{Zhou2024ValueRL,
2348
+ title = {Value Residual Learning For Alleviating Attention Concentration In Transformers},
2349
+ author = {Zhanchao Zhou and Tianyi Wu and Zhiyun Jiang and Zhenzhong Lan},
2350
+ year = {2024},
2351
+ url = {https://api.semanticscholar.org/CorpusID:273532030}
2352
+ }
2353
+ ```
2354
+
2355
+ ```bibtex
2356
+ @inproceedings{anonymous2024forgetting,
2357
+ title = {Forgetting Transformer: Softmax Attention with a Forget Gate},
2358
+ author = {Anonymous},
2359
+ booktitle = {Submitted to The Thirteenth International Conference on Learning Representations},
2360
+ year = {2024},
2361
+ url = {https://openreview.net/forum?id=q2Lnyegkr8},
2362
+ note = {under review}
2363
+ }
2364
+ ```
2365
+
2366
+ ```bibtex
2367
+ @inproceedings{anonymous2024from,
2368
+ title = {From {MLP} to Neo{MLP}: Leveraging Self-Attention for Neural Fields},
2369
+ author = {Anonymous},
2370
+ booktitle = {Submitted to The Thirteenth International Conference on Learning Representations},
2371
+ year = {2024},
2372
+ url = {https://openreview.net/forum?id=A8Vuf2e8y6},
2373
+ note = {under review}
2374
+ }
2375
+ ```
2376
+
2377
+ ```bibtex
2378
+ @inproceedings{Duvvuri2024LASERAW,
2379
+ title = {LASER: Attention with Exponential Transformation},
2380
+ author = {Sai Surya Duvvuri and Inderjit S. Dhillon},
2381
+ year = {2024},
2382
+ url = {https://api.semanticscholar.org/CorpusID:273849947}
2383
+ }
2384
+ ```
2385
+
2386
+ ```bibtex
2387
+ @article{Zhu2024HyperConnections,
2388
+ title = {Hyper-Connections},
2389
+ author = {Defa Zhu and Hongzhi Huang and Zihao Huang and Yutao Zeng and Yunyao Mao and Banggu Wu and Qiyang Min and Xun Zhou},
2390
+ journal = {ArXiv},
2391
+ year = {2024},
2392
+ volume = {abs/2409.19606},
2393
+ url = {https://api.semanticscholar.org/CorpusID:272987528}
2394
+ }
2395
+ ```
2396
+
2397
+ ```bibtex
2398
+ @inproceedings{anonymous2024hymba,
2399
+ title = {Hymba: A Hybrid-head Architecture for Small Language Models},
2400
+ author = {Anonymous},
2401
+ booktitle = {Submitted to The Thirteenth International Conference on Learning Representations},
2402
+ year = {2024},
2403
+ url = {https://openreview.net/forum?id=A1ztozypga},
2404
+ note = {under review}
2405
+ }
2406
+ ```
2407
+
2408
+ ```bibtex
2409
+ @article{Shao2024DeepSeekV2AS,
2410
+ title = {DeepSeek-V2: A Strong, Economical, and Efficient Mixture-of-Experts Language Model},
2411
+ author = {Zhihong Shao and Damai Dai and Daya Guo and Bo Liu (Benjamin Liu) and Zihan Wang and Huajian Xin},
2412
+ journal = {ArXiv},
2413
+ year = {2024},
2414
+ volume = {abs/2405.04434},
2415
+ url = {https://api.semanticscholar.org/CorpusID:269613809}
2416
+ }
2417
+ ```
2418
+
2419
+ *solve intelligence... then use that to solve everything else.* - Demis Hassabis