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

Deprecate LightningModule.use_amp #12315

Merged
merged 10 commits into from
Mar 18, 2022
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Deprecated `LightningDataModule.on_save/load_checkpoint` in favor of `state_dict/load_state_dict` ([#11893](https://github.com/PyTorchLightning/pytorch-lightning/pull/11893))


- Deprecated `LightingModule.use_amp` ([#12315](https://github.com/PyTorchLightning/pytorch-lightning/pull/12315))

### Removed

- Removed deprecated parameter `method` in `pytorch_lightning.utilities.model_helpers.is_overridden` ([#10507](https://github.com/PyTorchLightning/pytorch-lightning/pull/10507))
Expand Down
5 changes: 0 additions & 5 deletions docs/source/common/lightning_module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1037,11 +1037,6 @@ Pointer to the trainer
max_steps = self.trainer.max_steps
any_flag = self.trainer.any_flag

use_amp
~~~~~~~

``True`` if using Automatic Mixed Precision (AMP)

prepare_data_per_node
~~~~~~~~~~~~~~~~~~~~~

Expand Down
4 changes: 2 additions & 2 deletions docs/source/starter/core_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -944,15 +944,15 @@ default implementation

.. testcode::

def backward(self, use_amp, loss, optimizer):
def backward(self, loss, optimizer):
loss.backward()

With your own

.. testcode::

class LitMNIST(LightningModule):
def backward(self, use_amp, loss, optimizer, optimizer_idx):
def backward(self, loss, optimizer, optimizer_idx):
# do a custom way of backward
loss.backward(retain_graph=True)

Expand Down
20 changes: 18 additions & 2 deletions pytorch_lightning/core/lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
# pointer to the trainer object
self.trainer = None

# true if using amp
self.use_amp: bool = False
self._use_amp: bool = False

# the precision used
self.precision: int = 32
Expand Down Expand Up @@ -1966,6 +1965,23 @@ def model_size(self) -> float:
)
return get_model_size_mb(self)

@property
def use_amp(self) -> bool:
"""Returns true if using AMP.

.. deprecated:: v1.6 This property was deprecated in v1.6 and will be removed in v1.8.
"""
rank_zero_deprecation(
"`LightningModule.use_amp` was deprecated in v1.6 and will be removed in v1.8."
" Please use `Trainer.amp_backend`.",
stacklevel=5,
)
return self._use_amp

@use_amp.setter
def use_amp(self, use_amp: bool) -> None:
self._use_amp = use_amp

def add_to_queue(self, queue: pl.strategies.launchers.spawn._FakeQueue) -> None:
"""Appends the :attr:`trainer.callback_metrics` dictionary to the given queue. To avoid issues with memory
sharing, we cast the data to numpy.
Expand Down
1 change: 1 addition & 0 deletions pytorch_lightning/trainer/connectors/data_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ def _copy_trainer_model_properties(self, model):

for m in [model, ref_model]:
m.trainer = proxy(self.trainer)
# Remove next line in v1.8
m.use_amp = self.trainer.amp_backend is not None
m.precision = self.trainer.precision

Expand Down
6 changes: 6 additions & 0 deletions tests/deprecated_api/test_remove_1-8.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,3 +817,9 @@ def on_load_checkpoint(self, checkpoint):
" v1.6 and will be removed in v1.8. Use `load_state_dict` instead."
):
_check_datamodule_checkpoint_hooks(trainer)


def test_v1_8_0_lightning_module_use_amp():
model = BoringModel()
with pytest.deprecated_call(match="`LightningModule.use_amp` was deprecated in v1.6"):
_ = model.use_amp