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
|
2021-02-25 15:48:19 +00:00
|
|
|
from inspect import signature
|
2021-03-15 19:28:18 +00:00
|
|
|
from typing import Any, Callable, Dict, List, Optional, Type
|
2020-02-26 04:17:27 +00:00
|
|
|
|
2021-07-09 06:15:57 +00:00
|
|
|
import torch
|
|
|
|
|
2021-06-25 19:16:11 +00:00
|
|
|
import pytorch_lightning as pl
|
2020-02-26 04:17:27 +00:00
|
|
|
from pytorch_lightning.callbacks import Callback
|
2021-04-29 12:39:45 +00:00
|
|
|
from pytorch_lightning.utilities import rank_zero_deprecation, rank_zero_warn
|
2021-03-16 16:15:16 +00:00
|
|
|
from pytorch_lightning.utilities.signature_utils import is_param_in_hook_signature
|
2021-04-22 14:05:28 +00:00
|
|
|
from pytorch_lightning.utilities.types import EPOCH_OUTPUT, STEP_OUTPUT
|
2021-03-16 16:15:16 +00:00
|
|
|
from pytorch_lightning.utilities.warnings import WarningCache
|
|
|
|
|
|
|
|
warning_cache = WarningCache()
|
2020-02-26 04:17:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
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] = []
|
2021-06-25 19:16:11 +00:00
|
|
|
lightning_module: 'pl.LightningModule'
|
2020-02-26 04:17:27 +00:00
|
|
|
|
2021-06-25 19:16:11 +00:00
|
|
|
def on_before_accelerator_backend_setup(self, model: 'pl.LightningModule') -> None:
|
2021-03-08 14:27:07 +00:00
|
|
|
"""Called at the beginning of fit (train + validate), validate, test, or predict, or tune."""
|
2021-01-08 21:33:05 +00:00
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_before_accelerator_backend_setup(self, model)
|
|
|
|
|
2021-06-25 19:16:11 +00:00
|
|
|
def configure_sharded_model(self, model: 'pl.LightningModule') -> None:
|
2021-03-29 20:50:51 +00:00
|
|
|
"""Called at the beginning of fit (train + validate), validate, test, or predict, or tune."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_configure_sharded_model(self, model)
|
|
|
|
|
2021-06-25 19:16:11 +00:00
|
|
|
def setup(self, model: 'pl.LightningModule', stage: Optional[str]) -> None:
|
2021-03-08 14:27:07 +00:00
|
|
|
"""Called at the beginning of fit (train + validate), validate, test, or predict, or tune."""
|
2020-06-17 23:49:58 +00:00
|
|
|
for callback in self.callbacks:
|
2021-06-16 13:09:24 +00:00
|
|
|
callback.setup(self, model, stage=stage)
|
2020-06-17 23:49:58 +00:00
|
|
|
|
2021-03-08 14:27:07 +00:00
|
|
|
def teardown(self, stage: Optional[str] = None) -> None:
|
|
|
|
"""Called at the end of fit (train + validate), validate, test, or predict, or tune."""
|
2020-06-17 23:49:58 +00:00
|
|
|
for callback in self.callbacks:
|
2021-06-16 13:09:24 +00:00
|
|
|
callback.teardown(self, self.lightning_module, stage=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:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_fit_start(self, self.lightning_module)
|
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:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_fit_end(self, self.lightning_module)
|
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:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_sanity_check_start(self, self.lightning_module)
|
2020-04-24 00:46:18 +00:00
|
|
|
|
|
|
|
def on_sanity_check_end(self):
|
|
|
|
"""Called when the validation sanity check ends."""
|
|
|
|
for callback in self.callbacks:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_sanity_check_end(self, self.lightning_module)
|
2020-04-24 00:46:18 +00:00
|
|
|
|
2020-07-20 23:00:20 +00:00
|
|
|
def on_train_epoch_start(self):
|
|
|
|
"""Called when the epoch begins."""
|
|
|
|
for callback in self.callbacks:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_train_epoch_start(self, self.lightning_module)
|
2020-07-20 23:00:20 +00:00
|
|
|
|
2021-04-22 14:05:28 +00:00
|
|
|
def on_train_epoch_end(self, outputs: EPOCH_OUTPUT):
|
2021-03-16 16:15:16 +00:00
|
|
|
"""Called when the epoch ends.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
outputs: List of outputs on each ``train`` epoch
|
|
|
|
"""
|
2020-07-20 23:00:20 +00:00
|
|
|
for callback in self.callbacks:
|
2021-05-05 15:18:16 +00:00
|
|
|
if is_param_in_hook_signature(callback.on_train_epoch_end, "outputs"):
|
2021-06-18 11:50:24 +00:00
|
|
|
warning_cache.deprecation(
|
2021-05-05 15:18:16 +00:00
|
|
|
"The signature of `Callback.on_train_epoch_end` has changed in v1.3."
|
|
|
|
" `outputs` parameter has been removed."
|
2021-06-18 11:50:24 +00:00
|
|
|
" Support for the old signature will be removed in v1.5"
|
2021-05-05 15:18:16 +00:00
|
|
|
)
|
|
|
|
callback.on_train_epoch_end(self, self.lightning_module, outputs)
|
|
|
|
else:
|
|
|
|
callback.on_train_epoch_end(self, self.lightning_module)
|
2020-07-20 23:00:20 +00:00
|
|
|
|
|
|
|
def on_validation_epoch_start(self):
|
|
|
|
"""Called when the epoch begins."""
|
|
|
|
for callback in self.callbacks:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_validation_epoch_start(self, self.lightning_module)
|
2020-07-20 23:00:20 +00:00
|
|
|
|
2021-05-05 19:50:58 +00:00
|
|
|
def on_validation_epoch_end(self):
|
|
|
|
"""Called when the validation epoch ends."""
|
2020-07-20 23:00:20 +00:00
|
|
|
for callback in self.callbacks:
|
2021-05-05 19:50:58 +00:00
|
|
|
callback.on_validation_epoch_end(self, self.lightning_module)
|
2020-07-20 23:00:20 +00:00
|
|
|
|
|
|
|
def on_test_epoch_start(self):
|
|
|
|
"""Called when the epoch begins."""
|
|
|
|
for callback in self.callbacks:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_test_epoch_start(self, self.lightning_module)
|
2020-07-20 23:00:20 +00:00
|
|
|
|
2021-05-05 19:50:58 +00:00
|
|
|
def on_test_epoch_end(self):
|
|
|
|
"""Called when the test epoch ends."""
|
2021-03-16 16:15:16 +00:00
|
|
|
for callback in self.callbacks:
|
2021-05-05 19:50:58 +00:00
|
|
|
callback.on_test_epoch_end(self, self.lightning_module)
|
2020-07-20 23:00:20 +00:00
|
|
|
|
2021-04-22 14:05:28 +00:00
|
|
|
def on_predict_epoch_start(self) -> None:
|
|
|
|
"""Called when the epoch begins."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_predict_epoch_start(self, self.lightning_module)
|
|
|
|
|
|
|
|
def on_predict_epoch_end(self, outputs: List[Any]) -> None:
|
|
|
|
"""Called when the epoch ends."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_predict_epoch_end(self, self.lightning_module, outputs)
|
|
|
|
|
2020-02-26 04:17:27 +00:00
|
|
|
def on_epoch_start(self):
|
2021-03-25 13:20:49 +00:00
|
|
|
"""Called when either of train/val/test epoch begins."""
|
2020-02-26 04:17:27 +00:00
|
|
|
for callback in self.callbacks:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_epoch_start(self, self.lightning_module)
|
2020-02-26 04:17:27 +00:00
|
|
|
|
|
|
|
def on_epoch_end(self):
|
2021-03-25 13:20:49 +00:00
|
|
|
"""Called when either of train/val/test epoch ends."""
|
2020-02-26 04:17:27 +00:00
|
|
|
for callback in self.callbacks:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_epoch_end(self, self.lightning_module)
|
2020-02-26 04:17:27 +00:00
|
|
|
|
|
|
|
def on_train_start(self):
|
|
|
|
"""Called when the train begins."""
|
|
|
|
for callback in self.callbacks:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_train_start(self, self.lightning_module)
|
2020-02-26 04:17:27 +00:00
|
|
|
|
|
|
|
def on_train_end(self):
|
|
|
|
"""Called when the train ends."""
|
|
|
|
for callback in self.callbacks:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_train_end(self, self.lightning_module)
|
2020-02-26 04:17:27 +00:00
|
|
|
|
2021-03-08 14:27:07 +00:00
|
|
|
def on_pretrain_routine_start(self) -> None:
|
|
|
|
"""Called when the pre-train routine begins."""
|
2020-08-07 13:29:57 +00:00
|
|
|
for callback in self.callbacks:
|
2021-03-08 14:27:07 +00:00
|
|
|
callback.on_pretrain_routine_start(self, self.lightning_module)
|
2020-08-07 13:29:57 +00:00
|
|
|
|
2021-03-08 14:27:07 +00:00
|
|
|
def on_pretrain_routine_end(self) -> None:
|
|
|
|
"""Called when the pre-train routine ends."""
|
2020-08-07 13:29:57 +00:00
|
|
|
for callback in self.callbacks:
|
2021-03-08 14:27:07 +00:00
|
|
|
callback.on_pretrain_routine_end(self, self.lightning_module)
|
2020-08-07 13:29:57 +00:00
|
|
|
|
2020-02-26 04:17:27 +00:00
|
|
|
def on_batch_start(self):
|
|
|
|
"""Called when the training batch begins."""
|
|
|
|
for callback in self.callbacks:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_batch_start(self, self.lightning_module)
|
2020-02-26 04:17:27 +00:00
|
|
|
|
|
|
|
def on_batch_end(self):
|
|
|
|
"""Called when the training batch ends."""
|
|
|
|
for callback in self.callbacks:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_batch_end(self, self.lightning_module)
|
2020-02-26 04:17:27 +00:00
|
|
|
|
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:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_train_batch_start(self, self.lightning_module, batch, batch_idx, dataloader_idx)
|
2020-08-06 00:01:30 +00:00
|
|
|
|
2021-04-22 14:05:28 +00:00
|
|
|
def on_train_batch_end(self, outputs: STEP_OUTPUT, batch, batch_idx, dataloader_idx):
|
2020-08-06 00:01:30 +00:00
|
|
|
"""Called when the training batch ends."""
|
|
|
|
for callback in self.callbacks:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_train_batch_end(self, self.lightning_module, outputs, 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:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_validation_batch_start(self, self.lightning_module, batch, batch_idx, dataloader_idx)
|
2020-04-24 00:46:18 +00:00
|
|
|
|
2021-04-22 14:05:28 +00:00
|
|
|
def on_validation_batch_end(self, outputs: STEP_OUTPUT, batch, batch_idx, dataloader_idx):
|
2020-04-24 00:46:18 +00:00
|
|
|
"""Called when the validation batch ends."""
|
|
|
|
for callback in self.callbacks:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_validation_batch_end(self, self.lightning_module, outputs, 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:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_test_batch_start(self, self.lightning_module, batch, batch_idx, dataloader_idx)
|
2020-04-24 00:46:18 +00:00
|
|
|
|
2021-04-22 14:05:28 +00:00
|
|
|
def on_test_batch_end(self, outputs: STEP_OUTPUT, batch, batch_idx, dataloader_idx):
|
2020-04-24 00:46:18 +00:00
|
|
|
"""Called when the test batch ends."""
|
|
|
|
for callback in self.callbacks:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_test_batch_end(self, self.lightning_module, outputs, batch, batch_idx, dataloader_idx)
|
2020-04-24 00:46:18 +00:00
|
|
|
|
2021-04-22 14:05:28 +00:00
|
|
|
def on_predict_batch_start(self, batch: Any, batch_idx: int, dataloader_idx: int) -> None:
|
|
|
|
"""Called when the predict batch begins."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_predict_batch_start(self, self.lightning_module, batch, batch_idx, dataloader_idx)
|
|
|
|
|
|
|
|
def on_predict_batch_end(self, outputs: STEP_OUTPUT, batch: Any, batch_idx: int, dataloader_idx: int) -> None:
|
|
|
|
"""Called when the predict batch ends."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_predict_batch_end(self, self.lightning_module, outputs, batch, batch_idx, dataloader_idx)
|
|
|
|
|
2020-08-26 04:45:43 +00:00
|
|
|
def on_validation_start(self):
|
|
|
|
"""Called when the validation loop begins."""
|
|
|
|
for callback in self.callbacks:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_validation_start(self, self.lightning_module)
|
2020-08-26 04:45:43 +00:00
|
|
|
|
2020-02-26 04:17:27 +00:00
|
|
|
def on_validation_end(self):
|
|
|
|
"""Called when the validation loop ends."""
|
|
|
|
for callback in self.callbacks:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_validation_end(self, self.lightning_module)
|
2020-02-26 04:17:27 +00:00
|
|
|
|
2020-08-26 04:45:43 +00:00
|
|
|
def on_test_start(self):
|
|
|
|
"""Called when the test begins."""
|
|
|
|
for callback in self.callbacks:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_test_start(self, self.lightning_module)
|
2020-08-26 04:45:43 +00:00
|
|
|
|
2020-02-26 04:17:27 +00:00
|
|
|
def on_test_end(self):
|
|
|
|
"""Called when the test ends."""
|
|
|
|
for callback in self.callbacks:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_test_end(self, self.lightning_module)
|
2020-06-15 10:35:26 +00:00
|
|
|
|
2021-04-22 14:05:28 +00:00
|
|
|
def on_predict_start(self) -> None:
|
|
|
|
"""Called when predict begins."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_predict_start(self, self.lightning_module)
|
|
|
|
|
|
|
|
def on_predict_end(self) -> None:
|
|
|
|
"""Called when predict ends."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_predict_end(self, self.lightning_module)
|
|
|
|
|
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:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_keyboard_interrupt(self, self.lightning_module)
|
2020-08-28 14:50:52 +00:00
|
|
|
|
2021-02-25 15:48:19 +00:00
|
|
|
@staticmethod
|
2021-04-30 15:14:43 +00:00
|
|
|
def __is_old_signature_on_save_checkpoint(fn: Callable) -> bool:
|
2021-02-25 15:48:19 +00:00
|
|
|
parameters = list(signature(fn).parameters)
|
2021-06-21 17:27:37 +00:00
|
|
|
return len(parameters) == 2 and parameters[0] != "args"
|
2021-04-30 15:14:43 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def __is_old_signature_on_load_checkpoint(fn: Callable) -> bool:
|
|
|
|
parameters = list(signature(fn).parameters)
|
|
|
|
return len(parameters) == 1 and parameters[0] != "args"
|
2021-02-25 15:48:19 +00:00
|
|
|
|
|
|
|
def on_save_checkpoint(self, checkpoint: Dict[str, Any]) -> Dict[Type, dict]:
|
2020-08-28 14:50:52 +00:00
|
|
|
"""Called when saving a model checkpoint."""
|
|
|
|
callback_states = {}
|
|
|
|
for callback in self.callbacks:
|
2021-04-30 15:14:43 +00:00
|
|
|
if self.__is_old_signature_on_save_checkpoint(callback.on_save_checkpoint):
|
2021-03-25 14:26:38 +00:00
|
|
|
rank_zero_deprecation(
|
2021-02-25 15:48:19 +00:00
|
|
|
"`Callback.on_save_checkpoint` signature has changed in v1.3."
|
|
|
|
" A `checkpoint` parameter has been added."
|
2021-03-25 14:26:38 +00:00
|
|
|
" Support for the old signature will be removed in v1.5"
|
2021-02-25 15:48:19 +00:00
|
|
|
)
|
|
|
|
state = callback.on_save_checkpoint(self, self.lightning_module) # noqa: parameter-unfilled
|
|
|
|
else:
|
|
|
|
state = callback.on_save_checkpoint(self, self.lightning_module, checkpoint)
|
2020-08-28 14:50:52 +00:00
|
|
|
if state:
|
2021-02-25 15:48:19 +00:00
|
|
|
callback_states[type(callback)] = state
|
2020-08-28 14:50:52 +00:00
|
|
|
return callback_states
|
|
|
|
|
|
|
|
def on_load_checkpoint(self, checkpoint):
|
|
|
|
"""Called when loading a model checkpoint."""
|
2021-04-29 12:39:45 +00:00
|
|
|
|
PoC: Accelerator refactor (#5743)
* restoring the result from subprocess
* fix queue.get() order for results
* add missing "block_backward_sync" context manager
* add missing "block_backward_sync" context manager
* fix sync_batchnorm
* fix supported gpu-ids for tuple
* fix clip gradients and inf recursion
* accelerator selection: added cluster_environment plugin
* fix torchelastic test
* fix reduce early stopping decision for DDP
* fix tests: callbacks, conversion to lightning optimizer
* fix lightning optimizer does not pickle
* fix setting benchmark and deterministic option
* fix slurm amp test
* fix prepare_data test and determine node_rank
* fix retrieving last path when testing
* remove obsolete plugin argument
* fix test: test_trainer_config
* fix torchscript tests
* fix trainer.model access
* move properties
* fix test_transfer_batch_hook
* fix auto_select_gpus
* fix omegaconf test
* fix test that needs to simulate slurm ddp
* add horovod plugin
* fix test with named arguments
* clean up whitespace
* fix datamodules test
* remove old accelerators
* fix naming
* move old plugins
* move to plugins
* create precision subpackage
* create training_type subpackage
* fix all new import errors
* fix wrong arguments order passed to test
* fix LR finder
* Added sharded training type and amp plugin
* Move clip grad to precision plugin
* Added sharded spawn, select accelerators based on distributed_backend + enable custom fp16 plugin automatically
* Fix import issue, attempting to fix tests
* Fix initial test
* Reflect hook logic from master, should wrap model after move to device
* Optional state consolidation, since master has optimizers not wrapped
* change attribute for instance test
* reset optimizers
optimizers are not used in main process, so state would be wrong.
* legacy
* imports in accel
* legacy2
* trainer imports
* fix import errors after rebase
* move hook to new setup location
* provide unwrapping logic
* fix trainer callback system
* added ddp2 implementation
* fix imports .legacy
* move plugins
* restore legacy
* drop test.py from root
* add tpu accelerator and plugins
* fixes
* fix lightning optimizer merge
* reset bugreportmodel
* unwrapping
* step routing forward
* model access
* unwrap
* opt
* integrate distrib_type
* sync changes
* sync
* fixes
* add forgotten generators
* add missing logic
* update
* import
* missed imports
* import fixes
* isort
* mv f
* changelog
* format
* move helper to parallel plugin
* d
* add world size
* clean up
* duplicate
* activate ddp_sharded and tpu
* set nvidia flags
* remove unused colab var
* use_tpu <-> on_tpu attrs
* make some ddp_cpu and clusterplugin tests pass
* Ref/accelerator connector (#5742)
* final cleanup
Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>
* connector cleanup
Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>
* trainer cleanup
Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>
* accelerator cleanup + missing logic in accelerator connector
Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>
* add missing changes to callbacks
Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>
* reflect accelerator changes to lightning module
Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>
* clean cluster envs
Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>
* cleanup plugins
Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>
* add broadcasting
Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>
* yapf
* remove plugin connector
Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>
* plugins
* manual optimization
* update optimizer routing
* add rank to torchelastic
* fix memory mixed precision
* setstate on trainer for pickling in ddp spawn
* add predict method
* add back commented accelerator code
* adapt test for sync_batch_norm to new plugin
* fix deprecated tests
* fix ddp cpu choice when no num_processes are given
* yapf format
* skip a memory test that cannot pass anymore
* fix pickle error in spawn plugin
* x
* avoid
* x
* fix cyclic import in docs build
* add support for sharded
* update typing
* add sharded and sharded_spawn to distributed types
* make unwrap model default
* refactor LightningShardedDataParallel similar to LightningDistributedDataParallel
* update sharded spawn to reflect changes
* update sharded to reflect changes
* Merge 1.1.5 changes
* fix merge
* fix merge
* yapf isort
* fix merge
* yapf isort
* fix indentation in test
* copy over reinit scheduler implementation from dev1.2
* fix apex tracking calls with dev_debugger
* reduce diff to dev1.2, clean up
* fix trainer config test when gpus>0 and num_processes >0 and ddp_cpu
* sort plugin tests legacy/new
* fix error handling for amp on cpu
* fix merge
fix merge
fix merge
* [Feat] Resolve manual_backward (#5837)
* resolve manual_backward
* resolve flake8
* update
* resolve for ddp_spawn
* resolve flake8
* resolve flake8
* resolve flake8
Co-authored-by: Ubuntu <ubuntu@ip-172-31-88-60.ec2.internal>
* fix tests/accelerator tests on cpu
* [BugFix] Resolve manual optimization (#5852)
* resolve manual_optimization
* update
* update
Co-authored-by: Ubuntu <ubuntu@ip-172-31-88-60.ec2.internal>
* Remove copy trainer parameters to happen earlier within the loop and add safe guard to get ref model (#5856)
* resovle a bug
* Accelerator refactor sharded rpc (#5854)
* rpc branch
* merge
* update handling of rpc
* make devices etc. Optional in RPC
* set devices etc. later if necessary
* remove devices from sequential
* make devices optional in rpc
* fix import
* uncomment everything
* fix cluster selection
Co-authored-by: Ubuntu <ubuntu@ip-172-31-88-60.ec2.internal>
* resolve bug
* fix assert in rpc test
* resolve a test
* fix docs compilation
* accelerator refactor - fix for sharded parity test (#5866)
* fix memory issue with ddp_spawn
* x
x
x
x
x
x
x
x
x
* x
* Remove DDP2 as this does not apply
* Add missing pre optimizer hook to ensure lambda closure is called
* fix apex docstring
* [accelerator][BugFix] Resolve some test for 1 gpu (#5863)
* update
* revert init
* resolve a bug
* update
* resolve flake8
* update
* update
* update
* revert init
* resolve a bug
* update
* resolve flake8
* update
* update
* update
* update
* update
* revert init
* resolve a bug
* update
* resolve flake8
* update
* update
* update
* revert init
* update
* resolve flake8
* update
* update
* update
* update
* update
* all_gather
* update
* make plugins work, add misconfig for RPC
* update
* update
* remove breaking test
* resolve some tests
* resolve flake8
* revert to ddp_spawn
Co-authored-by: root <root@ip-172-31-88-60.ec2.internal>
Co-authored-by: Ubuntu <ubuntu@ip-172-31-88-60.ec2.internal>
Co-authored-by: Justus Schock <justus.schock@rwth-aachen.de>
* yapf isort
* resolve flake8
* fix apex doctests
* fix apex doctests 2
* resolve docs
* update drone
* clean env
* update
* update
* update
* update
* merge
* Fix RPC related tests, clean out old API, update for new accelerator API [skip ci] (#5881)
* Fix RPC related tests, clean out old API, update for new accelerator API
* Move tests out of legacy folder, update paths and names
* Update test_remove_1-4.py
* Expose properties for tpu cores/gpus/num_gpus
* Add root GPU property
* Move properties to properties.py
* move tests that were previously in drone
* Fix root GPU property (#5908)
* Move root GPU to property, remove horovod set as this is handled in horovod plugin, ensure we mock correctly to set GPU accelerator
* Add missing tests back
* fix best model path transfer when no checkpoint callback available
* Fix setup hook order [wip] (#5858)
* Call trainer setup hook before accelerator setup
* Add test case
* add new test
* typo
* fix callback order in test
Co-authored-by: tchaton <thomas@grid.ai>
Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>
* rename ddp sequential -> rpc sequential for special test
* revert
* fix stupid merge problem
* Use property in connector for sampler (#5913)
* merge the import conflicts
* fix spawning of processes in slurm
* [wip] Fix some bugs for TPU [skip ci] (#5878)
* fixed for single tpu
* fixed spawn
* fixed spawn
* update
* update
* wip
* resolve bugs
* resolve bug
* update on comment
* removed decorator
* resolve comments
* set to 4
* update
* update
* need cleaning
* update
* update
* update
* resolve flake8
* resolve bugs
* exclude broadcast
* resolve bugs
* change test
* update
* update
* skip if meet fails
* properly raise trace
* update
* add catch
* wrap test
* resolve typo
* update
* typo
Co-authored-by: Lezwon Castelino <lezwon@gmail.com>
Co-authored-by: Your Name <you@example.com>
* resolve some tests
* update
* fix imports
* update
* resolve flake8
* update azure pipeline
* skip a sharded test on cpu that requires a gpu
* resolve tpus
* resolve bug
* resolve flake8
* update
* updat utils
* revert permission change on files
* suggestions from carlos
Co-authored-by: Carlos Mocholí <carlossmocholi@gmail.com>
* remove unrelated formatting changes
* remove incomplete comment
* Update pytorch_lightning/accelerators/__init__.py
Co-authored-by: Carlos Mocholí <carlossmocholi@gmail.com>
* remove unrelated formatting change
* add types
* warn 1.7 ddp manual backward only if ddp kwarg unset
* yapf + isort
* pep8 unused imports
* fix cyclic import in docs
* Apply suggestions from code review
* typer in accelerator.py
* typo
* Apply suggestions from code review
* formatting
* update on comments
* update typo
* Update pytorch_lightning/trainer/properties.py
Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>
* update
* suggestion from code review
* suggestion from code review
Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>
Co-authored-by: SeanNaren <sean@grid.ai>
Co-authored-by: Jirka Borovec <jirka.borovec@seznam.cz>
Co-authored-by: chaton <thomas@grid.ai>
Co-authored-by: Ubuntu <ubuntu@ip-172-31-88-60.ec2.internal>
Co-authored-by: Sean Naren <sean.narenthiran@gmail.com>
Co-authored-by: root <root@ip-172-31-88-60.ec2.internal>
Co-authored-by: Lezwon Castelino <lezwon@gmail.com>
Co-authored-by: Your Name <you@example.com>
Co-authored-by: Carlos Mocholí <carlossmocholi@gmail.com>
Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2021-02-12 20:48:56 +00:00
|
|
|
# Todo: the `callback_states` are dropped with TPUSpawn as they
|
|
|
|
# can't be saved using `xm.save`
|
|
|
|
# https://github.com/pytorch/xla/issues/2773
|
2021-04-29 12:39:45 +00:00
|
|
|
callback_states = checkpoint.get('callbacks')
|
|
|
|
|
2021-04-30 10:48:53 +00:00
|
|
|
if callback_states is None:
|
|
|
|
return
|
|
|
|
|
2021-04-29 12:39:45 +00:00
|
|
|
current_callbacks_type = {type(cb) for cb in self.callbacks}
|
|
|
|
saved_callbacks_type = set(callback_states.keys())
|
|
|
|
difference = saved_callbacks_type.difference(current_callbacks_type)
|
|
|
|
if difference:
|
|
|
|
rank_zero_warn(
|
|
|
|
"Be aware that when using ``resume_from_checkpoint``, "
|
|
|
|
"callbacks used to create the checkpoint need to be provided. "
|
|
|
|
f"Please, add the following callbacks: {list(difference)}. ", UserWarning
|
|
|
|
)
|
|
|
|
|
2021-04-30 10:48:53 +00:00
|
|
|
for callback in self.callbacks:
|
|
|
|
state = callback_states.get(type(callback))
|
|
|
|
if state:
|
|
|
|
state = deepcopy(state)
|
2021-04-30 15:14:43 +00:00
|
|
|
if self.__is_old_signature_on_load_checkpoint(callback.on_load_checkpoint):
|
|
|
|
rank_zero_deprecation(
|
|
|
|
"`Callback.on_load_checkpoint` signature has changed in v1.3."
|
|
|
|
" `trainer` and `pl_module` parameters have been added."
|
|
|
|
" Support for the old signature will be removed in v1.5"
|
|
|
|
)
|
|
|
|
callback.on_load_checkpoint(state) # noqa: parameter-unfilled
|
|
|
|
else:
|
|
|
|
callback.on_load_checkpoint(self, self.lightning_module, state)
|
2020-10-28 12:15:22 +00:00
|
|
|
|
2021-07-09 06:15:57 +00:00
|
|
|
def on_before_backward(self, loss: torch.Tensor) -> None:
|
|
|
|
"""Called before ``loss.backward()``."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_before_backward(self, self.lightning_module, loss)
|
|
|
|
|
2020-10-28 12:15:22 +00:00
|
|
|
def on_after_backward(self):
|
|
|
|
"""
|
|
|
|
Called after loss.backward() and before optimizers do anything.
|
|
|
|
"""
|
|
|
|
for callback in self.callbacks:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_after_backward(self, self.lightning_module)
|
2020-10-28 12:15:22 +00:00
|
|
|
|
2021-07-09 11:30:52 +00:00
|
|
|
def on_before_optimizer_step(self, optimizer, optimizer_idx):
|
|
|
|
"""
|
|
|
|
Called after on_after_backward() once the gradient is accumulated and before optimizer.step().
|
|
|
|
"""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_before_optimizer_step(self, self.lightning_module, optimizer, optimizer_idx)
|
|
|
|
|
2020-10-28 12:15:22 +00:00
|
|
|
def on_before_zero_grad(self, optimizer):
|
|
|
|
"""
|
|
|
|
Called after optimizer.step() and before optimizer.zero_grad().
|
|
|
|
"""
|
|
|
|
for callback in self.callbacks:
|
2021-02-18 14:59:54 +00:00
|
|
|
callback.on_before_zero_grad(self, self.lightning_module, optimizer)
|