2019-10-23 10:10:13 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2020-02-11 03:47:18 +00:00
|
|
|
import tests.models.utils as tutils
|
2019-10-23 10:10:13 +00:00
|
|
|
from pytorch_lightning import Trainer
|
2020-02-11 03:47:18 +00:00
|
|
|
from tests.models import (
|
2019-10-23 10:10:13 +00:00
|
|
|
LightningTestModel,
|
|
|
|
)
|
|
|
|
from pytorch_lightning.utilities.debugging import MisconfigurationException
|
|
|
|
|
|
|
|
|
2019-12-03 13:01:04 +00:00
|
|
|
def test_amp_single_gpu(tmpdir):
|
2019-12-04 11:48:53 +00:00
|
|
|
"""Make sure DDP + AMP work."""
|
2019-11-28 17:06:05 +00:00
|
|
|
tutils.reset_seed()
|
2019-10-23 10:10:13 +00:00
|
|
|
|
2019-11-28 17:06:05 +00:00
|
|
|
if not tutils.can_run_gpu_test():
|
2019-10-23 10:10:13 +00:00
|
|
|
return
|
|
|
|
|
2019-11-28 17:06:05 +00:00
|
|
|
hparams = tutils.get_hparams()
|
2019-10-23 10:10:13 +00:00
|
|
|
model = LightningTestModel(hparams)
|
|
|
|
|
|
|
|
trainer_options = dict(
|
2019-12-03 13:01:04 +00:00
|
|
|
default_save_path=tmpdir,
|
2019-10-23 10:10:13 +00:00
|
|
|
show_progress_bar=True,
|
2019-12-07 13:50:21 +00:00
|
|
|
max_epochs=1,
|
2019-10-23 10:10:13 +00:00
|
|
|
gpus=1,
|
|
|
|
distributed_backend='ddp',
|
|
|
|
use_amp=True
|
|
|
|
)
|
|
|
|
|
2019-12-04 11:48:53 +00:00
|
|
|
tutils.run_model_test(trainer_options, model)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
|
2020-01-05 19:34:25 +00:00
|
|
|
@pytest.mark.spawn
|
2019-12-03 13:01:04 +00:00
|
|
|
def test_no_amp_single_gpu(tmpdir):
|
2019-12-04 11:48:53 +00:00
|
|
|
"""Make sure DDP + AMP work."""
|
2019-11-28 17:06:05 +00:00
|
|
|
tutils.reset_seed()
|
2019-10-23 10:10:13 +00:00
|
|
|
|
2019-11-28 17:06:05 +00:00
|
|
|
if not tutils.can_run_gpu_test():
|
2019-10-23 10:10:13 +00:00
|
|
|
return
|
|
|
|
|
2019-11-28 17:06:05 +00:00
|
|
|
hparams = tutils.get_hparams()
|
2019-10-23 10:10:13 +00:00
|
|
|
model = LightningTestModel(hparams)
|
|
|
|
|
|
|
|
trainer_options = dict(
|
2019-12-03 13:01:04 +00:00
|
|
|
default_save_path=tmpdir,
|
2019-10-23 10:10:13 +00:00
|
|
|
show_progress_bar=True,
|
2019-12-07 13:50:21 +00:00
|
|
|
max_epochs=1,
|
2019-10-23 10:10:13 +00:00
|
|
|
gpus=1,
|
|
|
|
distributed_backend='dp',
|
|
|
|
use_amp=True
|
|
|
|
)
|
|
|
|
|
2020-01-05 19:34:25 +00:00
|
|
|
trainer = Trainer(**trainer_options)
|
|
|
|
result = trainer.fit(model)
|
|
|
|
|
|
|
|
assert result == 1
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
|
2019-12-03 13:01:04 +00:00
|
|
|
def test_amp_gpu_ddp(tmpdir):
|
2019-12-04 11:48:53 +00:00
|
|
|
"""Make sure DDP + AMP work."""
|
2019-11-28 17:06:05 +00:00
|
|
|
if not tutils.can_run_gpu_test():
|
2019-10-23 10:10:13 +00:00
|
|
|
return
|
|
|
|
|
2019-11-28 17:06:05 +00:00
|
|
|
tutils.reset_seed()
|
|
|
|
tutils.set_random_master_port()
|
2019-10-23 10:10:13 +00:00
|
|
|
|
2019-11-28 17:06:05 +00:00
|
|
|
hparams = tutils.get_hparams()
|
2019-10-23 10:10:13 +00:00
|
|
|
model = LightningTestModel(hparams)
|
|
|
|
|
|
|
|
trainer_options = dict(
|
2019-12-03 13:01:04 +00:00
|
|
|
default_save_path=tmpdir,
|
2019-10-23 10:10:13 +00:00
|
|
|
show_progress_bar=True,
|
2019-12-07 13:50:21 +00:00
|
|
|
max_epochs=1,
|
2019-10-23 10:10:13 +00:00
|
|
|
gpus=2,
|
|
|
|
distributed_backend='ddp',
|
|
|
|
use_amp=True
|
|
|
|
)
|
|
|
|
|
2019-12-04 11:48:53 +00:00
|
|
|
tutils.run_model_test(trainer_options, model)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
|
2020-01-05 19:34:25 +00:00
|
|
|
@pytest.mark.spawn
|
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-11-28 17:06:05 +00:00
|
|
|
if not tutils.can_run_gpu_test():
|
2019-10-23 10:10:13 +00:00
|
|
|
return
|
|
|
|
|
2019-11-28 17:06:05 +00:00
|
|
|
tutils.reset_seed()
|
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)
|
|
|
|
|
2019-11-28 17:06:05 +00:00
|
|
|
hparams = tutils.get_hparams()
|
2019-10-23 10:10:13 +00:00
|
|
|
model = LightningTestModel(hparams)
|
|
|
|
|
|
|
|
trainer_options = dict(
|
|
|
|
show_progress_bar=True,
|
2019-12-07 13:50:21 +00:00
|
|
|
max_epochs=1,
|
2019-10-23 10:10:13 +00:00
|
|
|
gpus=[0],
|
|
|
|
distributed_backend='ddp',
|
|
|
|
use_amp=True
|
|
|
|
)
|
|
|
|
|
|
|
|
# exp file to get meta
|
2019-12-04 11:48:53 +00:00
|
|
|
logger = tutils.get_test_tube_logger(tmpdir, False)
|
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
|
|
|
|
|
|
|
# add these to the trainer options
|
|
|
|
trainer_options['checkpoint_callback'] = checkpoint
|
|
|
|
trainer_options['logger'] = logger
|
|
|
|
|
|
|
|
# fit model
|
|
|
|
trainer = Trainer(**trainer_options)
|
|
|
|
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-11-28 17:06:05 +00:00
|
|
|
tutils.reset_seed()
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
trainer_options = dict(
|
2019-12-03 13:01:04 +00:00
|
|
|
default_save_path=tmpdir,
|
2019-10-23 10:10:13 +00:00
|
|
|
show_progress_bar=False,
|
2019-12-03 13:01:04 +00:00
|
|
|
logger=tutils.get_test_tube_logger(tmpdir),
|
2019-12-07 13:50:21 +00:00
|
|
|
max_epochs=1,
|
2019-10-23 10:10:13 +00:00
|
|
|
train_percent_check=0.4,
|
|
|
|
val_percent_check=0.4,
|
|
|
|
use_amp=True
|
|
|
|
)
|
|
|
|
|
2019-11-28 17:06:05 +00:00
|
|
|
model, hparams = tutils.get_model()
|
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)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
|
2020-01-05 19:34:25 +00:00
|
|
|
@pytest.mark.spawn
|
2019-12-03 13:01:04 +00:00
|
|
|
def test_amp_gpu_dp(tmpdir):
|
2019-12-04 11:48:53 +00:00
|
|
|
"""Make sure DP + AMP work."""
|
2019-11-28 17:06:05 +00:00
|
|
|
tutils.reset_seed()
|
2019-10-23 10:10:13 +00:00
|
|
|
|
2019-11-28 17:06:05 +00:00
|
|
|
if not tutils.can_run_gpu_test():
|
2019-10-23 10:10:13 +00:00
|
|
|
return
|
|
|
|
|
2019-11-28 17:06:05 +00:00
|
|
|
model, hparams = tutils.get_model()
|
2019-10-23 10:10:13 +00:00
|
|
|
trainer_options = dict(
|
2019-12-03 13:01:04 +00:00
|
|
|
default_save_path=tmpdir,
|
2019-12-07 13:50:21 +00:00
|
|
|
max_epochs=1,
|
2019-10-23 10:10:13 +00:00
|
|
|
gpus='0, 1', # test init with gpu string
|
|
|
|
distributed_backend='dp',
|
|
|
|
use_amp=True
|
|
|
|
)
|
2020-01-05 19:34:25 +00:00
|
|
|
|
|
|
|
trainer = Trainer(**trainer_options)
|
|
|
|
result = trainer.fit(model)
|
|
|
|
|
|
|
|
assert result == 1
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
pytest.main([__file__])
|