Skip to content

Commit 4008f9c

Browse files
authored
Convert subprocess test to standalone test (#14101)
1 parent 5396b18 commit 4008f9c

File tree

7 files changed

+55
-149
lines changed

7 files changed

+55
-149
lines changed

tests/tests_pytorch/run_standalone_tasks.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ fi
3434
# test that a user can manually launch individual processes
3535
echo "Running manual ddp launch test"
3636
export PYTHONPATH="${PYTHONPATH}:$(pwd)"
37-
args="--trainer.accelerator gpu --trainer.devices 2 --trainer.strategy ddp --trainer.max_epochs=1 --trainer.limit_train_batches=1 --trainer.limit_val_batches=1 --trainer.limit_test_batches=1"
38-
MASTER_ADDR="localhost" MASTER_PORT=1234 LOCAL_RANK=1 python ../../examples/convert_from_pt_to_pl/image_classifier_5_lightning_datamodule.py ${args} &
39-
MASTER_ADDR="localhost" MASTER_PORT=1234 LOCAL_RANK=0 python ../../examples/convert_from_pt_to_pl/image_classifier_5_lightning_datamodule.py ${args}
37+
args="fit --trainer.accelerator gpu --trainer.devices 2 --trainer.strategy ddp --trainer.max_epochs=1 --trainer.limit_train_batches=1 --trainer.limit_val_batches=1 --trainer.limit_test_batches=1"
38+
MASTER_ADDR="localhost" MASTER_PORT=1234 LOCAL_RANK=1 python strategies/scripts/cli_script.py ${args} &
39+
MASTER_ADDR="localhost" MASTER_PORT=1234 LOCAL_RANK=0 python strategies/scripts/cli_script.py ${args}
40+
41+
# test that ddp can launched as a module (-m option)
42+
echo "Running ddp example as module"
43+
python -m strategies.scripts.cli_script ${args}

tests/tests_pytorch/serve/__init__.py

Whitespace-only changes.

tests/tests_pytorch/strategies/ddp_model.py

Lines changed: 0 additions & 58 deletions
This file was deleted.

tests/tests_pytorch/strategies/scripts/__init__.py

Whitespace-only changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright The PyTorch Lightning team.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""A trivial script that wraps a LightningCLI around the BoringModel and BoringDataModule."""
15+
from pytorch_lightning.cli import LightningCLI
16+
from pytorch_lightning.demos.boring_classes import BoringDataModule, BoringModel
17+
18+
if __name__ == "__main__":
19+
LightningCLI(
20+
BoringModel,
21+
BoringDataModule,
22+
seed_everything_default=42,
23+
save_config_overwrite=True,
24+
)

tests/tests_pytorch/strategies/test_ddp.py

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,60 +21,41 @@
2121
from torch.nn.parallel.distributed import DistributedDataParallel
2222

2323
import pytorch_lightning as pl
24-
from pytorch_lightning import Trainer
24+
from pytorch_lightning import seed_everything, Trainer
2525
from pytorch_lightning.callbacks import Callback
2626
from pytorch_lightning.demos.boring_classes import BoringModel
2727
from pytorch_lightning.strategies import DDPStrategy
28+
from tests_pytorch.helpers.datamodules import ClassifDataModule
2829
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
3131

32-
CLI_ARGS = "--max_epochs 1 --accelerator gpu --devices 2 --strategy ddp"
3332

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)
3439

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)
4040

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)
4447

45-
# verify the file wrote the expected outputs
46-
assert result["status"] == "complete"
4748

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)
4857

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:
7859
assert out["test_acc"] > 0.7
7960

8061

tests/tests_pytorch/utilities/distributed.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)