From f8a434692e1ff831897b7cf024afe482f96636a7 Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Thu, 1 Oct 2020 02:51:03 +0900 Subject: [PATCH 1/4] Fix exception chaining --- pytorch_lightning/accelerators/ddp2_backend.py | 2 +- pytorch_lightning/metrics/metric.py | 4 ++-- pytorch_lightning/trainer/logging.py | 4 ++-- pytorch_lightning/utilities/parsing.py | 4 ++-- tests/base/models.py | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pytorch_lightning/accelerators/ddp2_backend.py b/pytorch_lightning/accelerators/ddp2_backend.py index 1b55a983c894e..4d7b340ee380c 100644 --- a/pytorch_lightning/accelerators/ddp2_backend.py +++ b/pytorch_lightning/accelerators/ddp2_backend.py @@ -49,7 +49,7 @@ def _resolve_task_idx(self): self.task_idx = int(os.environ['LOCAL_RANK']) except Exception as e: 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 e def train(self): model = self.trainer.model diff --git a/pytorch_lightning/metrics/metric.py b/pytorch_lightning/metrics/metric.py index 45c50b084956f..4ec58185a8861 100644 --- a/pytorch_lightning/metrics/metric.py +++ b/pytorch_lightning/metrics/metric.py @@ -165,7 +165,7 @@ def aggregate(self, *tensors: torch.Tensor) -> torch.Tensor: """ try: return torch.cat(tensors).mean(0) - except (ValueError, TypeError): + except (ValueError, TypeError) as e: if isinstance(tensors[0], Mapping): return {k: torch.stack([tensor[k] for tensor in tensors]).mean(0) for k in tensors[0].keys()} elif isinstance(tensors[0], Sequence) and not isinstance(tensors[0], torch.Tensor): @@ -173,7 +173,7 @@ def aggregate(self, *tensors: torch.Tensor) -> torch.Tensor: elif isinstance(tensors[0], torch.Tensor): return torch.stack(tensors).mean(0) else: - raise TypeError("unknown metric value format to aggregate") + raise TypeError("unknown metric value format to aggregate") from e @staticmethod def compute(self, data: Any, output: Any): diff --git a/pytorch_lightning/trainer/logging.py b/pytorch_lightning/trainer/logging.py index 26572feb5e35e..1ac4e7e1c865c 100644 --- a/pytorch_lightning/trainer/logging.py +++ b/pytorch_lightning/trainer/logging.py @@ -141,13 +141,13 @@ def process_dict_result(self, output, train=False): if train: try: loss = output['loss'] - except Exception: + except Exception as e: if isinstance(output, torch.Tensor): loss = output else: raise RuntimeError( 'No `loss` value in the dictionary returned from `model.training_step()`.' - ) + ) from e # when using dp need to reduce the loss if self.use_dp or self.use_ddp2: diff --git a/pytorch_lightning/utilities/parsing.py b/pytorch_lightning/utilities/parsing.py index dab1127579b87..82fa6e865f242 100644 --- a/pytorch_lightning/utilities/parsing.py +++ b/pytorch_lightning/utilities/parsing.py @@ -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 e: + raise AttributeError(f'Missing attribute "{key}"') from e def __setattr__(self, key, val): self[key] = val diff --git a/tests/base/models.py b/tests/base/models.py index 9c319add4aca1..60dcf1777f1b2 100644 --- a/tests/base/models.py +++ b/tests/base/models.py @@ -10,9 +10,9 @@ try: from test_tube import HyperOptArgumentParser -except ImportError: +except ImportError as e: # TODO: this should be discussed and moved out of this package - raise ImportError('Missing test-tube package.') + raise ImportError('Missing test-tube package.') from e from pytorch_lightning.core.lightning import LightningModule From 69f521eaf0a1d99628a100b193e1552c0a8aab8e Mon Sep 17 00:00:00 2001 From: Jirka Borovec Date: Thu, 1 Oct 2020 13:55:40 +0200 Subject: [PATCH 2/4] names --- pytorch_lightning/accelerators/ddp2_backend.py | 4 ++-- pytorch_lightning/trainer/logging.py | 4 ++-- pytorch_lightning/utilities/parsing.py | 4 ++-- tests/base/models.py | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pytorch_lightning/accelerators/ddp2_backend.py b/pytorch_lightning/accelerators/ddp2_backend.py index 4d7b340ee380c..28af6558a0fef 100644 --- a/pytorch_lightning/accelerators/ddp2_backend.py +++ b/pytorch_lightning/accelerators/ddp2_backend.py @@ -47,9 +47,9 @@ def _resolve_task_idx(self): # 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) from e + raise MisconfigurationException(m) from exp def train(self): model = self.trainer.model diff --git a/pytorch_lightning/trainer/logging.py b/pytorch_lightning/trainer/logging.py index 1ac4e7e1c865c..b585647fb5a0e 100644 --- a/pytorch_lightning/trainer/logging.py +++ b/pytorch_lightning/trainer/logging.py @@ -141,13 +141,13 @@ def process_dict_result(self, output, train=False): if train: try: loss = output['loss'] - except Exception as e: + 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 e + ) from exp # when using dp need to reduce the loss if self.use_dp or self.use_ddp2: diff --git a/pytorch_lightning/utilities/parsing.py b/pytorch_lightning/utilities/parsing.py index 82fa6e865f242..c562f780e88ee 100644 --- a/pytorch_lightning/utilities/parsing.py +++ b/pytorch_lightning/utilities/parsing.py @@ -158,8 +158,8 @@ class AttributeDict(Dict): def __getattr__(self, key): try: return self[key] - except KeyError as e: - raise AttributeError(f'Missing attribute "{key}"') from e + except KeyError as exp: + raise AttributeError(f'Missing attribute "{key}"') from exp def __setattr__(self, key, val): self[key] = val diff --git a/tests/base/models.py b/tests/base/models.py index 60dcf1777f1b2..79f407d62aadf 100644 --- a/tests/base/models.py +++ b/tests/base/models.py @@ -10,9 +10,9 @@ try: from test_tube import HyperOptArgumentParser -except ImportError as e: +except ImportError as exp: # TODO: this should be discussed and moved out of this package - raise ImportError('Missing test-tube package.') from e + raise ImportError('Missing test-tube package.') from exp from pytorch_lightning.core.lightning import LightningModule From 83dc13960a8f0109612db6429f4acdec364e2c28 Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Thu, 1 Oct 2020 21:44:26 +0900 Subject: [PATCH 3/4] Change exception names for consistency Co-authored-by: Nicki Skafte --- pytorch_lightning/metrics/metric.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytorch_lightning/metrics/metric.py b/pytorch_lightning/metrics/metric.py index 4ec58185a8861..082d859e3f2b1 100644 --- a/pytorch_lightning/metrics/metric.py +++ b/pytorch_lightning/metrics/metric.py @@ -165,7 +165,7 @@ def aggregate(self, *tensors: torch.Tensor) -> torch.Tensor: """ try: return torch.cat(tensors).mean(0) - except (ValueError, TypeError) as e: + except (ValueError, TypeError) as exp: if isinstance(tensors[0], Mapping): return {k: torch.stack([tensor[k] for tensor in tensors]).mean(0) for k in tensors[0].keys()} elif isinstance(tensors[0], Sequence) and not isinstance(tensors[0], torch.Tensor): From 60096b1aa2c66c924e2b24e17445b29d0284be7b Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Thu, 1 Oct 2020 21:44:34 +0900 Subject: [PATCH 4/4] Change exception names for consistency Co-authored-by: Nicki Skafte --- pytorch_lightning/metrics/metric.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytorch_lightning/metrics/metric.py b/pytorch_lightning/metrics/metric.py index 082d859e3f2b1..50e23cb3272ed 100644 --- a/pytorch_lightning/metrics/metric.py +++ b/pytorch_lightning/metrics/metric.py @@ -173,7 +173,7 @@ def aggregate(self, *tensors: torch.Tensor) -> torch.Tensor: elif isinstance(tensors[0], torch.Tensor): return torch.stack(tensors).mean(0) else: - raise TypeError("unknown metric value format to aggregate") from e + raise TypeError("unknown metric value format to aggregate") from exp @staticmethod def compute(self, data: Any, output: Any):