Repository navigation
Zero-grad more aggressively to save memory - #71
Conversation
|
@cchan can you share intuition on why this makes so much difference? |
|
@karpathy it's just that after you run Here's a bad diagram to mull over :) this is actually almost exactly what the real memory profile for minGPT looks like. As a side note, if you think about it you can also eliminate the purple memory almost entirely if you run the optimizer immediately after every individual tensor is generated in the backward pass (ofc doesn't work with local accumulation). But that doesn't improve peak memory usage, which is usually what matters. |
|
I'm not sure if I follow because my original code zero_grads right before the forward pass, so those gradients should be gone during the forward pass. |
|
Ah for nanoGPT you're right. In minGPT it's right after the fwd but not here. My habit is just to zero_grad(set_to_none=True) immediately after the optimizer, since you might as well get rid of gradients ASAP. |
|
ok that makes more sense :) yes I agree with you, feels much better and safer to free it right away once it is not needed. |
|
Hey @cchan where did you get that diagram from? Could you please share the URL. thanks! |
|
@vgoklani I drew it on an MS Paint-like website in about 10 minutes lol, why? |
|
@cchan it's a pretty professional-looking drawing! I was looking for some info about optimizing memory usage during training... |

Same as karpathy/minGPT#106 - sorry for the spam, am actually using both of these repos (they've been insanely useful <3)