Skip to content

Commit

Permalink
Add AMP for validation, prediction and testing (#6565)
Browse files Browse the repository at this point in the history
* Add Tests for val and test-steps

* Add native AMP

* pep8 tests

* pep8 plugin

* changelog
  • Loading branch information
justusschock authored Mar 20, 2021
1 parent cb59039 commit 634d831
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

### Fixed

- Added Autocast in validation, test and predict modes for Native AMP ([#6565](https://github.com/PyTorchLightning/pytorch-lightning/pull/6565))

- Made the `Plugin.reduce` method more consistent across all Plugins to reflect a mean-reduction by default ([#6011](https://github.com/PyTorchLightning/pytorch-lightning/pull/6011))


Expand Down
18 changes: 18 additions & 0 deletions pytorch_lightning/plugins/precision/native_amp.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,21 @@ def train_step_context(self) -> Generator[None, None, None]:
"""Enable autocast context"""
with torch.cuda.amp.autocast():
yield

@contextmanager
def val_step_context(self) -> Generator[None, None, None]:
"""Enable autocast context"""
with torch.cuda.amp.autocast():
yield

@contextmanager
def test_step_context(self) -> Generator[None, None, None]:
"""Enable autocast context"""
with torch.cuda.amp.autocast():
yield

@contextmanager
def predict_context(self) -> Generator[None, None, None]:
"""Enable autocast context"""
with torch.cuda.amp.autocast():
yield
31 changes: 28 additions & 3 deletions tests/models/test_amp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,43 @@
import pytest
import torch
from torch import optim
from torch.utils.data import DataLoader

import tests.helpers.utils as tutils
from pytorch_lightning import Trainer
from pytorch_lightning.plugins.environments import SLURMEnvironment
from pytorch_lightning.trainer.states import TrainerState
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from tests.helpers import BoringModel
from tests.helpers import BoringModel, RandomDataset
from tests.helpers.runif import RunIf


class AMPTestModel(BoringModel):

def training_step(self, batch, batch_idx):
def _step(self, batch, batch_idx):
assert torch.is_autocast_enabled()
output = self(batch)
assert output.dtype == torch.float16
loss = self.loss(batch, output)
return {"loss": loss}
return loss

def training_step(self, batch, batch_idx):
output = self._step(batch, batch_idx)
return {"loss": output}

def validation_step(self, batch, batch_idx):
output = self._step(batch, batch_idx)
return {"x": output}

def test_step(self, batch, batch_idx):
output = self._step(batch, batch_idx)
return {"y": output}

def predict(self, batch, batch_idx, dataloader_idx=None):
assert torch.is_autocast_enabled()
output = self(batch)
assert output.dtype == torch.float16
return output


@pytest.mark.skip(reason='dp + amp not supported currently') # TODO
Expand All @@ -54,6 +73,8 @@ def test_amp_single_gpu_dp(tmpdir):
model = AMPTestModel()
# tutils.run_model_test(trainer_options, model)
trainer.fit(model)
trainer.test(model)
trainer.predict(model, DataLoader(RandomDataset(32, 64)))

assert trainer.state == TrainerState.FINISHED, f"Training failed with {trainer.state}"

Expand All @@ -73,6 +94,8 @@ def test_amp_single_gpu_ddp_spawn(tmpdir):
model = AMPTestModel()
# tutils.run_model_test(trainer_options, model)
trainer.fit(model)
trainer.test(model)
trainer.predict(model, DataLoader(RandomDataset(32, 64)))
assert trainer.state == TrainerState.FINISHED, f"Training failed with {trainer.state}"


Expand Down Expand Up @@ -112,6 +135,8 @@ def test_amp_multi_gpu_ddp_spawn(tmpdir):
model = AMPTestModel()
# tutils.run_model_test(trainer_options, model)
trainer.fit(model)
trainer.test(model)
trainer.predict(model, DataLoader(RandomDataset(32, 64)))
assert trainer.state == TrainerState.FINISHED, f"Training failed with {trainer.state}"


Expand Down

0 comments on commit 634d831

Please sign in to comment.