Skip to content

Difference between loss.backward() and model_engine.backward(loss) ? #329

Closed

Description

@rsn870

Hi ,

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.

Please look into this if you could.

Activity

  1. added
    enhancement
    on Aug 21, 2020
  2. ShadenSmith commented on Aug 21, 2020

    @ShadenSmith
    Contributor

    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).

    @tjruwase / @samyam / @jeffra : do you have any thoughts on this?

  3. aakash-saboo commented on Aug 21, 2020

    @aakash-saboo

    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

  4. rsn870 commented on Aug 21, 2020

    @rsn870
    Author

    If the model_engine() is based on DDP() for example I think computed loss should be synchronized among all nodes ?

  5. self-assigned this
    on Aug 24, 2020
  6. ShadenSmith commented on Aug 24, 2020

    @ShadenSmith
    Contributor

    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.

  7. kracwarlock commented on Aug 26, 2020

    @kracwarlock

    @ShadenSmith it would be great if we can add retain_graph and other arguments.

  8. asit2898 commented on Nov 3, 2020

    @asit2898

    @ShadenSmith is this issue still open? If yes can I pick this up?

  9. ShadenSmith commented on Nov 3, 2020

    @ShadenSmith
    Contributor

    @asit2898 yes it is, apologies for us not getting to it quickly. PRs are very much welcome.

  10. asit2898 commented on Nov 4, 2020

    @asit2898

    Thanks a lot @ShadenSmith, I'll work on this issue. This would be my first commit, so any pointers would be truly helpful!

  11. bhushan-borole commented on Jul 21, 2021

    @bhushan-borole

    Hey @ShadenSmith
    Is this issue open to work?
    I believe @ncilfone has submitted a PR for this issue.

  12. buttercutter commented on Jul 16, 2022

    @buttercutter

    @ncilfone May I know why retain_graph still does not work for me ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions