2020-08-20 02:03:22 +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-02-26 04:17:27 +00:00
|
|
|
from abc import ABC
|
2020-08-28 14:50:52 +00:00
|
|
|
from copy import deepcopy
|
2020-04-24 00:46:18 +00:00
|
|
|
from typing import Callable, List
|
2020-02-26 04:17:27 +00:00
|
|
|
|
|
|
|
from pytorch_lightning.callbacks import Callback
|
|
|
|
|
|
|
|
|
|
|
|
class TrainerCallbackHookMixin(ABC):
|
|
|
|
|
2020-05-17 13:14:54 +00:00
|
|
|
# this is just a summary on variables used in this abstract class,
|
|
|
|
# the proper values/initialisation should be done in child class
|
|
|
|
callbacks: List[Callback] = []
|
2020-08-06 00:01:30 +00:00
|
|
|
get_model: Callable
|
2020-02-26 04:17:27 +00:00
|
|
|
|
2020-06-18 11:21:44 +00:00
|
|
|
def setup(self, stage: str):
|
2020-06-17 23:49:58 +00:00
|
|
|
"""Called in the beginning of fit and test"""
|
|
|
|
for callback in self.callbacks:
|
2020-08-07 13:29:57 +00:00
|
|
|
callback.setup(self, self.get_model(), stage)
|
2020-06-17 23:49:58 +00:00
|
|
|
|
2020-06-18 11:21:44 +00:00
|
|
|
def teardown(self, stage: str):
|
2020-06-17 23:49:58 +00:00
|
|
|
"""Called at the end of fit and test"""
|
|
|
|
for callback in self.callbacks:
|
2020-08-07 13:29:57 +00:00
|
|
|
callback.teardown(self, self.get_model(), stage)
|
2020-06-17 23:49:58 +00:00
|
|
|
|
2020-03-03 04:51:32 +00:00
|
|
|
def on_init_start(self):
|
|
|
|
"""Called when the trainer initialization begins, model has not yet been set."""
|
2020-02-26 04:17:27 +00:00
|
|
|
for callback in self.callbacks:
|
2020-03-03 04:51:32 +00:00
|
|
|
callback.on_init_start(self)
|
2020-02-26 04:17:27 +00:00
|
|
|
|
2020-03-03 04:51:32 +00:00
|
|
|
def on_init_end(self):
|
|
|
|
"""Called when the trainer initialization ends, model has not yet been set."""
|
2020-02-26 04:17:27 +00:00
|
|
|
for callback in self.callbacks:
|
2020-03-03 04:51:32 +00:00
|
|
|
callback.on_init_end(self)
|
2020-02-26 04:17:27 +00:00
|
|
|
|
2020-09-23 08:38:33 +00:00
|
|
|
def on_fit_start(self):
|
2020-06-17 11:37:16 +00:00
|
|
|
"""Called when the trainer initialization begins, model has not yet been set."""
|
|
|
|
for callback in self.callbacks:
|
2020-09-23 08:38:33 +00:00
|
|
|
callback.on_fit_start(self, self.get_model())
|
2020-06-17 11:37:16 +00:00
|
|
|
|
|
|
|
def on_fit_end(self):
|
|
|
|
"""Called when the trainer initialization begins, model has not yet been set."""
|
|
|
|
for callback in self.callbacks:
|
2020-08-07 13:29:57 +00:00
|
|
|
callback.on_fit_end(self, self.get_model())
|
2020-06-17 11:37:16 +00:00
|
|
|
|
2020-04-24 00:46:18 +00:00
|
|
|
def on_sanity_check_start(self):
|
|
|
|
"""Called when the validation sanity check starts."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_sanity_check_start(self, self.get_model())
|
|
|
|
|
|
|
|
def on_sanity_check_end(self):
|
|
|
|
"""Called when the validation sanity check ends."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_sanity_check_end(self, self.get_model())
|
|
|
|
|
2020-07-20 23:00:20 +00:00
|
|
|
def on_train_epoch_start(self):
|
|
|
|
"""Called when the epoch begins."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_train_epoch_start(self, self.get_model())
|
|
|
|
|
|
|
|
def on_train_epoch_end(self):
|
|
|
|
"""Called when the epoch ends."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_train_epoch_end(self, self.get_model())
|
|
|
|
|
|
|
|
def on_validation_epoch_start(self):
|
|
|
|
"""Called when the epoch begins."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_validation_epoch_start(self, self.get_model())
|
|
|
|
|
|
|
|
def on_validation_epoch_end(self):
|
|
|
|
"""Called when the epoch ends."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_validation_epoch_end(self, self.get_model())
|
|
|
|
|
|
|
|
def on_test_epoch_start(self):
|
|
|
|
"""Called when the epoch begins."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_test_epoch_start(self, self.get_model())
|
|
|
|
|
|
|
|
def on_test_epoch_end(self):
|
|
|
|
"""Called when the epoch ends."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_test_epoch_end(self, self.get_model())
|
|
|
|
|
2020-02-26 04:17:27 +00:00
|
|
|
def on_epoch_start(self):
|
|
|
|
"""Called when the epoch begins."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_epoch_start(self, self.get_model())
|
|
|
|
|
|
|
|
def on_epoch_end(self):
|
|
|
|
"""Called when the epoch ends."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_epoch_end(self, self.get_model())
|
|
|
|
|
|
|
|
def on_train_start(self):
|
|
|
|
"""Called when the train begins."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_train_start(self, self.get_model())
|
|
|
|
|
|
|
|
def on_train_end(self):
|
|
|
|
"""Called when the train ends."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_train_end(self, self.get_model())
|
|
|
|
|
2020-08-07 13:29:57 +00:00
|
|
|
def on_pretrain_routine_start(self, model):
|
|
|
|
"""Called when the train begins."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_pretrain_routine_start(self, model)
|
|
|
|
|
|
|
|
def on_pretrain_routine_end(self, model):
|
|
|
|
"""Called when the train ends."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_pretrain_routine_end(self, model)
|
|
|
|
|
2020-02-26 04:17:27 +00:00
|
|
|
def on_batch_start(self):
|
|
|
|
"""Called when the training batch begins."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_batch_start(self, self.get_model())
|
|
|
|
|
|
|
|
def on_batch_end(self):
|
|
|
|
"""Called when the training batch ends."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_batch_end(self, self.get_model())
|
|
|
|
|
2020-08-07 13:29:57 +00:00
|
|
|
def on_train_batch_start(self, batch, batch_idx, dataloader_idx):
|
2020-08-06 00:01:30 +00:00
|
|
|
"""Called when the training batch begins."""
|
|
|
|
for callback in self.callbacks:
|
2020-08-07 13:29:57 +00:00
|
|
|
callback.on_train_batch_start(self, self.get_model(), batch, batch_idx, dataloader_idx)
|
2020-08-06 00:01:30 +00:00
|
|
|
|
2020-08-07 13:29:57 +00:00
|
|
|
def on_train_batch_end(self, batch, batch_idx, dataloader_idx):
|
2020-08-06 00:01:30 +00:00
|
|
|
"""Called when the training batch ends."""
|
|
|
|
for callback in self.callbacks:
|
2020-08-07 13:29:57 +00:00
|
|
|
callback.on_train_batch_end(self, self.get_model(), batch, batch_idx, dataloader_idx)
|
2020-08-06 00:01:30 +00:00
|
|
|
|
2020-08-07 13:29:57 +00:00
|
|
|
def on_validation_batch_start(self, batch, batch_idx, dataloader_idx):
|
2020-04-24 00:46:18 +00:00
|
|
|
"""Called when the validation batch begins."""
|
|
|
|
for callback in self.callbacks:
|
2020-08-07 13:29:57 +00:00
|
|
|
callback.on_validation_batch_start(self, self.get_model(), batch, batch_idx, dataloader_idx)
|
2020-04-24 00:46:18 +00:00
|
|
|
|
2020-08-07 13:29:57 +00:00
|
|
|
def on_validation_batch_end(self, batch, batch_idx, dataloader_idx):
|
2020-04-24 00:46:18 +00:00
|
|
|
"""Called when the validation batch ends."""
|
|
|
|
for callback in self.callbacks:
|
2020-08-07 13:29:57 +00:00
|
|
|
callback.on_validation_batch_end(self, self.get_model(), batch, batch_idx, dataloader_idx)
|
2020-04-24 00:46:18 +00:00
|
|
|
|
2020-08-07 13:29:57 +00:00
|
|
|
def on_test_batch_start(self, batch, batch_idx, dataloader_idx):
|
2020-04-24 00:46:18 +00:00
|
|
|
"""Called when the test batch begins."""
|
|
|
|
for callback in self.callbacks:
|
2020-08-07 13:29:57 +00:00
|
|
|
callback.on_test_batch_start(self, self.get_model(), batch, batch_idx, dataloader_idx)
|
2020-04-24 00:46:18 +00:00
|
|
|
|
2020-08-07 13:29:57 +00:00
|
|
|
def on_test_batch_end(self, batch, batch_idx, dataloader_idx):
|
2020-04-24 00:46:18 +00:00
|
|
|
"""Called when the test batch ends."""
|
|
|
|
for callback in self.callbacks:
|
2020-08-07 13:29:57 +00:00
|
|
|
callback.on_test_batch_end(self, self.get_model(), batch, batch_idx, dataloader_idx)
|
2020-04-24 00:46:18 +00:00
|
|
|
|
2020-08-26 04:45:43 +00:00
|
|
|
def on_validation_start(self):
|
|
|
|
"""Called when the validation loop begins."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_validation_start(self, self.get_model())
|
|
|
|
|
2020-02-26 04:17:27 +00:00
|
|
|
def on_validation_end(self):
|
|
|
|
"""Called when the validation loop ends."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_validation_end(self, self.get_model())
|
|
|
|
|
2020-08-26 04:45:43 +00:00
|
|
|
def on_test_start(self):
|
|
|
|
"""Called when the test begins."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_test_start(self, self.get_model())
|
|
|
|
|
2020-02-26 04:17:27 +00:00
|
|
|
def on_test_end(self):
|
|
|
|
"""Called when the test ends."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_test_end(self, self.get_model())
|
2020-06-15 10:35:26 +00:00
|
|
|
|
|
|
|
def on_keyboard_interrupt(self):
|
|
|
|
"""Called when the training is interrupted by KeyboardInterrupt."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_keyboard_interrupt(self, self.get_model())
|
2020-08-28 14:50:52 +00:00
|
|
|
|
|
|
|
def on_save_checkpoint(self):
|
|
|
|
"""Called when saving a model checkpoint."""
|
|
|
|
callback_states = {}
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback_class = type(callback)
|
|
|
|
state = callback.on_save_checkpoint(self, self.get_model())
|
|
|
|
if state:
|
|
|
|
callback_states[callback_class] = state
|
|
|
|
return callback_states
|
|
|
|
|
|
|
|
def on_load_checkpoint(self, checkpoint):
|
|
|
|
"""Called when loading a model checkpoint."""
|
|
|
|
callback_states = checkpoint.get('callbacks')
|
|
|
|
for callback in self.callbacks:
|
|
|
|
state = callback_states.get(type(callback))
|
|
|
|
if state:
|
|
|
|
state = deepcopy(state)
|
|
|
|
callback.on_load_checkpoint(state)
|