Skip to content

Commit 2a19336

Browse files
authored
Merge branch 'master' into s-rog-patch-2
2 parents d1cd509 + 5f34f2b commit 2a19336

26 files changed

+528
-134
lines changed

.github/workflows/ci_test-base.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ jobs:
7676
with:
7777
name: pytest-results-${{ runner.os }}-${{ matrix.python-version }}-${{ matrix.requires }}
7878
path: junit/test-results-${{ runner.os }}-${{ matrix.python-version }}-${{ matrix.requires }}.xml
79-
# Use always() to always run this step to publish test results when there are test failures
80-
if: always()
79+
if: failure()
8180

8281
- name: Statistics
8382
if: success()

.github/workflows/ci_test-conda.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,4 @@ jobs:
5050
with:
5151
name: pytest-results-${{ runner.os }}-${{ matrix.python-version }}-${{ matrix.requires }}
5252
path: junit/test-results-${{ runner.os }}-${{ matrix.python-version }}-${{ matrix.requires }}.xml
53-
# Use always() to always run this step to publish test results when there are test failures
54-
if: always()
53+
if: failure()

.github/workflows/ci_test-full.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ jobs:
129129
with:
130130
name: pytest-results-${{ runner.os }}-${{ matrix.python-version }}-${{ matrix.requires }}
131131
path: junit/test-results-${{ runner.os }}-${{ matrix.python-version }}-${{ matrix.requires }}.xml
132-
# Use always() to always run this step to publish test results when there are test failures
133-
if: always()
132+
if: failure()
134133

135134
- name: Statistics
136135
if: success()

CHANGELOG.md

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

4040
### Fixed
4141

