Skip to content

Commit

Permalink
average iterates in code
Browse files Browse the repository at this point in the history
  • Loading branch information
karpathy committed Apr 13, 2024
1 parent c70fbd0 commit c02bae2
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions train_gpt2.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ def get_batch():
data_iter = iter(get_batch())
x, y = next(data_iter) # we'll overfit this batch below
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
timings = []
for i in range(args.num_iterations):
t0 = time.time()
logits, loss = model(x, y)
Expand All @@ -391,7 +392,11 @@ def get_batch():
elif device == "cuda":
torch.cuda.synchronize()
t1 = time.time()
if i > args.num_iterations - 20:
timings.append(t1-t0)
print(f"iteration {i}, loss: {loss.item()}, time: {(t1-t0)*1000:.3f}ms")
if len(timings) > 0:
print(f"final 20 iters avg: {np.mean(timings)*1000:.3f}ms")

# before we end, let's also do one round of inference
# we'll kick off the generation with "<|endoftext|>", which designates the start of a new sequence
Expand Down

0 comments on commit c02bae2

Please sign in to comment.