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.
|
2021-04-29 12:40:51 +00:00
|
|
|
import pytorch_lightning as pl
|
2021-05-04 10:50:56 +00:00
|
|
|
from pytorch_lightning.trainer.states import TrainerFn
|
2020-08-07 22:33:51 +00:00
|
|
|
from pytorch_lightning.utilities.exceptions import MisconfigurationException
|
2020-12-21 23:23:33 +00:00
|
|
|
from pytorch_lightning.utilities.model_helpers import is_overridden
|
2021-08-24 18:45:54 +00:00
|
|
|
from pytorch_lightning.utilities.signature_utils import is_param_in_hook_signature
|
2021-09-01 08:49:00 +00:00
|
|
|
from pytorch_lightning.utilities.warnings import rank_zero_deprecation, rank_zero_warn
|
2020-07-25 16:57:40 +00:00
|
|
|
|
|
|
|
|
2021-04-29 12:40:51 +00:00
|
|
|
class ConfigValidator:
|
2021-07-26 11:37:35 +00:00
|
|
|
def __init__(self, trainer: "pl.Trainer") -> None:
|
2020-07-25 16:57:40 +00:00
|
|
|
self.trainer = trainer
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def verify_loop_configurations(self, model: "pl.LightningModule") -> None:
|
2020-07-25 16:57:40 +00:00
|
|
|
r"""
|
2021-03-06 12:40:19 +00:00
|
|
|
Checks that the model is configured correctly before the run is started.
|
2020-07-25 16:57:40 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
model: The model to check the configuration.
|
|
|
|
|
|
|
|
"""
|
2021-05-04 10:50:56 +00:00
|
|
|
if self.trainer.state.fn in (TrainerFn.FITTING, TrainerFn.TUNING):
|
2020-07-25 16:57:40 +00:00
|
|
|
self.__verify_train_loop_configuration(model)
|
2021-07-26 11:37:35 +00:00
|
|
|
self.__verify_eval_loop_configuration(model, "val")
|
2021-06-03 15:42:37 +00:00
|
|
|
self.__verify_manual_optimization_support(model)
|
2021-08-24 18:45:54 +00:00
|
|
|
self.__check_training_step_requires_dataloader_iter(model)
|
2021-05-04 10:50:56 +00:00
|
|
|
elif self.trainer.state.fn == TrainerFn.VALIDATING:
|
2021-07-26 11:37:35 +00:00
|
|
|
self.__verify_eval_loop_configuration(model, "val")
|
2021-05-04 10:50:56 +00:00
|
|
|
elif self.trainer.state.fn == TrainerFn.TESTING:
|
2021-07-26 11:37:35 +00:00
|
|
|
self.__verify_eval_loop_configuration(model, "test")
|
2021-05-04 10:50:56 +00:00
|
|
|
elif self.trainer.state.fn == TrainerFn.PREDICTING:
|
2021-03-21 21:07:54 +00:00
|
|
|
self.__verify_predict_loop_configuration(model)
|
2021-04-29 12:40:51 +00:00
|
|
|
self.__verify_dp_batch_transfer_support(model)
|
2020-07-25 16:57:40 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def __verify_train_loop_configuration(self, model: "pl.LightningModule") -> None:
|
2020-07-25 16:57:40 +00:00
|
|
|
# -----------------------------------
|
|
|
|
# verify model has a training step
|
|
|
|
# -----------------------------------
|
2021-07-26 11:37:35 +00:00
|
|
|
has_training_step = is_overridden("training_step", model)
|
2020-07-25 16:57:40 +00:00
|
|
|
if not has_training_step:
|
|
|
|
raise MisconfigurationException(
|
2021-07-26 11:37:35 +00:00
|
|
|
"No `training_step()` method defined. Lightning `Trainer` expects as minimum a"
|
|
|
|
" `training_step()`, `train_dataloader()` and `configure_optimizers()` to be defined."
|
2020-07-25 16:57:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# -----------------------------------
|
|
|
|
# verify model has a train dataloader
|
|
|
|
# -----------------------------------
|
2021-07-26 11:37:35 +00:00
|
|
|
has_train_dataloader = is_overridden("train_dataloader", model)
|
2021-02-16 22:11:56 +00:00
|
|
|
if not has_train_dataloader:
|
2020-07-25 16:57:40 +00:00
|
|
|
raise MisconfigurationException(
|
2021-07-26 11:37:35 +00:00
|
|
|
"No `train_dataloader()` method defined. Lightning `Trainer` expects as minimum a"
|
|
|
|
" `training_step()`, `train_dataloader()` and `configure_optimizers()` to be defined."
|
2020-07-25 16:57:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# -----------------------------------
|
|
|
|
# verify model has optimizer
|
|
|
|
# -----------------------------------
|
2021-07-26 11:37:35 +00:00
|
|
|
has_optimizers = is_overridden("configure_optimizers", model)
|
2021-02-16 22:11:56 +00:00
|
|
|
if not has_optimizers:
|
2020-07-25 16:57:40 +00:00
|
|
|
raise MisconfigurationException(
|
2021-07-26 11:37:35 +00:00
|
|
|
"No `configure_optimizers()` method defined. Lightning `Trainer` expects as minimum a"
|
|
|
|
" `training_step()`, `train_dataloader()` and `configure_optimizers()` to be defined."
|
2020-07-25 16:57:40 +00:00
|
|
|
)
|
|
|
|
|
2021-08-28 17:27:56 +00:00
|
|
|
# ----------------------------------------------
|
|
|
|
# verify model does not have
|
|
|
|
# - on_train_dataloader
|
|
|
|
# - on_val_dataloader
|
|
|
|
# ----------------------------------------------
|
|
|
|
has_on_train_dataloader = is_overridden("on_train_dataloader", model)
|
|
|
|
if has_on_train_dataloader:
|
|
|
|
rank_zero_deprecation(
|
|
|
|
"Method `on_train_dataloader` in DataHooks is deprecated and will be removed in v1.7.0."
|
|
|
|
" Please use `train_dataloader()` directly."
|
|
|
|
)
|
|
|
|
|
|
|
|
has_on_val_dataloader = is_overridden("on_val_dataloader", model)
|
|
|
|
if has_on_val_dataloader:
|
|
|
|
rank_zero_deprecation(
|
|
|
|
"Method `on_val_dataloader` in DataHooks is deprecated and will be removed in v1.7.0."
|
|
|
|
" Please use `val_dataloader()` directly."
|
|
|
|
)
|
|
|
|
|
2020-12-07 12:55:49 +00:00
|
|
|
trainer = self.trainer
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
trainer.overriden_optimizer_step = is_overridden("optimizer_step", model)
|
|
|
|
trainer.overriden_optimizer_zero_grad = is_overridden("optimizer_zero_grad", model)
|
2021-04-26 05:36:26 +00:00
|
|
|
automatic_optimization = model.automatic_optimization
|
2020-12-07 12:55:49 +00:00
|
|
|
going_to_accumulate_grad_batches = trainer.accumulation_scheduler.going_to_accumulate_grad_batches()
|
|
|
|
|
|
|
|
has_overriden_optimization_functions = trainer.overriden_optimizer_step or trainer.overriden_optimizer_zero_grad
|
2021-04-29 12:40:51 +00:00
|
|
|
if has_overriden_optimization_functions and going_to_accumulate_grad_batches and automatic_optimization:
|
2021-06-17 10:50:37 +00:00
|
|
|
rank_zero_warn(
|
2021-07-26 11:37:35 +00:00
|
|
|
"When using `Trainer(accumulate_grad_batches != 1)` and overriding"
|
|
|
|
"`LightningModule.optimizer_{step,zero_grad}`, the hooks will not be called on every batch"
|
|
|
|
"(rather, they are called on every optimization step)."
|
2020-12-07 12:55:49 +00:00
|
|
|
)
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def __verify_eval_loop_configuration(self, model: "pl.LightningModule", stage: str) -> None:
|
|
|
|
loader_name = f"{stage}_dataloader"
|
|
|
|
step_name = "validation_step" if stage == "val" else "test_step"
|
2020-07-25 16:57:40 +00:00
|
|
|
|
2020-08-31 15:08:22 +00:00
|
|
|
has_loader = is_overridden(loader_name, model)
|
|
|
|
has_step = is_overridden(step_name, model)
|
2020-07-25 16:57:40 +00:00
|
|
|
|
|
|
|
if has_loader and not has_step:
|
2021-07-26 11:37:35 +00:00
|
|
|
rank_zero_warn(f"you passed in a {loader_name} but have no {step_name}. Skipping {stage} loop")
|
2020-07-25 16:57:40 +00:00
|
|
|
if has_step and not has_loader:
|
2021-07-26 11:37:35 +00:00
|
|
|
rank_zero_warn(f"you defined a {step_name} but have no {loader_name}. Skipping {stage} loop")
|
2021-03-21 21:07:54 +00:00
|
|
|
|
2021-08-28 17:27:56 +00:00
|
|
|
# ----------------------------------------------
|
|
|
|
# verify model does not have
|
|
|
|
# - on_val_dataloader
|
|
|
|
# - on_test_dataloader
|
|
|
|
# ----------------------------------------------
|
|
|
|
has_on_val_dataloader = is_overridden("on_val_dataloader", model)
|
|
|
|
if has_on_val_dataloader:
|
|
|
|
rank_zero_deprecation(
|
|
|
|
"Method `on_val_dataloader` in DataHooks is deprecated and will be removed in v1.7.0."
|
|
|
|
" Please use `val_dataloader()` directly."
|
|
|
|
)
|
|
|
|
|
|
|
|
has_on_test_dataloader = is_overridden("on_test_dataloader", model)
|
|
|
|
if has_on_test_dataloader:
|
|
|
|
rank_zero_deprecation(
|
|
|
|
"Method `on_test_dataloader` in DataHooks is deprecated and will be removed in v1.7.0."
|
|
|
|
" Please use `test_dataloader()` directly."
|
|
|
|
)
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def __verify_predict_loop_configuration(self, model: "pl.LightningModule") -> None:
|
|
|
|
has_predict_dataloader = is_overridden("predict_dataloader", model)
|
2021-03-21 21:07:54 +00:00
|
|
|
if not has_predict_dataloader:
|
2021-07-26 11:37:35 +00:00
|
|
|
raise MisconfigurationException("Dataloader not found for `Trainer.predict`")
|
2021-08-28 17:27:56 +00:00
|
|
|
# ----------------------------------------------
|
|
|
|
# verify model does not have
|
|
|
|
# - on_predict_dataloader
|
|
|
|
# ----------------------------------------------
|
|
|
|
has_on_predict_dataloader = is_overridden("on_predict_dataloader", model)
|
|
|
|
if has_on_predict_dataloader:
|
|
|
|
rank_zero_deprecation(
|
|
|
|
"Method `on_predict_dataloader` in DataHooks is deprecated and will be removed in v1.7.0."
|
|
|
|
" Please use `predict_dataloader()` directly."
|
|
|
|
)
|
2021-04-29 12:40:51 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def __verify_dp_batch_transfer_support(self, model: "pl.LightningModule") -> None:
|
2021-04-29 12:40:51 +00:00
|
|
|
"""Raise Misconfiguration exception since these hooks are not supported in DP mode"""
|
|
|
|
# TODO: Remove this blocker once batch transfer to device is integrated in Lightning for DP mode.
|
2021-07-26 11:37:35 +00:00
|
|
|
batch_transfer_hooks = ("on_before_batch_transfer", "transfer_batch_to_device", "on_after_batch_transfer")
|
2021-04-29 12:40:51 +00:00
|
|
|
for hook in batch_transfer_hooks:
|
|
|
|
if self.trainer.accelerator_connector.use_dp and is_overridden(hook, model):
|
2021-07-26 11:37:35 +00:00
|
|
|
raise MisconfigurationException(f"Overriding `{hook}` is not supported in DP mode.")
|
2021-06-03 15:42:37 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def __verify_manual_optimization_support(self, model: "pl.LightningModule") -> None:
|
2021-06-03 15:42:37 +00:00
|
|
|
if model.automatic_optimization:
|
|
|
|
return
|
|
|
|
if self.trainer.gradient_clip_val > 0:
|
|
|
|
raise MisconfigurationException(
|
2021-08-13 14:28:14 +00:00
|
|
|
"Automatic gradient clipping is not supported for manual optimization."
|
2021-06-03 15:42:37 +00:00
|
|
|
f" Remove `Trainer(gradient_clip_val={self.trainer.gradient_clip_val})`"
|
2021-08-13 14:28:14 +00:00
|
|
|
" or switch to automatic optimization."
|
2021-06-03 15:42:37 +00:00
|
|
|
)
|
|
|
|
if self.trainer.accumulate_grad_batches != 1:
|
|
|
|
raise MisconfigurationException(
|
2021-08-13 14:28:14 +00:00
|
|
|
"Automatic gradient accumulation is not supported for manual optimization."
|
2021-06-03 15:42:37 +00:00
|
|
|
f" Remove `Trainer(accumulate_grad_batches={self.trainer.accumulate_grad_batches})`"
|
2021-08-13 14:28:14 +00:00
|
|
|
" or switch to automatic optimization."
|
2021-06-03 15:42:37 +00:00
|
|
|
)
|
2021-08-24 18:45:54 +00:00
|
|
|
|
|
|
|
def __check_training_step_requires_dataloader_iter(self, model: "pl.LightningModule"):
|
|
|
|
"""Check if the current `training_step` is requesting `dataloader_iter`."""
|
|
|
|
training_step_fx = getattr(model, "training_step")
|
|
|
|
if is_param_in_hook_signature(training_step_fx, "dataloader_iter", explicit=True):
|
|
|
|
|
|
|
|
if is_overridden("on_train_batch_start", model):
|
|
|
|
raise MisconfigurationException(
|
|
|
|
"The model hook `on_train_batch_start` is not compatible with "
|
|
|
|
"taking a `dataloader_iter` argument in your `training_step`."
|
|
|
|
)
|
|
|
|
|
|
|
|
if is_overridden("on_train_batch_end", model):
|
|
|
|
raise MisconfigurationException(
|
|
|
|
"The model hook `on_train_batch_end` is not compatible with "
|
|
|
|
"taking a `dataloader_iter` argument in your `training_step`."
|
|
|
|
)
|
|
|
|
|
|
|
|
if model.truncated_bptt_steps > 0:
|
|
|
|
raise MisconfigurationException(
|
|
|
|
"The model taking a `dataloader_iter` argument in your `training_step` "
|
|
|
|
"is incompatible with `truncated_bptt_steps > 0`."
|
|
|
|
)
|