81M parameter GPT model trained on WikiText-103 using torch.distributed
for DDP and torch.cuda.amp
for mixed precision. Used youtokentome for tokenizing the corpus.
d_model | 768 |
n_head | 16 |
n_layer | 8 |
ctx_len | 512 |
vocab_size | 16384 |
max_lr | 3e-4 |
min_lr | 5e-5 |
weight_decay | 1e-2 |
train_steps | 15000 |
lr_warmup_steps | 1500 |
lr_schedule | inverse square root |
batch_size | 128 |
gpu | RTX 3090 |
n_gpu | 4 |
Few changes: RMS norm instead of layer norm, RoPE instead of learned position encoding, SwiGLU instead of GELU, and Lion optimizer instead of Adam(W).
The difference between AdamW and Lion is as follows:
![Lion](https://private-user-images.githubusercontent.com/86470305/250017909-488c267e-a966-49db-bbe5-77cd669e6349.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ1MzA4OTIsIm5iZiI6MTcyNDUzMDU5MiwicGF0aCI6Ii84NjQ3MDMwNS8yNTAwMTc5MDktNDg4YzI2N2UtYTk2Ni00OWRiLWJiZTUtNzdjZDY2OWU2MzQ5LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI0VDIwMTYzMlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTViNWI0Y2ZkZGMyYjI5YjM5YjY3ZGRiOGM1OTRhMmVkNDZjNmM0MWI3NTZiNGY1MjY4MDI2YmVmYWEzYjg1ODImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.JUQ_y896abS6lv-Bp6wqfj4GAYMopIdMCellGLwTVaU)
Lion saves memory by only keeping track of the first moment (the EMA of gradients) whereas Adam and variants use the second moment (the EMA of gradients squared) as well.
A few days ago 4chan anon kaiokendev made a two line edit in his RoPE implementation which appeared to double the model's context length at test time. The equivalent edit in the implementation here would be:
![RoPE-scale](https://private-user-images.githubusercontent.com/86470305/250023865-d4999d5c-75b9-4b67-baa4-4e923bf7f8fc.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ1MzA4OTIsIm5iZiI6MTcyNDUzMDU5MiwicGF0aCI6Ii84NjQ3MDMwNS8yNTAwMjM4NjUtZDQ5OTlkNWMtNzViOS00YjY3LWJhYTQtNGU5MjNiZjdmOGZjLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI0VDIwMTYzMlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTI4NTUwYTIzODUwNTBhMzIxMGZiMTQ0MmViNzcyOTRkMDlmOGU4MjJmOTJjNGFhODQ3N2NmOTc0ZDI0ZmRmNzQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.pyivf-6Axph6ZyaDs-ZUdtJT1_Hrn0OeeidqwPtbgY8)
There's no fine-tuning going on, all we have to do is scale torch.arange(l)
by scale
. What's happening? Here, x
will be the query q
or key k
batch on which we apply RoPE. x
will be of shape (batch_size, seq_len, d_model)
- we only need to care about the last two dimensions (assume we're working with a single batch, which is what we do at test time). We first define a vector theta
of d
angle values like so
[θ_1, θ_1, θ_2, θ_2, ..., θ_{d/2}]
where d
is assumed to be even. Then we consider torch.arange(l)
which is simply
[1, 2, ..., l]
where l
is seq_len
. torch.einsum('i, j -> ij', torch.arange(l), theta)
is the outer product between the two arguments, so you get ltheta
which is a matrix that looks like
[[1 * θ_1, 1 * θ_1, ..., 1 * θ_{d/2}, 1 * θ_{d/2}],
[2 * θ_1, 2 * θ_1, ..., 2 * θ_{d/2}, 2 * θ_{d/2}],
...
[l * θ_1, l * θ_1, ..., l * θ_{d/2}, l * θ_{d/2}]]
So ltheta
is same shape as x
- (seq_len, d_model)
. x_rot
is just x
with alternating columns and even columns made negative. We apply sin
and cos
along each row of ltheta
, and elementwise multiply the outputs with x_rot
and x
respectively. To double the context length, we halve the frequencies
[[1/2 * θ_1, 1/2 * θ_1, ..., 1/2 * θ_{d/2}, 1/2 * θ_{d/2}],
[2/2 * θ_1, 2/2 * θ_1, ..., 2/2 * θ_{d/2}, 2/2 * θ_{d/2}],
...
[2l/2 * θ_1, 2l/2 * θ_1, ..., 2l/2 * θ_{d/2}, 2l/2 * θ_{d/2}]]
Intuitively, the model has learned to map sections of a context to specific sections of the sin
and cos
curves. Imagine dividing an arbitrary context into uniform chunks. Then no matter the context length, we want each chunk to be mapped to the same section of the trig curves. We do this by scaling down the frequencies. A paper laying out the exact trick came out just recently and it has this helpful visualization of what's going on
![RoPE-explain](https://private-user-images.githubusercontent.com/86470305/250044165-00a433b1-b12d-414d-b5cc-22dd2e085b25.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ1MzA4OTIsIm5iZiI6MTcyNDUzMDU5MiwicGF0aCI6Ii84NjQ3MDMwNS8yNTAwNDQxNjUtMDBhNDMzYjEtYjEyZC00MTRkLWI1Y2MtMjJkZDJlMDg1YjI1LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI0VDIwMTYzMlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWIzODAxMmRlYTg1ZGUxMmZkOTA0MjMzNzEzMWE0MGFhNzRhZDc2Y2EwMWFjNGJiYWNhZTI2YWQ0OWE2NDI3NTYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.jmiH8kmm1FZA7ELSE3FOWsZ5PMnWlg8iuKtqD6VEd2w)
If we have a trained model with context length l
, we can make it work with longer context length k * l
by doing
k = 2
model = GPT(...)
model.load_state_dict(torch.load('trained_model.pt'))
# update RoPE
for layer in model.layers:
layer.pe = RotaryEmbedding(k * model.l, model.d, scale=k)
model.l *= k
torch.save(model.state_dict(), 'extended_model.pt')
(Update again: Jianlin Su's explanation)
Attention scores visualized for 64 tokens in the middle of a completion: