-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
executable file
·163 lines (130 loc) · 4.67 KB
/
train.py
File metadata and controls
executable file
·163 lines (130 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python3
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import wandb
from model import Transformer
from dataloader import TinyShakespeare
import tiktoken
from tqdm import trange
import os
import math
N_EPOCHS = 5000
MAX_LR = 1e-4
MIN_LR = MAX_LR * 0.1
WARMUP_STEPS = 500
from config.tiny import cfg
import sys
if len(sys.argv) > 1:
exec(open(f"config/{sys.argv[1]}.py").read())
globals().update(vars(cfg))
device = torch.device(
'cuda' if torch.cuda.is_available() else
'mps' if torch.backends.mps.is_available() else
'cpu'
)
print(f"Using device: {device}")
def train_model(run):
dataset = TinyShakespeare(MAX_SEQ_LEN)
model = Transformer(
vocab_size=dataset.vocab_size,
d_model=D_MODEL,
n_heads=N_HEADS,
n_layers=N_LAYERS,
d_ff=D_FF,
max_seq_len=MAX_SEQ_LEN
).to(device)
optimizer = torch.optim.AdamW(model.parameters(), lr=MAX_LR, weight_decay=0.1)
criterion = nn.CrossEntropyLoss(label_smoothing=0.1)
eval_interval = 500
batch_size = BS
def get_lr(iter_num):
# linear warmup
if iter_num < WARMUP_STEPS:
return MAX_LR * iter_num / WARMUP_STEPS
# cosine decay
if iter_num > N_EPOCHS:
return MIN_LR
decay_ratio = (iter_num - WARMUP_STEPS) / (N_EPOCHS - WARMUP_STEPS)
cosine_decay = 0.5 * (1.0 + math.cos(math.pi * decay_ratio))
return MIN_LR + (MAX_LR - MIN_LR) * cosine_decay
model.train()
for iter_num in (t := trange(N_EPOCHS)):
X_train, Y_train = dataset.get_batch('train', batch_size)
X_train, Y_train = X_train.to(device), Y_train.to(device)
lr = get_lr(iter_num)
for param_group in optimizer.param_groups:
param_group['lr'] = lr
logits = model(X_train)
# entropy regularization
probs = torch.softmax(logits, dim=-1)
entropy = - (probs * torch.log(probs + 1e-8)).sum(dim=-1).mean()
loss = criterion(logits.view(-1, logits.size(-1)), Y_train.view(-1)) + 0.01 * entropy
accuracy = calculate_accuracy(logits, Y_train)
optimizer.zero_grad()
loss.backward()
run.log({'accuracy': accuracy, 'loss': loss.item(), 'lr': lr})
t.set_description("loss %.2f accuracy %.2f" % (loss.item(), accuracy))
# gradient clipping
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
optimizer.step()
# eval
if iter_num % eval_interval == 0:
model.eval()
with torch.no_grad():
train_acc = calculate_accuracy(logits, Y_train)
X_test, Y_test = dataset.get_batch('test', batch_size)
X_test, Y_test = X_test.to(device), Y_test.to(device)
val_logits = model(X_test)
val_loss = criterion(val_logits.view(-1, val_logits.size(-1)), Y_test.view(-1))
val_acc = calculate_accuracy(val_logits, Y_test)
print(f"\niter: {iter_num}, val loss: {val_loss.item():.4f}, val acc: {val_acc:.4f}")
run.log({'val_acc': val_acc, 'val_loss': val_loss})
sample_text = generate_sample(model, dataset.vocab_size, device, length=100)
print(f"Sample: {sample_text}")
model.train()
return model, dataset
def calculate_accuracy(logits, targets):
predictions = torch.argmax(logits, dim=-1)
correct = (predictions == targets).float()
return correct.mean().item()
def generate_sample(model, vocab_size, device, length=100, temperature=0.3):
model.eval()
tokenizer = tiktoken.get_encoding("gpt2")
with torch.no_grad():
context = torch.randint(0, vocab_size, (1, 1)).to(device)
print(f"context: {tokenizer.decode([context.item()])}")
generated = []
for _ in range(length):
if context.size(1) > model.max_seq_len:
context = context[:, -model.max_seq_len:]
logits = model(context)
logits = logits[0, -1, :]
# sample from distribution
probs = F.softmax(logits / temperature, dim=-1)
next_token = torch.multinomial(probs, 1)
generated.append(next_token.item())
context = torch.cat([context, next_token.unsqueeze(0)], dim=1)
return tokenizer.decode(generated)
if __name__ == "__main__":
print("Starting transformer training...")
run = wandb.init(entity='davidcho', project='llm-wiki', config={
"d_model": D_MODEL,
"n_heads": N_HEADS,
"n_layers": N_LAYERS,
"d_ff": D_FF,
"max_seq_len": MAX_SEQ_LEN,
"bs": BS,
"max_lr": MAX_LR,
})
model, dataset = train_model(run)
try:
os.mkdir('weights')
except FileExistsError:
pass
torch.save({
'model_state_dict': model.state_dict(),
'vocab_size': dataset.vocab_size,
}, f"weights/model-{run.name}.pth")
print(f"Training completed! Model saved as weights/model-{run.name}.pth")