-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddp_train_gpt2.py
262 lines (218 loc) · 10.6 KB
/
ddp_train_gpt2.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
'''
A training script to be used when running in a distributed mode using torchrun.
usage: torchrun --standalone --nproc_per_node=<num_processes> ddp_train_gpt2.py
If there are multiple nodes involved in training, replace --standalone option with
rendezvous (rdzv_*) config properties.
'''
import os
import sys
import torch
import torch.distributed
import torch.nn as nn
from torch.nn import functional as F
from transformers import GPT2LMHeadModel
from utils import FineWebDataLoader, LRScheduler, configure_adam_with_weight_decay
import time
from models import GPTConfig, GPT
from torch.distributed import init_process_group, destroy_process_group, all_reduce
from torch.nn.parallel import DistributedDataParallel as DDP
from hellaswag import render_example, iterate_examples, get_most_likely_row
def run_val(model, val_data_loader:FineWebDataLoader, device):
'''
Given the validation data loader `val_data_loader` and the `model`
run the validation datapoints through the model and calculate the loss.
'''
model.eval()
val_data_loader.reset()
val_loss_accm = 0
val_steps = 5
with torch.no_grad():
for _ in range(val_steps):
x, y = val_data_loader.next_batch()
x, y = x.to(device), y.to(device)
with torch.autocast(device_type="cuda", dtype=torch.float16):
logits, loss = model(x, y)
loss = loss / val_steps
val_loss_accm += loss.detach()
return val_loss_accm
def run_hellaswag_eval(model, rank, world_size, device):
'''
Run hellaswag evaluation.
'''
num_correct_norm = 0
num_total = 0
for i, example in enumerate(iterate_examples("val")):
# process examples in the round robin fashion across num of processes.
if i % world_size != rank:
continue
# convert the example into tokens and corresponding masks and labels.
_, tokens, mask, label = render_example(example)
tokens, mask = tokens.to(device), mask.to(device)
# get the logits.
with torch.no_grad():
with torch.autocast(device_type="cuda", dtype=torch.float16):
logits, loss = model(tokens)
pred_norm = get_most_likely_row(tokens, mask, logits)
num_total += 1
num_correct_norm += int(pred_norm == label)
num_total = torch.tensor(num_total, dtype=torch.long, device=device)
num_correct_norm = torch.tensor(num_correct_norm, dtype=torch.long, device=device)
all_reduce(num_total, torch.distributed.ReduceOp.SUM)
all_reduce(num_correct_norm, torch.distributed.ReduceOp.SUM)
num_total = num_total.item()
num_correct_norm = num_correct_norm.item()
acc_norm = num_correct_norm / num_total
return acc_norm
def setup_ddp():
'''
Initialize the process group and get the required environment details.
'''
init_process_group(backend="nccl")
# get the execution environment details.
world_size = int(os.environ.get("WORLD_SIZE"))
rank = int(os.environ.get("RANK"))
local_rank = int(os.environ.get("LOCAL_RANK"))
return (world_size, rank, local_rank)
if __name__ == "__main__":
'''
Load the GPT2 params from the pre-trained HF model.
'''
torch.manual_seed(1337)
torch.cuda.manual_seed(1337)
# Create logs directory and define a path to logs.txt
logs_dir = "logs"
os.makedirs(logs_dir, exist_ok=True)
log_file = os.path.join(logs_dir, "logs.txt")
# Is the script executed in ddp mode.
ddp = int(os.environ.get('RANK', -1)) != -1
if not ddp:
print("This script is expecred to be executed in ddp mode using torchrun")
sys.exit(0)
# Check CUDA availability.
assert torch.cuda.is_available(), "This script needs GPU devices to run. No CUDA devices found."
ddp_world_size, ddp_rank, ddp_local_rank = setup_ddp()
master_process = ddp_rank == 0
# Pin the process to a particular GPU.
device = f'cuda:{ddp_local_rank}'
torch.cuda.set_device(device)
#device = "mps"
prompt = "Hello, I'm a language model,"
max_length = 30
model_type = "gpt2"
data_file = "data/input.txt"
data_dir = "fine_web/edu_fineweb10B/"
#device = "cpu"
max_steps = 10
val_steps = 5
chkpoint_steps = 5
hellaswag_steps = 5
model_compilation = False
chkpoint_save_enabled = True
print(f"Using the device: {device}")
# Define batch size and sequence length
batch_size, seq_length = 16, 1024
# Parameters for gradient scaling.
total_batch_size = (163840 * ddp_world_size) # Increase this size based on available resources.
#total_batch_size = 524288 # 2^19 (~0.5M as used to train GPT-2)
assert total_batch_size % (batch_size * seq_length * ddp_world_size) == 0 #Total Batch Size is divisble by batch_size * seq_length
grad_accm_steps = total_batch_size // (batch_size * seq_length * ddp_world_size)
if master_process:
print(f"Desired batch size: {total_batch_size}; Required gradient accumulation steps: {grad_accm_steps}")
# optimization1 : Set the lower precision for float32 multiplications.
# This optimization only works for CUDA devices. For MPS, it worsens the performance.
#torch.set_float32_matmul_precision("high")
# Create a GPT2 model with random weights for the purposes of training.
# optimization 5 - Use the vocab size of 50304 (a power of 2) instead of 50257.
model = GPT(GPTConfig(vocab_size=50304))
model.to(device=device)
# optimization3 - Compile the model upfront and let torch perform optimizations.
# Again, this only works for CUDA and for latest versions - V100, A100, H100.
if model_compilation:
model = torch.compile(model)
#Wrap the model into DDP.
model = DDP(model, device_ids=[ddp_local_rank])
raw_model = model.module
# Create a data loader.
loader = FineWebDataLoader( data_dir, "train", batch_size=batch_size, seq_length=seq_length,
rank=ddp_rank,
num_processes=ddp_world_size,
model_type=model_type)
# Create a data loader.
val_loader = FineWebDataLoader( data_dir, "val", batch_size=batch_size, seq_length=seq_length,
rank=ddp_rank,
num_processes=ddp_world_size,
model_type=model_type)
# Define the optimizer.
# Optimizer tuning1: Define - betas, lr and eps based on GPT3 training details.
# Optimizer tuning4: Configuring weigth decay for AdamW.
optimizer = configure_adam_with_weight_decay(raw_model, weight_dacay=0.1, learning_rate=6e-4, device=device)
lr_scheduler = LRScheduler.get()
loop_start = time.time()
for step in range(max_steps):
last_step = step == (max_steps - 1)
# Check val_steps to see if we need to run validation.
if step % val_steps == 0 or last_step:
val_loss_accm = run_val(model, val_loader, device)
all_reduce(val_loss_accm, op=torch.distributed.ReduceOp.AVG)
if master_process:
print(f"validation loss: {val_loss_accm.item():.4f}")
with open(log_file, "a") as f:
f.write(f"{step} val {val_loss_accm.item():.4f}\n")
# Check chkpoint_steps to see if the model need to be saved.
if master_process and chkpoint_save_enabled \
and step > 0 and (step % chkpoint_steps == 0 or last_step):
chkpoint_path = os.path.join(logs_dir, f"model_{step:05d}.pt")
chkpoint = {
"model": raw_model.state_dict(),
"config": raw_model.config,
"step": step,
"val_loss": val_loss_accm.item()
}
torch.save(chkpoint, chkpoint_path)
# Check hellaswag steps to see if the hellaswag evaluation need to run.
if (step % hellaswag_steps == 0 or last_step) and (not model_compilation):
acc_norm = run_hellaswag_eval(model, ddp_rank, ddp_world_size, device)
if master_process:
print(f"Hellaswag accuracy: {acc_norm:.4f}")
with open(log_file, 'a') as f:
f.write(f"{step} hella {acc_norm:.4f}\n")
model.train()
loss_accm = 0
s = time.time()
optimizer.zero_grad()
# Optimization 5: implement gradient accumulation to account for small batches.
for micro_step in range(grad_accm_steps):
x, labels = loader.next_batch()
x, labels = x.to(device=device), labels.to(device=device)
## read ddp's no_sync() context manager.
# This instructs ddp to not sync gradients across all processes.
# This is needed for gradient accumulation. Only at the last micro_step
# we need the gradients to be syncronized.
model.require_backward_grad_sync = (micro_step == grad_accm_steps-1)
# optimization2 - Use autocast for mixed precision computation. Some of
# the operations will run with bfloat16 precision while others main continue to
# run with float32 precision. Only works on latest CUDA chips - Ampere onwards.
#with torch.autocast(device_type="cuda", dtype=torch.bfloat16):
with torch.autocast(device_type="cuda", dtype=torch.float16):
logits, loss = model(x, labels)
loss = loss / grad_accm_steps
loss_accm += loss.detach()
loss.backward()
# Accumulate the loss across all processes.
all_reduce(loss_accm, op=torch.distributed.ReduceOp.AVG)
# Optimizer tuning2: Clip gradient's norm to 1. Refer GPT3 paper training details.
norm = torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
# Optimizer tuning3: Implement the cosine learning scheduler.
lr = lr_scheduler(step)
for param_group in optimizer.param_groups:
param_group["lr"] = lr
optimizer.step()
torch.cuda.synchronize() # Wait for the GPU to finish work.
e = time.time()
tokens_per_sec = (loader.B * loader.T * grad_accm_steps * loader.num_processes) / (e-s)
if master_process:
print(f"step: {step+1:3d} | loss: {loss_accm:9.6f} | lr: {lr:.4e} | norm: {norm:7.4f} | dt: {(e-s)*1000:.6f}ms | toks/sec: {tokens_per_sec:.5f}")
loop_end = time.time()
if master_process:
print(f'\nTotal execution time: {(loop_end-loop_start) * 1000}')
destroy_process_group()