You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Difference between loss.backward() and model_engine.backward(loss) ? #329
I have tried out both loss.backward() and model_engine.backward(loss) for my code. There are several subtle differences that I have observed , for one retain_graph = True does not work for model_engine.backward(loss) . This is creating a problem since buffers are not being retained every time I run the code for some reason.
Thanks for the report @rsn870! Our model_engine.backward() is a wrapper around loss.backward() that also manages gradient accumulation, gradient reduction for data parallelism, timing, etc. You are seeing differences because additional arguments like retain_graph are not passed along to loss.backward().
I would think that we can simply add a **backward_kwargs argument to our model_engine.backward() that forwards to loss.backward(**backward_kwargs).
Hi , if one does loss.backward() instead of model_engine.backward(loss), so it will be wrong because gradients will not be averaged and therefore at every GPU, the weights will differ eventually leading us to train N models on N GPUs with batch_size/N samples per iteration.
Also please tell me if there is any workaround for retain_graph=True for now?
Hi , if one does loss.backward() instead of model_engine.backward(loss), so it will be wrong because gradients will not be averaged and therefore at every GPU, the weights will differ eventually leading us to train N models on N GPUs with batch_size/N samples per iteration.
Correct, you need model_engine.backward() to reduce gradients and perform other DeepSpeed bookkeeping.
If the model_engine() is based on DDP() for example I think computed loss should be synchronized among all nodes ?
It is not based on DDP directly, so without model_engine.backward() you don't get synchronization.
Also please tell me if there is any workaround for retain_graph=True for now?
Not currently, DeepSpeed needs to propagate additional arguments to our backward interface. I self-assigned but will not be able to get to it until hopefully next week. We are very welcoming of PRs if you would like to contribute :-). I would be happy to help guide through the process.
Activity
ShadenSmith commentedon Aug 21, 2020
Thanks for the report @rsn870! Our
model_engine.backward()is a wrapper aroundloss.backward()that also manages gradient accumulation, gradient reduction for data parallelism, timing, etc. You are seeing differences because additional arguments likeretain_graphare not passed along toloss.backward().I would think that we can simply add a
**backward_kwargsargument to ourmodel_engine.backward()that forwards toloss.backward(**backward_kwargs).@tjruwase / @samyam / @jeffra : do you have any thoughts on this?
aakash-saboo commentedon Aug 21, 2020
Hi , if one does loss.backward() instead of model_engine.backward(loss), so it will be wrong because gradients will not be averaged and therefore at every GPU, the weights will differ eventually leading us to train N models on N GPUs with batch_size/N samples per iteration.
Also please tell me if there is any workaround for retain_graph=True for now?
Please correct me if I am wrong
rsn870 commentedon Aug 21, 2020
If the model_engine() is based on DDP() for example I think computed loss should be synchronized among all nodes ?
ShadenSmith commentedon Aug 24, 2020
Correct, you need
model_engine.backward()to reduce gradients and perform other DeepSpeed bookkeeping.It is not based on DDP directly, so without
model_engine.backward()you don't get synchronization.Not currently, DeepSpeed needs to propagate additional arguments to our backward interface. I self-assigned but will not be able to get to it until hopefully next week. We are very welcoming of PRs if you would like to contribute :-). I would be happy to help guide through the process.
kracwarlock commentedon Aug 26, 2020
@ShadenSmith it would be great if we can add
retain_graphand other arguments.asit2898 commentedon Nov 3, 2020
@ShadenSmith is this issue still open? If yes can I pick this up?
ShadenSmith commentedon Nov 3, 2020
@asit2898 yes it is, apologies for us not getting to it quickly. PRs are very much welcome.
asit2898 commentedon Nov 4, 2020
Thanks a lot @ShadenSmith, I'll work on this issue. This would be my first commit, so any pointers would be truly helpful!
bhushan-borole commentedon Jul 21, 2021
Hey @ShadenSmith
Is this issue open to work?
I believe @ncilfone has submitted a PR for this issue.
buttercutter commentedon Jul 16, 2022
@ncilfone May I know why retain_graph still does not work for me ?