2020-08-16 15:19:57 +00:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2020-06-16 02:03:40 +00:00
|
|
|
from collections import namedtuple
|
2020-08-16 15:19:57 +00:00
|
|
|
from pathlib import Path
|
2020-08-11 23:28:37 +00:00
|
|
|
from unittest.mock import patch
|
2019-12-04 11:48:53 +00:00
|
|
|
|
2019-10-23 10:10:13 +00:00
|
|
|
import pytest
|
|
|
|
import torch
|
2020-08-06 14:58:51 +00:00
|
|
|
from torchtext.data import Batch, Dataset, Example, Field, LabelField
|
2019-10-23 10:10:13 +00:00
|
|
|
|
2020-08-16 15:19:57 +00:00
|
|
|
import pytorch_lightning
|
2020-06-27 01:38:25 +00:00
|
|
|
import tests.base.develop_pipelines as tpipes
|
|
|
|
import tests.base.develop_utils as tutils
|
2019-10-23 10:10:13 +00:00
|
|
|
from pytorch_lightning import Trainer
|
2019-11-27 03:39:18 +00:00
|
|
|
from pytorch_lightning.core import memory
|
2020-09-08 22:46:42 +00:00
|
|
|
from pytorch_lightning.utilities import device_parser
|
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
|
2020-08-16 15:19:57 +00:00
|
|
|
from tests.models.data.ddp import train_test_variations
|
2020-09-09 02:14:17 +00:00
|
|
|
from pytorch_lightning.accelerators.gpu_backend import GPUBackend
|
|
|
|
from pytorch_lightning.accelerators.cpu_backend import CPUBackend
|
|
|
|
|
2020-08-06 14:58:51 +00:00
|
|
|
|
2019-10-23 10:10:13 +00:00
|
|
|
PRETEND_N_OF_GPUS = 16
|
|
|
|
|
|
|
|
|
2020-07-22 17:53:10 +00:00
|
|
|
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine")
|
|
|
|
def test_multi_gpu_early_stop_dp(tmpdir):
|
|
|
|
"""Make sure DDP works. with early stopping"""
|
|
|
|
tutils.set_random_master_port()
|
|
|
|
|
|
|
|
trainer_options = dict(
|
|
|
|
default_root_dir=tmpdir,
|
|
|
|
early_stop_callback=True,
|
|
|
|
max_epochs=50,
|
|
|
|
limit_train_batches=10,
|
|
|
|
limit_val_batches=10,
|
|
|
|
gpus=[0, 1],
|
|
|
|
distributed_backend='dp',
|
|
|
|
)
|
|
|
|
|
|
|
|
model = EvalModelTemplate()
|
|
|
|
tpipes.run_model_test(trainer_options, model)
|
|
|
|
|
|
|
|
|
2020-07-07 16:24:56 +00:00
|
|
|
@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.set_random_master_port()
|
2020-06-01 15:00:32 +00:00
|
|
|
trainer_options = dict(
|
|
|
|
default_root_dir=tmpdir,
|
2020-07-07 16:24:56 +00:00
|
|
|
distributed_backend=None,
|
2020-06-01 15:00:32 +00:00
|
|
|
progress_bar_refresh_rate=0,
|
|
|
|
max_epochs=1,
|
2020-07-07 16:24:56 +00:00
|
|
|
limit_train_batches=0.2,
|
|
|
|
limit_val_batches=0.2,
|
|
|
|
gpus=2
|
|
|
|
)
|
|
|
|
|
|
|
|
model = EvalModelTemplate()
|
|
|
|
tpipes.run_model_test(trainer_options, model)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine")
|
|
|
|
def test_multi_gpu_early_stop_ddp_spawn(tmpdir):
|
|
|
|
"""Make sure DDP works. with early stopping"""
|
|
|
|
tutils.set_random_master_port()
|
|
|
|
|
|
|
|
trainer_options = dict(
|
|
|
|
default_root_dir=tmpdir,
|
|
|
|
early_stop_callback=True,
|
|
|
|
max_epochs=50,
|
|
|
|
limit_train_batches=10,
|
|
|
|
limit_val_batches=10,
|
|
|
|
gpus=[0, 1],
|
|
|
|
distributed_backend='ddp_spawn',
|
2020-06-01 15:00:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
model = EvalModelTemplate()
|
2020-06-27 01:38:25 +00:00
|
|
|
tpipes.run_model_test(trainer_options, model)
|
2020-06-01 15:00:32 +00:00
|
|
|
|
|
|
|
|
2020-03-30 22:29:23 +00:00
|
|
|
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine")
|
2020-07-07 16:24:56 +00:00
|
|
|
def test_multi_gpu_model_dp(tmpdir):
|
2019-11-28 17:06:05 +00:00
|
|
|
tutils.set_random_master_port()
|
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-07-07 16:24:56 +00:00
|
|
|
limit_train_batches=10,
|
|
|
|
limit_val_batches=10,
|
2019-10-23 10:10:13 +00:00
|
|
|
gpus=[0, 1],
|
2020-07-07 16:24:56 +00:00
|
|
|
distributed_backend='dp',
|
|
|
|
progress_bar_refresh_rate=0
|
2019-10-23 10:10:13 +00:00
|
|
|
)
|
|
|
|
|
2020-05-10 17:15:28 +00:00
|
|
|
model = EvalModelTemplate()
|
2020-07-07 16:24:56 +00:00
|
|
|
|
|
|
|
tpipes.run_model_test(trainer_options, model)
|
2020-04-22 00:33:10 +00:00
|
|
|
|
|
|
|
# test memory helper functions
|
|
|
|
memory.get_memory_profile('min_max')
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
|
2020-08-16 15:19:57 +00:00
|
|
|
@pytest.mark.parametrize('cli_args', [
|
|
|
|
pytest.param('--max_epochs 1 --gpus 2 --distributed_backend ddp'),
|
|
|
|
])
|
|
|
|
@pytest.mark.parametrize('variation', train_test_variations.get_variations())
|
|
|
|
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine")
|
|
|
|
def test_multi_gpu_model_ddp(tmpdir, cli_args, variation):
|
|
|
|
""" Runs a basic training and test run with distributed_backend=ddp. """
|
|
|
|
file = Path(train_test_variations.__file__).absolute()
|
|
|
|
cli_args = cli_args.split(' ') if cli_args else []
|
|
|
|
cli_args += ['--default_root_dir', str(tmpdir)]
|
|
|
|
cli_args += ['--variation', variation]
|
|
|
|
command = [sys.executable, str(file)] + cli_args
|
|
|
|
|
|
|
|
# need to set the PYTHONPATH in case pytorch_lightning was not installed into the environment
|
|
|
|
env = os.environ.copy()
|
|
|
|
env['PYTHONPATH'] = f'{pytorch_lightning.__file__}:' + env.get('PYTHONPATH', '')
|
|
|
|
|
|
|
|
# for running in ddp mode, we need to lauch it's own process or pytest will get stuck
|
|
|
|
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
|
|
|
|
|
|
|
|
std, err = p.communicate(timeout=60)
|
|
|
|
std = std.decode('utf-8').strip()
|
|
|
|
err = err.decode('utf-8').strip()
|
|
|
|
assert std, f"{variation} produced no output"
|
|
|
|
if p.returncode > 0:
|
|
|
|
pytest.fail(err)
|
|
|
|
|
|
|
|
|
2020-07-03 04:38:29 +00:00
|
|
|
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine")
|
2020-07-07 16:24:56 +00:00
|
|
|
def test_multi_gpu_model_ddp_spawn(tmpdir):
|
|
|
|
tutils.set_random_master_port()
|
|
|
|
|
|
|
|
trainer_options = dict(
|
|
|
|
default_root_dir=tmpdir,
|
|
|
|
max_epochs=1,
|
|
|
|
limit_train_batches=10,
|
|
|
|
limit_val_batches=10,
|
|
|
|
gpus=[0, 1],
|
|
|
|
distributed_backend='ddp_spawn',
|
|
|
|
progress_bar_refresh_rate=0
|
|
|
|
)
|
|
|
|
|
|
|
|
model = EvalModelTemplate()
|
|
|
|
|
|
|
|
tpipes.run_model_test(trainer_options, model)
|
|
|
|
|
|
|
|
# test memory helper functions
|
|
|
|
memory.get_memory_profile('min_max')
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires GPU machine")
|
|
|
|
@pytest.mark.parametrize('gpus', [1, [0], [1]])
|
|
|
|
def test_single_gpu_model(tmpdir, gpus):
|
|
|
|
"""Make sure single GPU works (DP mode)."""
|
|
|
|
trainer_options = dict(
|
|
|
|
default_root_dir=tmpdir,
|
|
|
|
progress_bar_refresh_rate=0,
|
|
|
|
max_epochs=1,
|
|
|
|
limit_train_batches=0.1,
|
|
|
|
limit_val_batches=0.1,
|
|
|
|
gpus=gpus
|
|
|
|
)
|
|
|
|
|
|
|
|
model = EvalModelTemplate()
|
|
|
|
tpipes.run_model_test(trainer_options, model)
|
|
|
|
|
|
|
|
|
2020-03-30 22:29:23 +00:00
|
|
|
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine")
|
2020-03-02 03:50:49 +00:00
|
|
|
def test_ddp_all_dataloaders_passed_to_fit(tmpdir):
|
|
|
|
"""Make sure DDP works with dataloaders passed to fit()"""
|
|
|
|
tutils.set_random_master_port()
|
|
|
|
|
2020-07-28 13:47:53 +00:00
|
|
|
model = EvalModelTemplate()
|
|
|
|
fit_options = dict(train_dataloader=model.train_dataloader(),
|
|
|
|
val_dataloaders=model.val_dataloader())
|
|
|
|
|
|
|
|
trainer = Trainer(
|
2020-06-27 01:38:25 +00:00
|
|
|
default_root_dir=tmpdir,
|
|
|
|
progress_bar_refresh_rate=0,
|
|
|
|
max_epochs=1,
|
2020-07-07 16:24:56 +00:00
|
|
|
limit_train_batches=0.2,
|
|
|
|
limit_val_batches=0.2,
|
2020-06-27 01:38:25 +00:00
|
|
|
gpus=[0, 1],
|
2020-07-07 16:24:56 +00:00
|
|
|
distributed_backend='ddp_spawn'
|
2020-06-27 01:38:25 +00:00
|
|
|
)
|
2020-05-02 12:38:22 +00:00
|
|
|
result = trainer.fit(model, **fit_options)
|
2020-03-02 03:50:49 +00:00
|
|
|
assert result == 1, "DDP doesn't work with dataloaders passed to fit()."
|
|
|
|
|
|
|
|
|
2019-10-23 10:10:13 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def mocked_device_count(monkeypatch):
|
|
|
|
def device_count():
|
|
|
|
return PRETEND_N_OF_GPUS
|
|
|
|
|
|
|
|
monkeypatch.setattr(torch.cuda, 'device_count', device_count)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mocked_device_count_0(monkeypatch):
|
|
|
|
def device_count():
|
|
|
|
return 0
|
|
|
|
|
|
|
|
monkeypatch.setattr(torch.cuda, 'device_count', device_count)
|
|
|
|
|
|
|
|
|
2020-03-05 04:02:19 +00:00
|
|
|
@pytest.mark.gpus_param_tests
|
|
|
|
@pytest.mark.parametrize(["gpus", "expected_num_gpus", "distributed_backend"], [
|
2019-10-23 10:10:13 +00:00
|
|
|
pytest.param(None, 0, None, id="None - expect 0 gpu to use."),
|
|
|
|
pytest.param(0, 0, None, id="Oth gpu, expect 1 gpu to use."),
|
|
|
|
pytest.param(1, 1, None, id="1st gpu, expect 1 gpu to use."),
|
|
|
|
pytest.param(-1, PRETEND_N_OF_GPUS, "ddp", id="-1 - use all gpus"),
|
|
|
|
pytest.param('-1', PRETEND_N_OF_GPUS, "ddp", id="'-1' - use all gpus"),
|
|
|
|
pytest.param(3, 3, "ddp", id="3rd gpu - 1 gpu to use (backend:ddp)")
|
2020-03-05 04:02:19 +00:00
|
|
|
])
|
2019-10-23 10:10:13 +00:00
|
|
|
def test_trainer_gpu_parse(mocked_device_count, gpus, expected_num_gpus, distributed_backend):
|
|
|
|
assert Trainer(gpus=gpus, distributed_backend=distributed_backend).num_gpus == expected_num_gpus
|
|
|
|
|
|
|
|
|
2020-03-05 04:02:19 +00:00
|
|
|
@pytest.mark.gpus_param_tests
|
|
|
|
@pytest.mark.parametrize(["gpus", "expected_num_gpus", "distributed_backend"], [
|
2019-10-23 10:10:13 +00:00
|
|
|
pytest.param(None, 0, None, id="None - expect 0 gpu to use."),
|
|
|
|
pytest.param(None, 0, "ddp", id="None - expect 0 gpu to use."),
|
2020-03-05 04:02:19 +00:00
|
|
|
])
|
2019-10-23 10:10:13 +00:00
|
|
|
def test_trainer_num_gpu_0(mocked_device_count_0, gpus, expected_num_gpus, distributed_backend):
|
|
|
|
assert Trainer(gpus=gpus, distributed_backend=distributed_backend).num_gpus == expected_num_gpus
|
|
|
|
|
|
|
|
|
2020-03-05 04:02:19 +00:00
|
|
|
@pytest.mark.gpus_param_tests
|
|
|
|
@pytest.mark.parametrize(['gpus', 'expected_root_gpu', "distributed_backend"], [
|
2019-10-23 10:10:13 +00:00
|
|
|
pytest.param(None, None, "ddp", id="None is None"),
|
|
|
|
pytest.param(0, None, "ddp", id="O gpus, expect gpu root device to be None."),
|
|
|
|
pytest.param(1, 0, "ddp", id="1 gpu, expect gpu root device to be 0."),
|
|
|
|
pytest.param(-1, 0, "ddp", id="-1 - use all gpus, expect gpu root device to be 0."),
|
|
|
|
pytest.param('-1', 0, "ddp", id="'-1' - use all gpus, expect gpu root device to be 0."),
|
2020-03-05 04:02:19 +00:00
|
|
|
pytest.param(3, 0, "ddp", id="3 gpus, expect gpu root device to be 0.(backend:ddp)")
|
|
|
|
])
|
2019-10-23 10:10:13 +00:00
|
|
|
def test_root_gpu_property(mocked_device_count, gpus, expected_root_gpu, distributed_backend):
|
|
|
|
assert Trainer(gpus=gpus, distributed_backend=distributed_backend).root_gpu == expected_root_gpu
|
|
|
|
|
|
|
|
|
2020-03-05 04:02:19 +00:00
|
|
|
@pytest.mark.gpus_param_tests
|
2020-04-22 00:33:10 +00:00
|
|
|
@pytest.mark.parametrize(['gpus', 'expected_root_gpu', "distributed_backend"], [
|
2019-10-23 10:10:13 +00:00
|
|
|
pytest.param(None, None, None, id="None is None"),
|
|
|
|
pytest.param(None, None, "ddp", id="None is None"),
|
|
|
|
pytest.param(0, None, "ddp", id="None is None"),
|
2020-03-05 04:02:19 +00:00
|
|
|
])
|
2020-04-22 00:33:10 +00:00
|
|
|
def test_root_gpu_property_0_passing(mocked_device_count_0, gpus, expected_root_gpu, distributed_backend):
|
2019-10-23 10:10:13 +00:00
|
|
|
assert Trainer(gpus=gpus, distributed_backend=distributed_backend).root_gpu == expected_root_gpu
|
|
|
|
|
|
|
|
|
|
|
|
# Asking for a gpu when non are available will result in a MisconfigurationException
|
2020-03-05 04:02:19 +00:00
|
|
|
@pytest.mark.gpus_param_tests
|
2020-04-22 00:33:10 +00:00
|
|
|
@pytest.mark.parametrize(['gpus', 'expected_root_gpu', "distributed_backend"], [
|
2019-10-23 10:10:13 +00:00
|
|
|
pytest.param(1, None, "ddp"),
|
|
|
|
pytest.param(3, None, "ddp"),
|
|
|
|
pytest.param(3, None, "ddp"),
|
|
|
|
pytest.param([1, 2], None, "ddp"),
|
|
|
|
pytest.param([0, 1], None, "ddp"),
|
|
|
|
pytest.param(-1, None, "ddp"),
|
|
|
|
pytest.param('-1', None, "ddp")
|
2020-03-05 04:02:19 +00:00
|
|
|
])
|
2020-04-22 00:33:10 +00:00
|
|
|
def test_root_gpu_property_0_raising(mocked_device_count_0, gpus, expected_root_gpu, distributed_backend):
|
2019-10-23 10:10:13 +00:00
|
|
|
with pytest.raises(MisconfigurationException):
|
2020-06-20 03:41:42 +00:00
|
|
|
Trainer(gpus=gpus, distributed_backend=distributed_backend)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
|
2020-03-05 04:02:19 +00:00
|
|
|
@pytest.mark.gpus_param_tests
|
|
|
|
@pytest.mark.parametrize(['gpus', 'expected_root_gpu'], [
|
2019-10-23 10:10:13 +00:00
|
|
|
pytest.param(None, None, id="No gpus, expect gpu root device to be None"),
|
|
|
|
pytest.param([0], 0, id="Oth gpu, expect gpu root device to be 0."),
|
|
|
|
pytest.param([1], 1, id="1st gpu, expect gpu root device to be 1."),
|
|
|
|
pytest.param([3], 3, id="3rd gpu, expect gpu root device to be 3."),
|
|
|
|
pytest.param([1, 2], 1, id="[1, 2] gpus, expect gpu root device to be 1."),
|
2020-03-05 04:02:19 +00:00
|
|
|
])
|
2019-10-23 10:10:13 +00:00
|
|
|
def test_determine_root_gpu_device(gpus, expected_root_gpu):
|
2020-09-08 22:46:42 +00:00
|
|
|
assert device_parser.determine_root_gpu_device(gpus) == expected_root_gpu
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
|
2020-03-05 04:02:19 +00:00
|
|
|
@pytest.mark.gpus_param_tests
|
|
|
|
@pytest.mark.parametrize(['gpus', 'expected_gpu_ids'], [
|
2019-10-23 10:10:13 +00:00
|
|
|
pytest.param(None, None),
|
|
|
|
pytest.param(0, None),
|
|
|
|
pytest.param(1, [0]),
|
2019-12-07 13:48:45 +00:00
|
|
|
pytest.param(3, [0, 1, 2]),
|
2019-10-23 10:10:13 +00:00
|
|
|
pytest.param(-1, list(range(PRETEND_N_OF_GPUS)), id="-1 - use all gpus"),
|
2019-12-07 13:48:45 +00:00
|
|
|
pytest.param([0], [0]),
|
|
|
|
pytest.param([1, 3], [1, 3]),
|
|
|
|
pytest.param('0', [0]),
|
|
|
|
pytest.param('3', [3]),
|
|
|
|
pytest.param('1, 3', [1, 3]),
|
2020-04-24 01:00:41 +00:00
|
|
|
pytest.param('2,', [2]),
|
2019-10-23 10:10:13 +00:00
|
|
|
pytest.param('-1', list(range(PRETEND_N_OF_GPUS)), id="'-1' - use all gpus"),
|
2020-03-05 04:02:19 +00:00
|
|
|
])
|
2019-10-23 10:10:13 +00:00
|
|
|
def test_parse_gpu_ids(mocked_device_count, gpus, expected_gpu_ids):
|
2020-09-08 22:46:42 +00:00
|
|
|
assert device_parser.parse_gpu_ids(gpus) == expected_gpu_ids
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
|
2020-03-05 04:02:19 +00:00
|
|
|
@pytest.mark.gpus_param_tests
|
|
|
|
@pytest.mark.parametrize(['gpus'], [
|
2019-12-07 13:48:45 +00:00
|
|
|
pytest.param(0.1),
|
|
|
|
pytest.param(-2),
|
|
|
|
pytest.param(False),
|
|
|
|
pytest.param([]),
|
|
|
|
pytest.param([-1]),
|
|
|
|
pytest.param([None]),
|
|
|
|
pytest.param(['0']),
|
|
|
|
pytest.param((0, 1)),
|
2020-03-05 04:02:19 +00:00
|
|
|
])
|
2019-12-07 13:48:45 +00:00
|
|
|
def test_parse_gpu_fail_on_unsupported_inputs(mocked_device_count, gpus):
|
|
|
|
with pytest.raises(MisconfigurationException):
|
2020-09-08 22:46:42 +00:00
|
|
|
device_parser.parse_gpu_ids(gpus)
|
2019-12-07 13:48:45 +00:00
|
|
|
|
|
|
|
|
2019-10-23 10:10:13 +00:00
|
|
|
@pytest.mark.gpus_param_tests
|
|
|
|
@pytest.mark.parametrize("gpus", [[1, 2, 19], -1, '-1'])
|
2020-05-07 13:25:54 +00:00
|
|
|
def test_parse_gpu_fail_on_non_existent_id(mocked_device_count_0, gpus):
|
2019-10-23 10:10:13 +00:00
|
|
|
with pytest.raises(MisconfigurationException):
|
2020-09-08 22:46:42 +00:00
|
|
|
device_parser.parse_gpu_ids(gpus)
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.gpus_param_tests
|
2020-05-07 13:25:54 +00:00
|
|
|
def test_parse_gpu_fail_on_non_existent_id_2(mocked_device_count):
|
2019-10-23 10:10:13 +00:00
|
|
|
with pytest.raises(MisconfigurationException):
|
2020-09-08 22:46:42 +00:00
|
|
|
device_parser.parse_gpu_ids([1, 2, 19])
|
2019-10-23 10:10:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.gpus_param_tests
|
|
|
|
@pytest.mark.parametrize("gpus", [-1, '-1'])
|
2020-07-07 16:24:56 +00:00
|
|
|
def test_parse_gpu_returns_none_when_no_devices_are_available(mocked_device_count_0, gpus):
|
2019-10-23 10:10:13 +00:00
|
|
|
with pytest.raises(MisconfigurationException):
|
2020-09-08 22:46:42 +00:00
|
|
|
device_parser.parse_gpu_ids(gpus)
|
2020-06-16 02:03:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires GPU machine")
|
|
|
|
def test_single_gpu_batch_parse():
|
2020-09-09 02:14:17 +00:00
|
|
|
trainer = Trainer(gpus=1)
|
|
|
|
trainer.accelerator_backend = GPUBackend(trainer)
|
2020-06-16 02:03:40 +00:00
|
|
|
|
2020-09-11 14:55:58 +00:00
|
|
|
# non-transferrable types
|
|
|
|
primitive_objects = [None, {}, [], 1.0, "x", [None, 2], {"x": (1, 2), "y": None}]
|
|
|
|
for batch in primitive_objects:
|
|
|
|
data = trainer.accelerator_backend.batch_to_device(batch, torch.device('cuda:0'))
|
|
|
|
assert data == batch
|
|
|
|
|
2020-06-16 02:03:40 +00:00
|
|
|
# batch is just a tensor
|
|
|
|
batch = torch.rand(2, 3)
|
2020-09-09 02:14:17 +00:00
|
|
|
batch = trainer.accelerator_backend.batch_to_device(batch, torch.device('cuda:0'))
|
2020-06-16 02:03:40 +00:00
|
|
|
assert batch.device.index == 0 and batch.type() == 'torch.cuda.FloatTensor'
|
|
|
|
|
|
|
|
# tensor list
|
|
|
|
batch = [torch.rand(2, 3), torch.rand(2, 3)]
|
2020-09-09 02:14:17 +00:00
|
|
|
batch = trainer.accelerator_backend.batch_to_device(batch, torch.device('cuda:0'))
|
2020-06-16 02:03:40 +00:00
|
|
|
assert batch[0].device.index == 0 and batch[0].type() == 'torch.cuda.FloatTensor'
|
|
|
|
assert batch[1].device.index == 0 and batch[1].type() == 'torch.cuda.FloatTensor'
|
|
|
|
|
|
|
|
# tensor list of lists
|
|
|
|
batch = [[torch.rand(2, 3), torch.rand(2, 3)]]
|
2020-09-09 02:14:17 +00:00
|
|
|
batch = trainer.accelerator_backend.batch_to_device(batch, torch.device('cuda:0'))
|
2020-06-16 02:03:40 +00:00
|
|
|
assert batch[0][0].device.index == 0 and batch[0][0].type() == 'torch.cuda.FloatTensor'
|
|
|
|
assert batch[0][1].device.index == 0 and batch[0][1].type() == 'torch.cuda.FloatTensor'
|
|
|
|
|
|
|
|
# tensor dict
|
|
|
|
batch = [{'a': torch.rand(2, 3), 'b': torch.rand(2, 3)}]
|
2020-09-09 02:14:17 +00:00
|
|
|
batch = trainer.accelerator_backend.batch_to_device(batch, torch.device('cuda:0'))
|
2020-06-16 02:03:40 +00:00
|
|
|
assert batch[0]['a'].device.index == 0 and batch[0]['a'].type() == 'torch.cuda.FloatTensor'
|
|
|
|
assert batch[0]['b'].device.index == 0 and batch[0]['b'].type() == 'torch.cuda.FloatTensor'
|
|
|
|
|
|
|
|
# tuple of tensor list and list of tensor dict
|
|
|
|
batch = ([torch.rand(2, 3) for _ in range(2)],
|
|
|
|
[{'a': torch.rand(2, 3), 'b': torch.rand(2, 3)} for _ in range(2)])
|
2020-09-09 02:14:17 +00:00
|
|
|
batch = trainer.accelerator_backend.batch_to_device(batch, torch.device('cuda:0'))
|
2020-06-16 02:03:40 +00:00
|
|
|
assert batch[0][0].device.index == 0 and batch[0][0].type() == 'torch.cuda.FloatTensor'
|
|
|
|
|
|
|
|
assert batch[1][0]['a'].device.index == 0
|
|
|
|
assert batch[1][0]['a'].type() == 'torch.cuda.FloatTensor'
|
|
|
|
|
|
|
|
assert batch[1][0]['b'].device.index == 0
|
|
|
|
assert batch[1][0]['b'].type() == 'torch.cuda.FloatTensor'
|
|
|
|
|
|
|
|
# namedtuple of tensor
|
|
|
|
BatchType = namedtuple('BatchType', ['a', 'b'])
|
|
|
|
batch = [BatchType(a=torch.rand(2, 3), b=torch.rand(2, 3)) for _ in range(2)]
|
2020-09-09 02:14:17 +00:00
|
|
|
batch = trainer.accelerator_backend.batch_to_device(batch, torch.device('cuda:0'))
|
2020-06-16 02:03:40 +00:00
|
|
|
assert batch[0].a.device.index == 0
|
|
|
|
assert batch[0].a.type() == 'torch.cuda.FloatTensor'
|
2020-06-24 03:41:02 +00:00
|
|
|
|
|
|
|
# non-Tensor that has `.to()` defined
|
|
|
|
class CustomBatchType:
|
|
|
|
def __init__(self):
|
|
|
|
self.a = torch.rand(2, 2)
|
|
|
|
|
|
|
|
def to(self, *args, **kwargs):
|
|
|
|
self.a = self.a.to(*args, **kwargs)
|
|
|
|
return self
|
|
|
|
|
2020-09-09 02:14:17 +00:00
|
|
|
batch = trainer.accelerator_backend.batch_to_device(CustomBatchType(), torch.device('cuda:0'))
|
2020-06-24 03:41:02 +00:00
|
|
|
assert batch.a.type() == 'torch.cuda.FloatTensor'
|
2020-06-27 20:36:45 +00:00
|
|
|
|
|
|
|
# torchtext.data.Batch
|
|
|
|
samples = [
|
|
|
|
{'text': 'PyTorch Lightning is awesome!', 'label': 0},
|
|
|
|
{'text': 'Please make it work with torchtext', 'label': 1}
|
|
|
|
]
|
|
|
|
|
|
|
|
text_field = Field()
|
|
|
|
label_field = LabelField()
|
|
|
|
fields = {
|
|
|
|
'text': ('text', text_field),
|
|
|
|
'label': ('label', label_field)
|
|
|
|
}
|
|
|
|
|
|
|
|
examples = [Example.fromdict(sample, fields) for sample in samples]
|
|
|
|
dataset = Dataset(
|
|
|
|
examples=examples,
|
|
|
|
fields=fields.values()
|
|
|
|
)
|
|
|
|
|
|
|
|
# Batch runs field.process() that numericalizes tokens, but it requires to build dictionary first
|
|
|
|
text_field.build_vocab(dataset)
|
|
|
|
label_field.build_vocab(dataset)
|
|
|
|
|
|
|
|
batch = Batch(data=examples, dataset=dataset)
|
2020-09-09 02:14:17 +00:00
|
|
|
batch = trainer.accelerator_backend.batch_to_device(batch, torch.device('cuda:0'))
|
2020-06-27 20:36:45 +00:00
|
|
|
|
|
|
|
assert batch.text.type() == 'torch.cuda.LongTensor'
|
|
|
|
assert batch.label.type() == 'torch.cuda.LongTensor'
|
2020-08-11 23:28:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires GPU machine")
|
|
|
|
def test_non_blocking():
|
|
|
|
""" Tests that non_blocking=True only gets passed on torch.Tensor.to, but not on other objects. """
|
|
|
|
trainer = Trainer()
|
2020-09-09 02:14:17 +00:00
|
|
|
trainer.accelerator_backend = GPUBackend(trainer)
|
2020-08-11 23:28:37 +00:00
|
|
|
|
|
|
|
batch = torch.zeros(2, 3)
|
|
|
|
with patch.object(batch, 'to', wraps=batch.to) as mocked:
|
2020-09-09 02:14:17 +00:00
|
|
|
batch = trainer.accelerator_backend.batch_to_device(batch, torch.device('cuda:0'))
|
2020-08-11 23:28:37 +00:00
|
|
|
mocked.assert_called_with(torch.device('cuda', 0), non_blocking=True)
|
|
|
|
|
|
|
|
class BatchObject(object):
|
|
|
|
|
|
|
|
def to(self, *args, **kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
batch = BatchObject()
|
|
|
|
with patch.object(batch, 'to', wraps=batch.to) as mocked:
|
2020-09-09 02:14:17 +00:00
|
|
|
batch = trainer.accelerator_backend.batch_to_device(batch, torch.device('cuda:0'))
|
2020-08-11 23:28:37 +00:00
|
|
|
mocked.assert_called_with(torch.device('cuda', 0))
|