2019-10-23 10:10:13 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
import pytest
|
2020-03-30 22:29:23 +00:00
|
|
|
import torch
|
2019-10-23 10:10:13 +00:00
|
|
|
|
2020-03-25 11:46:27 +00:00
|
|
|
import tests.base.utils as tutils
|
2019-10-23 10:10:13 +00:00
|
|
|
from pytorch_lightning import Trainer
|
2020-03-31 12:57:48 +00:00
|
|
|
from pytorch_lightning.utilities.exceptions import MisconfigurationException
|
2020-05-04 15:38:08 +00:00
|
|
|
from tests.base import EvalModelTemplate
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
|
2020-01-05 19:34:25 +00:00
|
|
|
@pytest.mark.spawn
|
2020-04-22 00:33:10 +00:00
|
|
|
@pytest.mark.parametrize("backend", ['dp', 'ddp'])
|
2020-03-30 22:29:23 +00:00
|
|
|
@pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires GPU machine")
|
2020-04-22 00:33:10 +00:00
|
|
|
def test_amp_single_gpu(tmpdir, backend):
|
|
|
|
"""Make sure DP/DDP + AMP work."""
|
2019-11-28 17:06:05 +00:00
|
|
|
tutils.reset_seed()
|
2020-05-01 14:43:58 +00:00
|
|
|
trainer = Trainer(
|
2020-04-10 16:02:59 +00:00
|
|
|
default_root_dir=tmpdir,
|
2019-12-07 13:50:21 +00:00
|
|
|
max_epochs=1,
|
2019-10-23 10:10:13 +00:00
|
|
|
gpus=1,
|
2020-04-22 00:33:10 +00:00
|
|
|
distributed_backend=backend,
|
2020-02-17 21:01:20 +00:00
|
|
|
precision=16
|
2019-10-23 10:10:13 +00:00
|
|
|
)
|
|
|
|
|
2020-05-10 17:15:28 +00:00
|
|
|
model = EvalModelTemplate()
|
2020-04-22 00:33:10 +00:00
|
|
|
# tutils.run_model_test(trainer_options, model)
|
2020-01-05 19:34:25 +00:00
|
|
|
result = trainer.fit(model)
|
|
|
|
|
|
|
|
assert result == 1
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
|
2020-04-22 00:33:10 +00:00
|
|
|
@pytest.mark.spawn
|
|
|
|
@pytest.mark.parametrize("backend", ['dp', 'ddp'])
|
2020-03-30 22:29:23 +00:00
|
|
|
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine")
|
2020-04-22 00:33:10 +00:00
|
|
|
def test_amp_multi_gpu(tmpdir, backend):
|
|
|
|
"""Make sure DP/DDP + AMP work."""
|
2019-11-28 17:06:05 +00:00
|
|
|
tutils.set_random_master_port()
|
2019-10-23 10:10:13 +00:00
|
|
|
|
2020-05-10 17:15:28 +00:00
|
|
|
model = EvalModelTemplate()
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
trainer_options = dict(
|
2020-04-10 16:02:59 +00:00
|
|
|
default_root_dir=tmpdir,
|
2019-12-07 13:50:21 +00:00
|
|
|
max_epochs=1,
|
2020-04-22 00:33:10 +00:00
|
|
|
# gpus=2,
|
|
|
|
gpus='0, 1', # test init with gpu string
|
|
|
|
distributed_backend=backend,
|
2020-02-17 21:01:20 +00:00
|
|
|
precision=16
|
2019-10-23 10:10:13 +00:00
|
|
|
)
|
|
|
|
|
2020-04-22 00:33:10 +00:00
|
|
|
# tutils.run_model_test(trainer_options, model)
|
|
|
|
trainer = Trainer(**trainer_options)
|
|
|
|
result = trainer.fit(model)
|
|
|
|
assert result
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
|
2020-06-19 18:43:07 +00:00
|
|
|
@pytest.mark.spawn
|
|
|
|
@pytest.mark.parametrize("backend", ['dp', 'ddp'])
|
|
|
|
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine")
|
|
|
|
def test_multi_gpu_wandb(tmpdir, backend):
|
|
|
|
"""Make sure DP/DDP + AMP work."""
|
|
|
|
from pytorch_lightning.loggers import WandbLogger
|
|
|
|
tutils.set_random_master_port()
|
|
|
|
|
|
|
|
model = EvalModelTemplate()
|
|
|
|
logger = WandbLogger(name='utest')
|
|
|
|
|
|
|
|
trainer_options = dict(
|
|
|
|
default_root_dir=tmpdir,
|
|
|
|
max_epochs=1,
|
|
|
|
gpus=2,
|
|
|
|
distributed_backend=backend,
|
|
|
|
precision=16,
|
|
|
|
logger=logger,
|
|
|
|
|
|
|
|
)
|
|
|
|
# tutils.run_model_test(trainer_options, model)
|
|
|
|
trainer = Trainer(**trainer_options)
|
|
|
|
result = trainer.fit(model)
|
|
|
|
assert result
|
|
|
|
trainer.test(model)
|
|
|
|
|
|
|
|
|
2020-01-05 19:34:25 +00:00
|
|
|
@pytest.mark.spawn
|
2020-03-30 22:29:23 +00:00
|
|
|
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine")
|
2019-12-03 13:01:04 +00:00
|
|
|
def test_amp_gpu_ddp_slurm_managed(tmpdir):
|
2019-12-04 11:48:53 +00:00
|
|
|
"""Make sure DDP + AMP work."""
|
2019-10-23 10:10:13 +00:00
|
|
|
# simulate setting slurm flags
|
2019-11-28 17:06:05 +00:00
|
|
|
tutils.set_random_master_port()
|
2019-10-23 10:10:13 +00:00
|
|
|
os.environ['SLURM_LOCALID'] = str(0)
|
|
|
|
|
2020-05-10 17:15:28 +00:00
|
|
|
model = EvalModelTemplate()
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
# exp file to get meta
|
2020-04-22 00:33:10 +00:00
|
|
|
logger = tutils.get_default_logger(tmpdir)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
# exp file to get weights
|
2019-11-28 17:06:05 +00:00
|
|
|
checkpoint = tutils.init_checkpoint_callback(logger)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
# fit model
|
2020-05-01 14:43:58 +00:00
|
|
|
trainer = Trainer(
|
|
|
|
max_epochs=1,
|
|
|
|
gpus=[0],
|
|
|
|
distributed_backend='ddp',
|
|
|
|
precision=16,
|
|
|
|
checkpoint_callback=checkpoint,
|
|
|
|
logger=logger,
|
|
|
|
)
|
2019-10-23 10:10:13 +00:00
|
|
|
trainer.is_slurm_managing_tasks = True
|
|
|
|
result = trainer.fit(model)
|
|
|
|
|
|
|
|
# correct result and ok accuracy
|
|
|
|
assert result == 1, 'amp + ddp model failed to complete'
|
|
|
|
|
|
|
|
# test root model address
|
|
|
|
assert trainer.resolve_root_node_address('abc') == 'abc'
|
|
|
|
assert trainer.resolve_root_node_address('abc[23]') == 'abc23'
|
|
|
|
assert trainer.resolve_root_node_address('abc[23-24]') == 'abc23'
|
|
|
|
assert trainer.resolve_root_node_address('abc[23-24, 45-40, 40]') == 'abc23'
|
|
|
|
|
|
|
|
|
2019-12-03 13:01:04 +00:00
|
|
|
def test_cpu_model_with_amp(tmpdir):
|
2019-12-04 11:48:53 +00:00
|
|
|
"""Make sure model trains on CPU."""
|
2019-10-23 10:10:13 +00:00
|
|
|
trainer_options = dict(
|
2020-04-10 16:02:59 +00:00
|
|
|
default_root_dir=tmpdir,
|
2020-04-02 22:53:00 +00:00
|
|
|
progress_bar_refresh_rate=0,
|
2019-12-07 13:50:21 +00:00
|
|
|
max_epochs=1,
|
2020-06-17 17:42:28 +00:00
|
|
|
limit_train_batches=0.4,
|
2020-06-17 12:03:28 +00:00
|
|
|
limit_val_batches=0.4,
|
2020-02-17 21:01:20 +00:00
|
|
|
precision=16
|
2019-10-23 10:10:13 +00:00
|
|
|
)
|
|
|
|
|
2020-05-10 17:15:28 +00:00
|
|
|
model = EvalModelTemplate()
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
with pytest.raises((MisconfigurationException, ModuleNotFoundError)):
|
2019-12-04 11:48:53 +00:00
|
|
|
tutils.run_model_test(trainer_options, model, on_gpu=False)
|