From 2ca53564296f8bc4a4ac57113709a93b3342e741 Mon Sep 17 00:00:00 2001 From: Jirka Borovec Date: Tue, 31 Mar 2020 00:29:23 +0200 Subject: [PATCH] clear skipping tests (#1285) * clear skipping tests * fix simple/multi GPU * review: simplify --- tests/base/utils.py | 12 ------------ tests/models/test_amp.py | 21 ++++++--------------- tests/models/test_cpu.py | 4 +--- tests/models/test_gpu.py | 17 +++++------------ tests/models/test_restore.py | 10 +++------- 5 files changed, 15 insertions(+), 49 deletions(-) diff --git a/tests/base/utils.py b/tests/base/utils.py index c6b8e3ce..ee206d67 100644 --- a/tests/base/utils.py +++ b/tests/base/utils.py @@ -210,18 +210,6 @@ def assert_ok_model_acc(trainer, key='test_acc', thr=0.4): assert acc > thr, f"Model failed to get expected {thr} accuracy. {key} = {acc}" -def can_run_gpu_test(): - if not torch.cuda.is_available(): - warnings.warn('test_multi_gpu_model_ddp cannot run.' - ' Rerun on a GPU node to run this test') - return False - if not torch.cuda.device_count() > 1: - warnings.warn('test_multi_gpu_model_ddp cannot run.' - ' Rerun on a node with 2+ GPUs to run this test') - return False - return True - - def reset_seed(): seed = RANDOM_SEEDS.pop() torch.manual_seed(seed) diff --git a/tests/models/test_amp.py b/tests/models/test_amp.py index 13d6ff0b..eea3bf98 100644 --- a/tests/models/test_amp.py +++ b/tests/models/test_amp.py @@ -1,6 +1,7 @@ import os import pytest +import torch import tests.base.utils as tutils from pytorch_lightning import Trainer @@ -10,13 +11,11 @@ from tests.base import ( ) +@pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires GPU machine") def test_amp_single_gpu(tmpdir): """Make sure DDP + AMP work.""" tutils.reset_seed() - if not tutils.can_run_gpu_test(): - return - hparams = tutils.get_default_hparams() model = LightningTestModel(hparams) @@ -33,13 +32,11 @@ def test_amp_single_gpu(tmpdir): @pytest.mark.spawn +@pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires GPU machine") def test_no_amp_single_gpu(tmpdir): """Make sure DDP + AMP work.""" tutils.reset_seed() - if not tutils.can_run_gpu_test(): - return - hparams = tutils.get_default_hparams() model = LightningTestModel(hparams) @@ -58,11 +55,9 @@ def test_no_amp_single_gpu(tmpdir): assert result == 1 +@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine") def test_amp_gpu_ddp(tmpdir): """Make sure DDP + AMP work.""" - if not tutils.can_run_gpu_test(): - return - tutils.reset_seed() tutils.set_random_master_port() @@ -82,11 +77,9 @@ def test_amp_gpu_ddp(tmpdir): @pytest.mark.spawn +@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine") def test_amp_gpu_ddp_slurm_managed(tmpdir): """Make sure DDP + AMP work.""" - if not tutils.can_run_gpu_test(): - return - tutils.reset_seed() # simulate setting slurm flags @@ -150,13 +143,11 @@ def test_cpu_model_with_amp(tmpdir): @pytest.mark.spawn +@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine") def test_amp_gpu_dp(tmpdir): """Make sure DP + AMP work.""" tutils.reset_seed() - if not tutils.can_run_gpu_test(): - return - model, hparams = tutils.get_default_model() trainer_options = dict( default_save_path=tmpdir, diff --git a/tests/models/test_cpu.py b/tests/models/test_cpu.py index 07c9968a..aae8aada 100644 --- a/tests/models/test_cpu.py +++ b/tests/models/test_cpu.py @@ -206,12 +206,10 @@ def test_disabled_validation(): assert model.validation_end_invoked, 'did not run `validation_end` with `fast_dev_run=True`' +@pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires GPU machine") def test_single_gpu_batch_parse(): tutils.reset_seed() - if not tutils.can_run_gpu_test(): - return - trainer = Trainer() # batch is just a tensor diff --git a/tests/models/test_gpu.py b/tests/models/test_gpu.py index 9c684ca6..0ee77351 100644 --- a/tests/models/test_gpu.py +++ b/tests/models/test_gpu.py @@ -17,10 +17,9 @@ from tests.base import LightningTestModel PRETEND_N_OF_GPUS = 16 +@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine") def test_multi_gpu_model_ddp2(tmpdir): """Make sure DDP2 works.""" - if not tutils.can_run_gpu_test(): - return tutils.reset_seed() tutils.set_random_master_port() @@ -40,10 +39,9 @@ def test_multi_gpu_model_ddp2(tmpdir): tutils.run_model_test(trainer_options, model) +@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine") def test_multi_gpu_model_ddp(tmpdir): """Make sure DDP works.""" - if not tutils.can_run_gpu_test(): - return tutils.reset_seed() tutils.set_random_master_port() @@ -62,10 +60,9 @@ def test_multi_gpu_model_ddp(tmpdir): tutils.run_model_test(trainer_options, model) +@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine") def test_ddp_all_dataloaders_passed_to_fit(tmpdir): """Make sure DDP works with dataloaders passed to fit()""" - if not tutils.can_run_gpu_test(): - return tutils.reset_seed() tutils.set_random_master_port() @@ -195,13 +192,11 @@ def test_cpu_slurm_save_load(tmpdir): trainer.fit(model) +@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine") def test_multi_gpu_none_backend(tmpdir): """Make sure when using multiple GPUs the user can't use `distributed_backend = None`.""" tutils.reset_seed() - if not tutils.can_run_gpu_test(): - return - model, hparams = tutils.get_default_model() trainer_options = dict( default_save_path=tmpdir, @@ -216,13 +211,11 @@ def test_multi_gpu_none_backend(tmpdir): tutils.run_model_test(trainer_options, model) +@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine") def test_multi_gpu_model_dp(tmpdir): """Make sure DP works.""" tutils.reset_seed() - if not tutils.can_run_gpu_test(): - return - model, hparams = tutils.get_default_model() trainer_options = dict( default_save_path=tmpdir, diff --git a/tests/models/test_restore.py b/tests/models/test_restore.py index d0088c26..07434274 100644 --- a/tests/models/test_restore.py +++ b/tests/models/test_restore.py @@ -16,10 +16,9 @@ from tests.base import ( ) +@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine") def test_running_test_pretrained_model_ddp(tmpdir): """Verify `test()` on pretrained model.""" - if not tutils.can_run_gpu_test(): - return tutils.reset_seed() tutils.set_random_master_port() @@ -151,13 +150,11 @@ def test_load_model_from_checkpoint(tmpdir): tutils.assert_ok_model_acc(new_trainer) +@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine") def test_running_test_pretrained_model_dp(tmpdir): """Verify test() on pretrained model.""" tutils.reset_seed() - if not tutils.can_run_gpu_test(): - return - hparams = tutils.get_default_hparams() model = LightningTestModel(hparams) @@ -195,10 +192,9 @@ def test_running_test_pretrained_model_dp(tmpdir): tutils.assert_ok_model_acc(new_trainer) +@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine") def test_dp_resume(tmpdir): """Make sure DP continues training correctly.""" - if not tutils.can_run_gpu_test(): - return tutils.reset_seed()