Skip to content

Commit

Permalink
flake8 ++
Browse files Browse the repository at this point in the history
  • Loading branch information
Borda committed Dec 23, 2020
1 parent 68ce6fd commit f15dca2
Show file tree
Hide file tree
Showing 21 changed files with 59 additions and 66 deletions.
6 changes: 0 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Support number for logging with `sync_dist=True` ([#5080](https://github.com/PyTorchLightning/pytorch-lightning/pull/5080)
- Added offset logging step when resuming for Wandb logger ([#5050](https://github.com/PyTorchLightning/pytorch-lightning/pull/5050)

### Changed


### Deprecated


### Removed

- `enable_pl_optimizer=False` by default to temporarily fix AMP issues ([#5163](https://github.com/PyTorchLightning/pytorch-lightning/pull/5163)
Expand Down
10 changes: 5 additions & 5 deletions pl_examples/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

from pytorch_lightning.utilities import _module_available

EXAMPLES_ROOT = os.path.dirname(__file__)
PACKAGE_ROOT = os.path.dirname(EXAMPLES_ROOT)
DATASETS_PATH = os.path.join(PACKAGE_ROOT, 'Datasets')
_EXAMPLES_ROOT = os.path.dirname(__file__)
_PACKAGE_ROOT = os.path.dirname(_EXAMPLES_ROOT)
_DATASETS_PATH = os.path.join(_PACKAGE_ROOT, 'Datasets')

TORCHVISION_AVAILABLE = _module_available("torchvision")
DALI_AVAILABLE = _module_available("nvidia.dali")
_TORCHVISION_AVAILABLE = _module_available("torchvision")
_DALI_AVAILABLE = _module_available("nvidia.dali")


LIGHTNING_LOGO = """
Expand Down
4 changes: 2 additions & 2 deletions pl_examples/basic_examples/autoencoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
from torch.utils.data import random_split

import pytorch_lightning as pl
from pl_examples import TORCHVISION_AVAILABLE, cli_lightning_logo
from pl_examples import _TORCHVISION_AVAILABLE, cli_lightning_logo

if TORCHVISION_AVAILABLE:
if _TORCHVISION_AVAILABLE:
from torchvision.datasets.mnist import MNIST
from torchvision import transforms
else:
Expand Down
8 changes: 4 additions & 4 deletions pl_examples/basic_examples/backbone_image_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
from torch.utils.data import DataLoader, random_split

import pytorch_lightning as pl
from pl_examples import DATASETS_PATH, TORCHVISION_AVAILABLE, cli_lightning_logo
from pl_examples import _DATASETS_PATH, _TORCHVISION_AVAILABLE, cli_lightning_logo

if TORCHVISION_AVAILABLE:
if _TORCHVISION_AVAILABLE:
from torchvision.datasets.mnist import MNIST
from torchvision import transforms
else:
Expand Down Expand Up @@ -111,8 +111,8 @@ def cli_main():
# ------------
# data
# ------------
dataset = MNIST(DATASETS_PATH, train=True, download=True, transform=transforms.ToTensor())
mnist_test = MNIST(DATASETS_PATH, train=False, download=True, transform=transforms.ToTensor())
dataset = MNIST(_DATASETS_PATH, train=True, download=True, transform=transforms.ToTensor())
mnist_test = MNIST(_DATASETS_PATH, train=False, download=True, transform=transforms.ToTensor())
mnist_train, mnist_val = random_split(dataset, [55000, 5000])

train_loader = DataLoader(mnist_train, batch_size=args.batch_size)
Expand Down
8 changes: 4 additions & 4 deletions pl_examples/basic_examples/dali_image_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
from torch.utils.data import random_split

import pytorch_lightning as pl
from pl_examples import TORCHVISION_AVAILABLE, DALI_AVAILABLE, cli_lightning_logo
from pl_examples import _TORCHVISION_AVAILABLE, _DALI_AVAILABLE, cli_lightning_logo

if TORCHVISION_AVAILABLE:
if _TORCHVISION_AVAILABLE:
from torchvision.datasets.mnist import MNIST
from torchvision import transforms
else:
from tests.base.datasets import MNIST

if DALI_AVAILABLE:
if _DALI_AVAILABLE:
from nvidia.dali import ops
from nvidia.dali.pipeline import Pipeline
from nvidia.dali.plugin.pytorch import DALIClassificationIterator
Expand Down Expand Up @@ -166,7 +166,7 @@ def add_model_specific_args(parent_parser):


def cli_main():
if not DALI_AVAILABLE:
if not _DALI_AVAILABLE:
return

pl.seed_everything(1234)
Expand Down
8 changes: 4 additions & 4 deletions pl_examples/basic_examples/mnist_datamodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

from torch.utils.data import DataLoader, random_split

from pl_examples import DATASETS_PATH, TORCHVISION_AVAILABLE
from pl_examples import _DATASETS_PATH, _TORCHVISION_AVAILABLE
from pytorch_lightning import LightningDataModule

if TORCHVISION_AVAILABLE:
if _TORCHVISION_AVAILABLE:
from torchvision import transforms as transform_lib
from torchvision.datasets import MNIST
else:
Expand All @@ -38,7 +38,7 @@ class MNISTDataModule(LightningDataModule):

def __init__(
self,
data_dir: str = DATASETS_PATH,
data_dir: str = _DATASETS_PATH,
val_split: int = 5000,
num_workers: int = 16,
normalize: bool = False,
Expand Down Expand Up @@ -123,7 +123,7 @@ def test_dataloader(self):

@property
def default_transforms(self):
if not TORCHVISION_AVAILABLE:
if not _TORCHVISION_AVAILABLE:
return None
if self.normalize:
mnist_transforms = transform_lib.Compose(
Expand Down
8 changes: 4 additions & 4 deletions pl_examples/domain_templates/semantic_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ class KITTI(Dataset):
encoded using `encode_segmap`, and given `transform` (if any) are applied to the image only
(mask does not usually require transforms, but they can be implemented in a similar way).
>>> from pl_examples import DATASETS_PATH
>>> dataset_path = os.path.join(DATASETS_PATH, "Kitti")
>>> from pl_examples import _DATASETS_PATH
>>> dataset_path = os.path.join(_DATASETS_PATH, "Kitti")
>>> _create_synth_kitti_dataset(dataset_path, image_dims=(1024, 512))
>>> KITTI(dataset_path, 'train') # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
<...semantic_segmentation.KITTI object at ...>
Expand Down Expand Up @@ -161,8 +161,8 @@ class SegModel(pl.LightningModule):
Adam optimizer is used along with Cosine Annealing learning rate scheduler.
>>> from pl_examples import DATASETS_PATH
>>> dataset_path = os.path.join(DATASETS_PATH, "Kitti")
>>> from pl_examples import _DATASETS_PATH
>>> dataset_path = os.path.join(_DATASETS_PATH, "Kitti")
>>> _create_synth_kitti_dataset(dataset_path, image_dims=(1024, 512))
>>> SegModel(dataset_path) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
SegModel(
Expand Down
4 changes: 2 additions & 2 deletions pl_examples/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import pytest
import torch

from pl_examples import DALI_AVAILABLE
from pl_examples import _DALI_AVAILABLE

ARGS_DEFAULT = """
--default_root_dir %(tmpdir)s \
Expand Down Expand Up @@ -104,7 +104,7 @@ def test_examples_cpu(tmpdir, import_cli, cli_args):
module.cli_main()


@pytest.mark.skipif(not DALI_AVAILABLE, reason="Nvidia DALI required")
@pytest.mark.skipif(not _DALI_AVAILABLE, reason="Nvidia DALI required")
@pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires GPU machine")
@pytest.mark.skipif(platform.system() != 'Linux', reason='Only applies to Linux platform.')
@pytest.mark.parametrize('cli_args', [ARGS_GPU])
Expand Down
4 changes: 2 additions & 2 deletions pytorch_lightning/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
_logger.addHandler(python_logging.StreamHandler())
_logger.setLevel(python_logging.INFO)

PACKAGE_ROOT = os.path.dirname(__file__)
PROJECT_ROOT = os.path.dirname(PACKAGE_ROOT)
_PACKAGE_ROOT = os.path.dirname(__file__)
_PROJECT_ROOT = os.path.dirname(_PACKAGE_ROOT)

try:
# This variable is injected in the __builtins__ by the build
Expand Down
4 changes: 3 additions & 1 deletion pytorch_lightning/loggers/wandb.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ def log_metrics(self, metrics: Dict[str, float], step: Optional[int] = None) ->

metrics = self._add_prefix(metrics)
if step is not None and step + self._step_offset < self.experiment.step:
self.warning_cache.warn('Trying to log at a previous step. Use `commit=False` when logging metrics manually.')
self.warning_cache.warn(
'Trying to log at a previous step. Use `commit=False` when logging metrics manually.'
)
self.experiment.log(metrics, step=(step + self._step_offset) if step is not None else None)

@property
Expand Down
6 changes: 3 additions & 3 deletions pytorch_lightning/setup_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from urllib.request import Request, urlopen
import warnings

from pytorch_lightning import __homepage__, __version__, PROJECT_ROOT
from pytorch_lightning import __homepage__, __version__, _PROJECT_ROOT

_PATH_BADGES = os.path.join('.', 'docs', 'source', '_images', 'badges')
# badge to download
Expand All @@ -37,7 +37,7 @@
def _load_requirements(path_dir: str , file_name: str = 'requirements.txt', comment_char: str = '#') -> List[str]:
"""Load requirements from a file
>>> _load_requirements(PROJECT_ROOT) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
>>> _load_requirements(_PROJECT_ROOT) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
['numpy...', 'torch...', ...]
"""
with open(os.path.join(path_dir, file_name), 'r') as file:
Expand Down Expand Up @@ -155,7 +155,7 @@ def _download_badge(url_badge: str, badge_name: str, target_dir: str) -> str:
def _load_long_description(path_dir: str) -> str:
"""Load readme as decribtion
>>> _load_long_description(PROJECT_ROOT) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
>>> _load_long_description(_PROJECT_ROOT) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
'<div align="center">...'
"""
path_readme = os.path.join(path_dir, "README.md")
Expand Down
6 changes: 3 additions & 3 deletions pytorch_lightning/utilities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import numpy
import torch

from pytorch_lightning.utilities.apply_func import move_data_to_device # noqa: F401
from pytorch_lightning.utilities.apply_func import _TORCHTEXT_AVAILABLE, move_data_to_device # noqa: F401
from pytorch_lightning.utilities.distributed import ( # noqa: F401
AllGatherGrad,
rank_zero_info,
Expand Down Expand Up @@ -59,14 +59,14 @@ def _module_available(module_path: str) -> bool:
_OMEGACONF_AVAILABLE = _module_available("omegaconf")
_HYDRA_AVAILABLE = _module_available("hydra")
_HOROVOD_AVAILABLE = _module_available("horovod.torch")

_TPU_AVAILABLE = XLADeviceUtils.tpu_device_exists()
_FAIRSCALE_AVAILABLE = platform.system() != 'Windows' and _module_available('fairscale.nn.data_parallel')
_RPC_AVAILABLE = platform.system() != 'Windows' and _module_available('torch.distributed.rpc')
_GROUP_AVAILABLE = platform.system() != 'Windows' and _module_available('torch.distributed.group')
_FAIRSCALE_PIPE_AVAILABLE = _FAIRSCALE_AVAILABLE and LooseVersion(torch.__version__) >= LooseVersion("1.6.0")
_BOLTS_AVAILABLE = _module_available('pl_bolts')

_TPU_AVAILABLE = XLADeviceUtils.tpu_device_exists()

FLOAT16_EPSILON = numpy.finfo(numpy.float16).eps
FLOAT32_EPSILON = numpy.finfo(numpy.float32).eps
FLOAT64_EPSILON = numpy.finfo(numpy.float64).eps
Expand Down
10 changes: 5 additions & 5 deletions pytorch_lightning/utilities/apply_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# 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.

import importlib
from abc import ABC
from collections.abc import Mapping, Sequence
Expand All @@ -20,8 +19,9 @@

import torch

TORCHTEXT_AVAILABLE = importlib.util.find_spec("torchtext") is not None
if TORCHTEXT_AVAILABLE:
_TORCHTEXT_AVAILABLE = importlib.util.find_spec("torchtext") is not None

if _TORCHTEXT_AVAILABLE:
from torchtext.data import Batch
else:
Batch = type(None)
Expand Down Expand Up @@ -109,7 +109,7 @@ def move_data_to_device(batch: Any, device: torch.device):

def batch_to(data):
# try to move torchtext data first
if TORCHTEXT_AVAILABLE and isinstance(data, Batch):
if _TORCHTEXT_AVAILABLE and isinstance(data, Batch):

# Shallow copy because each Batch has a reference to Dataset which contains all examples
device_data = copy(data)
Expand All @@ -123,5 +123,5 @@ def batch_to(data):
kwargs = dict(non_blocking=True) if isinstance(data, torch.Tensor) else {}
return data.to(device, **kwargs)

dtype = (TransferableDataType, Batch) if TORCHTEXT_AVAILABLE else TransferableDataType
dtype = (TransferableDataType, Batch) if _TORCHTEXT_AVAILABLE else TransferableDataType
return apply_to_collection(batch, dtype=dtype, function=batch_to)
14 changes: 7 additions & 7 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@

import numpy as np

TEST_ROOT = os.path.dirname(__file__)
PROJECT_ROOT = os.path.dirname(TEST_ROOT)
TEMP_PATH = os.path.join(PROJECT_ROOT, 'test_temp')
_TEST_ROOT = os.path.dirname(__file__)
_PROJECT_ROOT = os.path.dirname(_TEST_ROOT)
_TEMP_PATH = os.path.join(_PROJECT_ROOT, 'test_temp')

# todo: this setting `PYTHONPATH` may not be used by other evns like Conda for import packages
if PROJECT_ROOT not in os.getenv('PYTHONPATH', ""):
if _PROJECT_ROOT not in os.getenv('PYTHONPATH', ""):
splitter = ":" if os.environ.get("PYTHONPATH", "") else ""
os.environ['PYTHONPATH'] = f'{PROJECT_ROOT}{splitter}{os.environ.get("PYTHONPATH", "")}'
os.environ['PYTHONPATH'] = f'{_PROJECT_ROOT}{splitter}{os.environ.get("PYTHONPATH", "")}'

# generate a list of random seeds for each test
RANDOM_PORTS = list(np.random.randint(12000, 19000, 1000))

if not os.path.isdir(TEMP_PATH):
os.mkdir(TEMP_PATH)
if not os.path.isdir(_TEMP_PATH):
os.mkdir(_TEMP_PATH)
4 changes: 2 additions & 2 deletions tests/base/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
from torch import Tensor
from torch.utils.data import Dataset

from tests import PROJECT_ROOT
from tests import _PROJECT_ROOT

#: local path to test datasets
PATH_DATASETS = os.path.join(PROJECT_ROOT, 'Datasets')
PATH_DATASETS = os.path.join(_PROJECT_ROOT, 'Datasets')


class MNIST(Dataset):
Expand Down
4 changes: 2 additions & 2 deletions tests/base/develop_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pytorch_lightning import seed_everything
from pytorch_lightning.callbacks import ModelCheckpoint
from pytorch_lightning.loggers import TensorBoardLogger, TestTubeLogger
from tests import TEMP_PATH, RANDOM_PORTS
from tests import _TEMP_PATH, RANDOM_PORTS
from tests.base.model_template import EvalModelTemplate


Expand Down Expand Up @@ -63,7 +63,7 @@ def get_data_path(expt_logger, path_dir=None):
if hasattr(expt_logger, 'save_dir') and expt_logger.save_dir:
path_dir = expt_logger.save_dir
else:
path_dir = TEMP_PATH
path_dir = _TEMP_PATH
path_expt = os.path.join(path_dir, name, 'version_%s' % version)

# try if the new sub-folder exists, typical case for test-tube
Expand Down
1 change: 0 additions & 1 deletion tests/deprecated_api/test_remove_1-3.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from pytorch_lightning import LightningModule, Trainer
from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint
from pytorch_lightning.profiler.profilers import PassThroughProfiler, SimpleProfiler
from tests.deprecated_api import _soft_unimport_module


def test_v1_3_0_deprecated_arguments(tmpdir):
Expand Down
2 changes: 1 addition & 1 deletion tests/loggers/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def test_logger_with_prefix_all(tmpdir, monkeypatch):
logger.experiment.log.assert_called_once_with({"tmp-test": 1.0}, global_step=0)

# WandB
with mock.patch('pytorch_lightning.loggers.wandb.wandb'):
with mock.patch('pytorch_lightning.loggers.wandb.wandb') as wandb:
logger = _instantiate_logger(WandbLogger, save_idr=tmpdir, prefix=prefix)
wandb.run = None
wandb.init().step = 0
Expand Down
12 changes: 6 additions & 6 deletions tests/models/test_horovod.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
from horovod.common.util import nccl_built
nccl_built()
except (ImportError, ModuleNotFoundError, AttributeError):
HOROVOD_NCCL_AVAILABLE = False
_HOROVOD_NCCL_AVAILABLE = False
finally:
HOROVOD_NCCL_AVAILABLE = True
_HOROVOD_NCCL_AVAILABLE = True


def _run_horovod(trainer_options, on_gpu=False):
Expand Down Expand Up @@ -105,7 +105,7 @@ def test_horovod_cpu_implicit(enable_pl_optimizer, tmpdir):


@pytest.mark.skipif(platform.system() == "Windows", reason="Horovod is not supported on Windows")
@pytest.mark.skipif(not HOROVOD_NCCL_AVAILABLE, reason="test requires Horovod with NCCL support")
@pytest.mark.skipif(not _HOROVOD_NCCL_AVAILABLE, reason="test requires Horovod with NCCL support")
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine")
def test_horovod_multi_gpu(tmpdir):
"""Test Horovod with multi-GPU support."""
Expand All @@ -125,7 +125,7 @@ def test_horovod_multi_gpu(tmpdir):


@pytest.mark.skipif(platform.system() == "Windows", reason="Horovod is not supported on Windows")
@pytest.mark.skipif(not HOROVOD_NCCL_AVAILABLE, reason="test requires Horovod with NCCL support")
@pytest.mark.skipif(not _HOROVOD_NCCL_AVAILABLE, reason="test requires Horovod with NCCL support")
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine")
@pytest.mark.skipif(not _APEX_AVAILABLE, reason="test requires apex")
def test_horovod_apex(tmpdir):
Expand All @@ -149,7 +149,7 @@ def test_horovod_apex(tmpdir):

@pytest.mark.skip(reason="Skip till Horovod fixes integration with Native torch.cuda.amp")
@pytest.mark.skipif(platform.system() == "Windows", reason="Horovod is not supported on Windows")
@pytest.mark.skipif(not HOROVOD_NCCL_AVAILABLE, reason="test requires Horovod with NCCL support")
@pytest.mark.skipif(not _HOROVOD_NCCL_AVAILABLE, reason="test requires Horovod with NCCL support")
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine")
@pytest.mark.skipif(not _NATIVE_AMP_AVAILABLE, reason="test requires torch.cuda.amp")
def test_horovod_amp(tmpdir):
Expand All @@ -172,7 +172,7 @@ def test_horovod_amp(tmpdir):


@pytest.mark.skipif(platform.system() == "Windows", reason="Horovod is not supported on Windows")
@pytest.mark.skipif(not HOROVOD_NCCL_AVAILABLE, reason="test requires Horovod with NCCL support")
@pytest.mark.skipif(not _HOROVOD_NCCL_AVAILABLE, reason="test requires Horovod with NCCL support")
@pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires GPU machine")
def test_horovod_transfer_batch_to_gpu(tmpdir):
class TestTrainingStepModel(EvalModelTemplate):
Expand Down
Loading

0 comments on commit f15dca2

Please sign in to comment.