-
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
[Trainer] Use name mangling to allow for subclassing #7242
Comments
Does #7232 address the concerns here? |
Partially yes, sorry that I missed that. It's still possible to override the However I still believe that using class local references with double underscore is a cleaner way of doing this and is also recomended by Python docs. https://docs.python.org/3/tutorial/classes.html#private-variables |
Thanks for the input @Fassty ! We should try to use private methods more, but in this case, it needs to stay as protected as it's also called by the Tuner. |
Thanks @carmocca, I see that you've had a discussion about that in the PR. I didn't realize that the Anyway the linked PR solves this issue. |
🐛 Bug
The
Trainer
class uses itsself.fit
directly in several places. That causes problems when theTrainer
class is subclassed and the behavior of thefit
method altered. As it's now impossible to call thetest
method because it usesself.fit
internally.Since things like
trainer.validate
andGridSearch
are not yet implemented inPyTorchLightning
the user should be allowed to subclass and modify those methods without worrying about breaking theTrainer
code.Please reproduce using the BoringModel
https://colab.research.google.com/drive/1jcq5g0XF_3xTLR0p7ZuEMfHcowtZhf4j?usp=sharing
Expected behavior
SeedSearchTrainer
uses thefit
method of the base class (pl.Trainer
) whentest
method of the base class is called.Additional context
This could be solved using name mangling.
Store a local reference to the
fit
method by adding a__fit
attribute to theTrainer
class.Then on lines:
https://github.com/PyTorchLightning/pytorch-lightning/blob/master/pytorch_lightning/trainer/trainer.py#L920
https://github.com/PyTorchLightning/pytorch-lightning/blob/master/pytorch_lightning/trainer/trainer.py#L981
https://github.com/PyTorchLightning/pytorch-lightning/blob/master/pytorch_lightning/trainer/trainer.py#L1075
replace
self.fit
byself.__fit
What this achieves is that whenever the
self.__fit
method is called the name mangling would change its name to_Trainer__fit
to assure that the method of theTrainer
class is called rather than some overwritten version of the method from the derived class.This approach can also be implemented for other methods that the user may want to subclass and that are used internally in the base class as
self.method
.The text was updated successfully, but these errors were encountered: