pytorch-lightning icon indicating copy to clipboard operation
pytorch-lightning copied to clipboard

DDP does not work well with `torch.no_grad()` in 1.2

Open Ingwar opened this issue 5 years ago • 9 comments

🐛 Bug

My code that uses torch.no_grad stopped working after updating to 1.2 (I'm doing knowledge distillation and using it to protect the teacher model computation). Now it throws

RuntimeError: Expected to have finished reduction in the prior iteration before starting a new one

To Reproduce

Here is my modification of Boring Model. Basically, I've changed model definition and train_step to provide guard part of the computational graph with torch.no_grad. Also, I've change the final loss, it's now a sum of the original one and one computed as a similarity between mutable and guarded layers. As you can see, it throws RuntimeError at the first training iteration.

Expected behavior

Training runs without any RuntimeException

Environment

  • CUDA:
    • GPU:
      • Tesla K80
    • available: True
    • version: 10.1
  • Packages:
    • numpy: 1.19.5
    • pyTorch_debug: True
    • pyTorch_version: 1.7.0+cu101
    • pytorch-lightning: 1.2.0
    • tqdm: 4.41.1
  • System:
    • OS: Linux
    • architecture:
      • 64bit
    • processor: x86_64
    • python: 3.6.9
    • version: #1 SMP Thu Jul 23 08:00:38 PDT 2020

Ingwar avatar Feb 19 '21 17:02 Ingwar

BTW, I've also seen it with different GPU and different versions of python, torch and CUDA.

Ingwar avatar Feb 19 '21 17:02 Ingwar

I think you need to set find_unused_parameters=True: Trainer(..., plugins=DDPPlugin(find_unused_parameters=True))

awaelchli avatar Feb 19 '21 20:02 awaelchli

I'll check it, thanks) But why the same code worked in Lightning 1.1.* just fine?

Ingwar avatar Feb 19 '21 20:02 Ingwar

I suspect it because we recently changed the default from find_unused_parameters=True to False. And the error message you reported also appears when find_unused_parameters is not set.

awaelchli avatar Feb 19 '21 21:02 awaelchli

@awaelchli , explicitly instantiating the DDPPlugin with find_unused_parameters=True allows me to run my code, thank you! Howewer, I'd like to avoid that instantiation if possible to keep my code simpler) Is there a way to do so? Maybe, it's possible to pass the plugin parameters via CLI?

Ingwar avatar Feb 21 '21 21:02 Ingwar

PL does not offer that option at the moment. If you need the find_unused_parameters in your CLI, you have to add it to the parser manually. We have started discussing how to expose plugin parameters but have not yet reached a conclusion. The accelerator + plugin API is still experimental and we will iterate on it. Thanks for your suggestion.

If it's ok I will close the issue. find_unused_parameters=True is the solution here.

awaelchli avatar Feb 26 '21 00:02 awaelchli

@awaelchli , sure, feel free to close it and thank you for the help) BTW, is there any issue about the exposure of plugin parameters? If so, could you, please, share a link to it?

Ingwar avatar Feb 26 '21 10:02 Ingwar

so far only internally, but have opened the discussion here just now: #6219 I will keep it updated

awaelchli avatar Feb 26 '21 10:02 awaelchli

Thanks!

Ingwar avatar Feb 26 '21 12:02 Ingwar