2020-10-13 11:18:07 +00:00
|
|
|
# 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.
|
2020-08-09 10:24:09 +00:00
|
|
|
import pytest
|
|
|
|
|
2021-01-14 12:51:20 +00:00
|
|
|
from pytorch_lightning import Callback, Trainer
|
2021-05-04 10:50:56 +00:00
|
|
|
from pytorch_lightning.trainer.states import RunningStage, TrainerFn, TrainerState, TrainerStatus
|
2021-03-06 12:40:19 +00:00
|
|
|
from tests.helpers import BoringModel
|
2020-08-09 10:24:09 +00:00
|
|
|
|
|
|
|
|
2021-05-04 10:50:56 +00:00
|
|
|
def test_initialize_state():
|
2021-07-26 11:37:35 +00:00
|
|
|
"""Tests that state is INITIALIZING after Trainer creation"""
|
2021-05-04 10:50:56 +00:00
|
|
|
trainer = Trainer()
|
|
|
|
assert trainer.state == TrainerState(status=TrainerStatus.INITIALIZING, fn=None, stage=None)
|
2020-08-09 10:24:09 +00:00
|
|
|
|
|
|
|
|
2021-02-06 15:06:17 +00:00
|
|
|
@pytest.mark.parametrize(
|
2021-07-26 11:37:35 +00:00
|
|
|
"extra_params",
|
|
|
|
[pytest.param(dict(fast_dev_run=True), id="Fast-Run"), pytest.param(dict(max_steps=1), id="Single-Step")],
|
2021-02-06 15:06:17 +00:00
|
|
|
)
|
2021-05-04 10:50:56 +00:00
|
|
|
def test_trainer_fn_while_running(tmpdir, extra_params):
|
2021-03-06 12:40:19 +00:00
|
|
|
trainer = Trainer(default_root_dir=tmpdir, **extra_params, auto_lr_find=True)
|
2020-08-09 10:24:09 +00:00
|
|
|
|
2021-03-06 12:40:19 +00:00
|
|
|
class TestModel(BoringModel):
|
2021-05-04 10:50:56 +00:00
|
|
|
def __init__(self, expected_fn, expected_stage):
|
2021-03-06 12:40:19 +00:00
|
|
|
super().__init__()
|
2021-06-11 11:47:00 +00:00
|
|
|
self.expected_fn = expected_fn
|
2021-05-04 10:50:56 +00:00
|
|
|
self.expected_stage = expected_stage
|
2021-03-06 12:40:19 +00:00
|
|
|
self.lr = 0.1
|
2020-08-09 10:24:09 +00:00
|
|
|
|
2021-03-06 12:40:19 +00:00
|
|
|
def on_train_batch_start(self, *_):
|
2021-06-11 11:47:00 +00:00
|
|
|
assert self.trainer.state.status == TrainerStatus.RUNNING
|
|
|
|
assert self.trainer.state.fn == self.expected_fn
|
2021-03-06 12:40:19 +00:00
|
|
|
assert self.trainer.training
|
2020-08-09 10:24:09 +00:00
|
|
|
|
2021-03-06 12:40:19 +00:00
|
|
|
def on_sanity_check_start(self, *_):
|
2021-06-11 11:47:00 +00:00
|
|
|
assert self.trainer.state.status == TrainerStatus.RUNNING
|
|
|
|
assert self.trainer.state.fn == self.expected_fn
|
2021-03-06 12:40:19 +00:00
|
|
|
assert self.trainer.sanity_checking
|
2020-08-09 10:24:09 +00:00
|
|
|
|
2021-03-06 12:40:19 +00:00
|
|
|
def on_validation_batch_start(self, *_):
|
2021-06-11 11:47:00 +00:00
|
|
|
assert self.trainer.state.status == TrainerStatus.RUNNING
|
|
|
|
assert self.trainer.state.fn == self.expected_fn
|
2021-03-06 12:40:19 +00:00
|
|
|
assert self.trainer.validating or self.trainer.sanity_checking
|
2020-08-09 10:24:09 +00:00
|
|
|
|
2021-03-06 12:40:19 +00:00
|
|
|
def on_test_batch_start(self, *_):
|
2021-06-11 11:47:00 +00:00
|
|
|
assert self.trainer.state.status == TrainerStatus.RUNNING
|
|
|
|
assert self.trainer.state.fn == self.expected_fn
|
2021-03-06 12:40:19 +00:00
|
|
|
assert self.trainer.testing
|
2020-08-09 10:24:09 +00:00
|
|
|
|
2021-05-04 10:50:56 +00:00
|
|
|
model = TestModel(TrainerFn.TUNING, RunningStage.TRAINING)
|
2021-03-06 12:40:19 +00:00
|
|
|
trainer.tune(model)
|
2021-05-04 10:50:56 +00:00
|
|
|
assert trainer.state.finished
|
2020-08-09 10:24:09 +00:00
|
|
|
|
2021-05-04 10:50:56 +00:00
|
|
|
model = TestModel(TrainerFn.FITTING, RunningStage.TRAINING)
|
2020-08-09 10:24:09 +00:00
|
|
|
trainer.fit(model)
|
2021-05-04 10:50:56 +00:00
|
|
|
assert trainer.state.finished
|
2020-08-09 10:24:09 +00:00
|
|
|
|
2021-05-04 10:50:56 +00:00
|
|
|
model = TestModel(TrainerFn.VALIDATING, RunningStage.VALIDATING)
|
|
|
|
trainer.validate(model)
|
|
|
|
assert trainer.state.finished
|
|
|
|
|
|
|
|
model = TestModel(TrainerFn.TESTING, RunningStage.TESTING)
|
2020-08-09 10:24:09 +00:00
|
|
|
trainer.test(model)
|
2021-05-04 10:50:56 +00:00
|
|
|
assert trainer.state.finished
|
2020-08-09 10:24:09 +00:00
|
|
|
|
|
|
|
|
2021-02-06 15:06:17 +00:00
|
|
|
@pytest.mark.parametrize(
|
2021-07-26 11:37:35 +00:00
|
|
|
"extra_params",
|
|
|
|
[pytest.param(dict(fast_dev_run=True), id="Fast-Run"), pytest.param(dict(max_steps=1), id="Single-Step")],
|
2021-02-06 15:06:17 +00:00
|
|
|
)
|
2020-08-09 10:24:09 +00:00
|
|
|
def test_interrupt_state_on_keyboard_interrupt(tmpdir, extra_params):
|
2021-07-26 11:37:35 +00:00
|
|
|
"""Tests that state is set to INTERRUPTED on KeyboardInterrupt"""
|
2021-03-06 12:40:19 +00:00
|
|
|
model = BoringModel()
|
2020-08-09 10:24:09 +00:00
|
|
|
|
|
|
|
class InterruptCallback(Callback):
|
|
|
|
def on_batch_start(self, trainer, pl_module):
|
|
|
|
raise KeyboardInterrupt
|
|
|
|
|
2021-02-06 15:06:17 +00:00
|
|
|
trainer = Trainer(callbacks=[InterruptCallback()], default_root_dir=tmpdir, **extra_params)
|
2020-08-09 10:24:09 +00:00
|
|
|
|
|
|
|
trainer.fit(model)
|
2021-05-04 10:50:56 +00:00
|
|
|
assert trainer.interrupted
|