2021-03-01 12:17:09 +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-03-02 18:57:13 +00:00
|
|
|
import os
|
2021-03-02 09:36:01 +00:00
|
|
|
import sys
|
2021-03-01 12:17:09 +00:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import torch
|
2021-04-22 18:25:51 +00:00
|
|
|
from packaging.version import Version
|
2021-03-01 12:17:09 +00:00
|
|
|
from pkg_resources import get_distribution
|
|
|
|
|
2021-03-02 16:21:20 +00:00
|
|
|
from pytorch_lightning.utilities import (
|
|
|
|
_APEX_AVAILABLE,
|
2021-03-02 19:45:13 +00:00
|
|
|
_DEEPSPEED_AVAILABLE,
|
|
|
|
_FAIRSCALE_AVAILABLE,
|
|
|
|
_FAIRSCALE_PIPE_AVAILABLE,
|
2021-03-02 16:21:20 +00:00
|
|
|
_HOROVOD_AVAILABLE,
|
|
|
|
_NATIVE_AMP_AVAILABLE,
|
2021-03-02 18:57:13 +00:00
|
|
|
_RPC_AVAILABLE,
|
2021-03-02 16:21:20 +00:00
|
|
|
_TORCH_QUANTIZE_AVAILABLE,
|
|
|
|
_TPU_AVAILABLE,
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
from horovod.common.util import nccl_built
|
|
|
|
nccl_built()
|
|
|
|
except (ImportError, ModuleNotFoundError, AttributeError):
|
|
|
|
_HOROVOD_NCCL_AVAILABLE = False
|
|
|
|
finally:
|
|
|
|
_HOROVOD_NCCL_AVAILABLE = True
|
2021-03-01 12:17:09 +00:00
|
|
|
|
|
|
|
|
2021-03-02 08:03:32 +00:00
|
|
|
class RunIf:
|
2021-03-01 15:26:09 +00:00
|
|
|
"""
|
2021-03-02 08:03:32 +00:00
|
|
|
RunIf wrapper for simple marking specific cases, fully compatible with pytest.mark::
|
2021-03-01 12:17:09 +00:00
|
|
|
|
2021-03-02 08:03:32 +00:00
|
|
|
@RunIf(min_torch="0.0")
|
2021-03-01 15:26:09 +00:00
|
|
|
@pytest.mark.parametrize("arg1", [1, 2.0])
|
|
|
|
def test_wrapper(arg1):
|
|
|
|
assert arg1 > 0.0
|
2021-03-01 12:17:09 +00:00
|
|
|
"""
|
|
|
|
|
2021-03-02 09:36:01 +00:00
|
|
|
def __new__(
|
|
|
|
self,
|
|
|
|
*args,
|
|
|
|
min_gpus: int = 0,
|
|
|
|
min_torch: Optional[str] = None,
|
2021-03-23 20:43:21 +00:00
|
|
|
max_torch: Optional[str] = None,
|
2021-03-02 16:21:20 +00:00
|
|
|
min_python: Optional[str] = None,
|
2021-03-02 09:36:01 +00:00
|
|
|
quantization: bool = False,
|
2021-03-02 12:43:53 +00:00
|
|
|
amp_apex: bool = False,
|
|
|
|
amp_native: bool = False,
|
2021-03-02 16:21:20 +00:00
|
|
|
tpu: bool = False,
|
|
|
|
horovod: bool = False,
|
|
|
|
horovod_nccl: bool = False,
|
2021-03-02 09:36:01 +00:00
|
|
|
skip_windows: bool = False,
|
2021-03-02 18:57:13 +00:00
|
|
|
special: bool = False,
|
|
|
|
rpc: bool = False,
|
2021-03-02 19:45:13 +00:00
|
|
|
fairscale: bool = False,
|
|
|
|
fairscale_pipe: bool = False,
|
|
|
|
deepspeed: bool = False,
|
2021-03-02 09:36:01 +00:00
|
|
|
**kwargs
|
|
|
|
):
|
2021-03-01 15:26:09 +00:00
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
args: native pytest.mark.skipif arguments
|
|
|
|
min_gpus: min number of gpus required to run test
|
|
|
|
min_torch: minimum pytorch version to run test
|
2021-03-23 20:43:21 +00:00
|
|
|
max_torch: maximum pytorch version to run test
|
2021-03-02 16:21:20 +00:00
|
|
|
min_python: minimum python version required to run test
|
2021-03-01 15:26:09 +00:00
|
|
|
quantization: if `torch.quantization` package is required to run test
|
2021-03-02 12:43:53 +00:00
|
|
|
amp_apex: NVIDIA Apex is installed
|
|
|
|
amp_native: if native PyTorch native AMP is supported
|
2021-03-02 16:21:20 +00:00
|
|
|
tpu: if TPU is available
|
|
|
|
horovod: if Horovod is installed
|
|
|
|
horovod_nccl: if Horovod is installed with NCCL support
|
2021-03-02 09:36:01 +00:00
|
|
|
skip_windows: skip test for Windows platform (typically fo some limited torch functionality)
|
2021-03-02 18:57:13 +00:00
|
|
|
special: running in special mode, outside pytest suit
|
|
|
|
rpc: requires Remote Procedure Call (RPC)
|
2021-03-02 19:45:13 +00:00
|
|
|
fairscale: if `fairscale` module is required to run the test
|
|
|
|
deepspeed: if `deepspeed` module is required to run the test
|
2021-03-01 15:26:09 +00:00
|
|
|
kwargs: native pytest.mark.skipif keyword arguments
|
|
|
|
"""
|
|
|
|
conditions = []
|
|
|
|
reasons = []
|
2021-03-01 12:17:09 +00:00
|
|
|
|
2021-03-01 15:26:09 +00:00
|
|
|
if min_gpus:
|
|
|
|
conditions.append(torch.cuda.device_count() < min_gpus)
|
|
|
|
reasons.append(f"GPUs>={min_gpus}")
|
2021-03-01 12:17:09 +00:00
|
|
|
|
2021-03-01 15:26:09 +00:00
|
|
|
if min_torch:
|
2021-04-22 18:25:51 +00:00
|
|
|
torch_version = get_distribution("torch").version
|
|
|
|
conditions.append(Version(torch_version) < Version(min_torch))
|
2021-03-01 15:26:09 +00:00
|
|
|
reasons.append(f"torch>={min_torch}")
|
2021-03-01 12:17:09 +00:00
|
|
|
|
2021-03-23 20:43:21 +00:00
|
|
|
if max_torch:
|
2021-04-22 18:25:51 +00:00
|
|
|
torch_version = get_distribution("torch").version
|
|
|
|
conditions.append(Version(torch_version) >= Version(max_torch))
|
2021-03-23 20:43:21 +00:00
|
|
|
reasons.append(f"torch<{max_torch}")
|
|
|
|
|
2021-03-02 16:21:20 +00:00
|
|
|
if min_python:
|
|
|
|
py_version = f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}"
|
2021-04-22 18:25:51 +00:00
|
|
|
conditions.append(Version(py_version) < Version(min_python))
|
2021-03-02 16:21:20 +00:00
|
|
|
reasons.append(f"python>={min_python}")
|
|
|
|
|
2021-03-01 15:26:09 +00:00
|
|
|
if quantization:
|
|
|
|
_miss_default = 'fbgemm' not in torch.backends.quantized.supported_engines
|
|
|
|
conditions.append(not _TORCH_QUANTIZE_AVAILABLE or _miss_default)
|
2021-03-02 16:21:20 +00:00
|
|
|
reasons.append("PyTorch quantization")
|
2021-03-01 12:17:09 +00:00
|
|
|
|
2021-03-02 12:43:53 +00:00
|
|
|
if amp_native:
|
|
|
|
conditions.append(not _NATIVE_AMP_AVAILABLE)
|
2021-03-02 16:21:20 +00:00
|
|
|
reasons.append("native AMP")
|
2021-03-02 12:43:53 +00:00
|
|
|
|
|
|
|
if amp_apex:
|
|
|
|
conditions.append(not _APEX_AVAILABLE)
|
2021-03-02 16:21:20 +00:00
|
|
|
reasons.append("NVIDIA Apex")
|
2021-03-02 12:43:53 +00:00
|
|
|
|
2021-03-02 09:36:01 +00:00
|
|
|
if skip_windows:
|
|
|
|
conditions.append(sys.platform == "win32")
|
|
|
|
reasons.append("unimplemented on Windows")
|
|
|
|
|
2021-03-02 16:21:20 +00:00
|
|
|
if tpu:
|
|
|
|
conditions.append(not _TPU_AVAILABLE)
|
|
|
|
reasons.append("TPU")
|
|
|
|
|
|
|
|
if horovod:
|
|
|
|
conditions.append(not _HOROVOD_AVAILABLE)
|
|
|
|
reasons.append("Horovod")
|
|
|
|
|
|
|
|
if horovod_nccl:
|
|
|
|
conditions.append(not _HOROVOD_NCCL_AVAILABLE)
|
|
|
|
reasons.append("Horovod with NCCL")
|
|
|
|
|
2021-03-02 18:57:13 +00:00
|
|
|
if special:
|
|
|
|
env_flag = os.getenv("PL_RUNNING_SPECIAL_TESTS", '0')
|
|
|
|
conditions.append(env_flag != '1')
|
|
|
|
reasons.append("Special execution")
|
|
|
|
|
|
|
|
if rpc:
|
|
|
|
conditions.append(not _RPC_AVAILABLE)
|
|
|
|
reasons.append("RPC")
|
|
|
|
|
2021-03-02 19:45:13 +00:00
|
|
|
if fairscale:
|
|
|
|
conditions.append(not _FAIRSCALE_AVAILABLE)
|
|
|
|
reasons.append("Fairscale")
|
|
|
|
|
|
|
|
if fairscale_pipe:
|
|
|
|
conditions.append(not _FAIRSCALE_PIPE_AVAILABLE)
|
|
|
|
reasons.append("Fairscale Pipe")
|
|
|
|
|
|
|
|
if deepspeed:
|
|
|
|
conditions.append(not _DEEPSPEED_AVAILABLE)
|
|
|
|
reasons.append("Deepspeed")
|
|
|
|
|
2021-03-01 15:26:09 +00:00
|
|
|
reasons = [rs for cond, rs in zip(conditions, reasons) if cond]
|
|
|
|
return pytest.mark.skipif(
|
|
|
|
*args,
|
|
|
|
condition=any(conditions),
|
|
|
|
reason=f"Requires: [{' + '.join(reasons)}]",
|
|
|
|
**kwargs,
|
|
|
|
)
|
2021-03-01 12:17:09 +00:00
|
|
|
|
|
|
|
|
2021-03-02 08:03:32 +00:00
|
|
|
@RunIf(min_torch="99")
|
2021-03-01 12:17:09 +00:00
|
|
|
def test_always_skip():
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
|
2021-03-01 15:26:09 +00:00
|
|
|
@pytest.mark.parametrize("arg1", [0.5, 1.0, 2.0])
|
2021-03-02 08:03:32 +00:00
|
|
|
@RunIf(min_torch="0.0")
|
2021-03-09 11:27:15 +00:00
|
|
|
def test_wrapper(arg1: float):
|
2021-03-01 15:26:09 +00:00
|
|
|
assert arg1 > 0.0
|