diff --git a/docs/source/conf.py b/docs/source/conf.py index 4a22f012c99cf..7c0f8d63f1b2a 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -311,6 +311,7 @@ def package_list_from_file(file): 'PyYAML': 'yaml', 'comet-ml': 'comet_ml', 'neptune-client': 'neptune', + 'hydra-core': 'hydra', } MOCK_PACKAGES = [] if SPHINX_MOCK_REQUIREMENTS: diff --git a/pytorch_lightning/accelerators/ddp2_accelerator.py b/pytorch_lightning/accelerators/ddp2_accelerator.py index 142e077cc461c..8f7f58c74684d 100644 --- a/pytorch_lightning/accelerators/ddp2_accelerator.py +++ b/pytorch_lightning/accelerators/ddp2_accelerator.py @@ -21,18 +21,14 @@ from pytorch_lightning.distributed.dist import LightningDistributed from pytorch_lightning import _logger as log from pytorch_lightning.accelerators.accelerator import Accelerator, ReduceOp -from pytorch_lightning.utilities import AMPType +from pytorch_lightning.utilities import AMPType, HYDRA_AVAILABLE from pytorch_lightning.utilities.distributed import rank_zero_only, sync_ddp_if_available from torch.nn.parallel import DistributedDataParallel from typing import List, Optional, Union, Any -try: +if HYDRA_AVAILABLE: from hydra.utils import to_absolute_path, get_original_cwd from hydra.core.hydra_config import HydraConfig -except ImportError: - HYDRA_AVAILABLE = False -else: - HYDRA_AVAILABLE = True class DDP2Accelerator(Accelerator): diff --git a/pytorch_lightning/accelerators/ddp_accelerator.py b/pytorch_lightning/accelerators/ddp_accelerator.py index 9dcc85594efbc..a4904d315fc5d 100644 --- a/pytorch_lightning/accelerators/ddp_accelerator.py +++ b/pytorch_lightning/accelerators/ddp_accelerator.py @@ -26,7 +26,7 @@ from pytorch_lightning.accelerators.accelerator import Accelerator, ReduceOp from pytorch_lightning.core.lightning import LightningModule from pytorch_lightning.distributed.dist import LightningDistributed -from pytorch_lightning.utilities import AMPType +from pytorch_lightning.utilities import AMPType, HYDRA_AVAILABLE from pytorch_lightning.utilities.distributed import find_free_network_port from pytorch_lightning.utilities.distributed import rank_zero_only from pytorch_lightning.utilities.distributed import sync_ddp_if_available @@ -35,13 +35,9 @@ from torch.nn.parallel import DistributedDataParallel -try: +if HYDRA_AVAILABLE: from hydra.utils import to_absolute_path, get_original_cwd from hydra.core.hydra_config import HydraConfig -except ImportError: - HYDRA_AVAILABLE = False -else: - HYDRA_AVAILABLE = True class DDPAccelerator(Accelerator): diff --git a/pytorch_lightning/accelerators/ddp_cpu_hpc_accelerator.py b/pytorch_lightning/accelerators/ddp_cpu_hpc_accelerator.py index 7b43dc9f6b68a..ed94a8fdae220 100644 --- a/pytorch_lightning/accelerators/ddp_cpu_hpc_accelerator.py +++ b/pytorch_lightning/accelerators/ddp_cpu_hpc_accelerator.py @@ -12,15 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License from pytorch_lightning.accelerators.ddp_hpc_accelerator import DDPHPCAccelerator +from pytorch_lightning.utilities import HYDRA_AVAILABLE - -try: +if HYDRA_AVAILABLE: from hydra.utils import to_absolute_path, get_original_cwd from hydra.core.hydra_config import HydraConfig -except ImportError: - HYDRA_AVAILABLE = False -else: - HYDRA_AVAILABLE = True class DDPCPUHPCAccelerator(DDPHPCAccelerator): diff --git a/pytorch_lightning/accelerators/ddp_cpu_spawn_accelerator.py b/pytorch_lightning/accelerators/ddp_cpu_spawn_accelerator.py index 109393dbc770a..feab74bd23f6b 100644 --- a/pytorch_lightning/accelerators/ddp_cpu_spawn_accelerator.py +++ b/pytorch_lightning/accelerators/ddp_cpu_spawn_accelerator.py @@ -23,18 +23,14 @@ from pytorch_lightning import _logger as log from pytorch_lightning.accelerators.accelerator import Accelerator, ReduceOp from pytorch_lightning.core.lightning import LightningModule -from pytorch_lightning.utilities import AMPType +from pytorch_lightning.utilities import AMPType, HYDRA_AVAILABLE from pytorch_lightning.utilities.distributed import rank_zero_only, rank_zero_warn from pytorch_lightning.utilities.distributed import find_free_network_port, sync_ddp_if_available from pytorch_lightning.distributed.dist import LightningDistributed -try: +if HYDRA_AVAILABLE: from hydra.core.hydra_config import HydraConfig from hydra.utils import get_original_cwd, to_absolute_path -except ImportError: - HYDRA_AVAILABLE = False -else: - HYDRA_AVAILABLE = True class DDPCPUSpawnAccelerator(Accelerator): diff --git a/pytorch_lightning/accelerators/ddp_hpc_accelerator.py b/pytorch_lightning/accelerators/ddp_hpc_accelerator.py index ab221f466f54a..96f743bb76d43 100644 --- a/pytorch_lightning/accelerators/ddp_hpc_accelerator.py +++ b/pytorch_lightning/accelerators/ddp_hpc_accelerator.py @@ -23,17 +23,13 @@ from pytorch_lightning.accelerators.accelerator import Accelerator, ReduceOp from pytorch_lightning.core.lightning import LightningModule from pytorch_lightning.distributed.dist import LightningDistributed -from pytorch_lightning.utilities import AMPType +from pytorch_lightning.utilities import AMPType, HYDRA_AVAILABLE from pytorch_lightning.utilities.distributed import rank_zero_only, sync_ddp_if_available -try: +if HYDRA_AVAILABLE: from hydra.utils import to_absolute_path, get_original_cwd from hydra.core.hydra_config import HydraConfig -except ImportError: - HYDRA_AVAILABLE = False -else: - HYDRA_AVAILABLE = True class DDPHPCAccelerator(Accelerator): diff --git a/pytorch_lightning/accelerators/ddp_spawn_accelerator.py b/pytorch_lightning/accelerators/ddp_spawn_accelerator.py index 07051f13d0255..c34fe18cac67f 100644 --- a/pytorch_lightning/accelerators/ddp_spawn_accelerator.py +++ b/pytorch_lightning/accelerators/ddp_spawn_accelerator.py @@ -24,20 +24,16 @@ from pytorch_lightning import _logger as log from pytorch_lightning.accelerators.accelerator import Accelerator, ReduceOp from pytorch_lightning.core.lightning import LightningModule -from pytorch_lightning.utilities import AMPType +from pytorch_lightning.utilities import AMPType, HYDRA_AVAILABLE from pytorch_lightning.utilities.cloud_io import atomic_save, load as pl_load from pytorch_lightning.utilities.distributed import rank_zero_only, rank_zero_warn, find_free_network_port from pytorch_lightning.utilities.distributed import sync_ddp_if_available from pytorch_lightning.utilities.seed import seed_everything from pytorch_lightning.distributed.dist import LightningDistributed -try: +if HYDRA_AVAILABLE: from hydra.core.hydra_config import HydraConfig from hydra.utils import get_original_cwd, to_absolute_path -except ImportError: - HYDRA_AVAILABLE = False -else: - HYDRA_AVAILABLE = True class DDPSpawnAccelerator(Accelerator): diff --git a/pytorch_lightning/core/saving.py b/pytorch_lightning/core/saving.py index 53210009db9ed..6741236a7e5f5 100644 --- a/pytorch_lightning/core/saving.py +++ b/pytorch_lightning/core/saving.py @@ -20,22 +20,20 @@ from typing import Union, Dict, Any, Optional, Callable, MutableMapping, IO from warnings import warn -import fsspec import torch import yaml from pytorch_lightning import _logger as log -from pytorch_lightning.utilities import rank_zero_warn, AttributeDict +from pytorch_lightning.utilities import rank_zero_warn, AttributeDict, OMEGACONF_AVAILABLE from pytorch_lightning.utilities.cloud_io import load as pl_load from pytorch_lightning.utilities.cloud_io import get_filesystem from pytorch_lightning.utilities.parsing import parse_class_init_keys PRIMITIVE_TYPES = (bool, int, float, str) ALLOWED_CONFIG_TYPES = (AttributeDict, MutableMapping, Namespace) -try: + +if OMEGACONF_AVAILABLE: from omegaconf import OmegaConf -except ImportError: - OmegaConf = None # the older shall be on the top CHECKPOINT_PAST_HPARAMS_KEYS = ( @@ -362,7 +360,7 @@ def save_hparams_to_yaml(config_yaml, hparams: Union[dict, Namespace]) -> None: hparams = dict(hparams) # saving with OmegaConf objects - if OmegaConf is not None: + if OMEGACONF_AVAILABLE: if OmegaConf.is_config(hparams): with fs.open(config_yaml, "w", encoding="utf-8") as fp: OmegaConf.save(hparams, fp, resolve=True) diff --git a/pytorch_lightning/loggers/tensorboard.py b/pytorch_lightning/loggers/tensorboard.py index 1e861ed25ab95..afdb98cb978de 100644 --- a/pytorch_lightning/loggers/tensorboard.py +++ b/pytorch_lightning/loggers/tensorboard.py @@ -20,7 +20,6 @@ import os from argparse import Namespace from typing import Any, Dict, Optional, Union -from warnings import warn import torch from torch.utils.tensorboard import SummaryWriter @@ -30,16 +29,11 @@ from pytorch_lightning.core.lightning import LightningModule from pytorch_lightning.core.saving import save_hparams_to_yaml from pytorch_lightning.loggers.base import LightningLoggerBase, rank_zero_experiment -from pytorch_lightning.utilities import rank_zero_only, rank_zero_warn +from pytorch_lightning.utilities import rank_zero_only, rank_zero_warn, OMEGACONF_AVAILABLE from pytorch_lightning.utilities.cloud_io import get_filesystem -from pytorch_lightning.utilities.exceptions import MisconfigurationException -try: +if OMEGACONF_AVAILABLE: from omegaconf import Container, OmegaConf -except ImportError: - OMEGACONF_AVAILABLE = False -else: - OMEGACONF_AVAILABLE = True class TensorBoardLogger(LightningLoggerBase): diff --git a/pytorch_lightning/trainer/connectors/checkpoint_connector.py b/pytorch_lightning/trainer/connectors/checkpoint_connector.py index c64f65e25b07c..2177ce530aa5c 100644 --- a/pytorch_lightning/trainer/connectors/checkpoint_connector.py +++ b/pytorch_lightning/trainer/connectors/checkpoint_connector.py @@ -12,20 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -import io import os import re -import signal -from abc import ABC -from subprocess import call import torch -import torch.distributed as torch_distrib import pytorch_lightning from pytorch_lightning import _logger as log from pytorch_lightning.core.lightning import LightningModule -from pytorch_lightning.utilities import AMPType, rank_zero_warn, APEX_AVAILABLE +from pytorch_lightning.utilities import APEX_AVAILABLE, AMPType, OMEGACONF_AVAILABLE, rank_zero_warn from pytorch_lightning.utilities.cloud_io import atomic_save, get_filesystem from pytorch_lightning.utilities.cloud_io import load as pl_load from pytorch_lightning.utilities.upgrade_checkpoint import KEYS_MAPPING as DEPRECATED_CHECKPOINT_KEYS @@ -34,12 +29,8 @@ if APEX_AVAILABLE: from apex import amp -try: +if OMEGACONF_AVAILABLE: from omegaconf import Container -except ImportError: - OMEGACONF_AVAILABLE = False -else: - OMEGACONF_AVAILABLE = True class CheckpointConnector: diff --git a/pytorch_lightning/utilities/__init__.py b/pytorch_lightning/utilities/__init__.py index 3b4dcfc7061ff..be229ac1dfea3 100644 --- a/pytorch_lightning/utilities/__init__.py +++ b/pytorch_lightning/utilities/__init__.py @@ -44,6 +44,8 @@ def _module_available(module_path: str) -> bool: APEX_AVAILABLE = _module_available("apex.amp") NATIVE_AMP_AVAILABLE = _module_available("torch.cuda.amp") and hasattr(torch.cuda.amp, "autocast") +OMEGACONF_AVAILABLE = _module_available("omegaconf") +HYDRA_AVAILABLE = _module_available("hydra") TPU_AVAILABLE = XLADeviceUtils.tpu_device_exists() diff --git a/requirements/extra.txt b/requirements/extra.txt index be21317a1d826..b5cbde0c15485 100644 --- a/requirements/extra.txt +++ b/requirements/extra.txt @@ -8,3 +8,4 @@ scikit-learn>=0.22.2 torchtext>=0.3.1, <0.7 # TODO: temporary fix fix for compatibility onnx>=1.7.0 onnxruntime>=1.3.0 +hydra-core>=1.0