Skip to content

Commit d6418a1

Browse files
tchatonSeanNaren
authored andcommitted
deprecate enable_pl_optimizer as it is not restored properly (#5244)
* update * clean test * still in progress * udpdate test * update * update * resolve flake * add test for zero_grad * update * works without accumulated_grad * update * update * resolve amp * revert back to True * update * clean tests * cleaned out * typo * update test * git repare bug * remove print * udpate * Fix formatting/optimizer imports * Refactor the test for cleanliness * Add vanilla model to the test, better var names * Fixed var names, let's clean up these mock tests * repare test * update test * resolve flake8 * add manual_optimization * update tests * resolve flake8 * add random accumulate_grad_batches * improve test * Update tests/trainer/optimization/test_parity_automatic_optimization.py Co-authored-by: Jirka Borovec <[email protected]> * Update tests/trainer/optimization/test_parity_automatic_optimization.py Co-authored-by: Jirka Borovec <[email protected]> * update * clean tests * correct bug * Apply suggestions from code review * format * adress comments * update on comments * wip * typo * depreceate enable_pl_optimizer * resolve latest bugs * update * resolve merge * add comment * Update pytorch_lightning/core/lightning.py Co-authored-by: Jirka Borovec <[email protected]> * Update tests/deprecated_api/test_remove_1-3.py Co-authored-by: Jirka Borovec <[email protected]> * Update pytorch_lightning/trainer/connectors/optimizer_connector.py Co-authored-by: Jirka Borovec <[email protected]> * Update pytorch_lightning/trainer/trainer.py Co-authored-by: Jirka Borovec <[email protected]> * Update pytorch_lightning/trainer/trainer.py Co-authored-by: Jirka Borovec <[email protected]> * Update tests/trainer/optimization/test_parity_automatic_optimization.py Co-authored-by: Jirka Borovec <[email protected]> * update on comments * update restore * add a property * remove setstate as not needed anymore * update test * provide optimizer to on_before_zero_grad * update on comments * update on comments * Update pytorch_lightning/trainer/trainer.py Co-authored-by: Adrian Wälchli <[email protected]> * Update tests/trainer/optimization/test_parity_automatic_optimization.py Co-authored-by: Adrian Wälchli <[email protected]> * Update tests/trainer/optimization/test_parity_automatic_optimization.py Co-authored-by: Adrian Wälchli <[email protected]> * Update tests/trainer/optimization/test_parity_automatic_optimization.py Co-authored-by: Adrian Wälchli <[email protected]> * mofidy import * update changelog * resolve flake8 * update * update * clean doc Co-authored-by: SeanNaren <[email protected]> Co-authored-by: Ubuntu <[email protected]> Co-authored-by: Jirka Borovec <[email protected]> Co-authored-by: Jirka Borovec <[email protected]> Co-authored-by: Adrian Wälchli <[email protected]> Co-authored-by: Sean Naren <[email protected]> (cherry picked from commit f2e99d6)
1 parent b2a9ec6 commit d6418a1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+162
-219
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
101101

102102
### Changed
103103

104+
- Changed depreceated `enable_pl_optimizer=True` ([#5244](https://github.com/PyTorchLightning/pytorch-lightning/pull/5244))
105+
104106

105107
### Deprecated
106108

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ with tempfile.NamedTemporaryFile(suffix='.onnx', delete=False) as tmpfile:
225225
```python
226226
class LitAutoEncoder(pl.LightningModule):
227227
def training_step(self, batch, batch_idx, opt_idx):
228-
(opt_a, opt_b) = self.optimizers()
228+
# access your optimizers with use_pl_optimizer=False. Default is True
229+
(opt_a, opt_b) = self.optimizers(use_pl_optimizer=True)
229230

230231
loss_a = ...
231232
self.manual_backward(loss_a, opt_a)

benchmarks/test_sharded_parity.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ def train_dataloader(self):
175175
class SeedTrainLoaderManualModel(SeedTrainLoaderModel):
176176
def training_step(self, batch, batch_idx, optimizer_idx):
177177
# manual
178-
(opt_a, opt_b) = self.optimizers()
178+
# access your optimizers with use_pl_optimizer=False. Default is True
179+
(opt_a, opt_b) = self.optimizers(use_pl_optimizer=True)
179180
loss_1 = self.step(batch)
180181

181182
self.manual_backward(loss_1, opt_a)

docs/source/new-project.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,8 @@ Now you own the train loop!
268268
.. code-block:: python
269269
270270
def training_step(self, batch, batch_idx, opt_idx):
271-
(opt_a, opt_b, opt_c) = self.optimizers()
271+
# access your optimizers with use_pl_optimizer=False. Default is True
272+
(opt_a, opt_b, opt_c) = self.optimizers(use_pl_optimizer=True)
272273
273274
loss_a = self.generator(batch[0])
274275

docs/source/optimizers.rst

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,15 @@ to manually manage the optimization process. To do so, do the following:
2828
.. code-block:: python
2929
3030
def training_step(self, batch, batch_idx, optimizer_idx):
31-
# ignore optimizer_idx
32-
(opt_g, opt_d) = self.optimizers()
31+
32+
# 1. ignore optimizer_idx
33+
# 2. `use_pl_optimizer=True` means `opt_g` and `opt_d` will be of type `LightingOptimizer`
34+
# `LightingOptimizer` simply wrapped your optimizer and behave the same way !
35+
# When calling `optimizer.step`, `LightingOptimizer` will just handle TPU, AMP, accumulate_grad_batches, etc ... for you.
36+
37+
# access your optimizers with `use_pl_optimizer=False` or `optimizer.optimizer` when using use_pl_optimizer=True
38+
# use_pl_optimizer=True is the default
39+
(opt_g, opt_d) = self.optimizers(use_pl_optimizer=True)
3340
3441
# do anything you want
3542
loss_a = ...
@@ -242,19 +249,29 @@ Here we add a learning-rate warm up
242249
# update params
243250
optimizer.step(closure=closure)
244251

245-
The default ``optimizer_step`` is relying on the internal ``LightningOptimizer`` to properly perform a step.
252+
.. note:: The default ``optimizer_step`` is relying on the internal ``LightningOptimizer`` to properly perform a step. It handles TPUs, AMP, accumulate_grad_batches, zero_grad, and much more ...
246253

247254
.. testcode::
248255

249-
from pytorch_lightning.core.optimizer import LightningOptimizer
256+
# function hook in LightningModule
257+
def optimizer_step(self, current_epoch, batch_nb, optimizer, optimizer_idx, closure, on_tpu=False, using_native_amp=False, using_lbfgs=False):
258+
optimizer.step(closure=closure)
259+
260+
.. note:: To access your wrapped Optimizer from ``LightningOptimizer``, do as follow.
261+
262+
.. testcode::
250263

251264
# function hook in LightningModule
252265
def optimizer_step(self, current_epoch, batch_nb, optimizer, optimizer_idx, closure, on_tpu=False, using_native_amp=False, using_lbfgs=False):
253-
if not isinstance(optimizer, LightningOptimizer):
254-
# wraps into LightingOptimizer only for running step
255-
optimizer = LightningOptimizer.to_lightning_optimizer(optimizer, self.trainer)
266+
267+
# `optimizer is a ``LightningOptimizer`` wrapping the optimizer.
268+
# To access it, do as follow:
269+
optimizer = optimizer.optimizer
270+
271+
# run step. However, it won't work on TPU, AMP, etc...
256272
optimizer.step(closure=closure)
257273

274+
258275
----------
259276

260277
Using the closure functions for optimization

docs/source/trainer.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ optimizer behavior
335335
Example::
336336
337337
def training_step(self, batch, batch_idx):
338-
opt = self.optimizers()
338+
# access your optimizers with use_pl_optimizer=False. Default is True
339+
opt = self.optimizers(use_pl_optimizer=True)
339340
340341
loss = ...
341342
self.manual_backward(loss, opt)
@@ -350,7 +351,8 @@ In the multi-optimizer case, ignore the optimizer_idx flag and use the optimizer
350351
Example::
351352
352353
def training_step(self, batch, batch_idx, optimizer_idx):
353-
(opt_a, opt_b) = self.optimizers()
354+
# access your optimizers with use_pl_optimizer=False. Default is True
355+
(opt_a, opt_b) = self.optimizers(use_pl_optimizer=True)
354356
355357
gen_loss = ...
356358
self.manual_backward(gen_loss, opt_a)

pytorch_lightning/accelerators/cpu_accelerator.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
from typing import Any, Optional, Union, Callable
14+
from typing import Any, Callable, Optional, Union
1515

1616
import torch
1717

@@ -48,8 +48,6 @@ def setup(self, model):
4848
# allow for lr schedulers as well
4949
self.setup_optimizers(model)
5050

51-
self.trainer.convert_to_lightning_optimizers()
52-
5351
self.trainer.model = model
5452

5553
def train(self):

pytorch_lightning/accelerators/ddp2_accelerator.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,6 @@ def ddp_train(self, process_idx, mp_queue, model):
189189
# 16-bit
190190
model = self.trainer.precision_connector.connect(model)
191191

192-
self.trainer.convert_to_lightning_optimizers()
193-
194192
# device ids change depending on the DDP setup
195193
device_ids = self.get_device_ids()
196194

pytorch_lightning/accelerators/ddp_accelerator.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License
1414
import os
15+
from os.path import abspath
1516
import subprocess
1617
import sys
17-
from os.path import abspath
1818
from time import sleep
1919
from typing import Any, List, Optional, Union
2020

@@ -292,8 +292,6 @@ def ddp_train(self, process_idx, model):
292292
# 16-bit
293293
model = self.trainer.precision_connector.connect(model)
294294

295-
self.trainer.convert_to_lightning_optimizers()
296-
297295
# device ids change depending on the DDP setup
298296
device_ids = self.get_device_ids()
299297

pytorch_lightning/accelerators/ddp_cpu_spawn_accelerator.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,6 @@ def ddp_train(self, process_idx, mp_queue, model):
148148
# 16-bit
149149
model = self.trainer.precision_connector.connect(model)
150150

151-
self.trainer.convert_to_lightning_optimizers()
152-
153151
# DDP spawn already spawned off each process... no need to do anything
154152
device_ids = self.get_device_ids()
155153

0 commit comments

Comments
 (0)