From 34f92801f24c705842b2597c56e818d6dc6dbb04 Mon Sep 17 00:00:00 2001 From: Kaushik B Date: Mon, 11 Apr 2022 14:41:52 +0530 Subject: [PATCH 1/4] Raise MisconfigurationException when the accelerator is available but the user passes invalid ([]/0/'0') values to the devices flag --- .../trainer/connectors/accelerator_connector.py | 6 ++++++ tests/accelerators/test_accelerator_connector.py | 16 +++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/pytorch_lightning/trainer/connectors/accelerator_connector.py b/pytorch_lightning/trainer/connectors/accelerator_connector.py index 95990a27427ca..652173854da70 100644 --- a/pytorch_lightning/trainer/connectors/accelerator_connector.py +++ b/pytorch_lightning/trainer/connectors/accelerator_connector.py @@ -413,6 +413,12 @@ def _check_device_config_and_set_final_flags( self._num_nodes_flag = int(num_nodes) if num_nodes is not None else 1 self._devices_flag = devices + if self._devices_flag in ([], 0, "0"): + raise MisconfigurationException( + f"`Trainer(devices={self._devices_flag!r})` value is not a valid input" + f" using {self._accelerator_flag} accelerator." + ) + # TODO: Delete this method when num_processes, gpus, ipus and tpu_cores gets removed self._map_deprecated_devices_specific_info_to_accelerator_and_device_flag( devices, num_processes, gpus, ipus, tpu_cores diff --git a/tests/accelerators/test_accelerator_connector.py b/tests/accelerators/test_accelerator_connector.py index 02c946e186b1e..1d64705d1bab0 100644 --- a/tests/accelerators/test_accelerator_connector.py +++ b/tests/accelerators/test_accelerator_connector.py @@ -504,15 +504,6 @@ def test_accelerator_cpu(_): trainer = Trainer(accelerator="cpu", gpus=1) -@mock.patch("torch.cuda.is_available", return_value=False) -@pytest.mark.parametrize("devices", ["0", 0, []]) -def test_passing_zero_and_empty_list_to_devices_flag(_, devices): - with pytest.raises( - MisconfigurationException, match="can not run on your system since the accelerator is not available." - ): - Trainer(accelerator="gpu", devices=devices) - - @RunIf(min_gpus=1) def test_accelerator_gpu(): trainer = Trainer(accelerator="gpu", devices=1) @@ -1014,3 +1005,10 @@ def __init__(self, **kwargs): def test_plugin_only_one_instance_for_one_type(plugins, expected): with pytest.raises(MisconfigurationException, match=f"Received multiple values for {expected}"): Trainer(plugins=plugins) + + +@pytest.mark.parametrize("accelerator", ("cpu", "gpu", "tpu", "ipu")) +@pytest.mark.parametrize("devices", ("0", 0, [])) +def test_passing_zero_and_empty_list_to_devices_flag(accelerator, devices): + with pytest.raises(MisconfigurationException, match="value is not a valid input using"): + Trainer(accelerator=accelerator, devices=devices) From 79b618b7810e54e282559e8f892c6be09ad2d8fd Mon Sep 17 00:00:00 2001 From: Kaushik B Date: Mon, 11 Apr 2022 14:45:57 +0530 Subject: [PATCH 2/4] Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a2a83ab54d06..14b0c73c0f175 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -117,7 +117,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Don't raise a warning when `nn.Module` is not saved under hparams ([#12669](https://github.com/PyTorchLightning/pytorch-lightning/pull/12669)) -- +- Raise `MisconfigurationException` when the accelerator is available but the user passes invalid `([]/0/"0")` values to the `devices` flag ([#12708](https://github.com/PyTorchLightning/pytorch-lightning/pull/12708)) ## [1.6.0] - 2022-03-29 From abe05ddb6622488cd6005b2be77ac79da5dffc68 Mon Sep 17 00:00:00 2001 From: Kaushik B Date: Mon, 11 Apr 2022 14:56:47 +0530 Subject: [PATCH 3/4] Better error message --- .../trainer/connectors/accelerator_connector.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pytorch_lightning/trainer/connectors/accelerator_connector.py b/pytorch_lightning/trainer/connectors/accelerator_connector.py index 652173854da70..d7dbb30c236a3 100644 --- a/pytorch_lightning/trainer/connectors/accelerator_connector.py +++ b/pytorch_lightning/trainer/connectors/accelerator_connector.py @@ -414,9 +414,14 @@ def _check_device_config_and_set_final_flags( self._devices_flag = devices if self._devices_flag in ([], 0, "0"): + accelerator_name = ( + self._accelerator_flag.__class__.__qualname__ + if isinstance(self._accelerator_flag, Accelerator) + else self._accelerator_flag + ) raise MisconfigurationException( f"`Trainer(devices={self._devices_flag!r})` value is not a valid input" - f" using {self._accelerator_flag} accelerator." + f" using {accelerator_name} accelerator." ) # TODO: Delete this method when num_processes, gpus, ipus and tpu_cores gets removed From 9317d0308cec8bd61576c5ed203c9adf24a65cf0 Mon Sep 17 00:00:00 2001 From: Kaushik B Date: Mon, 11 Apr 2022 17:32:33 +0530 Subject: [PATCH 4/4] Fix test --- tests/accelerators/test_cpu.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/accelerators/test_cpu.py b/tests/accelerators/test_cpu.py index 8abacd47afcec..ce9f6d6b21ca9 100644 --- a/tests/accelerators/test_cpu.py +++ b/tests/accelerators/test_cpu.py @@ -69,7 +69,7 @@ def load_checkpoint(self, checkpoint_path: Union[str, Path]) -> Dict[str, Any]: func(model, ckpt_path=checkpoint_path) -@pytest.mark.parametrize("devices", ([3], -1, 0)) +@pytest.mark.parametrize("devices", ([3], -1)) def test_invalid_devices_with_cpu_accelerator(devices): """Test invalid device flag raises MisconfigurationException with CPUAccelerator.""" with pytest.raises(MisconfigurationException, match="should be an int > 0"):