Skip to content
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
1 change: 1 addition & 0 deletions src/lightning/pytorch/trainer/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ def fit(
self.state.fn = TrainerFn.FITTING
self.state.status = TrainerStatus.RUNNING
self.training = True
self.should_stop = False
call._call_and_handle_interrupt(
self, self._fit_impl, model, train_dataloaders, val_dataloaders, datamodule, ckpt_path
)
Expand Down
15 changes: 12 additions & 3 deletions tests/tests_pytorch/loops/test_training_epoch_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import pytest
import torch
from lightning.fabric.utilities.warnings import PossibleUserWarning
from lightning.pytorch.callbacks import ModelCheckpoint
from lightning.pytorch.callbacks import EarlyStopping, ModelCheckpoint
from lightning.pytorch.demos.boring_classes import BoringModel
from lightning.pytorch.trainer.trainer import Trainer
from lightning_utilities.test.warning import no_warning_call
Expand Down Expand Up @@ -90,7 +90,16 @@ def test_should_stop_triggers_validation_once(min_epochs, min_steps, val_count,
(min_epochs/steps is satisfied).

"""
model = BoringModel()

class NewBoring(BoringModel):
def training_step(self, batch, batch_idx):
self.log("loss", self.step(batch))
return {"loss": self.step(batch)}

model = NewBoring()
# create a stopping condition with a high threshold so it triggers immediately
# check the condition before validation so the count is unaffected
stopping = EarlyStopping(monitor="loss", check_on_train_epoch_end=True, stopping_threshold=100)
trainer = Trainer(
default_root_dir=tmp_path,
num_sanity_val_steps=0,
Expand All @@ -101,8 +110,8 @@ def test_should_stop_triggers_validation_once(min_epochs, min_steps, val_count,
min_steps=min_steps,
enable_model_summary=False,
enable_checkpointing=False,
callbacks=[stopping],
)
trainer.should_stop = True # Request to stop before min_epochs/min_steps are reached
trainer.fit_loop.epoch_loop.val_loop.run = Mock()
trainer.fit(model)
assert trainer.fit_loop.epoch_loop.val_loop.run.call_count == val_count
Expand Down