-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Fix save_hyperparameters
for multiple inheritance and mixins
#16369
Fix save_hyperparameters
for multiple inheritance and mixins
#16369
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm ok with this change since it doesn't contradict #14151 and the existing tests :)
However, I believe in the future we need to be able to guarantee that calling save_hyperparameters
leads to a configuration that always can restore the model instance. We can't guarantee it at the moment, because it is allowed to call save_hyperparameters
multiple times (for example like here in multiple-inheritance) and this can easily break the contract unless the user handles the passing of args to super-inits in particular ways (like in the test).
Co-authored-by: Adrian Wälchli <[email protected]>
To be perfectly honest, I think that the current behaviour in terms of how class A(pl.LightningModule):
def __init__(self, foo, bar):
print(f"A({foo=}, {bar=})")
super().__init__()
self.save_hyperparameters()
class B(A):
def __init__(self, bam):
print(f"B({bam=})")
super().__init__(foo=2, bar=3)
self.save_hyperparameters()
model = B(bam=1)
print(model.hparams)
model_copy = B(**model.hparams) # Boom! This code is broken. But I don't think that this code is broken because of our Note, that this doesn't have anything to do with class A:
def __init__(self, foo, bar):
print(f"A({foo=}, {bar=})")
super().__init__()
class B(A):
def __init__(self, bam):
print(f"B({bam=})")
super().__init__(foo=2, bar=3)
kwargs_for_A = {"foo": 2, "bar": 3}
model = A(**kwargs_for_A)
model_b = B(bam=1)
# This should work under Liskov substitution:
assert isinstance(model_b, A)
model_specialization = type(model_b)(**kwargs_for_A) # Boom! So in my opinion, we shouldn't aim to support this kind of broken inheritance. IMHO, it should be sufficient to warn the user or maybe raise an Exception, if we are able to detect such situations, but I don't think that we should try to do anything "smart" to try and "support" this kind of broken inheritance. |
Co-authored-by: Adrian Wälchli <[email protected]> (cherry picked from commit 01668bf)
Co-authored-by: Adrian Wälchli <[email protected]> (cherry picked from commit 01668bf)
This was a breaking change and it broke https://github.com/jdb78/pytorch-forecasting/blob/4ba7a6ec213c395e0903a38a1a6429517dea3464/pytorch_forecasting/models/base_model.py#L260 I'll open a PR adding back compatibility for this |
What does this PR do?
This PR slightly changes the
save_hyperparameters
logic.Originally,
save_hyperparameters
captured the arguments from all stack frames. #14151 changed this behaviour. I believe, the intention was to only capture the arguments from the stack frames that correspond toLightningModule
/LightningDataModule
/etc children.This was originally implemented by checking if the current class was a subclass of
LightningModule
/LightningDataModule
/etc. Unfortunately, this turned out to be too restrictive of a requirement (imho) and it broke multiple inheritance and mixins in particular. (See #16206 and the added tests intests/tests_pytorch/models/test_hparams.py
)This PR slightly relaxes this check. Instead of checking
issubclass(current_class, ...)
we now checkisinstance(current_self, ...)
which correctly accounts for multiple inheritance MRO shenanigans.Fixes #16206
Does your PR introduce any breaking changes? If yes, please list them.
I modified the
get_init_args
function insrc/pytorch_lightning/utilities/parsing.py
to also return the self argument alongside thelocal_args
dict. I am assuming thatget_init_args
is not part of the public API. If it is, then this is a breaking change. Please, let me know if this was a breaking change.Before submitting
Not sure, where is the "unreleased section" in the CHANGELOG. I am assuming, that it's currently missing because you are going through the release process for 1.9.0?
PR review
Anyone in the community is welcome to review the PR.
Before you start reviewing, make sure you have read the review guidelines. In short, see the following bullet-list:
Did you have fun?
Yes, mother, I had fun.