add rank warning (#1428)

* add rank warning

* changelog

* use rank_zero_warn

* user trainer_init

* replace warnings

* fix test

* flake8

* docs

* changelog

* bug lol
This commit is contained in:
Jirka Borovec
2020-04-09 14:05:46 -04:00
committed by GitHub
parent b4eb3884cf
commit 17f58d2e11
41 changed files with 213 additions and 187 deletions
+11
View File
@@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
## [0.7.3] - 2020-04-09
### Added
- Added `rank_zero_warn` for warning only in rank 0 ([#1428](https://github.com/PyTorchLightning/pytorch-lightning/pull/1428))
### Fixed
- Fixed default `DistributedSampler` for DDP training ([#1425](https://github.com/PyTorchLightning/pytorch-lightning/pull/1425))
- Fixed workers warning not on windows ([#1430](https://github.com/PyTorchLightning/pytorch-lightning/pull/1430))
## [0.7.2] - 2020-04-07
### Added
+1 -1
View File
@@ -1,6 +1,6 @@
"""Root package info."""
__version__ = '0.7.2'
__version__ = '0.7.3rc1'
__author__ = 'William Falcon et al.'
__author_email__ = 'waf2107@columbia.edu'
__license__ = 'Apache-2.0'
@@ -6,12 +6,11 @@ Stop training when a monitored quantity has stopped improving.
"""
import warnings
import numpy as np
from pytorch_lightning import _logger as log
from pytorch_lightning.callbacks.base import Callback
from pytorch_lightning.utilities import rank_zero_warn
class EarlyStopping(Callback):
@@ -80,7 +79,7 @@ class EarlyStopping(Callback):
if self.strict:
raise RuntimeError(error_msg)
if self.verbose > 0:
warnings.warn(error_msg, RuntimeWarning)
rank_zero_warn(error_msg, RuntimeWarning)
return False
@@ -113,6 +112,6 @@ class EarlyStopping(Callback):
def on_train_end(self, trainer, pl_module):
if self.stopped_epoch > 0 and self.verbose > 0:
warnings.warn('Displayed epoch numbers by `EarlyStopping` start from "1" until v0.6.x,'
' but will start from "0" in v0.8.0.', DeprecationWarning)
rank_zero_warn('Displayed epoch numbers by `EarlyStopping` start from "1" until v0.6.x,'
' but will start from "0" in v0.8.0.', DeprecationWarning)
log.info(f'Epoch {self.stopped_epoch + 1:05d}: early stopping')
@@ -6,9 +6,8 @@ Change gradient accumulation factor according to scheduling.
"""
import warnings
from pytorch_lightning.callbacks.base import Callback
from pytorch_lightning.utilities import rank_zero_warn
class GradientAccumulationScheduler(Callback):
@@ -46,8 +45,8 @@ class GradientAccumulationScheduler(Callback):
raise TypeError("All epoches and accumulation factor must be integers")
minimal_epoch = min(scheduling.keys())
warnings.warn('Epochs indexing of `scheduling` starts from "1" until v0.6.x,'
' but will start from "0" in v0.8.0.', DeprecationWarning)
rank_zero_warn('Epochs indexing of `scheduling` starts from "1" until v0.6.x,'
' but will start from "0" in v0.8.0.', DeprecationWarning)
if minimal_epoch < 1:
msg = f"Epochs indexing from 1, epoch {minimal_epoch} cannot be interpreted correct"
raise IndexError(msg)
@@ -7,14 +7,13 @@ Automatically save model checkpoints during training.
"""
import os
import shutil
import warnings
import re
import numpy as np
from pytorch_lightning.callbacks.base import Callback
from pytorch_lightning import _logger as log
from pytorch_lightning.callbacks.base import Callback
from pytorch_lightning.utilities import rank_zero_warn
class ModelCheckpoint(Callback):
@@ -83,7 +82,7 @@ class ModelCheckpoint(Callback):
mode: str = 'auto', period: int = 1, prefix: str = ''):
super().__init__()
if save_top_k > 0 and os.path.isdir(filepath) and len(os.listdir(filepath)) > 0:
warnings.warn(
rank_zero_warn(
f"Checkpoint directory {filepath} exists and is not empty with save_top_k != 0."
"All files in this directory will be deleted when a checkpoint is saved!"
)
@@ -115,9 +114,7 @@ class ModelCheckpoint(Callback):
}
if mode not in mode_dict:
warnings.warn(
f'ModelCheckpoint mode {mode} is unknown, '
'fallback to auto mode.', RuntimeWarning)
rank_zero_warn(f'ModelCheckpoint mode {mode} is unknown, fallback to auto mode.', RuntimeWarning)
mode = 'auto'
self.monitor_op, self.kth_value, self.mode = mode_dict[mode]
@@ -206,7 +203,7 @@ class ModelCheckpoint(Callback):
current = metrics.get(self.monitor)
if current is None:
warnings.warn(f'Can save best model only with {self.monitor} available, skipping.', RuntimeWarning)
rank_zero_warn(f'Can save best model only with {self.monitor} available, skipping.', RuntimeWarning)
elif self.check_monitor_top_k(current):
self._do_check_save(filepath, current, epoch)
elif self.verbose > 0:
+2 -2
View File
@@ -1,4 +1,4 @@
import warnings
from pytorch_lightning.utilities import rank_zero_warn
def data_loader(fn):
@@ -7,7 +7,7 @@ def data_loader(fn):
Warnings:
This decorator deprecated in v0.7.0 and it will be removed v0.9.0.
"""
warnings.warn('`data_loader` decorator deprecated in v0.7.0. Will be removed v0.9.0', DeprecationWarning)
rank_zero_warn('`data_loader` decorator deprecated in v0.7.0. Will be removed v0.9.0', DeprecationWarning)
def inner_fx(self):
return fn(self)
+8 -8
View File
@@ -1,7 +1,6 @@
import collections
import inspect
import os
import warnings
from abc import ABC, abstractmethod
from argparse import Namespace
from typing import Any, Callable, Dict, List, Optional, Tuple, Union, Sequence
@@ -20,6 +19,7 @@ from pytorch_lightning.core.memory import ModelSummary
from pytorch_lightning.core.saving import ModelIO, load_hparams_from_tags_csv
from pytorch_lightning.overrides.data_parallel import LightningDistributedDataParallel
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from pytorch_lightning.utilities import rank_zero_warn
try:
import torch_xla.core.xla_model as xm
@@ -225,7 +225,7 @@ class LightningModule(ABC, GradInformation, ModelIO, ModelHooks):
The loss value shown in the progress bar is smoothed (averaged) over the last values,
so it differs from the actual loss returned in train/validation step.
"""
warnings.warn('`training_step` must be implemented to be used with the Lightning Trainer')
rank_zero_warn('`training_step` must be implemented to be used with the Lightning Trainer')
def training_end(self, *args, **kwargs):
"""
@@ -1088,7 +1088,7 @@ class LightningModule(ABC, GradInformation, ModelIO, ModelHooks):
}
"""
warnings.warn('`configure_optimizers` must be implemented to be used with the Lightning Trainer')
rank_zero_warn('`configure_optimizers` must be implemented to be used with the Lightning Trainer')
def optimizer_step(
self,
@@ -1291,7 +1291,7 @@ class LightningModule(ABC, GradInformation, ModelIO, ModelHooks):
return loader
"""
warnings.warn('`train_dataloader` must be implemented to be used with the Lightning Trainer')
rank_zero_warn('`train_dataloader` must be implemented to be used with the Lightning Trainer')
def tng_dataloader(self): # todo: remove in v1.0.0
"""
@@ -1299,8 +1299,8 @@ class LightningModule(ABC, GradInformation, ModelIO, ModelHooks):
Deprecated in v0.5.0. Use :meth:`train_dataloader` instead. Will be removed in 1.0.0.
"""
output = self.train_dataloader()
warnings.warn("`tng_dataloader` has been renamed to `train_dataloader` since v0.5.0."
" and this method will be removed in v1.0.0", DeprecationWarning)
rank_zero_warn("`tng_dataloader` has been renamed to `train_dataloader` since v0.5.0."
" and this method will be removed in v1.0.0", DeprecationWarning)
return output
def test_dataloader(self) -> Union[DataLoader, List[DataLoader]]:
@@ -1407,7 +1407,7 @@ class LightningModule(ABC, GradInformation, ModelIO, ModelHooks):
Deprecated in version 0.7.0. You should use :meth:`load_from_checkpoint` instead.
Will be removed in v0.9.0.
"""
warnings.warn(
rank_zero_warn(
"`load_from_metrics` method has been unified with `load_from_checkpoint` in v0.7.0."
" The deprecated method will be removed in v0.9.0.", DeprecationWarning
)
@@ -1519,7 +1519,7 @@ class LightningModule(ABC, GradInformation, ModelIO, ModelHooks):
is_namespace = checkpoint.get('hparams_type', 'namespace') == 'namespace'
hparams = Namespace(**ckpt_hparams) if is_namespace else ckpt_hparams
else:
warnings.warn(
rank_zero_warn(
f"Checkpoint does not contain hyperparameters but {cls.__name__}'s __init__ "
f"contains argument 'hparams'. Will pass in an empty Namespace instead."
" Did you forget to store your model hyperparameters in self.hparams?"
+3 -3
View File
@@ -3,9 +3,9 @@
The deprecated module name will be removed in v0.8.0.
"""
import warnings
from pytorch_lightning.utilities import rank_zero_warn
warnings.warn("`model_saving` module has been renamed to `saving` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
rank_zero_warn("`model_saving` module has been renamed to `saving` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
from pytorch_lightning.core.saving import * # noqa: F403
+4 -4
View File
@@ -3,9 +3,9 @@
The deprecated module name will be removed in v0.8.0.
"""
import warnings
from pytorch_lightning.utilities import rank_zero_warn
rank_zero_warn("`root_module` module has been renamed to `lightning` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
from pytorch_lightning.core.lightning import * # noqa: F403
warnings.warn("`root_module` module has been renamed to `lightning` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
+3 -3
View File
@@ -3,10 +3,10 @@
The deprecated package name will be removed in v0.9.0.
"""
import warnings
from pytorch_lightning.utilities import rank_zero_warn
warnings.warn("`logging` package has been renamed to `loggers` since v0.7.0"
" The deprecated package name will be removed in v0.9.0.", DeprecationWarning)
rank_zero_warn("`logging` package has been renamed to `loggers` since v0.7.0"
" The deprecated package name will be removed in v0.9.0.", DeprecationWarning)
from pytorch_lightning.loggers import * # noqa: F403
from pytorch_lightning.loggers import base, tensorboard # noqa: F403
+3 -3
View File
@@ -2,9 +2,9 @@
.. warning:: `logging` package has been renamed to `loggers` since v0.7.0 and will be removed in v0.9.0
"""
import warnings
from pytorch_lightning.utilities import rank_zero_warn
warnings.warn("`logging.comet` module has been renamed to `loggers.comet` since v0.7.0."
" The deprecated module name will be removed in v0.9.0.", DeprecationWarning)
rank_zero_warn("`logging.comet` module has been renamed to `loggers.comet` since v0.7.0."
" The deprecated module name will be removed in v0.9.0.", DeprecationWarning)
from pytorch_lightning.loggers.comet import CometLogger # noqa: F403
+3 -3
View File
@@ -3,9 +3,9 @@
The deprecated module name will be removed in v0.8.0.
"""
import warnings
from pytorch_lightning.utilities import rank_zero_warn
warnings.warn("`comet_logger` module has been renamed to `comet` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
rank_zero_warn("`comet_logger` module has been renamed to `comet` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
from pytorch_lightning.loggers.comet import CometLogger # noqa: E402
+3 -3
View File
@@ -2,9 +2,9 @@
.. warning:: `logging` package has been renamed to `loggers` since v0.7.0 and will be removed in v0.9.0
"""
import warnings
from pytorch_lightning.utilities import rank_zero_warn
warnings.warn("`logging.mlflow` module has been renamed to `loggers.mlflow` since v0.7.0."
" The deprecated module name will be removed in v0.9.0.", DeprecationWarning)
rank_zero_warn("`logging.mlflow` module has been renamed to `loggers.mlflow` since v0.7.0."
" The deprecated module name will be removed in v0.9.0.", DeprecationWarning)
from pytorch_lightning.loggers.mlflow import MLFlowLogger # noqa: F403
+3 -3
View File
@@ -3,9 +3,9 @@
The deprecated module name will be removed in v0.8.0.
"""
import warnings
from pytorch_lightning.utilities import rank_zero_warn
warnings.warn("`mlflow_logger` module has been renamed to `mlflow` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
rank_zero_warn("`mlflow_logger` module has been renamed to `mlflow` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
from pytorch_lightning.loggers.mlflow import MLFlowLogger # noqa: E402
+3 -3
View File
@@ -2,9 +2,9 @@
.. warning:: `logging` package has been renamed to `loggers` since v0.7.0 and will be removed in v0.9.0
"""
import warnings
from pytorch_lightning.utilities import rank_zero_warn
warnings.warn("`logging.neptune` module has been renamed to `loggers.neptune` since v0.7.0."
" The deprecated module name will be removed in v0.9.0.", DeprecationWarning)
rank_zero_warn("`logging.neptune` module has been renamed to `loggers.neptune` since v0.7.0."
" The deprecated module name will be removed in v0.9.0.", DeprecationWarning)
from pytorch_lightning.loggers.neptune import NeptuneLogger # noqa: F403
+3 -3
View File
@@ -2,9 +2,9 @@
.. warning:: `logging` package has been renamed to `loggers` since v0.7.0 and will be removed in v0.9.0
"""
import warnings
from pytorch_lightning.utilities import rank_zero_warn
warnings.warn("`logging.test_tube` module has been renamed to `loggers.test_tube` since v0.7.0."
" The deprecated module name will be removed in v0.9.0.", DeprecationWarning)
rank_zero_warn("`logging.test_tube` module has been renamed to `loggers.test_tube` since v0.7.0."
" The deprecated module name will be removed in v0.9.0.", DeprecationWarning)
from pytorch_lightning.loggers.test_tube import TestTubeLogger # noqa: F403
@@ -3,9 +3,9 @@
The deprecated module name will be removed in v0.8.0.
"""
import warnings
from pytorch_lightning.utilities import rank_zero_warn
warnings.warn("`test_tube_logger` module has been renamed to `test_tube` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
rank_zero_warn("`test_tube_logger` module has been renamed to `test_tube` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
from pytorch_lightning.loggers.test_tube import TestTubeLogger # noqa: E402
+3 -3
View File
@@ -2,9 +2,9 @@
.. warning:: `logging` package has been renamed to `loggers` since v0.7.0 and will be removed in v0.9.0
"""
import warnings
from pytorch_lightning.utilities import rank_zero_warn
warnings.warn("`logging.wandb` module has been renamed to `loggers.wandb` since v0.7.0."
" The deprecated module name will be removed in v0.9.0.", DeprecationWarning)
rank_zero_warn("`logging.wandb` module has been renamed to `loggers.wandb` since v0.7.0."
" The deprecated module name will be removed in v0.9.0.", DeprecationWarning)
from pytorch_lightning.loggers.wandb import WandbLogger # noqa: F403
@@ -3,10 +3,10 @@
The deprecated module name will be removed in v0.8.0.
"""
import warnings
from pytorch_lightning.utilities import rank_zero_warn
warnings.warn("`override_data_parallel` module has been renamed to `data_parallel` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
rank_zero_warn("`override_data_parallel` module has been renamed to `data_parallel` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
from pytorch_lightning.overrides.data_parallel import ( # noqa: E402
get_a_var, parallel_apply, LightningDataParallel, LightningDistributedDataParallel)
+3 -3
View File
@@ -3,7 +3,7 @@
The deprecated module name will be removed in v0.8.0.
"""
import warnings
from pytorch_lightning.utilities import rank_zero_warn
warnings.warn("`pt_overrides` package has been renamed to `overrides` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
rank_zero_warn("`pt_overrides` package has been renamed to `overrides` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
@@ -3,10 +3,10 @@
The deprecated module name will be removed in v0.8.0.
"""
import warnings
from pytorch_lightning.utilities import rank_zero_warn
warnings.warn("`override_data_parallel` module has been renamed to `data_parallel` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
rank_zero_warn("`override_data_parallel` module has been renamed to `data_parallel` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
from pytorch_lightning.overrides.data_parallel import ( # noqa: F402
get_a_var, parallel_apply, LightningDataParallel, LightningDistributedDataParallel)
+3 -3
View File
@@ -3,7 +3,7 @@
The deprecated package name will be removed in v0.8.0.
"""
import warnings
from pytorch_lightning.utilities import rank_zero_warn
warnings.warn("`root_module` package has been renamed to `core` since v0.6.0."
" The deprecated package name will be removed in v0.8.0.", DeprecationWarning)
rank_zero_warn("`root_module` package has been renamed to `core` since v0.6.0."
" The deprecated package name will be removed in v0.8.0.", DeprecationWarning)
+3 -3
View File
@@ -3,9 +3,9 @@
The deprecated module name will be removed in v0.8.0.
"""
import warnings
from pytorch_lightning.utilities import rank_zero_warn
warnings.warn("`root_module.decorators` module has been renamed to `core.decorators` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
rank_zero_warn("`root_module.decorators` module has been renamed to `core.decorators` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
from pytorch_lightning.core.decorators import * # noqa: F403
+3 -3
View File
@@ -3,9 +3,9 @@
The deprecated module name will be removed in v0.8.0.
"""
import warnings
from pytorch_lightning.utilities import rank_zero_warn
warnings.warn("`root_module.grads` module has been renamed to `core.grads` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
rank_zero_warn("`root_module.grads` module has been renamed to `core.grads` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
from pytorch_lightning.core.grads import * # noqa: F403
+3 -3
View File
@@ -3,9 +3,9 @@
The deprecated module name will be removed in v0.8.0.
"""
import warnings
from pytorch_lightning.utilities import rank_zero_warn
warnings.warn("`root_module.hooks` module has been renamed to `core.hooks` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
rank_zero_warn("`root_module.hooks` module has been renamed to `core.hooks` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
from pytorch_lightning.core.hooks import * # noqa: F403
+3 -3
View File
@@ -3,9 +3,9 @@
The deprecated module name will be removed in v0.8.0.
"""
import warnings
from pytorch_lightning.utilities import rank_zero_warn
warnings.warn("`root_module.memory` module has been renamed to `core.memory` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
rank_zero_warn("`root_module.memory` module has been renamed to `core.memory` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
from pytorch_lightning.core.memory import * # noqa: F403
@@ -3,9 +3,9 @@
The deprecated module name will be removed in v0.8.0.
"""
import warnings
from pytorch_lightning.utilities import rank_zero_warn
warnings.warn("`root_module.model_saving` module has been renamed to `core.saving` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
rank_zero_warn("`root_module.model_saving` module has been renamed to `core.saving` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
from pytorch_lightning.core.saving import * # noqa: F403
+3 -3
View File
@@ -3,9 +3,9 @@
The deprecated module name will be removed in v0.8.0.
"""
import warnings
from pytorch_lightning.utilities import rank_zero_warn
warnings.warn("`root_module.root_module` module has been renamed to `core.lightning` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
rank_zero_warn("`root_module.root_module` module has been renamed to `core.lightning` since v0.6.0."
" The deprecated module name will be removed in v0.8.0.", DeprecationWarning)
from pytorch_lightning.core.lightning import * # noqa: F403
+4 -4
View File
@@ -1,4 +1,3 @@
import warnings
import platform
from abc import ABC, abstractmethod
from typing import Union, List, Tuple, Callable
@@ -8,6 +7,7 @@ from torch.utils.data import DataLoader
from torch.utils.data.distributed import DistributedSampler
from pytorch_lightning.core import LightningModule
from pytorch_lightning.utilities import rank_zero_warn
from pytorch_lightning.utilities.exceptions import MisconfigurationException
try:
@@ -79,9 +79,9 @@ class TrainerDataLoadingMixin(ABC):
on_windows = platform.system() == 'Windows'
if isinstance(dataloader, DataLoader) and dataloader.num_workers <= 2 and not on_windows:
warnings.warn(f'The dataloader, {name}, does not have many workers which may be a bottleneck.'
' Consider increasing the value of the `num_workers` argument`'
' in the `DataLoader` init to improve performance.')
rank_zero_warn(f'The dataloader, {name}, does not have many workers which may be a bottleneck.'
' Consider increasing the value of the `num_workers` argument`'
' in the `DataLoader` init to improve performance.')
def auto_add_sampler(self, dataloader: DataLoader, train: bool) -> DataLoader:
+30 -29
View File
@@ -1,8 +1,9 @@
"""Mirroring deprecated API"""
import warnings
from abc import ABC
from pytorch_lightning.utilities import rank_zero_warn
class TrainerDeprecatedAPITillVer0_8(ABC):
@@ -12,80 +13,80 @@ class TrainerDeprecatedAPITillVer0_8(ABC):
@property
def nb_gpu_nodes(self):
"""Back compatibility, will be removed in v0.8.0"""
warnings.warn("Attribute `nb_gpu_nodes` has renamed to `num_nodes` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
rank_zero_warn("Attribute `nb_gpu_nodes` has renamed to `num_nodes` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
return self.num_nodes
@property
def num_gpu_nodes(self):
"""Back compatibility, will be removed in v0.8.0"""
warnings.warn("Attribute `num_gpu_nodes` has renamed to `num_nodes` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
rank_zero_warn("Attribute `num_gpu_nodes` has renamed to `num_nodes` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
return self.num_nodes
@num_gpu_nodes.setter
def num_gpu_nodes(self, num_nodes):
"""Back compatibility, will be removed in v0.8.0"""
warnings.warn("Attribute `num_gpu_nodes` has renamed to `num_nodes` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
rank_zero_warn("Attribute `num_gpu_nodes` has renamed to `num_nodes` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
self.num_nodes = num_nodes
@property
def gradient_clip(self):
"""Back compatibility, will be removed in v0.8.0"""
warnings.warn("Attribute `gradient_clip` has renamed to `gradient_clip_val` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
rank_zero_warn("Attribute `gradient_clip` has renamed to `gradient_clip_val` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
return self.gradient_clip_val
@gradient_clip.setter
def gradient_clip(self, gradient_clip):
"""Back compatibility, will be removed in v0.8.0"""
warnings.warn("Attribute `gradient_clip` has renamed to `gradient_clip_val` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
rank_zero_warn("Attribute `gradient_clip` has renamed to `gradient_clip_val` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
self.gradient_clip_val = gradient_clip
@property
def max_nb_epochs(self):
"""Back compatibility, will be removed in v0.8.0"""
warnings.warn("Attribute `max_nb_epochs` has renamed to `max_epochs` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
rank_zero_warn("Attribute `max_nb_epochs` has renamed to `max_epochs` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
return self.max_epochs
@max_nb_epochs.setter
def max_nb_epochs(self, max_epochs):
"""Back compatibility, will be removed in v0.8.0"""
warnings.warn("Attribute `max_nb_epochs` has renamed to `max_epochs` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
rank_zero_warn("Attribute `max_nb_epochs` has renamed to `max_epochs` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
self.max_epochs = max_epochs
@property
def min_nb_epochs(self):
"""Back compatibility, will be removed in v0.8.0"""
warnings.warn("Attribute `min_nb_epochs` has renamed to `min_epochs` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
rank_zero_warn("Attribute `min_nb_epochs` has renamed to `min_epochs` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
return self.min_epochs
@min_nb_epochs.setter
def min_nb_epochs(self, min_epochs):
"""Back compatibility, will be removed in v0.8.0"""
warnings.warn("Attribute `min_nb_epochs` has renamed to `min_epochs` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
rank_zero_warn("Attribute `min_nb_epochs` has renamed to `min_epochs` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
self.min_epochs = min_epochs
@property
def nb_sanity_val_steps(self):
"""Back compatibility, will be removed in v0.8.0"""
warnings.warn("Attribute `nb_sanity_val_steps` has renamed to "
"`num_sanity_val_steps` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
rank_zero_warn("Attribute `nb_sanity_val_steps` has renamed to "
"`num_sanity_val_steps` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
return self.num_sanity_val_steps
@nb_sanity_val_steps.setter
def nb_sanity_val_steps(self, nb):
"""Back compatibility, will be removed in v0.8.0"""
warnings.warn("Attribute `nb_sanity_val_steps` has renamed to "
"`num_sanity_val_steps` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
rank_zero_warn("Attribute `nb_sanity_val_steps` has renamed to "
"`num_sanity_val_steps` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
self.num_sanity_val_steps = nb
@@ -97,12 +98,12 @@ class TrainerDeprecatedAPITillVer0_9(ABC):
@property
def show_progress_bar(self):
"""Back compatibility, will be removed in v0.9.0"""
warnings.warn("Argument `show_progress_bar` is now set by `progress_bar_refresh_rate` since v0.7.2"
" and this method will be removed in v0.9.0", DeprecationWarning)
rank_zero_warn("Argument `show_progress_bar` is now set by `progress_bar_refresh_rate` since v0.7.2"
" and this method will be removed in v0.9.0", DeprecationWarning)
return self.progress_bar_refresh_rate >= 1
@show_progress_bar.setter
def show_progress_bar(self, tf):
"""Back compatibility, will be removed in v0.9.0"""
warnings.warn("Argument `show_progress_bar` is now set by `progress_bar_refresh_rate` since v0.7.2"
" and this method will be removed in v0.9.0", DeprecationWarning)
rank_zero_warn("Argument `show_progress_bar` is now set by `progress_bar_refresh_rate` since v0.7.2"
" and this method will be removed in v0.9.0", DeprecationWarning)
@@ -115,7 +115,6 @@ When the script starts again, Lightning will:
import os
import re
import warnings
from abc import ABC, abstractmethod
from typing import Union
@@ -123,6 +122,7 @@ import torch
from pytorch_lightning import _logger as log
from pytorch_lightning.loggers import LightningLoggerBase
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from pytorch_lightning.utilities.warnings import set_proc_rank, rank_zero_warn
try:
from apex import amp
@@ -203,20 +203,19 @@ class TrainerDDPMixin(ABC):
self.use_ddp2 = distributed_backend == 'ddp2'
elif distributed_backend is None:
warnings.warn('You requested multiple GPUs but did not specify a backend, e.g.'
' Trainer(distributed_backend=dp) (or ddp, ddp2).'
' Setting distributed_backend=dp for you.')
rank_zero_warn('You requested multiple GPUs but did not specify a backend, e.g.'
' Trainer(distributed_backend=dp) (or ddp, ddp2).'
' Setting distributed_backend=dp for you.')
self.use_dp = True
self.use_ddp = False
self.use_ddp2 = False
# throw error to force user ddp or ddp2 choice
if num_gpu_nodes > 1 and not (self.use_ddp2 or self.use_ddp):
w = 'DataParallel does not support num_nodes > 1. ' \
'Switching to DistributedDataParallel for you. ' \
'To silence this warning set distributed_backend=ddp' \
'or distributed_backend=ddp2'
raise MisconfigurationException(w)
raise MisconfigurationException(
'DataParallel does not support num_nodes > 1. Switching to DistributedDataParallel for you. '
'To silence this warning set distributed_backend=ddp or distributed_backend=ddp2'
)
log.info(f'GPU available: {torch.cuda.is_available()}, used: {self.on_gpu}')
@@ -295,6 +294,8 @@ class TrainerDDPMixin(ABC):
elif self.use_ddp2:
self.proc_rank = self.node_rank
self.world_size = self.num_gpu_nodes
# set warning rank
set_proc_rank(self.proc_rank)
# let the exp know the rank to avoid overwriting logs
if self.logger is not None:
@@ -348,6 +348,7 @@ from pytorch_lightning.overrides.data_parallel import (
LightningDataParallel,
)
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from pytorch_lightning.utilities.warnings import set_proc_rank
try:
from apex import amp
@@ -489,6 +490,7 @@ class TrainerDPMixin(ABC):
# track current tpu
self.current_tpu_idx = tpu_core_idx
self.proc_rank = self.tpu_local_core_rank
set_proc_rank(self.proc_rank)
# CHOOSE OPTIMIZER
# allow for lr schedulers as well
+5 -5
View File
@@ -124,7 +124,6 @@ In this second case, the options you pass to trainer will be used when running
"""
import sys
import warnings
from abc import ABC, abstractmethod
from pprint import pprint
from typing import Callable
@@ -136,6 +135,7 @@ from tqdm.auto import tqdm
from pytorch_lightning.core.lightning import LightningModule
from pytorch_lightning.overrides.data_parallel import LightningDistributedDataParallel, LightningDataParallel
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from pytorch_lightning.utilities import rank_zero_warn
try:
import torch_xla.distributed.parallel_loader as xla_pl
@@ -299,8 +299,8 @@ class TrainerEvaluationLoopMixin(ABC):
if self.is_overriden('test_end', model=model):
# TODO: remove in v1.0.0
eval_results = model.test_end(outputs)
warnings.warn('Method `test_end` was deprecated in 0.7.0 and will be removed 1.0.0.'
' Use `test_epoch_end` instead.', DeprecationWarning)
rank_zero_warn('Method `test_end` was deprecated in 0.7.0 and will be removed 1.0.0.'
' Use `test_epoch_end` instead.', DeprecationWarning)
elif self.is_overriden('test_epoch_end', model=model):
eval_results = model.test_epoch_end(outputs)
@@ -309,8 +309,8 @@ class TrainerEvaluationLoopMixin(ABC):
if self.is_overriden('validation_end', model=model):
# TODO: remove in v1.0.0
eval_results = model.validation_end(outputs)
warnings.warn('Method `validation_end` was deprecated in 0.7.0 and will be removed 1.0.0.'
' Use `validation_epoch_end` instead.', DeprecationWarning)
rank_zero_warn('Method `validation_end` was deprecated in 0.7.0 and will be removed 1.0.0.'
' Use `validation_epoch_end` instead.', DeprecationWarning)
elif self.is_overriden('validation_epoch_end', model=model):
eval_results = model.validation_epoch_end(outputs)
+3 -3
View File
@@ -1,4 +1,3 @@
import warnings
from abc import ABC
from typing import List, Tuple
@@ -7,6 +6,7 @@ from torch import optim
from torch.optim.optimizer import Optimizer
from pytorch_lightning.core.lightning import LightningModule
from pytorch_lightning.utilities import rank_zero_warn
class TrainerOptimizersMixin(ABC):
@@ -18,8 +18,8 @@ class TrainerOptimizersMixin(ABC):
optim_conf = model.configure_optimizers()
if optim_conf is None:
warnings.warn('`LightningModule.configure_optimizers` returned `None`, '
'this fit will run with no optimizer', UserWarning)
rank_zero_warn('`LightningModule.configure_optimizers` returned `None`, '
'this fit will run with no optimizer', UserWarning)
optim_conf = _MockOptimizer()
# single output, single optimizer
+29 -29
View File
@@ -2,7 +2,6 @@ import distutils
import inspect
import os
import sys
import warnings
from argparse import ArgumentParser
from typing import Union, Optional, List, Dict, Tuple, Iterable, Any
@@ -33,6 +32,7 @@ from pytorch_lightning.trainer.training_io import TrainerIOMixin
from pytorch_lightning.trainer.training_loop import TrainerTrainLoopMixin
from pytorch_lightning.trainer.training_tricks import TrainerTrainingTricksMixin
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from pytorch_lightning.utilities import rank_zero_warn
try:
from apex import amp
@@ -266,16 +266,16 @@ class Trainer(
self.num_nodes = num_nodes
# Backward compatibility, TODO: remove in v0.8.0
if nb_gpu_nodes is not None:
warnings.warn("Argument `nb_gpu_nodes` has renamed to `num_nodes` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
rank_zero_warn("Argument `nb_gpu_nodes` has renamed to `num_nodes` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
self.num_gpu_nodes = nb_gpu_nodes
self.log_gpu_memory = log_gpu_memory
self.gradient_clip_val = gradient_clip_val
# Backward compatibility, TODO: remove in v0.8.0
if gradient_clip is not None:
warnings.warn("Argument `gradient_clip` has renamed to `gradient_clip_val` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
rank_zero_warn("Argument `gradient_clip` has renamed to `gradient_clip_val` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
self.gradient_clip = gradient_clip
self.progress_bar_refresh_rate = progress_bar_refresh_rate
@@ -294,15 +294,15 @@ class Trainer(
self.max_epochs = max_epochs
# Backward compatibility, TODO: remove in v0.8.0
if max_nb_epochs is not None:
warnings.warn("Argument `max_nb_epochs` has renamed to `max_epochs` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
rank_zero_warn("Argument `max_nb_epochs` has renamed to `max_epochs` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
self.max_nb_epochs = max_nb_epochs
self.min_epochs = min_epochs
# Backward compatibility, TODO: remove in v0.8.0
if min_nb_epochs is not None:
warnings.warn("Argument `min_nb_epochs` has renamed to `min_epochs` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
rank_zero_warn("Argument `min_nb_epochs` has renamed to `min_epochs` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
self.min_nb_epochs = min_nb_epochs
self.max_steps = max_steps
@@ -311,16 +311,15 @@ class Trainer(
self.num_sanity_val_steps = num_sanity_val_steps
# Backward compatibility, TODO: remove in v0.8.0
if nb_sanity_val_steps is not None:
warnings.warn("Argument `nb_sanity_val_steps` has renamed to "
"`num_sanity_val_steps` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
rank_zero_warn("Argument `nb_sanity_val_steps` has renamed to "
"`num_sanity_val_steps` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
self.nb_sanity_val_steps = nb_sanity_val_steps
# Backward compatibility, TODO: remove in v0.9.0
if print_nan_grads:
warnings.warn("Argument `print_nan_grads` has no effect and will be removed in v0.9.0."
" NaN grads will be printed automatically when detected.",
DeprecationWarning)
rank_zero_warn("Argument `print_nan_grads` has no effect and will be removed in v0.9.0."
" NaN grads will be printed automatically when detected.", DeprecationWarning)
self.reload_dataloaders_every_epoch = reload_dataloaders_every_epoch
@@ -430,8 +429,8 @@ class Trainer(
# backward compatibility
if add_row_log_interval is not None:
warnings.warn("`add_row_log_interval` has renamed to `row_log_interval` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
rank_zero_warn("`add_row_log_interval` has renamed to `row_log_interval` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
if not row_log_interval: # in case you did not set the proper value
row_log_interval = add_row_log_interval
self.row_log_interval = row_log_interval
@@ -447,8 +446,8 @@ class Trainer(
# Backward compatibility, TODO: remove in v0.9.0
if use_amp is not None:
warnings.warn("`use_amp` has been replaced by `precision` since v0.7.0"
" and this argument will be removed in v0.9.0", DeprecationWarning)
rank_zero_warn("`use_amp` has been replaced by `precision` since v0.7.0"
" and this argument will be removed in v0.9.0", DeprecationWarning)
self.precision = 16 if use_amp else 32
assert self.precision in (16, 32), 'only 32 or 16 bit precision supported'
@@ -602,8 +601,8 @@ class Trainer(
Use `training_tqdm_dict` instead. Will remove 0.8.0.
"""
warnings.warn("`tng_tqdm_dic` has renamed to `training_tqdm_dict` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
rank_zero_warn("`tng_tqdm_dic` has renamed to `training_tqdm_dict` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
return self.training_tqdm_dict
# -----------------------------
@@ -937,10 +936,11 @@ class Trainer(
' but have not defined `validation_step()`.')
else:
if not self.is_overriden('validation_epoch_end', model):
warnings.warn('You have defined a `val_dataloader()` and have'
' defined a `validation_step()`, you may also want to'
' define `validation_epoch_end()` for accumulating stats.',
RuntimeWarning)
rank_zero_warn(
'You have defined a `val_dataloader()` and have defined a `validation_step()`,'
' you may also want to define `validation_epoch_end()` for accumulating stats.',
RuntimeWarning
)
else:
if self.is_overriden('validation_step', model):
raise MisconfigurationException('You have defined `validation_step()`,'
@@ -953,10 +953,10 @@ class Trainer(
' but have not defined `test_step()`.')
else:
if not self.is_overriden('test_epoch_end', model):
warnings.warn('You have defined a `test_dataloader()` and'
' have defined a `test_step()`, you may also want to'
' define `test_epoch_end()` for accumulating stats.',
RuntimeWarning)
rank_zero_warn(
'You have defined a `test_dataloader()` and have defined a `test_step()`, you may also want to'
' define `test_epoch_end()` for accumulating stats.', RuntimeWarning
)
else:
if self.is_overriden('test_step', model):
raise MisconfigurationException('You have defined `test_step()`,'
+4 -5
View File
@@ -86,7 +86,6 @@ At a rough level, here's what happens inside Trainer :py:mod:`pytorch_lightning.
import os
import re
import signal
import warnings
from abc import ABC
from argparse import Namespace
from subprocess import call
@@ -102,6 +101,7 @@ from pytorch_lightning.overrides.data_parallel import (
LightningDistributedDataParallel,
LightningDataParallel,
)
from pytorch_lightning.utilities import rank_zero_warn
try:
import torch_xla
@@ -321,9 +321,8 @@ class TrainerIOMixin(ABC):
checkpoint['hparams'] = vars(model.hparams) if is_namespace else model.hparams
checkpoint['hparams_type'] = 'namespace' if is_namespace else 'dict'
else:
warnings.warn(
"Did not find hyperparameters at model.hparams. Saving checkpoint without"
" hyperparameters"
rank_zero_warn(
"Did not find hyperparameters at model.hparams. Saving checkpoint without hyperparameters."
)
# give the model a chance to add a few things
@@ -372,7 +371,7 @@ class TrainerIOMixin(ABC):
n_accum = 1 if self.accumulate_grad_batches is None else self.accumulate_grad_batches
expected_steps = self.num_training_batches / n_accum
if self.num_training_batches != 0 and self.global_step % expected_steps > 1:
warnings.warn(
rank_zero_warn(
"You're resuming from a checkpoint that ended mid-epoch. "
"This can cause unreliable results if further training is done, "
"consider using an end of epoch checkpoint. "
+5 -5
View File
@@ -133,7 +133,6 @@ in your model.
"""
import copy
import warnings
from abc import ABC, abstractmethod
from typing import Callable
from typing import Union, List
@@ -148,6 +147,7 @@ from pytorch_lightning.loggers import LightningLoggerBase
from pytorch_lightning.overrides.data_parallel import LightningDistributedDataParallel, LightningDataParallel
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from pytorch_lightning.trainer.supporters import TensorRunningAccum
from pytorch_lightning.utilities import rank_zero_warn
try:
from apex import amp
@@ -284,8 +284,8 @@ class TrainerTrainLoopMixin(ABC):
"""Warning: this is just empty shell for code implemented in other class."""
def train(self):
warnings.warn('Displayed epoch numbers in the progress bar start from "1" until v0.6.x,'
' but will start from "0" in v0.8.0.', RuntimeWarning)
rank_zero_warn('Displayed epoch numbers in the progress bar start from "1" until v0.6.x,'
' but will start from "0" in v0.8.0.', RuntimeWarning)
# get model
model = self.get_model()
@@ -750,8 +750,8 @@ class TrainerTrainLoopMixin(ABC):
with self.profiler.profile('training_end'):
output = model_ref.training_end(output)
warnings.warn('`training_end` was deprecated in 0.7.0 and will be removed 1.0.0.'
' Use training_epoch_end instead', DeprecationWarning)
rank_zero_warn('`training_end` was deprecated in 0.7.0 and will be removed 1.0.0.'
' Use training_epoch_end instead', DeprecationWarning)
return output
+3
View File
@@ -0,0 +1,3 @@
"""General utilities"""
from pytorch_lightning.utilities.warnings import rank_zero_warn
+18
View File
@@ -0,0 +1,18 @@
"""Custom Lightning warnings"""
import warnings
_proc_rank = 0
def set_proc_rank(value: int) -> None:
"""Set the (sub)process rank."""
global _proc_rank
_proc_rank = value
def rank_zero_warn(*args, **kwargs) -> None:
"""Warning only if (sub)process has rank 0."""
global _proc_rank
if _proc_rank == 0:
warnings.warn(*args, **kwargs)
+1 -2
View File
@@ -1,5 +1,4 @@
import os
import warnings
from argparse import Namespace
import numpy as np
@@ -92,7 +91,7 @@ def run_model_test(trainer_options, model, on_gpu=True):
def get_default_hparams(continue_training=False, hpc_exp_number=0):
tests_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
_ = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
args = {
'drop_prob': 0.2,
+1 -4
View File
@@ -331,14 +331,11 @@ def test_tbptt_cpu_model(tmpdir):
assert result == 1, 'training failed to complete'
@pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires GPU machine")
def test_single_gpu_model(tmpdir):
"""Make sure single GPU works (DP mode)."""
tutils.reset_seed()
if not torch.cuda.is_available():
warnings.warn('test_single_gpu_model cannot run.'
' Rerun on a GPU node to run this test')
return
model, hparams = tutils.get_default_model()
trainer_options = dict(