42+
- Fixed trainer by default `None` in `DDPAccelerator` ([#4915](https://github.com/PyTorchLightning/pytorch-lightning/pull/4915))
43+
44+
45+
- Fixed `LightningOptimizer` exposes optimizer attributes ([#5095](https://github.com/PyTorchLightning/pytorch-lightning/pull/5095))
46+
4247

4348

4449
## [1.1.0] - 2020-12-09
@@ -80,9 +85,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
8085

8186
### Changed
8287

83-
- Removed `multiclass_roc` and `multiclass_precision_recall_curve`, use `roc` and `precision_recall_curve` instead ([#4549](https://github.com/PyTorchLightning/pytorch-lightning/pull/4549))
8488
- Tuner algorithms will be skipped if `fast_dev_run=True` ([#3903](https://github.com/PyTorchLightning/pytorch-lightning/pull/3903))
85-
- WandbLogger does not force wandb `reinit` arg to True anymore and creates a run only when needed ([#4648](https://github.com/PyTorchLightning/pytorch-lightning/pull/4648))
89+
- `WandbLogger` does not force wandb `reinit` arg to True anymore and creates a run only when needed ([#4648](https://github.com/PyTorchLightning/pytorch-lightning/pull/4648))
8690
- Changed `automatic_optimization` to be a model attribute ([#4602](https://github.com/PyTorchLightning/pytorch-lightning/pull/4602))
8791
- Changed `Simple Profiler` report to order by percentage time spent + num calls ([#4880](https://github.com/PyTorchLightning/pytorch-lightning/pull/4880))
8892
- Simplify optimization Logic ([#4984](https://github.com/PyTorchLightning/pytorch-lightning/pull/4984))
@@ -100,6 +104,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
100104
### Removed
101105

102106
- Removed `reorder` parameter of the `auc` metric ([#5004](https://github.com/PyTorchLightning/pytorch-lightning/pull/5004))
107+
- Removed `multiclass_roc` and `multiclass_precision_recall_curve`, use `roc` and `precision_recall_curve` instead ([#4549](https://github.com/PyTorchLightning/pytorch-lightning/pull/4549))
103108

104109
### Fixed
105110

docs/source/multi_gpu.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ It is highly recommended to use Sharded Training in multi-GPU environments where
663663
A technical note: as batch size scales, storing activations for the backwards pass becomes the bottleneck in training. As a result, sharding optimizer state and gradients becomes less impactful.
664664
Work within the future will bring optional sharding to activations and model parameters to reduce memory further, but come with a speed cost.
665665

666-
To use Sharded Training, you need to first install FairScale using the command below or install all extras using ``pip install pytorch-lightning["extra"]``.
666+
To use Sharded Training, you need to first install FairScale using the command below.
667667

668668
.. code-block:: bash
669669

docs/source/optimizers.rst

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -191,46 +191,69 @@ override the :meth:`optimizer_step` function.
191191
192192
For example, here step optimizer A every 2 batches and optimizer B every 4 batches
193193
194-
.. testcode::
194+
.. note:: When using Trainer(enable_pl_optimizer=True), there is no need to call `.zero_grad()`.
195195
196-
def optimizer_step(self, current_epoch, batch_nb, optimizer, optimizer_idx, second_order_closure=None, on_tpu=False, using_native_amp=False, using_lbfgs=False):
197-
optimizer.step()
196+
.. testcode::
198197
199198
def optimizer_zero_grad(self, current_epoch, batch_idx, optimizer, opt_idx):
200199
optimizer.zero_grad()
201200
202201
# Alternating schedule for optimizer steps (ie: GANs)
203-
def optimizer_step(self, current_epoch, batch_nb, optimizer, optimizer_idx, second_order_closure=None, on_tpu=False, using_native_amp=False, using_lbfgs=False):
202+
def optimizer_step(self, current_epoch, batch_nb, optimizer, optimizer_idx, closure, on_tpu=False, using_native_amp=False, using_lbfgs=False):
204203
# update generator opt every 2 steps
205204
if optimizer_i == 0:
206205
if batch_nb % 2 == 0 :
207-
optimizer.step()
208-
optimizer.zero_grad()
206+
optimizer.step(closure=closure)
209207
210208
# update discriminator opt every 4 steps
211209
if optimizer_i == 1:
212210
if batch_nb % 4 == 0 :
213-
optimizer.step()
214-
optimizer.zero_grad()
211+
optimizer.step(closure=closure)
212+
213+
.. note:: When using ``Trainer(enable_pl_optimizer=True)``, ``.step`` accepts a boolean ``make_optimizer_step`` which can be used as follow.
214+
215+
.. testcode::
216+
217+
def optimizer_zero_grad(self, current_epoch, batch_idx, optimizer, opt_idx):
218+
optimizer.zero_grad()
219+
220+
# Alternating schedule for optimizer steps (ie: GANs)
221+
def optimizer_step(self, current_epoch, batch_nb, optimizer, optimizer_idx, closure, on_tpu=False, using_native_amp=False, using_lbfgs=False):
222+
# update generator opt every 2 steps
223+
if optimizer_i == 0:
224+
optimizer.step(closure=closure, make_optimizer_step=(batch_nb % 2) == 0)
215225
216-
# ...
217-
# add as many optimizers as you want
226+
# update discriminator opt every 4 steps
227+
if optimizer_i == 1:
228+
optimizer.step(closure=closure, make_optimizer_step=(batch_nb % 4) == 0)
218229
219230
Here we add a learning-rate warm up
220231
221232
.. testcode::
222233
223234
# learning rate warm-up
224-
def optimizer_step(self, current_epoch, batch_nb, optimizer, optimizer_idx, second_order_closure=None, on_tpu=False, using_native_amp=False, using_lbfgs=False):
235+
def optimizer_step(self, current_epoch, batch_nb, optimizer, optimizer_idx, closure, on_tpu=False, using_native_amp=False, using_lbfgs=False):
225236
# warm up lr
226237
if self.trainer.global_step < 500:
227238
lr_scale = min(1., float(self.trainer.global_step + 1) / 500.)
228239
for pg in optimizer.param_groups:
229240
pg['lr'] = lr_scale * self.hparams.learning_rate
230241
231242
# update params
232-
optimizer.step()
233-
optimizer.zero_grad()
243+
optimizer.step(closure=closure)
244+
245+
The default ``optimizer_step`` is relying on the internal ``LightningOptimizer`` to properly perform a step.
246+
247+
.. testcode::
248+
249+
from pytorch_lightning.core.optimizer import LightningOptimizer
250+
251+
# function hook in LightningModule
252+
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)
256+
optimizer.step(closure=closure)
234257
235258
----------
236259

pytorch_lightning/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Root package info."""
22

3-
__version__ = '1.1.0'
3+
__version__ = '1.1.1rc0'
44
__author__ = 'William Falcon et al.'
55
__author_email__ = '[email protected]'
66
__license__ = 'Apache-2.0'

pytorch_lightning/core/lightning.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,6 @@ def toggle_optimizer(self, optimizer: Optimizer, optimizer_idx: int):
11701170

11711171
def optimizer_step(
11721172
self,
1173-
*args,
11741173
epoch: int = None,
11751174
batch_idx: int = None,
11761175
optimizer: Optimizer = None,
@@ -1179,7 +1178,6 @@ def optimizer_step(
11791178
on_tpu: bool = None,
11801179
using_native_amp: bool = None,
11811180
using_lbfgs: bool = None,
1182-
**kwargs,
11831181
) -> None:
11841182
r"""
11851183
Override this method to adjust the default way the
@@ -1254,7 +1252,7 @@ def optimizer_step(self, epoch, batch_idx, optimizer, optimizer_idx,
12541252
if not isinstance(optimizer, LightningOptimizer):
12551253
# wraps into LightingOptimizer only for running step
12561254
optimizer = LightningOptimizer.to_lightning_optimizer(optimizer, self.trainer)
1257-
optimizer.step(closure=optimizer_closure, *args, **kwargs)
1255+
optimizer.step(closure=optimizer_closure)
12581256

12591257
def optimizer_zero_grad(
12601258
self, epoch: int, batch_idx: int, optimizer: Optimizer, optimizer_idx: int

pytorch_lightning/core/optimizer.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,35 @@ def __init__(self,
5757
else:
5858
self.__class__ = type("Lightning" + optimizer.__class__.__name__, (self.__class__, optimizer.__class__), {})
5959

60-
self._trainer = None
6160
self._optimizer = optimizer
61+
self._trainer = None
6262
self._accumulate_grad_batches = accumulate_grad_batches
63-
self._automatic_optimization = None
6463
self._optimizer_idx = None
6564

65+
@property
66+
def defaults(self):
67+
return self._optimizer.defaults
68+
69+
@defaults.setter
70+
def defaults(self, defaults):
71+
self._optimizer.defaults = defaults
72+
73+
@property
74+
def state(self):
75+
return self._optimizer.state
76+
77+
@state.setter
78+
def state(self, state):
79+
self._optimizer.state = state
80+
81+
@property
82+
def param_groups(self):
83+
return self._optimizer.param_groups
84+
85+
@param_groups.setter
86+
def param_groups(self, param_groups):
87+
self._optimizer.param_groups = param_groups
88+
6689
@property
6790
def accumulate_grad_batches(self):
6891
return self._accumulate_grad_batches
@@ -73,7 +96,6 @@ def accumulate_grad_batches(self, accumulate_grad_batches):
7396

7497
def _on_trainer_init(self, trainer):
7598
self._trainer = proxy(trainer)
76-
self._automatic_optimization = trainer.train_loop.automatic_optimization
7799
for opt_idx, opt in enumerate(trainer.optimizers):
78100
if opt == self._optimizer:
79101
self._optimizer_idx = opt_idx

pytorch_lightning/metrics/classification/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from pytorch_lightning.metrics.classification.accuracy import Accuracy
1515
from pytorch_lightning.metrics.classification.average_precision import AveragePrecision
1616
from pytorch_lightning.metrics.classification.confusion_matrix import ConfusionMatrix
17-
from pytorch_lightning.metrics.classification.f_beta import FBeta, F1
17+
from pytorch_lightning.metrics.classification.f_beta import FBeta, Fbeta, F1
1818
from pytorch_lightning.metrics.classification.precision_recall import Precision, Recall
1919
from pytorch_lightning.metrics.classification.precision_recall_curve import PrecisionRecallCurve
2020
from pytorch_lightning.metrics.classification.roc import ROC

0 commit comments

Comments
 (0)