Skip to content

Remove manual tracking of optimizer steps by the LightningOptimizer #9957

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

Merged
merged 3 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 0 additions & 2 deletions pytorch_lightning/core/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def __init__(self, optimizer: Optimizer):
self._optimizer = optimizer
self._trainer = None
self._optimizer_idx = None
self._total_optimizer_step_calls = 0

@property
def optimizer(self):
Expand Down Expand Up @@ -201,7 +200,6 @@ def closure_dis():
profiler_name = f"optimizer_step_and_closure_{self._optimizer_idx}"

self.__optimizer_step(closure=closure, profiler_name=profiler_name, **kwargs)
self._total_optimizer_step_calls += 1

def __repr__(self):
groups = [
Expand Down
15 changes: 8 additions & 7 deletions tests/accelerators/test_tpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License
import collections
from copy import deepcopy
from unittest.mock import patch

import pytest
import torch
Expand All @@ -21,7 +22,6 @@
from pytorch_lightning import Trainer
from pytorch_lightning.accelerators.cpu import CPUAccelerator
from pytorch_lightning.accelerators.tpu import TPUAccelerator
from pytorch_lightning.callbacks import Callback
from pytorch_lightning.plugins import TPUSpawnPlugin
from pytorch_lightning.utilities import find_shared_parameters
from pytorch_lightning.utilities.exceptions import MisconfigurationException
Expand Down Expand Up @@ -189,16 +189,18 @@ def on_train_batch_end(self, outputs, batch, batch_idx):
assert torch.all(self.layer.weight.grad == 0)
self.count += 1

def on_train_start(self):
opt = self.optimizers()
self.opt_step_patch = patch.object(opt, "step", wraps=opt.step)
self.opt_step_mock = self.opt_step_patch.start()

def on_train_end(self):
assert self.called["training_step"] == 5
assert self.called["on_train_batch_start"] == 5
assert self.called["on_train_batch_end"] == 5

class TestManualOptimizationCallack(Callback):
def on_train_end(self, trainer, pl_module):

opt = pl_module.optimizers()
assert opt._total_optimizer_step_calls == 3
self.opt_step_patch.stop()
assert self.opt_step_mock.call_count == 3

model = ManualOptimizationModel()
model_copy = deepcopy(model)
Expand All @@ -212,7 +214,6 @@ def on_train_end(self, trainer, pl_module):
limit_test_batches=0,
limit_val_batches=0,
tpu_cores=8,
callbacks=[TestManualOptimizationCallack()],
)
trainer.fit(model)

Expand Down
1 change: 0 additions & 1 deletion tests/core/test_lightning_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ def test_state(tmpdir):
"zero_grad",
"__setstate__",
"add_param_group",
"_total_optimizer_step_calls",
]

for k, v in lightning_optimizer.__dict__.items():
Expand Down
24 changes: 14 additions & 10 deletions tests/trainer/optimization/test_manual_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

from pytorch_lightning import seed_everything, Trainer
from pytorch_lightning.accelerators import Accelerator
from pytorch_lightning.callbacks import Callback
from tests.helpers.boring_model import BoringModel
from tests.helpers.runif import RunIf

Expand Down Expand Up @@ -706,14 +705,6 @@ def configure_optimizers(self):
mock_adam_step.assert_has_calls(expected_calls)


class TestManualOptimizationDDPCallack(Callback):
def on_train_end(self, trainer, pl_module):

opt_a, opt_b = pl_module.optimizers()
assert opt_a._total_optimizer_step_calls == 4
assert opt_b._total_optimizer_step_calls == 2


class TesManualOptimizationDDPModel(BoringModel):
def __init__(self):
super().__init__()
Expand Down Expand Up @@ -787,6 +778,20 @@ def configure_optimizers(self):
optimizer_dis = torch.optim.Adam(self.layer.parameters(), lr=0.001)
return [optimizer_gen, optimizer_dis]

def on_train_start(self):
# this is done here instead of in the calling function due to `spawn`
sgd, adam = self.optimizers()
self.sgd_step_patch = patch.object(sgd, "step", wraps=sgd.step)
self.sgd_step_mock = self.sgd_step_patch.start()
self.adam_step_patch = patch.object(adam, "step", wraps=adam.step)
self.adam_step_mock = self.adam_step_patch.start()

def on_train_end(self):
self.sgd_step_patch.stop()
assert self.sgd_step_mock.call_count == 4
self.adam_step_patch.stop()
assert self.adam_step_mock.call_count == 2


def train_manual_optimization(tmpdir, strategy, model_cls=TesManualOptimizationDDPModel):

Expand All @@ -806,7 +811,6 @@ def train_manual_optimization(tmpdir, strategy, model_cls=TesManualOptimizationD
log_every_n_steps=1,
gpus=2,
strategy=strategy,
callbacks=[TestManualOptimizationDDPCallack()],
)

trainer.fit(model)
Expand Down