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"""
|
2022-01-24 08:19:57 +00:00
|
|
|
Base class used to build new callbacks.
|
2020-04-05 09:38:52 +00:00
|
|
|
|
2020-02-23 02:45:34 +00:00
|
|
|
"""
|
|
|
|
|
2021-07-28 22:12:32 +00:00
|
|
|
from typing import Any, Dict, List, Optional, Type
|
2021-02-11 14:33:10 +00:00
|
|
|
|
2021-07-09 06:15:57 +00:00
|
|
|
import torch
|
2021-04-22 14:05:28 +00:00
|
|
|
from torch.optim import Optimizer
|
|
|
|
|
|
|
|
import pytorch_lightning as pl
|
2021-05-05 19:50:58 +00:00
|
|
|
from pytorch_lightning.utilities.types import STEP_OUTPUT
|
2020-02-23 02:45:34 +00:00
|
|
|
|
|
|
|
|
2022-01-24 08:19:57 +00:00
|
|
|
class Callback:
|
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-07-28 22:12:32 +00:00
|
|
|
@property
|
2021-08-24 17:35:19 +00:00
|
|
|
def state_key(self) -> str:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Identifier for the state of the callback.
|
|
|
|
|
|
|
|
Used to store and retrieve a callback's state from the checkpoint dictionary by
|
|
|
|
``checkpoint["callbacks"][state_key]``. Implementations of a callback need to provide a unique state key if 1)
|
|
|
|
the callback has state and 2) it is desired to maintain the state of multiple instances of that callback.
|
2021-07-28 22:12:32 +00:00
|
|
|
"""
|
|
|
|
return self.__class__.__qualname__
|
|
|
|
|
|
|
|
@property
|
2021-08-24 17:35:19 +00:00
|
|
|
def _legacy_state_key(self) -> Type["Callback"]:
|
|
|
|
"""State key for checkpoints saved prior to version 1.5.0."""
|
2021-07-28 22:12:32 +00:00
|
|
|
return type(self)
|
|
|
|
|
2021-08-24 17:35:19 +00:00
|
|
|
def _generate_state_key(self, **kwargs: Any) -> str:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Formats a set of key-value pairs into a state key string with the callback class name prefixed. Useful
|
|
|
|
for defining a :attr:`state_key`.
|
2021-08-24 17:35:19 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
**kwargs: A set of key-value pairs. Must be serializable to :class:`str`.
|
|
|
|
"""
|
|
|
|
return f"{self.__class__.__qualname__}{repr(kwargs)}"
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_configure_sharded_model(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2022-02-03 02:29:26 +00:00
|
|
|
r"""
|
|
|
|
.. deprecated:: v1.6
|
|
|
|
This callback hook was deprecated in v1.6 and will be removed in v1.8. Use `setup()` instead.
|
|
|
|
|
|
|
|
Called before configure sharded model.
|
|
|
|
"""
|
2021-03-29 20:50:51 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_before_accelerator_backend_setup(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2022-02-07 11:07:21 +00:00
|
|
|
r"""
|
|
|
|
.. deprecated:: v1.6
|
|
|
|
This callback hook was deprecated in v1.6 and will be removed in v1.8. Use ``setup()`` instead.
|
|
|
|
|
|
|
|
Called before accelerator is being setup.
|
|
|
|
"""
|
2021-01-08 21:33:05 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def setup(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", stage: Optional[str] = None) -> None:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Called when fit, validate, test, predict, or tune begins."""
|
2020-06-17 23:49:58 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def teardown(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", stage: Optional[str] = None) -> None:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Called when fit, validate, test, predict, or tune ends."""
|
2020-06-17 23:49:58 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_init_start(self, trainer: "pl.Trainer") -> None:
|
2021-12-08 07:42:19 +00:00
|
|
|
r"""
|
|
|
|
.. deprecated:: v1.6
|
|
|
|
This callback hook was deprecated in v1.6 and will be removed in v1.8.
|
|
|
|
|
|
|
|
Called when the trainer initialization begins, model has not yet been set.
|
|
|
|
"""
|
2020-02-26 04:17:27 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_init_end(self, trainer: "pl.Trainer") -> None:
|
2021-12-08 07:42:19 +00:00
|
|
|
r"""
|
|
|
|
.. deprecated:: v1.6
|
|
|
|
This callback hook was deprecated in v1.6 and will be removed in v1.8.
|
|
|
|
|
|
|
|
Called when the trainer initialization ends, model has not yet been set.
|
|
|
|
"""
|
2020-02-23 02:45:34 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_fit_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Called when fit begins."""
|
2020-06-17 11:37:16 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_fit_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Called when fit ends."""
|
2020-06-17 11:37:16 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_sanity_check_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2020-04-24 00:46:18 +00:00
|
|
|
"""Called when the validation sanity check starts."""
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_sanity_check_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2020-04-24 00:46:18 +00:00
|
|
|
"""Called when the validation sanity check ends."""
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_train_batch_start(
|
2021-10-07 10:18:11 +00:00
|
|
|
self,
|
|
|
|
trainer: "pl.Trainer",
|
|
|
|
pl_module: "pl.LightningModule",
|
|
|
|
batch: Any,
|
|
|
|
batch_idx: int,
|
2021-11-04 00:39:55 +00:00
|
|
|
unused: int = 0,
|
2021-02-11 14:33:10 +00:00
|
|
|
) -> None:
|
2020-08-08 11:58:18 +00:00
|
|
|
"""Called when the train batch begins."""
|
2020-08-06 00:01:30 +00:00
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_train_batch_end(
|
2021-04-22 14:05:28 +00:00
|
|
|
self,
|
2021-07-26 11:37:35 +00:00
|
|
|
trainer: "pl.Trainer",
|
|
|
|
pl_module: "pl.LightningModule",
|
2021-04-22 14:05:28 +00:00
|
|
|
outputs: STEP_OUTPUT,
|
|
|
|
batch: Any,
|
|
|
|
batch_idx: int,
|
2021-11-04 00:39:55 +00:00
|
|
|
unused: int = 0,
|
2021-02-11 14:33:10 +00:00
|
|
|
) -> None:
|
2020-08-08 11:58:18 +00:00
|
|
|
"""Called when the train batch ends."""
|
2020-08-06 00:01:30 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_train_epoch_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2020-07-20 23:00:20 +00:00
|
|
|
"""Called when the train epoch begins."""
|
|
|
|
|
2021-07-28 16:27:54 +00:00
|
|
|
def on_train_epoch_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2021-05-05 22:18:45 +00:00
|
|
|
"""Called when the train epoch ends.
|
|
|
|
|
|
|
|
To access all batch outputs at the end of the epoch, either:
|
|
|
|
|
|
|
|
1. Implement `training_epoch_end` in the `LightningModule` and access outputs via the module OR
|
|
|
|
2. Cache data across train batch hooks inside the callback implementation to post-process in this hook.
|
|
|
|
"""
|
2020-07-20 23:00:20 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_validation_epoch_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2020-07-20 23:00:20 +00:00
|
|
|
"""Called when the val epoch begins."""
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_validation_epoch_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2020-07-20 23:00:20 +00:00
|
|
|
"""Called when the val epoch ends."""
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_test_epoch_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2020-07-20 23:00:20 +00:00
|
|
|
"""Called when the test epoch begins."""
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_test_epoch_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2020-07-20 23:00:20 +00:00
|
|
|
"""Called when the test epoch ends."""
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_predict_epoch_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2021-04-22 14:05:28 +00:00
|
|
|
"""Called when the predict epoch begins."""
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_predict_epoch_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", outputs: List[Any]) -> None:
|
2021-04-22 14:05:28 +00:00
|
|
|
"""Called when the predict epoch ends."""
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_epoch_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2022-02-07 14:15:27 +00:00
|
|
|
r"""
|
|
|
|
.. deprecated:: v1.6
|
|
|
|
This callback hook was deprecated in v1.6 and will be removed in v1.8. Use
|
|
|
|
``on_<train/validation/test>_epoch_start`` instead.
|
|
|
|
|
|
|
|
Called when either of train/val/test epoch begins.
|
|
|
|
"""
|
2020-02-23 02:45:34 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_epoch_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2022-02-07 14:15:27 +00:00
|
|
|
r"""
|
|
|
|
.. deprecated:: v1.6
|
|
|
|
This callback hook was deprecated in v1.6 and will be removed in v1.8. Use
|
|
|
|
``on_<train/validation/test>_epoch_end`` instead.
|
|
|
|
|
|
|
|
Called when either of train/val/test epoch ends.
|
|
|
|
"""
|
2020-02-23 02:45:34 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_batch_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2022-02-03 19:51:56 +00:00
|
|
|
r"""
|
|
|
|
.. deprecated:: v1.6
|
|
|
|
This callback hook was deprecated in v1.6 and will be removed in v1.8. Use
|
|
|
|
``on_train_batch_start`` instead.
|
|
|
|
|
|
|
|
Called when the training batch begins.
|
|
|
|
"""
|
2020-02-23 02:45:34 +00:00
|
|
|
|
2021-11-29 20:38:23 +00:00
|
|
|
def on_batch_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2022-02-03 19:51:56 +00:00
|
|
|
r"""
|
|
|
|
.. deprecated:: v1.6
|
|
|
|
This callback hook was deprecated in v1.6 and will be removed in v1.8. Use
|
|
|
|
``on_train_batch_end`` instead.
|
|
|
|
|
|
|
|
Called when the training batch ends.
|
|
|
|
"""
|
2021-11-29 20:38:23 +00:00
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_validation_batch_start(
|
2021-07-26 11:37:35 +00:00
|
|
|
self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", batch: Any, batch_idx: int, dataloader_idx: int
|
2021-02-11 14:33:10 +00:00
|
|
|
) -> None:
|
2020-04-24 00:46:18 +00:00
|
|
|
"""Called when the validation batch begins."""
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_validation_batch_end(
|
2021-04-22 14:05:28 +00:00
|
|
|
self,
|
2021-07-26 11:37:35 +00:00
|
|
|
trainer: "pl.Trainer",
|
|
|
|
pl_module: "pl.LightningModule",
|
2021-04-22 14:05:28 +00:00
|
|
|
outputs: Optional[STEP_OUTPUT],
|
|
|
|
batch: Any,
|
|
|
|
batch_idx: int,
|
|
|
|
dataloader_idx: int,
|
2021-02-11 14:33:10 +00:00
|
|
|
) -> None:
|
2020-04-24 00:46:18 +00:00
|
|
|
"""Called when the validation batch ends."""
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_test_batch_start(
|
2021-07-26 11:37:35 +00:00
|
|
|
self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", batch: Any, batch_idx: int, dataloader_idx: int
|
2021-02-11 14:33:10 +00:00
|
|
|
) -> None:
|
2020-04-24 00:46:18 +00:00
|
|
|
"""Called when the test batch begins."""
|
|
|
|
|
2021-02-11 14:33:10 +00:00
|
|
|
def on_test_batch_end(
|
2021-04-22 14:05:28 +00:00
|
|
|
self,
|
2021-07-26 11:37:35 +00:00
|
|
|
trainer: "pl.Trainer",
|
|
|
|
pl_module: "pl.LightningModule",
|
2021-04-22 14:05:28 +00:00
|
|
|
outputs: Optional[STEP_OUTPUT],
|
|
|
|
batch: Any,
|
|
|
|
batch_idx: int,
|
|
|
|
dataloader_idx: int,
|
2021-02-11 14:33:10 +00:00
|
|
|
) -> None:
|
2020-04-24 00:46:18 +00:00
|
|
|
"""Called when the test batch ends."""
|
|
|
|
|
2021-04-22 14:05:28 +00:00
|
|
|
def on_predict_batch_start(
|
2021-07-26 11:37:35 +00:00
|
|
|
self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", batch: Any, batch_idx: int, dataloader_idx: int
|
2021-04-22 14:05:28 +00:00
|
|
|
) -> None:
|
|
|
|
"""Called when the predict batch begins."""
|
|
|
|
|
|
|
|
def on_predict_batch_end(
|
|
|
|
self,
|
2021-07-26 11:37:35 +00:00
|
|
|
trainer: "pl.Trainer",
|
|
|
|
pl_module: "pl.LightningModule",
|
2021-04-22 14:05:28 +00:00
|
|
|
outputs: Any,
|
|
|
|
batch: Any,
|
|
|
|
batch_idx: int,
|
|
|
|
dataloader_idx: int,
|
|
|
|
) -> None:
|
|
|
|
"""Called when the predict batch ends."""
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_train_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2020-02-23 02:45:34 +00:00
|
|
|
"""Called when the train begins."""
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_train_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2020-02-23 02:45:34 +00:00
|
|
|
"""Called when the train ends."""
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_pretrain_routine_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2022-02-24 19:32:48 +00:00
|
|
|
r"""
|
|
|
|
.. deprecated:: v1.6
|
|
|
|
|
|
|
|
This callback hook was deprecated in v1.6 and will be removed in v1.8. Use ``on_fit_start`` instead.
|
|
|
|
|
|
|
|
Called when the pretrain routine begins.
|
|
|
|
"""
|
2020-08-07 13:29:57 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_pretrain_routine_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2022-02-24 19:32:48 +00:00
|
|
|
r"""
|
|
|
|
.. deprecated:: v1.6
|
|
|
|
|
|
|
|
This callback hook was deprecated in v1.6 and will be removed in v1.8. Use ``on_fit_start`` instead.
|
|
|
|
|
|
|
|
Called when the pretrain routine ends.
|
|
|
|
"""
|
2020-08-07 13:29:57 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_validation_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2020-08-26 04:45:43 +00:00
|
|
|
"""Called when the validation loop begins."""
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_validation_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2020-02-23 02:45:34 +00:00
|
|
|
"""Called when the validation loop ends."""
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_test_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2020-08-26 04:45:43 +00:00
|
|
|
"""Called when the test begins."""
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_test_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2020-02-23 02:45:34 +00:00
|
|
|
"""Called when the test ends."""
|
2020-06-15 10:35:26 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_predict_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2021-04-22 14:05:28 +00:00
|
|
|
"""Called when the predict begins."""
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_predict_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2021-04-22 14:05:28 +00:00
|
|
|
"""Called when predict ends."""
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_keyboard_interrupt(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2021-09-06 07:57:00 +00:00
|
|
|
r"""
|
|
|
|
.. deprecated:: v1.5
|
|
|
|
This callback hook was deprecated in v1.5 in favor of `on_exception` and will be removed in v1.7.
|
|
|
|
|
|
|
|
Called when any trainer execution is interrupted by KeyboardInterrupt.
|
|
|
|
"""
|
2020-08-28 14:50:52 +00:00
|
|
|
|
2021-09-01 08:49:00 +00:00
|
|
|
def on_exception(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", exception: BaseException) -> None:
|
|
|
|
"""Called when any trainer execution is interrupted by an exception."""
|
|
|
|
|
2022-03-04 02:41:48 +00:00
|
|
|
def state_dict(self) -> Dict[str, Any]:
|
|
|
|
"""Called when saving a checkpoint, implement to generate callback's ``state_dict``.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A dictionary containing callback state.
|
|
|
|
"""
|
|
|
|
return {}
|
|
|
|
|
|
|
|
def load_state_dict(self, state_dict: Dict[str, Any]) -> None:
|
|
|
|
"""Called when loading a checkpoint, implement to reload callback state given callback's ``state_dict``.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
state_dict: the callback state returned by ``state_dict``.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2021-04-22 14:05:28 +00:00
|
|
|
def on_save_checkpoint(
|
2021-07-26 11:37:35 +00:00
|
|
|
self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", checkpoint: Dict[str, Any]
|
2021-04-22 14:05:28 +00:00
|
|
|
) -> dict:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Called when saving a model checkpoint, use to persist state.
|
2021-02-25 15:48:19 +00:00
|
|
|
|
|
|
|
Args:
|
2021-04-30 15:14:43 +00:00
|
|
|
trainer: the current :class:`~pytorch_lightning.trainer.Trainer` instance.
|
|
|
|
pl_module: the current :class:`~pytorch_lightning.core.lightning.LightningModule` instance.
|
2021-02-25 15:48:19 +00:00
|
|
|
checkpoint: the checkpoint dictionary that will be saved.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The callback state.
|
|
|
|
"""
|
2020-08-28 14:50:52 +00:00
|
|
|
|
2021-04-30 15:14:43 +00:00
|
|
|
def on_load_checkpoint(
|
2021-07-26 11:37:35 +00:00
|
|
|
self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", callback_state: Dict[str, Any]
|
2021-04-30 15:14:43 +00:00
|
|
|
) -> None:
|
2021-02-25 15:48:19 +00:00
|
|
|
"""Called when loading a model checkpoint, use to reload state.
|
|
|
|
|
|
|
|
Args:
|
2021-04-30 15:14:43 +00:00
|
|
|
trainer: the current :class:`~pytorch_lightning.trainer.Trainer` instance.
|
|
|
|
pl_module: the current :class:`~pytorch_lightning.core.lightning.LightningModule` instance.
|
2021-02-25 15:48:19 +00:00
|
|
|
callback_state: the callback state returned by ``on_save_checkpoint``.
|
2021-04-30 15:14:43 +00:00
|
|
|
|
|
|
|
Note:
|
|
|
|
The ``on_load_checkpoint`` won't be called with an undefined state.
|
|
|
|
If your ``on_load_checkpoint`` hook behavior doesn't rely on a state,
|
|
|
|
you will still need to override ``on_save_checkpoint`` to return a ``dummy state``.
|
2021-02-25 15:48:19 +00:00
|
|
|
"""
|
2020-10-28 12:15:22 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_before_backward(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", loss: torch.Tensor) -> None:
|
2021-07-09 06:15:57 +00:00
|
|
|
"""Called before ``loss.backward()``."""
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_after_backward(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
|
2021-07-09 11:30:52 +00:00
|
|
|
"""Called after ``loss.backward()`` and before optimizers are stepped."""
|
|
|
|
|
|
|
|
def on_before_optimizer_step(
|
2021-07-26 11:37:35 +00:00
|
|
|
self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", optimizer: Optimizer, opt_idx: int
|
2021-07-09 11:30:52 +00:00
|
|
|
) -> None:
|
|
|
|
"""Called before ``optimizer.step()``."""
|
2020-10-28 12:15:22 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def on_before_zero_grad(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", optimizer: Optimizer) -> None:
|
2021-10-12 10:07:08 +00:00
|
|
|
"""Called before ``optimizer.zero_grad()``."""
|