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-03-03 16:39:43 +00:00
|
|
|
r"""
|
2020-11-13 15:05:54 +00:00
|
|
|
Abstract base class used to build new callbacks.
|
2020-04-05 09:38:52 +00:00
|
|
|
|
2020-02-23 02:45:34 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
import abc
|
2021-02-11 14:33:10 +00:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from pytorch_lightning.core.lightning import LightningModule
|
2020-02-23 02:45:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Callback(abc.ABC):
|
2020-03-03 16:39:43 +00:00
|
|
|
r"""
|
|
|
|
Abstract base class used to build new callbacks.
|
2020-10-06 21:28:45 +00:00
|
|
|
|
|
|
|
Subclass this class and override any of the relevant hooks
|
2020-03-03 16:39:43 +00:00
|
|
|
"""
|
2020-02-23 02:45:34 +00:00
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_before_accelerator_backend_setup(self, trainer, pl_module: LightningModule) -> None:
|
2021-01-08 21:33:05 +00:00
|
|
|
"""Called before accelerator is being setup"""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def setup(self, trainer, pl_module: LightningModule, stage: str) -> None:
|
2020-06-17 23:49:58 +00:00
|
|
|
"""Called when fit or test begins"""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def teardown(self, trainer, pl_module: LightningModule, stage: str) -> None:
|
2020-06-17 23:49:58 +00:00
|
|
|
"""Called when fit or test ends"""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_init_start(self, trainer) -> None:
|
2020-03-03 04:51:32 +00:00
|
|
|
"""Called when the trainer initialization begins, model has not yet been set."""
|
2020-02-27 22:21:51 +00:00
|
|
|
pass
|
2020-02-26 04:17:27 +00:00
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_init_end(self, trainer) -> None:
|
2020-03-03 04:51:32 +00:00
|
|
|
"""Called when the trainer initialization ends, model has not yet been set."""
|
2020-02-26 04:17:27 +00:00
|
|
|
pass
|
2020-02-23 02:45:34 +00:00
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_fit_start(self, trainer, pl_module: LightningModule) -> None:
|
2020-06-17 11:37:16 +00:00
|
|
|
"""Called when fit begins"""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_fit_end(self, trainer, pl_module: LightningModule) -> None:
|
2020-06-17 11:37:16 +00:00
|
|
|
"""Called when fit ends"""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_sanity_check_start(self, trainer, pl_module: LightningModule) -> None:
|
2020-04-24 00:46:18 +00:00
|
|
|
"""Called when the validation sanity check starts."""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_sanity_check_end(self, trainer, pl_module: LightningModule) -> None:
|
2020-04-24 00:46:18 +00:00
|
|
|
"""Called when the validation sanity check ends."""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_train_batch_start(
|
|
|
|
self, trainer, pl_module: LightningModule, batch: Any, batch_idx: int, dataloader_idx: int
|
|
|
|
) -> None:
|
2020-08-08 11:58:18 +00:00
|
|
|
"""Called when the train batch begins."""
|
2020-08-06 00:01:30 +00:00
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_train_batch_end(
|
|
|
|
self, trainer, pl_module: LightningModule, outputs: Any, batch: Any, batch_idx: int, dataloader_idx: int
|
|
|
|
) -> None:
|
2020-08-08 11:58:18 +00:00
|
|
|
"""Called when the train batch ends."""
|
2020-08-06 00:01:30 +00:00
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_train_epoch_start(self, trainer, pl_module: LightningModule) -> None:
|
2020-07-20 23:00:20 +00:00
|
|
|
"""Called when the train epoch begins."""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_train_epoch_end(self, trainer, pl_module: LightningModule, outputs: Any) -> None:
|
2020-07-20 23:00:20 +00:00
|
|
|
"""Called when the train epoch ends."""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_validation_epoch_start(self, trainer, pl_module: LightningModule) -> None:
|
2020-07-20 23:00:20 +00:00
|
|
|
"""Called when the val epoch begins."""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_validation_epoch_end(self, trainer, pl_module: LightningModule) -> None:
|
2020-07-20 23:00:20 +00:00
|
|
|
"""Called when the val epoch ends."""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_test_epoch_start(self, trainer, pl_module: LightningModule) -> None:
|
2020-07-20 23:00:20 +00:00
|
|
|
"""Called when the test epoch begins."""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_test_epoch_end(self, trainer, pl_module: LightningModule) -> None:
|
2020-07-20 23:00:20 +00:00
|
|
|
"""Called when the test epoch ends."""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_epoch_start(self, trainer, pl_module: LightningModule) -> None:
|
2020-02-23 02:45:34 +00:00
|
|
|
"""Called when the epoch begins."""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_epoch_end(self, trainer, pl_module: LightningModule) -> None:
|
2020-02-23 02:45:34 +00:00
|
|
|
"""Called when the epoch ends."""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_batch_start(self, trainer, pl_module: LightningModule) -> None:
|
2020-02-23 02:45:34 +00:00
|
|
|
"""Called when the training batch begins."""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_validation_batch_start(
|
|
|
|
self, trainer, pl_module: LightningModule, batch: Any, batch_idx: int, dataloader_idx: int
|
|
|
|
) -> None:
|
2020-04-24 00:46:18 +00:00
|
|
|
"""Called when the validation batch begins."""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_validation_batch_end(
|
|
|
|
self, trainer, pl_module: LightningModule, outputs: Any, batch: Any, batch_idx: int, dataloader_idx: int
|
|
|
|
) -> None:
|
2020-04-24 00:46:18 +00:00
|
|
|
"""Called when the validation batch ends."""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_test_batch_start(
|
|
|
|
self, trainer, pl_module: LightningModule, batch: Any, batch_idx: int, dataloader_idx: int
|
|
|
|
) -> None:
|
2020-04-24 00:46:18 +00:00
|
|
|
"""Called when the test batch begins."""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_test_batch_end(
|
|
|
|
self, trainer, pl_module: LightningModule, outputs: Any, batch: Any, batch_idx: int, dataloader_idx: int
|
|
|
|
) -> None:
|
2020-04-24 00:46:18 +00:00
|
|
|
"""Called when the test batch ends."""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_batch_end(self, trainer, pl_module: LightningModule) -> None:
|
2020-02-23 02:45:34 +00:00
|
|
|
"""Called when the training batch ends."""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_train_start(self, trainer, pl_module: LightningModule) -> None:
|
2020-02-23 02:45:34 +00:00
|
|
|
"""Called when the train begins."""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_train_end(self, trainer, pl_module: LightningModule) -> None:
|
2020-02-23 02:45:34 +00:00
|
|
|
"""Called when the train ends."""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_pretrain_routine_start(self, trainer, pl_module: LightningModule) -> None:
|
2020-08-07 13:29:57 +00:00
|
|
|
"""Called when the pretrain routine begins."""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_pretrain_routine_end(self, trainer, pl_module: LightningModule) -> None:
|
2020-08-07 13:29:57 +00:00
|
|
|
"""Called when the pretrain routine ends."""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_validation_start(self, trainer, pl_module: LightningModule) -> None:
|
2020-08-26 04:45:43 +00:00
|
|
|
"""Called when the validation loop begins."""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_validation_end(self, trainer, pl_module: LightningModule) -> None:
|
2020-02-23 02:45:34 +00:00
|
|
|
"""Called when the validation loop ends."""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_test_start(self, trainer, pl_module: LightningModule) -> None:
|
2020-08-26 04:45:43 +00:00
|
|
|
"""Called when the test begins."""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_test_end(self, trainer, pl_module: LightningModule) -> None:
|
2020-02-23 02:45:34 +00:00
|
|
|
"""Called when the test ends."""
|
|
|
|
pass
|
2020-06-15 10:35:26 +00:00
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_keyboard_interrupt(self, trainer, pl_module: LightningModule) -> None:
|
|
|
|
"""Called when the training is interrupted by ``KeyboardInterrupt``."""
|
2020-08-28 14:50:52 +00:00
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_save_checkpoint(self, trainer, pl_module: LightningModule) -> None:
|
2020-08-28 14:50:52 +00:00
|
|
|
"""Called when saving a model checkpoint, use to persist state."""
|
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_load_checkpoint(self, checkpointed_state) -> None:
|
2020-08-28 14:50:52 +00:00
|
|
|
"""Called when loading a model checkpoint, use to reload state."""
|
|
|
|
pass
|
2020-10-28 12:15:22 +00:00
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_after_backward(self, trainer, pl_module: LightningModule) -> None:
|
|
|
|
"""Called after ``loss.backward()`` and before optimizers do anything."""
|
2020-10-28 12:15:22 +00:00
|
|
|
pass
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_before_zero_grad(self, trainer, pl_module: LightningModule, optimizer) -> None:
|
|
|
|
"""Called after ``optimizer.step()`` and before ``optimizer.zero_grad()``."""
|
2020-10-28 12:15:22 +00:00
|
|
|
pass
|