From f6f06d4e42ba9f4cc01a0813a21628353e75a1d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20W=C3=A4lchli?= Date: Fri, 22 Jul 2022 21:34:30 +0200 Subject: [PATCH] Set default strategy to ddp_fork in interactive environments (#13746) --- .../trainer/connectors/accelerator_connector.py | 5 ++++- .../accelerators/test_accelerator_connector.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/pytorch_lightning/trainer/connectors/accelerator_connector.py b/src/pytorch_lightning/trainer/connectors/accelerator_connector.py index 9f87a68b4d..06e4c87d5a 100644 --- a/src/pytorch_lightning/trainer/connectors/accelerator_connector.py +++ b/src/pytorch_lightning/trainer/connectors/accelerator_connector.py @@ -87,6 +87,7 @@ from pytorch_lightning.utilities.imports import ( _HOROVOD_AVAILABLE, _HPU_AVAILABLE, _IPU_AVAILABLE, + _IS_INTERACTIVE, _TORCH_GREATER_EQUAL_1_11, _TPU_AVAILABLE, ) @@ -588,7 +589,9 @@ class AcceleratorConnector: # TODO: lazy initialized device, then here could be self._strategy_flag = "single_device" return SingleDeviceStrategy(device=device) # type: ignore if len(self._parallel_devices) > 1: - return DDPSpawnStrategy.strategy_name + if _IS_INTERACTIVE: + return "ddp_fork" + return "ddp_spawn" return DDPStrategy.strategy_name diff --git a/tests/tests_pytorch/accelerators/test_accelerator_connector.py b/tests/tests_pytorch/accelerators/test_accelerator_connector.py index a04418b62e..58ca7bd259 100644 --- a/tests/tests_pytorch/accelerators/test_accelerator_connector.py +++ b/tests/tests_pytorch/accelerators/test_accelerator_connector.py @@ -424,6 +424,18 @@ def test_strategy_choice_ddp_spawn_cpu(): assert trainer.strategy.launcher._start_method == "spawn" +@RunIf(skip_windows=True) +@mock.patch("pytorch_lightning.trainer.connectors.accelerator_connector._IS_INTERACTIVE", True) +def test_strategy_choice_ddp_fork_in_interactive(): + """Test that when accelerator and strategy are unspecified, the connector chooses DDP Fork in interactive + environments by default.""" + trainer = Trainer(devices=2) + assert isinstance(trainer.accelerator, CPUAccelerator) + assert isinstance(trainer.strategy, DDPSpawnStrategy) + assert isinstance(trainer.strategy.cluster_environment, LightningEnvironment) + assert trainer.strategy.launcher._start_method == "fork" + + @RunIf(skip_windows=True) def test_strategy_choice_ddp_fork_cpu(): trainer = Trainer(strategy="ddp_fork", accelerator="cpu", devices=2)