Issue
I'm extending a complex model (already with DistributedDataParallel
with find_unused_parameters
set to True
) in PyTorch on detectron2
.
I've added a new layer generating some additional output to the original network - initially, that layer was frozen (requires_grad = False
) and everything was working fine.
I later decided to unfreeze this layer.
Unfortunately, this results in this error on multiple GPUs:
-- Process 0 terminated with the following error:
Traceback (most recent call last):
...
File "/xxx/trainer.py", line 585, in some_method:
losses.backward()
File "/xxx/python3.7/site-packages/torch/tensor.py", line 221, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph)
File "/xxx/python3.7/site-packages/torch/autograd/__init__.py", line 132, in backward
allow_unreachable=True) # allow_unreachable flag
RuntimeError: Expected to mark a variable ready only once. This error is caused by one of the following reasons:
1) Use of a module parameter outside the `forward` function. Please make sure model parameters are not shared across multiple concurrent forward-backward passes
2) Reused parameters in multiple reentrant backward passes. For example, if you use multiple `checkpoint` functions to wrap the same part of your model, it would result in the same set of parameters been used by different reentrant backward passes multiple times, and hence marking a variable ready multiple times. DDP does not support such use cases yet.
Clearly, the error is related to that unfrozen layer, since everything was working before unfreezing it. This means that I need to check every change I made.
I wonder if there's a way/trick to determine which particular tensor/operation causes this behaviour? In other words - how can I speed up the debugging process?
Solution
TL;DR: try either find_unused_parameters=False
(DDP) or use_reentrant=True
(checkpoint).
With the help of the PyTorch community, I moved forward (see the original discussion here).
I updated my PyTorch to 1.9.0 (was using 1.7.0 before). Now I the error is a bit more informative:
RuntimeError: Expected to mark a variable ready only once. This error is caused by one of the following reasons: 1) Use of a module parameter outside the `forward` function. Please make sure model parameters are not shared across multiple concurrent forward-backward passes. or try to use _set_static_graph() as a workaround if this module graph does not change during training loop.2) Reused parameters in multiple reentrant backward passes. For example, if you use multiple `checkpoint` functions to wrap the same part of your model, it would result in the same set of parameters been used by different reentrant backward passes multiple times, and hence marking a variable ready multiple times. DDP does not support such use cases in default. You can try to use _set_static_graph() as a workaround if your module graph does not change over iterations.
Parameter at index 73 has been marked as ready twice. This means that multiple autograd engine hooks have fired for this particular parameter during this iteration. You can set the environment variable TORCH_DISTRIBUTED_DEBUG to either INFO or DETAIL to print parameter names for further debugging.
After setting the environmental variable TORCH_DISTRIBUTED_DEBUG
to DETAIL
(this requires PyTorch 1.9.0!) I got the name of the problematic variable:
Parameter at index 73 with name roi_heads.box_predictor.xxx.bias has been marked as ready twice. This means that multiple autograd engine hooks have fired for this particular parameter during this iteration.
So, technically, there was a problem with roi_heads.box_predictor.xxx
.
Could not find one.
However, with version 1.9.0 the console also outputs this:
[W reducer.cpp:1158] Warning: find_unused_parameters=True was specified in DDP constructor, but did not find any unused parameters in the forward pass. This flag results in an extra traversal of the autograd graph every iteration, which can adversely affect performance. If your model indeed never has any unused parameters in the forward pass, consider turning this flag off. Note that this warning may be a false positive if your model has flow control causing later iterations to have unused parameters. (function operator())
Turned out to be the root cause of the problem.
Switching find_unused_parameters
to False
in the DDP constructor makes the training run normally.
Not sure why this was the cause, but I don’t mind, since the network is trained now.
Anyhow, I hope this will help someone who struggles with similar debugging problems.
Edit: As of 2023 (PyTorch 2.0), people also report that use_reentrant=True
in torch.utils.checkpoint.checkpoint
can solve the problems as well.
Answered By - Dominik Filipiak
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.