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

Increment the total batch idx before the accumulation early exit #7692

Merged
merged 2 commits into from
May 25, 2021
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed global step update when the epoch is skipped ([#7677](https://github.com/PyTorchLightning/pytorch-lightning/pull/7677))


- Fixed training loop total batch counter when accumulate grad batches was enabled ([#7692](https://github.com/PyTorchLightning/pytorch-lightning/pull/7692))


- Fixed broadcasting in multi-node, multi-gpu DDP using torch 1.7 ([#7592](https://github.com/PyTorchLightning/pytorch-lightning/pull/7592))


Expand Down
4 changes: 2 additions & 2 deletions pytorch_lightning/trainer/training_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,8 @@ def run_training_epoch(self):
self.update_train_loop_lr_schedulers(monitor_metrics=monitor_metrics)
self.trainer.checkpoint_connector.has_trained = True

self.total_batch_idx += 1

# max steps reached, end training
if (
self.max_steps is not None and self.max_steps <= self.global_step + 1
Expand All @@ -542,8 +544,6 @@ def run_training_epoch(self):
if self.trainer.should_stop:
break

self.total_batch_idx += 1

# stop epoch if we limited the number of training batches
if self._num_training_batches_reached(is_last_batch):
break
Expand Down
27 changes: 10 additions & 17 deletions tests/tuner/test_lr_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,31 +197,24 @@ def test_datamodule_parameter(tmpdir):


def test_accumulation_and_early_stopping(tmpdir):
""" Test that early stopping of learning rate finder works, and that
accumulation also works for this feature """
""" Test that early stopping of learning rate finder works, and that accumulation also works for this feature """

hparams = EvalModelTemplate.get_default_hparams()
model = EvalModelTemplate(**hparams)
class TestModel(BoringModel):

before_lr = hparams.get('learning_rate')
# logger file to get meta
def __init__(self):
super().__init__()
self.lr = 1e-3

model = TestModel()
trainer = Trainer(
default_root_dir=tmpdir,
accumulate_grad_batches=2,
)

lrfinder = trainer.tuner.lr_find(model, early_stop_threshold=None)
after_lr = lrfinder.suggestion()

expected_num_lrs = 100
expected_batch_idx = 200 - 1

assert before_lr != after_lr, \
'Learning rate was not altered after running learning rate finder'
assert len(lrfinder.results['lr']) == expected_num_lrs, \
'Early stopping for learning rate finder did not work'
assert lrfinder._total_batch_idx == expected_batch_idx, \
'Accumulation parameter did not work'
assert lrfinder.suggestion() != 1e-3
assert len(lrfinder.results['lr']) == 100
assert lrfinder._total_batch_idx == 200


def test_suggestion_parameters_work(tmpdir):
Expand Down