|
| 1 | +import pytest |
| 2 | + |
| 3 | +from pytorch_lightning import Trainer, Callback |
| 4 | +from pytorch_lightning.trainer.states import TrainerState, trainer_state |
| 5 | +from tests.base import EvalModelTemplate |
| 6 | + |
| 7 | + |
| 8 | +class StateSnapshotCallback(Callback): |
| 9 | + """ Allows to shapshot the state inside a particular trainer method. """ |
| 10 | + |
| 11 | + def __init__(self, snapshot_method: str): |
| 12 | + super().__init__() |
| 13 | + assert snapshot_method in ['on_batch_start', 'on_test_batch_start'] |
| 14 | + self.snapshot_method = snapshot_method |
| 15 | + self.trainer_state = None |
| 16 | + |
| 17 | + def on_batch_start(self, trainer, pl_module): |
| 18 | + if self.snapshot_method == 'on_batch_start': |
| 19 | + self.trainer_state = trainer.state |
| 20 | + |
| 21 | + def on_test_batch_start(self, trainer, pl_module, batch, batch_idx, dataloader_idx): |
| 22 | + if self.snapshot_method == 'on_test_batch_start': |
| 23 | + self.trainer_state = trainer.state |
| 24 | + |
| 25 | + |
| 26 | +def test_state_decorator_nothing_passed(tmpdir): |
| 27 | + """ Test that state is not changed if nothing is passed to a decorator""" |
| 28 | + |
| 29 | + @trainer_state() |
| 30 | + def test_method(self): |
| 31 | + return self.state |
| 32 | + |
| 33 | + trainer = Trainer(default_root_dir=tmpdir) |
| 34 | + trainer.state = TrainerState.INITIALIZING |
| 35 | + |
| 36 | + snapshot_state = test_method(trainer) |
| 37 | + |
| 38 | + assert snapshot_state == TrainerState.INITIALIZING |
| 39 | + assert trainer.state == TrainerState.INITIALIZING |
| 40 | + |
| 41 | + |
| 42 | +def test_state_decorator_entering_only(tmpdir): |
| 43 | + """ Tests that state is set to entering inside a run function and restored to the previous value after. """ |
| 44 | + |
| 45 | + @trainer_state(entering=TrainerState.RUNNING) |
| 46 | + def test_method(self): |
| 47 | + return self.state |
| 48 | + |
| 49 | + trainer = Trainer(default_root_dir=tmpdir) |
| 50 | + trainer.state = TrainerState.INITIALIZING |
| 51 | + |
| 52 | + snapshot_state = test_method(trainer) |
| 53 | + |
| 54 | + assert snapshot_state == TrainerState.RUNNING |
| 55 | + assert trainer.state == TrainerState.INITIALIZING |
| 56 | + |
| 57 | + |
| 58 | +def test_state_decorator_exiting_only(tmpdir): |
| 59 | + """ Tests that state is not changed inside a run function and set to `exiting` after. """ |
| 60 | + |
| 61 | + @trainer_state(exiting=TrainerState.FINISHED) |
| 62 | + def test_method(self): |
| 63 | + return self.state |
| 64 | + |
| 65 | + trainer = Trainer(default_root_dir=tmpdir) |
| 66 | + trainer.state = TrainerState.INITIALIZING |
| 67 | + |
| 68 | + snapshot_state = test_method(trainer) |
| 69 | + |
| 70 | + assert snapshot_state == TrainerState.INITIALIZING |
| 71 | + assert trainer.state == TrainerState.FINISHED |
| 72 | + |
| 73 | + |
| 74 | +def test_state_decorator_entering_and_exiting(tmpdir): |
| 75 | + """ Tests that state is set to `entering` inside a run function and set ot `exiting` after. """ |
| 76 | + |
| 77 | + @trainer_state(entering=TrainerState.RUNNING, exiting=TrainerState.FINISHED) |
| 78 | + def test_method(self): |
| 79 | + return self.state |
| 80 | + |
| 81 | + trainer = Trainer(default_root_dir=tmpdir) |
| 82 | + trainer.state = TrainerState.INITIALIZING |
| 83 | + |
| 84 | + snapshot_state = test_method(trainer) |
| 85 | + |
| 86 | + assert snapshot_state == TrainerState.RUNNING |
| 87 | + assert trainer.state == TrainerState.FINISHED |
| 88 | + |
| 89 | + |
| 90 | +def test_state_decorator_interrupt(tmpdir): |
| 91 | + """ Tests that state remains `INTERRUPTED` is its set in run function. """ |
| 92 | + |
| 93 | + @trainer_state(exiting=TrainerState.FINISHED) |
| 94 | + def test_method(self): |
| 95 | + self.state = TrainerState.INTERRUPTED |
| 96 | + |
| 97 | + trainer = Trainer(default_root_dir=tmpdir) |
| 98 | + trainer.state = TrainerState.INITIALIZING |
| 99 | + |
| 100 | + test_method(trainer) |
| 101 | + assert trainer.state == TrainerState.INTERRUPTED |
| 102 | + |
| 103 | + |
| 104 | +def test_initialize_state(tmpdir): |
| 105 | + """ Tests that state is INITIALIZE after Trainer creation """ |
| 106 | + trainer = Trainer(default_root_dir=tmpdir) |
| 107 | + assert trainer.state == TrainerState.INITIALIZING |
| 108 | + |
| 109 | + |
| 110 | +@pytest.mark.parametrize("extra_params", [ |
| 111 | + pytest.param(dict(fast_dev_run=True), id='Fast-Run'), |
| 112 | + pytest.param(dict(max_steps=1), id='Single-Step'), |
| 113 | +]) |
| 114 | +def test_running_state_during_fit(tmpdir, extra_params): |
| 115 | + """ Tests that state is set to RUNNING during fit """ |
| 116 | + |
| 117 | + hparams = EvalModelTemplate.get_default_hparams() |
| 118 | + model = EvalModelTemplate(**hparams) |
| 119 | + |
| 120 | + snapshot_callback = StateSnapshotCallback(snapshot_method='on_batch_start') |
| 121 | + |
| 122 | + trainer = Trainer( |
| 123 | + callbacks=[snapshot_callback], |
| 124 | + default_root_dir=tmpdir, |
| 125 | + **extra_params |
| 126 | + ) |
| 127 | + |
| 128 | + trainer.fit(model) |
| 129 | + |
| 130 | + assert snapshot_callback.trainer_state == TrainerState.RUNNING |
| 131 | + |
| 132 | + |
| 133 | +@pytest.mark.parametrize("extra_params", [ |
| 134 | + pytest.param(dict(fast_dev_run=True), id='Fast-Run'), |
| 135 | + pytest.param(dict(max_steps=1), id='Single-Step'), |
| 136 | +]) |
| 137 | +def test_finished_state_after_fit(tmpdir, extra_params): |
| 138 | + """ Tests that state is FINISHED after fit """ |
| 139 | + hparams = EvalModelTemplate.get_default_hparams() |
| 140 | + model = EvalModelTemplate(**hparams) |
| 141 | + |
| 142 | + trainer = Trainer( |
| 143 | + default_root_dir=tmpdir, |
| 144 | + **extra_params |
| 145 | + ) |
| 146 | + |
| 147 | + trainer.fit(model) |
| 148 | + |
| 149 | + assert trainer.state == TrainerState.FINISHED |
| 150 | + |
| 151 | + |
| 152 | +def test_running_state_during_test(tmpdir): |
| 153 | + """ Tests that state is set to RUNNING during test """ |
| 154 | + |
| 155 | + hparams = EvalModelTemplate.get_default_hparams() |
| 156 | + model = EvalModelTemplate(**hparams) |
| 157 | + |
| 158 | + snapshot_callback = StateSnapshotCallback(snapshot_method='on_test_batch_start') |
| 159 | + |
| 160 | + trainer = Trainer( |
| 161 | + callbacks=[snapshot_callback], |
| 162 | + default_root_dir=tmpdir, |
| 163 | + fast_dev_run=True, |
| 164 | + ) |
| 165 | + |
| 166 | + trainer.test(model) |
| 167 | + |
| 168 | + assert snapshot_callback.trainer_state == TrainerState.RUNNING |
| 169 | + |
| 170 | + |
| 171 | +def test_finished_state_after_test(tmpdir): |
| 172 | + """ Tests that state is FINISHED after fit """ |
| 173 | + hparams = EvalModelTemplate.get_default_hparams() |
| 174 | + model = EvalModelTemplate(**hparams) |
| 175 | + |
| 176 | + trainer = Trainer( |
| 177 | + default_root_dir=tmpdir, |
| 178 | + fast_dev_run=True, |
| 179 | + ) |
| 180 | + |
| 181 | + trainer.test(model) |
| 182 | + |
| 183 | + assert trainer.state == TrainerState.FINISHED |
| 184 | + |
| 185 | + |
| 186 | +@pytest.mark.parametrize("extra_params", [ |
| 187 | + pytest.param(dict(fast_dev_run=True), id='Fast-Run'), |
| 188 | + pytest.param(dict(max_steps=1), id='Single-Step'), |
| 189 | +]) |
| 190 | +def test_interrupt_state_on_keyboard_interrupt(tmpdir, extra_params): |
| 191 | + """ Tests that state is set to INTERRUPTED on KeyboardInterrupt """ |
| 192 | + hparams = EvalModelTemplate.get_default_hparams() |
| 193 | + model = EvalModelTemplate(**hparams) |
| 194 | + |
| 195 | + class InterruptCallback(Callback): |
| 196 | + def __init__(self): |
| 197 | + super().__init__() |
| 198 | + |
| 199 | + def on_batch_start(self, trainer, pl_module): |
| 200 | + raise KeyboardInterrupt |
| 201 | + |
| 202 | + trainer = Trainer( |
| 203 | + callbacks=[InterruptCallback()], |
| 204 | + default_root_dir=tmpdir, |
| 205 | + **extra_params |
| 206 | + ) |
| 207 | + |
| 208 | + trainer.fit(model) |
| 209 | + |
| 210 | + assert trainer.state == TrainerState.INTERRUPTED |
0 commit comments