Skip to content

Set devices to 1 when it's just Trainer(accelerator='auto') #10192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
10 changes: 5 additions & 5 deletions pytorch_lightning/trainer/connectors/accelerator_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,19 +461,19 @@ def _set_devices_to_cpu_num_processes(self) -> None:
self._map_devices_to_accelerator(DeviceType.CPU)

def _map_devices_to_accelerator(self, accelerator: str) -> bool:
if self.devices is None:
return False
if accelerator == DeviceType.TPU and _TPU_AVAILABLE:
self.devices = self.devices or 1
self.tpu_cores = device_parser.parse_tpu_cores(self.devices)
return True
if accelerator == DeviceType.IPU and _IPU_AVAILABLE:
self.ipus = self.devices
self.ipus = self.devices = self.devices or 1
return True
if accelerator == DeviceType.GPU and torch.cuda.is_available():
self.gpus = self.devices
self.parallel_device_ids = device_parser.parse_gpu_ids(self.devices)
self.gpus = self.devices = self.devices or 1
self.parallel_device_ids = device_parser.parse_gpu_ids(self.gpus)
return True
if accelerator == DeviceType.CPU:
self.devices = self.devices or 1
if not isinstance(self.devices, int):
raise MisconfigurationException(
"The flag `devices` must be an int with `accelerator='cpu'`,"
Expand Down
16 changes: 15 additions & 1 deletion tests/accelerators/test_accelerator_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
SLURMEnvironment,
TorchElasticEnvironment,
)
from pytorch_lightning.utilities import DeviceType, DistributedType
from pytorch_lightning.utilities import _IPU_AVAILABLE, _TPU_AVAILABLE, DeviceType, DistributedType
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from tests.helpers.boring_model import BoringModel
from tests.helpers.runif import RunIf
Expand Down Expand Up @@ -1005,3 +1005,17 @@ def test_unsupported_ipu_choice(monkeypatch):
Trainer(accelerator="ipu", precision="bf16")
with pytest.raises(MisconfigurationException, match=r"accelerator='ipu', precision=64\)` is not supported"):
Trainer(accelerator="ipu", precision=64)


@pytest.mark.skipif(torch.cuda.is_available() or _TPU_AVAILABLE or _IPU_AVAILABLE, reason="test requires to run on CPU")
def test_accelerator_auto_choice_and_devices_cpu():
trainer = Trainer(accelerator="auto")
assert trainer.devices == 1
assert trainer.num_processes == 1


@RunIf(min_gpus=1)
def test_accelerator_auto_choice_and_devices_gpu():
trainer = Trainer(accelerator="auto")
assert trainer.devices == 1
assert trainer.gpus == 1
7 changes: 7 additions & 0 deletions tests/accelerators/test_ipu.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,10 @@ def test_device_type_when_training_plugin_ipu_passed(tmpdir):
assert isinstance(trainer.training_type_plugin, IPUPlugin)
assert trainer._device_type == DeviceType.IPU
assert isinstance(trainer.accelerator, IPUAccelerator)


@RunIf(ipu=True)
def test_accelerator_auto_choice_and_devices_ipu():
trainer = Trainer(accelerator="auto")
assert trainer.devices == 1
assert trainer.ipus == 1
7 changes: 7 additions & 0 deletions tests/accelerators/test_tpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,10 @@ def test_mp_device_dataloader_attribute(_):
dataloader = TPUSpawnPlugin().process_dataloader(DataLoader(dataset))

assert dataloader.dataset == dataset


@RunIf(tpu=True)
def test_accelerator_auto_choice_and_devices_tpu():
trainer = Trainer(accelerator="auto")
assert trainer.devices == 1
assert trainer.tpu_cores == 1