-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_multigpu.py
More file actions
executable file
·168 lines (141 loc) · 5 KB
/
train_multigpu.py
File metadata and controls
executable file
·168 lines (141 loc) · 5 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
164
165
166
167
168
#!/usr/bin/env python3
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.distributed as dist
import torch.multiprocessing as mp
from torch.nn.parallel import DistributedDataParallel as DDP
import numpy as np
from model import Transformer
from dataloader import OpenWebText
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())
print(f"loaded sys.argv[1]")
globals().update(vars(cfg))
def train(rank, world_size):
print(f"starting... {rank}")
dist.init_process_group(
backend="nccl" if torch.cuda.is_available() else 'gloo',
init_method=f"tcp://{os.environ['MASTER_ADDR']}:{os.environ['MASTER_PORT']}",
rank=rank,
world_size=world_size,
)
if torch.cuda.is_available():
device = torch.device(f"cuda:{local_rank}")
torch.cuda.set_device(device)
else:
device = torch.device('cpu')
print(f"Using device: {device}\n")
dataset = OpenWebText(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)
if torch.cuda.is_available():
model = DDP(model, device_ids=[local_rank])
else:
model = DDP(model)
optimizer = torch.optim.AdamW(model.parameters(), lr=MAX_LR, weight_decay=0.1)
criterion = nn.CrossEntropyLoss(label_smoothing=0.1)
eval_interval = 200
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()
progress_bar = trange(N_EPOCHS) if rank == 0 else range(N_EPOCHS)
for iter_num in (t := progress_bar):
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()
# gradient clipping
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
optimizer.step()
dist.barrier()
if dist.get_rank() == 0:
wandb.log({'accuracy': accuracy, 'loss': loss.item(), 'lr': lr})
t.set_description("loss %.2f accuracy %.2f" % (loss.item(), accuracy))
# 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)
dist.barrier()
if dist.get_rank() == 0:
print(f"\niter: {iter_num}, val loss: {val_loss.item():.4f}, val acc: {val_acc:.4f}")
wandb.log({'val_acc': val_acc, 'val_loss': val_loss})
os.makedirs('weights', exist_ok=True)
print("saving checkpoint")
torch.save({
'model_state_dict': model.state_dict(),
'optimizer': optimizer.state_dict(),
'vocab_size': dataset.vocab_size,
'iter_num': iter_num,
}, f"weights/ckpt.pt")
model.train()
dist.barrier()
if dist.get_rank() == 0:
os.makedirs('weights', exist_ok=True)
torch.save({
'model_state_dict': model.state_dict(),
'vocab_size': dataset.vocab_size,
}, f"weights/model-{wandb.run.name}.pth")
print(f"Training completed! Model saved as weights/model-{wandb.run.name}.pth")
dist.destroy_process_group()
def calculate_accuracy(logits, targets):
predictions = torch.argmax(logits, dim=-1)
correct = (predictions == targets).float()
return correct.mean().item()
if __name__ == "__main__":
#mp.set_start_method("spawn", force=True)
rank = int(os.environ["RANK"])
world_size = int(os.environ["WORLD_SIZE"])
local_rank = int(os.environ["LOCAL_RANK"]) # useful on multi-gpu boxes
if rank == 0:
import wandb
print(wandb.__file__)
wandb.init(entity='davidcho', project='transformer-multigpu', 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,
})
train(rank, world_size)