Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitgr7 committed Mar 12, 2021
1 parent 4800d46 commit d4a6e83
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ default_language_version:

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
rev: v3.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
Expand All @@ -35,6 +35,6 @@ repos:
args: [--parallel, --in-place]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.790
rev: v0.812
hooks:
- id: mypy
3 changes: 2 additions & 1 deletion pytorch_lightning/callbacks/progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,10 @@ def total_val_batches(self) -> int:
validation dataloader is of infinite size.
"""
total_val_batches = 0
if not self.trainer.disable_validation:
if self.trainer.enable_validation:
is_val_epoch = (self.trainer.current_epoch + 1) % self.trainer.check_val_every_n_epoch == 0
total_val_batches = sum(self.trainer.num_val_batches) if is_val_epoch else 0

return total_val_batches

@property
Expand Down
56 changes: 56 additions & 0 deletions tests/trainer/flags/test_check_val_every_n_epoch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright The PyTorch Lightning team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest

from pytorch_lightning.trainer import Trainer
from pytorch_lightning.trainer.states import TrainerState
from tests.helpers import BoringModel


@pytest.mark.parametrize(
'max_epochs,expected_val_loop_calls,expected_val_batches', [
(1, 0, [0]),
(4, 2, [0, 2, 0, 2]),
(5, 2, [0, 2, 0, 2, 0]),
]
)
def test_check_val_every_n_epoch(tmpdir, max_epochs, expected_val_loop_calls, expected_val_batches):

class TestModel(BoringModel):

def __init__(self):
super().__init__()
self.val_epoch_calls = 0
self.val_batches = []

def on_train_epoch_end(self, *args, **kwargs):
self.val_batches.append(self.trainer.progress_bar_callback.total_val_batches)

def on_validation_epoch_start(self) -> None:
self.val_epoch_calls += 1

model = TestModel()
trainer = Trainer(
default_root_dir=tmpdir,
max_epochs=max_epochs,
num_sanity_val_steps=0,
limit_val_batches=2,
check_val_every_n_epoch=2,
logger=False,
)
trainer.fit(model)
assert trainer.state == TrainerState.FINISHED, f"Training failed with {trainer.state}"

assert model.val_epoch_calls == expected_val_loop_calls
assert model.val_batches == expected_val_batches

0 comments on commit d4a6e83

Please sign in to comment.