Skip to content

Commit c81b2a8

Browse files
authored
Set find unused parameters to True by default to fix breaking compatibility (#6438)
* Set find unused parameters to True by default to fix breaking models, add suggestion to re-enable * Add changelog
1 parent 74d79e7 commit c81b2a8

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
4444
- Changed `setup()` and `teardown()` stage argument to take any of `{fit,validate,test,predict}` ([#6386](https://github.com/PyTorchLightning/pytorch-lightning/pull/6386))
4545

4646

47+
- Changed the default of `find_unused_parameters` back to `True` in DDP and DDP Spawn ([#6438](https://github.com/PyTorchLightning/pytorch-lightning/pull/6438))
48+
49+
4750
### Deprecated
4851

4952

docs/source/benchmarking/performance.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ DP performs three GPU transfers for EVERY batch:
9494
9595
Whereas DDP only performs 1 transfer to sync gradients. Because of this, DDP is MUCH faster than DP.
9696

97+
When using DDP set find_unused_parameters=False
98+
-----------------------------------------------
99+
100+
By default we have enabled find unused parameters to True. This is for compatibility issues that have arisen in the past (see the `discussion <https://github.com/PyTorchLightning/pytorch-lightning/discussions/6219>`_ for more information).
101+
This by default comes with a performance hit, and can be disabled in most cases.
102+
103+
.. code-block:: python
104+
105+
from pytorch_lightning.plugins import DDPPlugin
106+
107+
trainer = pl.Trainer(
108+
gpus=2,
109+
plugins=DDPPlugin(find_unused_parameters=False),
110+
)
111+
97112
----------
98113

99114
16-bit precision

pytorch_lightning/plugins/training_type/ddp.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,13 @@ def set_world_ranks(self):
175175
self.world_size = self.num_nodes * self.num_processes
176176

177177
def pre_configure_ddp(self):
178+
# if unset, default `find_unused_parameters` `True`
179+
# Many models require setting this parameter to True, as there are corner cases
180+
# when not all parameter backward hooks are fired by the autograd engine even if require_grad is set to True.
181+
# This flag does come with a performance hit, so it is suggested to disable in cases where it is possible.
182+
self._ddp_kwargs["find_unused_parameters"] = self._ddp_kwargs.get(
183+
"find_unused_parameters", True
184+
)
178185
# todo: PyTorch 1.7.0 DDP introduces ``self.reducer._rebuild_buckets()`` breaking manual_optimization
179186
if _TORCH_GREATER_EQUAL_1_7 and not self.lightning_module.automatic_optimization and not self._ddp_kwargs.get(
180187
"find_unused_parameters", False

pytorch_lightning/plugins/training_type/ddp_spawn.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ def post_dispatch(self):
168168
self.__recover_child_process_weights(best_path, last_path)
169169

170170
def pre_configure_ddp(self):
171+
# if unset, default `find_unused_parameters` `True`
172+
# Many models require setting this parameter to True, as there are corner cases
173+
# when not all parameter backward hooks are fired by the autograd engine even if require_grad is set to True.
174+
# This flag does come with a performance hit, so it is suggested to disable in cases where it is possible.
175+
self._ddp_kwargs["find_unused_parameters"] = self._ddp_kwargs.get(
176+
"find_unused_parameters", True
177+
)
171178
# todo: PyTorch 1.7.0 DDP introduces ``self.reducer._rebuild_buckets()`` breaking manual_optimization
172179
if _TORCH_GREATER_EQUAL_1_7 and not self.lightning_module.automatic_optimization and not self._ddp_kwargs.get(
173180
"find_unused_parameters", False

0 commit comments

Comments
 (0)