Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -454,6 +454,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Disbled sampler replacement when using `IterableDataset` ([#11507](https://github.com/PyTorchLightning/pytorch-lightning/pull/11507))


- Fixed an issue to avoid validation loop run on restart ([#11552](https://github.com/PyTorchLightning/pytorch-lightning/pull/11552))


## [1.5.8] - 2022-01-05

### Fixed
Expand Down
5 changes: 5 additions & 0 deletions pytorch_lightning/loops/epoch/training_epoch_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,11 @@ def _should_check_val_fx(self, batch_idx: int, is_last_batch: bool) -> bool:

# TODO(@awaelchli): let training/eval loop handle logic around limit_*_batches and val_check_batch
is_val_check_batch = is_last_batch

# while restarting with no fault-tolerant, batch_progress.current.ready is -1
if batch_idx == -1:
return False

if isinstance(self.trainer.limit_train_batches, int) and is_infinite_dataset:
is_val_check_batch = (batch_idx + 1) % self.trainer.limit_train_batches == 0
elif self.trainer.val_check_batch != float("inf"):
Expand Down
24 changes: 24 additions & 0 deletions tests/loops/epoch/test_training_epoch_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
# 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.
from unittest.mock import patch

import pytest

from pytorch_lightning.loops import TrainingEpochLoop
from pytorch_lightning.trainer.trainer import Trainer
from tests.helpers.boring_model import BoringModel

_out00 = {"loss": 0.0}
_out01 = {"loss": 0.1}
Expand Down Expand Up @@ -141,3 +145,23 @@ def test_prepare_outputs_training_batch_end_manual(batch_end_outputs, expected):
num_optimizers=-1, # does not matter for manual optimization
)
assert prepared == expected


def test_no_val_on_train_epoch_loop_restart(tmpdir):
Comment thread
carmocca marked this conversation as resolved.
"""Test that training validation loop doesn't get triggered at the beginning of a restart."""
trainer_kwargs = {"max_epochs": 1, "limit_train_batches": 1, "limit_val_batches": 1, "enable_checkpointing": False}
Comment thread
rohitgr7 marked this conversation as resolved.
Outdated
trainer = Trainer(**trainer_kwargs)
model = BoringModel()
trainer.fit(model)
ckpt_path = tmpdir / "last.ckpt"
trainer.save_checkpoint(str(ckpt_path))
Comment thread
rohitgr7 marked this conversation as resolved.
Outdated

trainer_kwargs["max_epochs"] = 2
trainer = Trainer(**trainer_kwargs)

with patch.object(
Comment thread
rohitgr7 marked this conversation as resolved.
trainer.fit_loop.epoch_loop.val_loop,
"run",
) as mocked:
trainer.fit(model, ckpt_path=str(ckpt_path))
Comment thread
rohitgr7 marked this conversation as resolved.
Outdated
assert mocked.call_count == 1