Skip to content
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

Ensure accelerator is valid if running interactively #5970

Merged
merged 25 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ca6dcb7
added ipython env check
ifsheldon Feb 14, 2021
5ee0322
Merge branch 'master' into ipython_env_check
tchaton Feb 15, 2021
df6348d
extracted a method and added a compatibility list
ifsheldon Feb 15, 2021
c2d3a12
Merge branch 'master' into ipython_env_check
ifsheldon Feb 15, 2021
5ad468e
fixed fstring
ifsheldon Feb 16, 2021
46dcd8d
inverted if, added early return
ifsheldon Feb 16, 2021
e37bbd6
Merge branch 'ipython_env_check' of https://github.com/ifsheldon/pyto…
ifsheldon Feb 16, 2021
6739c36
changed to only checking ipython env
ifsheldon Feb 17, 2021
165dffc
Merge branch 'master' into ipython_env_check
awaelchli Feb 18, 2021
da99886
move import to top
ifsheldon Feb 18, 2021
799059c
Merge branch 'ipython_env_check' of https://github.com/ifsheldon/pyto…
ifsheldon Feb 18, 2021
a6eaec2
fix formatting, docstring and line length
awaelchli Feb 18, 2021
86f2404
Merge branch 'master' into ipython_env_check
awaelchli Feb 18, 2021
c0ff8ce
fix isort
awaelchli Feb 18, 2021
70b7dbf
add test
awaelchli Feb 18, 2021
53dc217
moved compatible list to enum method
ifsheldon Feb 19, 2021
e2098c6
merged remote changes
ifsheldon Feb 19, 2021
0676368
fixed a minor print issue
ifsheldon Feb 19, 2021
386e3a9
changed to use utilities.imports
ifsheldon Feb 19, 2021
4384880
added a method to check ipython compatibility
ifsheldon Feb 19, 2021
73b18b1
fixed a minor issue when _distrib_type is None
ifsheldon Feb 19, 2021
d2dedab
Fix test
carmocca Feb 22, 2021
4619606
Merge branch 'master' into ipython_env_check
carmocca Feb 22, 2021
990c03e
IPython -> interactive
carmocca Feb 23, 2021
0cbb515
Update tests/accelerators/test_accelerator_connector.py
carmocca Feb 23, 2021
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
24 changes: 24 additions & 0 deletions pytorch_lightning/trainer/connectors/accelerator_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import os
import sys
from typing import List, Optional, Sequence, Union

import torch
Expand Down Expand Up @@ -84,6 +85,9 @@ def __init__(
# initialization
self._device_type = DeviceType.CPU
self._distrib_type = None
self.ipython_compatible_distrib_types = [
DistributedType.DP, DistributedType.DDP_SPAWN, DistributedType.DDP_SHARDED_SPAWN
]

self.num_processes = num_processes
self.tpu_cores = device_parser.parse_tpu_cores(tpu_cores)
Expand Down Expand Up @@ -515,6 +519,9 @@ def set_distributed_mode(self, distributed_backend: Optional[str] = None):
rank_zero_warn('You are running on single node with no parallelization, so distributed has no effect.')
self._distrib_type = None

# finished configuring self._distrib_type, check ipython environment
self.check_ipython_compatibility()

# for DDP overwrite nb processes by requested GPUs
if (
self._device_type == DeviceType.GPU
Expand Down Expand Up @@ -556,6 +563,23 @@ def _set_horovod_backend(self):
else:
self.num_processes = hvd.local_size()

def check_ipython_compatibility(self):
"""
Raises a `MisconfigurationException` if the accelerator and/or plugin is not compatible with IPython
and code is run in an IPython kernel.
"""
if self._distrib_type in self.ipython_compatible_distrib_types:
return
# check ipython env
if "IPython" in sys.modules:
from IPython import get_ipython
if get_ipython() is not None:
raise MisconfigurationException(
f"Selected distributed backend {self._distrib_type} is not compatible with IPython environment."
f" Run your code as a script, or choose one of the compatible backends:"
f" {', '.join(self.ipython_compatible_distrib_types)}"
)

def check_horovod(self):
"""Raises a `MisconfigurationException` if the Trainer is not configured correctly for Horovod."""
if not _HOROVOD_AVAILABLE:
Expand Down
17 changes: 17 additions & 0 deletions tests/accelerators/test_accelerator_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
# limitations under the License

import os
import sys
from unittest import mock
from unittest.mock import Mock

import pytest
import torch
Expand All @@ -25,6 +27,7 @@
from pytorch_lightning.callbacks import Callback
from pytorch_lightning.plugins import DDP2Plugin, DDPPlugin, DDPSpawnPlugin, PrecisionPlugin, SingleDevicePlugin
from pytorch_lightning.plugins.environments import ClusterEnvironment, SLURMEnvironment, TorchElasticEnvironment
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from tests.helpers.boring_model import BoringModel


Expand Down Expand Up @@ -378,3 +381,17 @@ def on_fit_start(self, trainer, pl_module):

with pytest.raises(SystemExit):
trainer.fit(model)


@mock.patch.dict(sys.modules, {"IPython": Mock()})
@mock.patch("IPython.get_ipython")
@mock.patch('torch.cuda.device_count', return_value=2)
def test_ipython_incompatible_backend_error(device_count_mock, ipython_mock):
with pytest.raises(MisconfigurationException, match="backend ddp is not compatible"):
Trainer(accelerator="ddp", gpus=2)

with pytest.raises(MisconfigurationException, match="backend ddp is not compatible"):
Trainer(accelerator="ddp_cpu", num_processes=2)

with pytest.raises(MisconfigurationException, match="backend ddp2 is not compatible"):
Trainer(accelerator="ddp2", gpus=2)