Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix grad getting enabled for parameters on which explicitly disabled in case of multiple optimizers #5356

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pytorch_lightning/core/lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def __init__(self, *args, **kwargs):
self._current_hook_fx_name = None
self._current_dataloader_idx = None
self._automatic_optimization: bool = True
self.param_grad_dict = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be private.


def optimizers(self, use_pl_optimizer: bool = True) -> Union[Optimizer, List[Optimizer], List[LightningOptimizer]]:
if use_pl_optimizer:
Expand Down Expand Up @@ -1171,11 +1172,13 @@ def toggle_optimizer(self, optimizer: Optimizer, optimizer_idx: int):
optimizer_idx:
"""
for param in self.parameters():
if param not in self.param_grad_dict:
self.param_grad_dict[param] = param.requires_grad
param.requires_grad = False

for group in optimizer.param_groups:
for param in group['params']:
param.requires_grad = True
param.requires_grad = self.param_grad_dict[param]

def optimizer_step(
self,
Expand Down