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
Changes from 4 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
27 changes: 26 additions & 1 deletion pytorch_lightning/accelerators/accelerator_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ 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 @@ -500,7 +501,10 @@ def set_distributed_mode(self, distributed_backend: Optional[str] = None):
else:
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 @@ -542,6 +546,27 @@ def _set_horovod_backend(self):
else:
self.num_processes = hvd.local_size()

def check_ipython_compatibility(self):
"""Raises a `MisconfigurationException` if the accelerator is not compatible with IPython and code is run in an IPython kernel."""
if self._distrib_type not in self.ipython_compatible_distrib_types:
# check ipython env
import sys
in_ipython = False
in_ipython_kernel = False
if 'IPython' in sys.modules:
from IPython import get_ipython
ip = get_ipython()
in_ipython = ip is not None

if in_ipython:
in_ipython_kernel = getattr(ip, 'kernel', None) is not None

if in_ipython_kernel:
raise MisconfigurationException(
"Selected distributed backend {self._distrib_type} not compatible with IPython environment"
"Run your code as a script, or choose one of compatible backends {self.ipython_compatible_distrib_types} as accelerator backend"
)

def check_horovod(self):
"""Raises a `MisconfigurationException` if the Trainer is not configured correctly for Horovod."""
if not _HOROVOD_AVAILABLE:
Expand Down