Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
_HOROVOD_AVAILABLE,
_HPU_AVAILABLE,
_IPU_AVAILABLE,
_IS_INTERACTIVE,
_TORCH_GREATER_EQUAL_1_11,
_TPU_AVAILABLE,
)
Expand Down Expand Up @@ -588,7 +589,9 @@ def _choose_strategy(self) -> Union[Strategy, str]:
# 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

Expand Down
12 changes: 12 additions & 0 deletions tests/tests_pytorch/accelerators/test_accelerator_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down