-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Make save_hyperparamters not save another class's args when LightningModule instantiated within another class's constructor
#13202
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
Make save_hyperparamters not save another class's args when LightningModule instantiated within another class's constructor
#13202
Conversation
…into bug/13181_save_hyperparameters
|
@tanmoyio could this introduce any breaking changes? I wasn't sure |
|
@awaelchli It will not. One unit test broke in |
|
@tanmoyio Just one more question regarding the issue I raised, this PR will take children's param as priority, but it still saves whatever params existing in the parent. And in my issue the two classes are not inheritance relationship, so will you solve this problem in a separate PR? |
class Boring_saving_hparams(LightningModule):
def __init__(self, same_arg):
super().__init__()
self.same_arg = same_arg
self.save_hyperparameters()
class BoringParent():
def __init__(self, same_arg="parent", diff_arg="test"):
super().__init__()
self.model = Boring_saving_hparams(same_arg="child")in your code, even if the @awaelchli what do you think ? |
…to bug/13181_save_hyperparameters
class Mod1(LightningModule):
def __init__(self, same_arg):
super().__init__()
self.same_arg = same_arg
self.save_hyperparameters()
class Mod2(LightningModule):
def __init__(self, same_arg):
super().__init__()
self.same_arg = same_arg
self.save_hyperparameters()
class Parent():
def __init__(self, same_arg="parent", diff_arg="test"):
super().__init__()
self.m1 = Mod1(same_arg='child1')
self.m2 = Mod2(same_arg='child2')can you share some cases where such kind of inheritance is used? |
@rohitgr7 I have the same thought and am still curious about the actual use cases (cc @tanmoyio @serena-ruan), but at the same time, I think we can't assume that a lightning module is always instantiated outside any class's |
@tanmoyio I don't think it should since it's completely out of scope of PL. If one wants to log other args of the wrapping class, they should be passed to the lightning module's In short, I believe the following assertions need to pass in this PR as @serena-ruan asks in #13202 (comment): from pytorch_lightning import LightningModule, Trainer
class BoringModel(LightningModule):
def __init__(self, same_arg):
super().__init__()
self.save_hyperparameters()
class SomeClass:
def __init__(self, same_arg="parent", diff_arg="test"):
super().__init__()
self.model = BoringModel(same_arg="child")
assert self.model.hparams.same_arg == "child", f"'{self.model.hparams.same_arg}' should be 'child'."
assert not hasattr(self.model.hparams, "diff_arg"), "`diff_arg` shouldn't be saved in `self.model.hparams`."
some_class = SomeClass() |
save_hyperparamters not save another class's args when LightningModule instantiated within another class's constructor
|
I propose to close this PR. Not having the |
|
@awaelchli It's not clear to me if your argument also applies to the linked issue and whether it should be closed too as non-supported usage. Closing is your call, as you are the most familiar on this topic :) |
|
@carmocca Actually, I am simply referring to the use case given in the description here. I just realized it is not at all the same as in the linked issue. The linked issue should be addressed, but it is clearly different. This PR tries to resolve a limitation with inheritance that I argue can't be logically supported. I will check if we can update this PR or need to open a new one. |
Duplicate args conflict with
.save_hyperparameters()Simple hack to fix the issue #13181 by @serena-ruan
quick test
Before submitting
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?
Make sure you had fun coding 🙃