Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Q] Async prefetch next batch while model is doing forward pass #486

Open
GM-git-dotcom opened this issue May 23, 2024 · 1 comment
Open

Comments

@GM-git-dotcom
Copy link

Hi,

Love the repo for the self-explanatory code and fun comments!

I would appreciate it if you could explain how the next batch is asynchronously being fetched in train.py:

nanoGPT/train.py

Lines 299 to 305 in 325be85

with ctx:
logits, loss = model(X, Y)
loss = loss / gradient_accumulation_steps # scale the loss to account for gradient accumulation
# immediately async prefetch next batch while model is doing the forward pass on the GPU
X, Y = get_batch('train')
# backward pass, with gradient scaling if training in fp16
scaler.scale(loss).backward()

At the outset, without explicitly using something like asyncio, the execution seemed sequential to me. Apologies for the potential triviality, thanks!

@karpathy
Copy link
Owner

karpathy commented Jun 8, 2024

It's due to this

x, y = x.pin_memory().to(device, non_blocking=True), y.pin_memory().to(device, non_blocking=True)

happens async

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants