2022-12-28 23:55:43 +00:00
|
|
|
"""
|
2022-12-29 00:05:32 +00:00
|
|
|
A much shorter version of train.py for benchmarking
|
2022-12-28 23:55:43 +00:00
|
|
|
"""
|
2022-12-29 00:05:32 +00:00
|
|
|
import os
|
2023-01-08 19:32:13 +00:00
|
|
|
from contextlib import nullcontext
|
2022-12-29 00:05:32 +00:00
|
|
|
import numpy as np
|
2022-12-28 23:55:43 +00:00
|
|
|
import time
|
|
|
|
import torch
|
|
|
|
from model import GPTConfig, GPT
|
|
|
|
|
2023-01-05 00:44:35 +00:00
|
|
|
# -----------------------------------------------------------------------------
|
2023-02-02 17:23:46 +00:00
|
|
|
batch_size = 12
|
2022-12-28 23:55:43 +00:00
|
|
|
block_size = 1024
|
2023-02-02 17:23:46 +00:00
|
|
|
bias = False
|
|
|
|
real_data = True
|
2023-01-08 19:32:13 +00:00
|
|
|
seed = 1337
|
|
|
|
device = 'cuda' # examples: 'cpu', 'cuda', 'cuda:0', 'cuda:1', etc.
|
|
|
|
dtype = 'bfloat16' # 'float32' or 'bfloat16' or 'float16'
|
|
|
|
compile = True # use PyTorch 2.0 to compile the model to be faster
|
2023-02-02 17:23:46 +00:00
|
|
|
profile = False # use pytorch profiler, or just simple benchmarking?
|
2023-01-05 00:44:35 +00:00
|
|
|
exec(open('configurator.py').read()) # overrides from command line or config file
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
|
2023-01-08 19:32:13 +00:00
|
|
|
torch.manual_seed(seed)
|
|
|
|
torch.cuda.manual_seed(seed)
|
2023-01-05 00:44:35 +00:00
|
|
|
torch.backends.cuda.matmul.allow_tf32 = True # allow tf32 on matmul
|
|
|
|
torch.backends.cudnn.allow_tf32 = True # allow tf32 on cudnn
|
2023-01-08 19:32:13 +00:00
|
|
|
device_type = 'cuda' if 'cuda' in device else 'cpu' # for later use in torch.autocast
|
|
|
|
ptdtype = {'float32': torch.float32, 'bfloat16': torch.bfloat16, 'float16': torch.float16}[dtype]
|
|
|
|
ctx = nullcontext() if device_type == 'cpu' else torch.amp.autocast(device_type=device_type, dtype=ptdtype)
|
2022-12-28 23:55:43 +00:00
|
|
|
|
2022-12-29 00:05:32 +00:00
|
|
|
# data loading init
|
|
|
|
if real_data:
|
|
|
|
dataset = 'openwebtext'
|
|
|
|
data_dir = os.path.join('data', dataset)
|
|
|
|
train_data = np.memmap(os.path.join(data_dir, 'train.bin'), dtype=np.uint16, mode='r')
|
|
|
|
def get_batch(split):
|
|
|
|
data = train_data # note ignore split in benchmarking script
|
|
|
|
ix = torch.randint(len(data) - block_size, (batch_size,))
|
|
|
|
x = torch.stack([torch.from_numpy((data[i:i+block_size]).astype(np.int64)) for i in ix])
|
|
|
|
y = torch.stack([torch.from_numpy((data[i+1:i+1+block_size]).astype(np.int64)) for i in ix])
|
2023-02-04 02:52:48 +00:00
|
|
|
x, y = x.pin_memory().to(device, non_blocking=True), y.pin_memory().to(device, non_blocking=True)
|
2022-12-29 00:05:32 +00:00
|
|
|
return x, y
|
|
|
|
else:
|
|
|
|
# alternatively, if fixed data is desired to not care about data loading
|
|
|
|
x = torch.randint(50257, (batch_size, block_size), device=device)
|
|
|
|
y = torch.randint(50257, (batch_size, block_size), device=device)
|
|
|
|
get_batch = lambda split: (x, y)
|
|
|
|
|
|
|
|
# model init
|
2022-12-28 23:55:43 +00:00
|
|
|
gptconf = GPTConfig(
|
|
|
|
block_size = block_size, # how far back does the model look? i.e. context size
|
|
|
|
n_layer = 12, n_head = 12, n_embd = 768, # size of the model
|
|
|
|
dropout = 0, # for determinism
|
2023-01-27 20:41:17 +00:00
|
|
|
bias = bias,
|
2022-12-28 23:55:43 +00:00
|
|
|
)
|
|
|
|
model = GPT(gptconf)
|
|
|
|
model.to(device)
|
|
|
|
|
|
|
|
optimizer = model.configure_optimizers(weight_decay=1e-2, learning_rate=1e-4, betas=(0.9, 0.95))
|
|
|
|
|
2023-01-02 01:15:55 +00:00
|
|
|
if compile:
|
2022-12-30 00:07:13 +00:00
|
|
|
print("Compiling model...")
|
|
|
|
model = torch.compile(model) # pytorch 2.0
|
|
|
|
|
2022-12-29 01:49:53 +00:00
|
|
|
if profile:
|
|
|
|
# useful docs on pytorch profiler:
|
|
|
|
# - tutorial https://pytorch.org/tutorials/intermediate/tensorboard_profiler_tutorial.html
|
|
|
|
# - api https://pytorch.org/docs/stable/profiler.html#torch.profiler.profile
|
|
|
|
wait, warmup, active = 5, 5, 5
|
|
|
|
num_steps = wait + warmup + active
|
|
|
|
with torch.profiler.profile(
|
|
|
|
activities=[torch.profiler.ProfilerActivity.CPU, torch.profiler.ProfilerActivity.CUDA],
|
|
|
|
schedule=torch.profiler.schedule(wait=wait, warmup=warmup, active=active, repeat=1),
|
|
|
|
on_trace_ready=torch.profiler.tensorboard_trace_handler('./bench_log'),
|
2023-02-02 17:23:46 +00:00
|
|
|
record_shapes=False,
|
|
|
|
profile_memory=False,
|
|
|
|
with_stack=False, # incurs an additional overhead, disable if not needed
|
2023-02-04 02:52:48 +00:00
|
|
|
with_flops=True,
|
2022-12-29 01:49:53 +00:00
|
|
|
with_modules=False, # only for torchscript models atm
|
|
|
|
) as prof:
|
2022-12-28 23:55:43 +00:00
|
|
|
|
2023-02-04 02:52:48 +00:00
|
|
|
X, Y = get_batch('train')
|
2022-12-29 01:49:53 +00:00
|
|
|
for k in range(num_steps):
|
2023-01-08 19:32:13 +00:00
|
|
|
with ctx:
|
2022-12-29 01:49:53 +00:00
|
|
|
logits, loss = model(X, Y)
|
2023-02-04 02:52:48 +00:00
|
|
|
X, Y = get_batch('train')
|
2022-12-29 01:49:53 +00:00
|
|
|
optimizer.zero_grad(set_to_none=True)
|
|
|
|
loss.backward()
|
|
|
|
optimizer.step()
|
|
|
|
lossf = loss.item()
|
|
|
|
print(f"{k}/{num_steps} loss: {lossf:.4f}")
|
2022-12-28 23:55:43 +00:00
|
|
|
|
2022-12-29 01:49:53 +00:00
|
|
|
prof.step() # notify the profiler at end of each step
|
2022-12-28 23:55:43 +00:00
|
|
|
|
2022-12-29 01:49:53 +00:00
|
|
|
else:
|
2022-12-28 23:55:43 +00:00
|
|
|
|
2022-12-29 01:49:53 +00:00
|
|
|
# simple benchmarking
|
|
|
|
torch.cuda.synchronize()
|
|
|
|
for stage, num_steps in enumerate([10, 20]): # burnin, then benchmark
|
|
|
|
t0 = time.time()
|
2023-02-04 02:52:48 +00:00
|
|
|
X, Y = get_batch('train')
|
2022-12-29 01:49:53 +00:00
|
|
|
for k in range(num_steps):
|
2023-01-08 19:32:13 +00:00
|
|
|
with ctx:
|
2022-12-29 01:49:53 +00:00
|
|
|
logits, loss = model(X, Y)
|
2023-02-04 02:52:48 +00:00
|
|
|
X, Y = get_batch('train')
|
2022-12-29 01:49:53 +00:00
|
|
|
optimizer.zero_grad(set_to_none=True)
|
|
|
|
loss.backward()
|
|
|
|
optimizer.step()
|
|
|
|
lossf = loss.item()
|
|
|
|
print(f"{k}/{num_steps} loss: {lossf:.4f}")
|
|
|
|
torch.cuda.synchronize()
|
|
|
|
t1 = time.time()
|
|
|
|
if stage == 1:
|
|
|
|
print(f"time per iteration: {(t1-t0)/num_steps*1000:.4f}ms")
|