pytorch-lightning: [RFC] Revisit Usage of MisconfigurationException
Proposed refactor
Replace Misconfiguration Exception with more diverse error types.
Motivation
Currently we are using the MisconfigurationException for everything even if they are not directly related to the user’s configuration. This means that the type of exception has no additional information about the kind of error. Python has some builtin exception types for that like RuntimeError, ValueError, TypeError etc. To gain more insights from the actual exception type, I propose to not use MisconfigurationException any longer and replace it with the other error types.
Pitch
Have Lightning raising appropriate error types instead of just MisconfigurationException everywhere.
Additional context
If we want to have some Lightning-Unique Exceptions we could still do something like
class LightningValueError(ValueError):
pass
for the necessary error types and then raise these. This has the advantage that people could still have custom error handling also depending on types and statements like except ValueError would still catch those but they could also decide to only catch Lightning Errors.
If you enjoy Lightning, check out our other projects! ⚡
-
Metrics: Machine learning metrics for distributed, scalable PyTorch applications.
-
Lite: enables pure PyTorch users to scale their existing code on any kind of device while retaining full control over their own loops and optimization logic.
-
Flash: The fastest way to get a Lightning baseline! A collection of tasks for fast prototyping, baselining, fine-tuning, and solving problems with deep learning.
-
Bolts: Pretrained SOTA Deep Learning models, callbacks, and more for research and production with PyTorch Lightning and PyTorch.
-
Lightning Transformers: Flexible interface for high-performance research using SOTA Transformers leveraging Pytorch Lightning, Transformers, and Hydra.
PS: Not sure under which part of the code to put it, since it probably touches all of them 😄
cc @borda @justusschock @awaelchli @akihironitta @rohitgr7 @ananthsub @tchaton @carmocca
EDIT: current usage of the MisconfigurationException: https://grep.app/search?q=MisconfigurationException&filter[repo][0]=PyTorchLightning/pytorch-lightning
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 7
- Comments: 24 (16 by maintainers)
I think the best would be to open a draft PR so that we can directly review the code at hand 😃 Of course if you have any questions before that, feel free to ask here:)
Yes, let’s go with the 3rd option. This is my suggested implementation:
Where we would use these custom protected variants until
MisconfigurationExceptionis removed.The work left for you @calebbuffa would be to update all usages of
MisconfigurationExceptionto use these new alternativesI believe we can deprecate it doing this:
where importing
MisconfigurationExceptionis what shows the deprecation message.Looks great so far @calebbuffa. Left minor comments
@awaelchli The reasoning behind creating this DeprecatedException “magic” was to not break users who had
except MisconfigurationExceptionin their code.The above is the reason
What is this coupling?
Yes, they would all be removed after the deprecation process is over.
This would not be different to what
MisconfigurationExceptiondoes. The end goal to use the more informative and native exceptions (ValueError, TypeError, …), but what’s tricky is how to get there whilst minimizing breaking changesNote that not all of them should be
ValueError.TypeError,RuntimeError, or a different exception might fit better depending on the case. I just wrote_ValueErrorin my previous message for conciseness.Choosing the most fitting exception class is what’s most important: https://docs.python.org/3/library/exceptions.html#base-classes
Agreed, so in option 3 we would want a deprecation warning if
MisconfigurationExceptionis imported but not if an error inheriting from it is, correct? Also, did we lean towards lightning specific sub classes (i.e LightingValueError) as originally proposed or using generic errors?