|
21 | 21 | from torch.nn.parallel.distributed import DistributedDataParallel |
22 | 22 |
|
23 | 23 | import pytorch_lightning as pl |
24 | | -from pytorch_lightning import Trainer |
| 24 | +from pytorch_lightning import seed_everything, Trainer |
25 | 25 | from pytorch_lightning.callbacks import Callback |
26 | 26 | from pytorch_lightning.demos.boring_classes import BoringModel |
27 | 27 | from pytorch_lightning.strategies import DDPStrategy |
| 28 | +from tests_pytorch.helpers.datamodules import ClassifDataModule |
28 | 29 | from tests_pytorch.helpers.runif import RunIf |
29 | | -from tests_pytorch.strategies import ddp_model |
30 | | -from tests_pytorch.utilities.distributed import call_training_script |
| 30 | +from tests_pytorch.helpers.simple_models import ClassificationModel |
31 | 31 |
|
32 | | -CLI_ARGS = "--max_epochs 1 --accelerator gpu --devices 2 --strategy ddp" |
33 | 32 |
|
| 33 | +@RunIf(min_cuda_gpus=2, standalone=True) |
| 34 | +def test_multi_gpu_model_ddp_fit_only(tmpdir): |
| 35 | + dm = ClassifDataModule() |
| 36 | + model = ClassificationModel() |
| 37 | + trainer = Trainer(default_root_dir=tmpdir, max_epochs=1, accelerator="gpu", devices=2, strategy="ddp") |
| 38 | + trainer.fit(model, datamodule=dm) |
34 | 39 |
|
35 | | -@RunIf(min_cuda_gpus=2) |
36 | | -@pytest.mark.parametrize("as_module", [True, False]) |
37 | | -def test_multi_gpu_model_ddp_fit_only(tmpdir, as_module): |
38 | | - # call the script |
39 | | - call_training_script(ddp_model, CLI_ARGS, "fit", tmpdir, timeout=120, as_module=as_module) |
40 | 40 |
|
41 | | - # load the results of the script |
42 | | - result_path = os.path.join(tmpdir, "ddp.result") |
43 | | - result = torch.load(result_path) |
| 41 | +@RunIf(min_cuda_gpus=2, standalone=True) |
| 42 | +def test_multi_gpu_model_ddp_test_only(tmpdir): |
| 43 | + dm = ClassifDataModule() |
| 44 | + model = ClassificationModel() |
| 45 | + trainer = Trainer(default_root_dir=tmpdir, max_epochs=1, accelerator="gpu", devices=2, strategy="ddp") |
| 46 | + trainer.test(model, datamodule=dm) |
44 | 47 |
|
45 | | - # verify the file wrote the expected outputs |
46 | | - assert result["status"] == "complete" |
47 | 48 |
|
| 49 | +@RunIf(min_cuda_gpus=2, standalone=True) |
| 50 | +def test_multi_gpu_model_ddp_fit_test(tmpdir): |
| 51 | + seed_everything(4321) |
| 52 | + dm = ClassifDataModule() |
| 53 | + model = ClassificationModel() |
| 54 | + trainer = Trainer(default_root_dir=tmpdir, max_epochs=1, accelerator="gpu", devices=2, strategy="ddp") |
| 55 | + trainer.fit(model, datamodule=dm) |
| 56 | + result = trainer.test(model, datamodule=dm) |
48 | 57 |
|
49 | | -@RunIf(min_cuda_gpus=2) |
50 | | -@pytest.mark.parametrize("as_module", [True, False]) |
51 | | -def test_multi_gpu_model_ddp_test_only(tmpdir, as_module): |
52 | | - # call the script |
53 | | - call_training_script(ddp_model, CLI_ARGS, "test", tmpdir, as_module=as_module) |
54 | | - |
55 | | - # load the results of the script |
56 | | - result_path = os.path.join(tmpdir, "ddp.result") |
57 | | - result = torch.load(result_path) |
58 | | - |
59 | | - # verify the file wrote the expected outputs |
60 | | - assert result["status"] == "complete" |
61 | | - |
62 | | - |
63 | | -@RunIf(min_cuda_gpus=2) |
64 | | -@pytest.mark.parametrize("as_module", [True, False]) |
65 | | -def test_multi_gpu_model_ddp_fit_test(tmpdir, as_module): |
66 | | - # call the script |
67 | | - call_training_script(ddp_model, CLI_ARGS, "fit_test", tmpdir, timeout=20, as_module=as_module) |
68 | | - |
69 | | - # load the results of the script |
70 | | - result_path = os.path.join(tmpdir, "ddp.result") |
71 | | - result = torch.load(result_path) |
72 | | - |
73 | | - # verify the file wrote the expected outputs |
74 | | - assert result["status"] == "complete" |
75 | | - |
76 | | - model_outs = result["result"] |
77 | | - for out in model_outs: |
| 58 | + for out in result: |
78 | 59 | assert out["test_acc"] > 0.7 |
79 | 60 |
|
80 | 61 |
|
|
0 commit comments