-
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
Avoid patching LightningModule methods during training #6030
Comments
Hi @awaelchli, I could work on this, as I already explores |
Aha!! Awesome, you are welcome to work on this. Ping me if you encounter any troubles along the way. |
Hi @awaelchli, I'm working on the issue and I opened a draft PR #6103 . I have some questions:
|
Yes it looks like combining is the current behavior. Makes sense. Even better would be to log which ones are used.
datamodule and dataloaders passed to fit have precedence over the methods defined in the model. |
@awaelchli @carmocca why do we need to bind the dataloader functions to the model at all? why can't we set these as attributes of the trainer? and then delete corresponding at the end of the call? or just call the right one of model/datamodule/dataloader inside of the training/evaluation/predict loop? |
I don't know why. It was there from the start. I don't see a reason why it should be necessary.
If @gianscarpe doesn't have time to do it, I can try to get this started in 1.4. It will be a relatively wide-spreading refactor. |
@awaelchli Adding you as an assignee as well! 🚀 |
Hei @awaelchli, I just forgot about this PR. I started working on the thing, if you want to we can work together :) |
Yes please, that would be awesome. Feel free to kick it off and I will be happy to help finish it and I can also help with failing tests. |
Originally posted by @awaelchli in #8885 (comment) |
Originally posted by @ananthsub in #8885 (comment) |
@awaelchli I agree with @ananthsub that this is definitely possible and should be done!
I like that idea. Should this be separate the from the DataConnector?
Personally, I would give priority to whatever was passed explicitly. So if the module has a loader implemented but a loader, datamodule with the loader is passed to the entrypoint I would only use that one. i wouldn't raise an exception but a warning. One point is, that we don't know the precedence otherwise. In validation it would be fine to chain them, but in training it isn't that easy and I think we should be consistent here between training and validation. Another point is, that this is also what's the current behaviour. I.e. by patching the model we also ignore the loaders from the model when given a loader explicitly. |
fyi @ninginthecloud |
One more data point, this patching "solution" is also making test mocking hard. Say I create a mock data module.
This line will fail https://github.com/PyTorchLightning/pytorch-lightning/blob/master/pytorch_lightning/trainer/connectors/data_connector.py#L225 because parent and instance code is the same. Therefore train_dataloader will not be attached to lightning_module |
@zzzwen I completely agree. If we had the possibility to mock these methods it would simplify and harden a bunch of dataloader tests. |
🚀 Feature
Can we implement the dataloaders without 🐒-patching the methods in LightningModule?
Motivation
Currently, we patch the LightningModule methods in the trainer when also a DataModule is used.
https://github.com/PyTorchLightning/pytorch-lightning/blob/5157ba55095a6a9f93ec1976aac877c87b00158f/pytorch_lightning/trainer/connectors/data_connector.py#L115
A datamodule's dataloader methods have precedence over the once defined in the LightningModule, but the LightningModule code should not be altered. The user does not know that this happens, and after training is complete, the user may wishes to continue using the model instance.
Pitch
Store the dataloader references in the trainer (or data connector) directly, without "attaching" them to the user's model.
This would also enable typing inference as mentioned by @gianscarpe.
Alternatives
Keep as is, but user will not be happy.
It's also harder to debug the way it is right now.
The text was updated successfully, but these errors were encountered: