Use `raise .. from ..` to explicitly chain exceptions (#3750)

* Fix exception chaining

* names

* Change exception names for consistency

Co-authored-by: Nicki Skafte <skaftenicki@gmail.com>

* Change exception names for consistency

Co-authored-by: Nicki Skafte <skaftenicki@gmail.com>

Co-authored-by: Jirka Borovec <jirka@pytorchlightning.ai>
Co-authored-by: Nicki Skafte <skaftenicki@gmail.com>
This commit is contained in:
Akihiro Nitta 2020-10-02 04:45:44 +09:00 committed by GitHub
parent e17712e5c3
commit ebc1b23fa3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 8 additions and 8 deletions

View File

@ -47,9 +47,9 @@ class DDP2Backend(DDPBase):
# torchelastic or general non_slurm ddp2
try:
self.task_idx = int(os.environ['LOCAL_RANK'])
except Exception as e:
except Exception as exp:
m = 'ddp2 only works in SLURM or via torchelastic with the WORLD_SIZE, LOCAL_RANK, GROUP_RANK flags'
raise MisconfigurationException(m)
raise MisconfigurationException(m) from exp
def train(self):
model = self.trainer.model

View File

@ -141,13 +141,13 @@ class TrainerLoggingMixin(ABC):
if train:
try:
loss = output['loss']
except Exception:
except Exception as exp:
if isinstance(output, torch.Tensor):
loss = output
else:
raise RuntimeError(
'No `loss` value in the dictionary returned from `model.training_step()`.'
)
) from exp
# when using dp need to reduce the loss
if self.use_dp or self.use_ddp2:

View File

@ -158,8 +158,8 @@ class AttributeDict(Dict):
def __getattr__(self, key):
try:
return self[key]
except KeyError:
raise AttributeError(f'Missing attribute "{key}"')
except KeyError as exp:
raise AttributeError(f'Missing attribute "{key}"') from exp
def __setattr__(self, key, val):
self[key] = val

View File

@ -10,9 +10,9 @@ from tests.base.datasets import TrialMNIST, AverageDataset, MNIST
try:
from test_tube import HyperOptArgumentParser
except ImportError:
except ImportError as exp:
# TODO: this should be discussed and moved out of this package
raise ImportError('Missing test-tube package.')
raise ImportError('Missing test-tube package.') from exp
from pytorch_lightning.core.lightning import LightningModule