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

fix auto scale batch size not working with precision=16 #3045

Merged
merged 6 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -148,6 +148,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

- Fixed gathering of results with tensors of varying shape ([#3020](https://github.com/PyTorchLightning/pytorch-lightning/pull/3020))

- Fixed automatic batch scaling not working with half precision ([#3045](https://github.com/PyTorchLightning/pytorch-lightning/pull/3045))

## [0.8.5] - 2020-07-09

### Added
Expand Down
2 changes: 1 addition & 1 deletion pytorch_lightning/trainer/training_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def dump_checkpoint(self, weights_only: bool = False) -> dict:
checkpoint['lr_schedulers'] = lr_schedulers

# save native amp scaling
if self.amp_backend == AMPType.NATIVE and not self.use_tpu:
if self.amp_backend == AMPType.NATIVE and not self.use_tpu and self.scaler is not None:
checkpoint['native_amp_scaling_state'] = self.scaler.state_dict()
elif self.amp_backend == AMPType.APEX:
checkpoint['amp_scaling_state'] = amp.state_dict()
Expand Down
20 changes: 20 additions & 0 deletions tests/trainer/test_trainer_tricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import tests.base.develop_utils as tutils
from pytorch_lightning import Trainer
from pytorch_lightning.utilities import AMPType, NATIVE_AMP_AVALAIBLE
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from tests.base import EvalModelTemplate

Expand Down Expand Up @@ -257,3 +258,22 @@ def test_error_on_dataloader_passed_to_fit(tmpdir):

with pytest.raises(MisconfigurationException):
trainer.fit(model, **fit_options)


@pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires GPU machine")
@pytest.mark.skipif(not NATIVE_AMP_AVALAIBLE, reason="test requires native AMP.")
def test_auto_scale_batch_size_with_amp(tmpdir):
model = EvalModelTemplate()
batch_size_before = model.batch_size
trainer = Trainer(
default_root_dir=tmpdir,
max_steps=1,
auto_scale_batch_size=True,
gpus=1,
precision=16
)
trainer.fit(model)
batch_size_after = model.batch_size
assert trainer.amp_backend == AMPType.NATIVE
assert trainer.scaler is not None
assert batch_size_after != batch_size_before