Skip to content

Commit

Permalink
add device and dtype support to train.py args
Browse files Browse the repository at this point in the history
  • Loading branch information
karpathy committed Jan 8, 2023
1 parent e7cd674 commit a855d31
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import os
import time
import math
from contextlib import nullcontext

import numpy as np
import torch
Expand Down Expand Up @@ -56,11 +57,14 @@
# DDP settings
backend = 'nccl' # 'nccl', 'gloo', etc.
# system
device = 'cuda'
device = 'cuda' # examples: 'cpu', 'cuda', 'cuda:0', 'cuda:1', etc.
dtype = 'bfloat16' # 'float32' or 'bfloat16'
compile = True # use PyTorch 2.0 to compile the model to be faster
# -----------------------------------------------------------------------------
exec(open('configurator.py').read()) # overrides from command line or config file
# -----------------------------------------------------------------------------

# various inits, derived attributes, I/O setup
ddp = int(os.environ.get('LOCAL_RANK', -1)) != -1 # is this a ddp run?
if ddp:
init_process_group(backend=backend)
Expand All @@ -74,6 +78,10 @@
torch.manual_seed(1337 + gpu_id) # note: each worker gets a different seed
torch.backends.cuda.matmul.allow_tf32 = True # allow tf32 on matmul
torch.backends.cudnn.allow_tf32 = True # allow tf32 on cudnn
device_type = 'cuda' if 'cuda' in device else 'cpu' # for later use in torch.autocast
# note: float16 would require us to change the code to use a GradScaler
ptdtype = {'float32': torch.float32, 'bfloat16': torch.bfloat16}[dtype]
ctx = nullcontext() if device_type == 'cpu' else torch.amp.autocast(device_type=device_type, dtype=ptdtype)

# poor man's data loader, TODO evaluate need for actual DataLoader
data_dir = os.path.join('data', dataset)
Expand Down Expand Up @@ -156,7 +164,7 @@ def estimate_loss():
losses = torch.zeros(eval_iters)
for k in range(eval_iters):
X, Y = get_batch(split)
with torch.amp.autocast(device_type="cuda", dtype=torch.bfloat16):
with ctx:
logits, loss = model(X, Y)
losses[k] = loss.item()
out[split] = losses.mean()
Expand Down Expand Up @@ -226,7 +234,7 @@ def get_lr(iter):
break

X, Y = get_batch('train')
with torch.amp.autocast(device_type="cuda", dtype=torch.bfloat16):
with ctx:
logits, loss = model(X, Y)

optimizer.zero_grad(set_to_none=True)
Expand Down

0 comments on commit a855d31

Please sign in to comment.