forked from EurekaLabsAI/mlp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmlp_pytorch.py
175 lines (156 loc) · 7.01 KB
/
mlp_pytorch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""
Implements a simple n-gram language model in PyTorch.
Acts as the correctness reference for all the other versions.
"""
import math
import torch
import torch.nn as nn
from torch.nn import functional as F
from common import RNG, StepTimer
# -----------------------------------------------------------------------------
# The PyTorch Module
class MLP(nn.Module):
"""
Takes the previous n tokens, encodes them with a lookup table,
concatenates the vectors and predicts the next token with an MLP.
Reference:
Bengio et al. 2003 https://www.jmlr.org/papers/volume3/bengio03a/bengio03a.pdf
"""
def __init__(self, vocab_size, context_length, embedding_size, hidden_size):
super().__init__()
self.wte = nn.Embedding(vocab_size, embedding_size) # token embedding table
self.mlp = nn.Sequential(
nn.Linear(context_length * embedding_size, hidden_size),
nn.Tanh(),
nn.Linear(hidden_size, vocab_size)
)
def forward(self, idx, targets=None):
# idx are the input tokens, (B, T) tensor of integers
# targets are the target tokens, (B, ) tensor of integers
B, T = idx.size()
# encode all the tokens using the embedding table
emb = self.wte(idx) # (B, T, embedding_size)
# concat all of the embeddings together
emb = emb.view(B, -1) # (B, T * embedding_size)
# forward through the MLP
logits = self.mlp(emb)
# if we are given desired targets, also calculate the loss
loss = None
if targets is not None:
loss = F.cross_entropy(logits, targets)
return logits, loss
@torch.no_grad()
def reinit(self, rng):
# This function is a bit of a hack and would not be present in
# typical PyTorch code. Basically:
# - we want to use our own RNG to initialize the weights.
# - but we don't want to change idiomatic PyTorch code (above).
# So here in this function we overwrite the weights using our own RNG.
# This ensures that we have full control over the initialization and
# can easily compare the results with other implementations.
def reinit_tensor_randn(w, mu, sigma):
winit = torch.tensor(rng.randn(w.numel(), mu=mu, sigma=sigma))
w.copy_(winit.view_as(w))
def reinit_tensor_rand(w, a, b):
winit = torch.tensor(rng.rand(w.numel(), a=a, b=b))
w.copy_(winit.view_as(w))
# Let's match the PyTorch default initialization:
# Embedding with N(0,1)
reinit_tensor_randn(self.wte.weight, mu=0, sigma=1.0)
# Linear (both W,b) with U(-K, K) where K = 1/sqrt(fan_in)
scale = (self.mlp[0].in_features)**-0.5
reinit_tensor_rand(self.mlp[0].weight, -scale, scale)
reinit_tensor_rand(self.mlp[0].bias, -scale, scale)
scale = (self.mlp[2].in_features)**-0.5
reinit_tensor_rand(self.mlp[2].weight, -scale, scale)
reinit_tensor_rand(self.mlp[2].bias, -scale, scale)
# -----------------------------------------------------------------------------
# simple DataLoader that iterates over all the n-grams
def dataloader(tokens, context_length, batch_size):
# returns inputs, targets as torch Tensors of shape (B, T), (B, )
n = len(tokens)
inputs, targets = [], []
pos = 0
while True:
# simple sliding window over the tokens, of size context_length + 1
window = tokens[pos:pos + context_length + 1]
inputs.append(window[:-1])
targets.append(window[-1])
# once we've collected a batch, emit it
if len(inputs) == batch_size:
yield (torch.tensor(inputs), torch.tensor(targets))
inputs, targets = [], []
# advance the position and wrap around if we reach the end
pos += 1
if pos + context_length >= n:
pos = 0
# -----------------------------------------------------------------------------
# evaluation function
@torch.inference_mode()
def eval_split(model, tokens, max_batches=None):
# calculate the loss on the given tokens
model.eval()
total_loss = 0
num_batches = len(tokens) // batch_size
if max_batches is not None:
num_batches = min(num_batches, max_batches)
data_iter = dataloader(tokens, context_length, batch_size)
for _ in range(num_batches):
inputs, targets = next(data_iter)
logits, loss = model(inputs, targets)
total_loss += loss.item()
mean_loss = total_loss / num_batches
return mean_loss
# -----------------------------------------------------------------------------
# let's train!
random = RNG(1337)
# "train" the Tokenizer, so we're able to map between characters and tokens
train_text = open('data/train.txt', 'r').read()
assert all(c == '\n' or ('a' <= c <= 'z') for c in train_text)
uchars = sorted(list(set(train_text))) # unique characters we see in the input
vocab_size = len(uchars)
char_to_token = {c: i for i, c in enumerate(uchars)}
token_to_char = {i: c for i, c in enumerate(uchars)}
EOT_TOKEN = char_to_token['\n'] # designate \n as the delimiting <|endoftext|> token
# pre-tokenize all the splits one time up here
test_tokens = [char_to_token[c] for c in open('data/test.txt', 'r').read()]
val_tokens = [char_to_token[c] for c in open('data/val.txt', 'r').read()]
train_tokens = [char_to_token[c] for c in open('data/train.txt', 'r').read()]
# create the model
context_length = 3 # if 3 tokens predict the 4th, this is a 4-gram model
embedding_size = 48
hidden_size = 512
model = MLP(vocab_size, context_length, embedding_size, hidden_size)
model.reinit(random) # reinitialize the model with our own RNG
# create the optimizer
learning_rate = 7e-4
optimizer = torch.optim.AdamW(model.parameters(), lr=learning_rate, weight_decay=1e-4)
# training loop
timer = StepTimer()
batch_size = 128
num_steps = 50000
print(f'num_steps {num_steps}, num_epochs {num_steps * batch_size / len(train_tokens):.2f}')
train_data_iter = dataloader(train_tokens, context_length, batch_size)
for step in range(num_steps):
# cosine learning rate schedule, from max lr to 0
lr = learning_rate * 0.5 * (1 + math.cos(math.pi * step / num_steps))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
# every now and then evaluate the validation loss
last_step = step == num_steps - 1
if step % 200 == 0 or last_step:
train_loss = eval_split(model, train_tokens, max_batches=20)
val_loss = eval_split(model, val_tokens)
print(f'step {step:6d} | train_loss {train_loss:.4f} | val_loss {val_loss:.4f} | lr {lr:e} | time/step {timer.get_dt()*1000:.4f}ms')
# training step
with timer:
# get the next batch of training data
inputs, targets = next(train_data_iter)
# forward pass (calculate the loss)
model.train() # ensure we're in training mode
logits, loss = model(inputs, targets)
# backpropagate pass (calculate the gradients)
loss.backward()
# step the optimizer (update the parameters)
optimizer.step()
optimizer.zero_grad()