-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial tracking of states in Trainer.
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from enum import Enum, auto | ||
|
||
from pytorch_lightning import Callback | ||
|
||
|
||
class TrainerState(Enum): | ||
""" State which is set to the Trainer to indicate what is being executed. """ | ||
INITIALIZE = auto() | ||
RUNNING = auto() | ||
FINISHED = auto() | ||
|
||
|
||
class _TrainerStateSwitcher(Callback): | ||
""" Special callback used by the Trainer. This callback sets proper | ||
state to the trainer depending on what is being executed. | ||
""" | ||
|
||
def on_init_start(self, trainer): | ||
trainer.state = TrainerState.INITIALIZE | ||
|
||
def on_init_end(self, trainer): | ||
trainer.state = TrainerState.INITIALIZE | ||
|
||
def setup(self, trainer, stage: str): | ||
trainer.state = TrainerState.RUNNING | ||
|
||
def teardown(self, trainer, stage: str): | ||
trainer.state = TrainerState.FINISHED | ||
|
||
def on_keyboard_interrupt(self, trainer, pl_module): | ||
trainer.state = TrainerState.FINISHED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from pytorch_lightning import Trainer | ||
from pytorch_lightning.trainer.states import TrainerState | ||
from tests.base import EvalModelTemplate | ||
|
||
|
||
def test_initialize_state(tmpdir): | ||
""" | ||
Tests that state is INITIALIZE after Trainer creation | ||
""" | ||
trainer = Trainer( | ||
default_root_dir=tmpdir, | ||
fast_dev_run=True, | ||
) | ||
|
||
assert trainer.state == TrainerState.INITIALIZE | ||
|
||
|
||
def test_finished_state_after_fit(tmpdir): | ||
""" | ||
Tests that state is FINISHED after fit | ||
""" | ||
hparams = EvalModelTemplate.get_default_hparams() | ||
model = EvalModelTemplate(**hparams) | ||
|
||
trainer = Trainer( | ||
default_root_dir=tmpdir, | ||
fast_dev_run=True, | ||
) | ||
|
||
trainer.fit(model) | ||
|
||
assert trainer.state == TrainerState.FINISHED | ||
|
||
|
||
def test_finished_state_after_test(tmpdir): | ||
""" | ||
Tests that state is FINISHED after fit | ||
""" | ||
hparams = EvalModelTemplate.get_default_hparams() | ||
model = EvalModelTemplate(**hparams) | ||
|
||
trainer = Trainer( | ||
default_root_dir=tmpdir, | ||
fast_dev_run=True, | ||
) | ||
|
||
trainer.test(model) | ||
|
||
assert trainer.state == TrainerState.FINISHED |