Skip to content
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

Provide the ability to instantiate the model using the config file generated with LightningCLI #15427

Closed
mouroutzoglou opened this issue Oct 31, 2022 · 6 comments · Fixed by #18105
Labels
discussion In a discussion stage feature Is an improvement or enhancement lightningcli pl.cli.LightningCLI
Milestone

Comments

@mouroutzoglou
Copy link

mouroutzoglou commented Oct 31, 2022

🚀 Feature

Provide to the user some new interface, or modify the existing LightningCLI so that a model can be instantiated for testing/deployment/demos/notebooks/etc using a config.yaml (e.g., the same one generated during training).

Perhaps LightningCLI can have an extra command (besides fit, etc) that only returns the instantiated model, and perhaps the dataloaders.

Motivation

Currently, pytorch lightning lacks this feature (or it's well hidden!). What's the purpose of having modular training setups if you can't instantiate the trained model (for applications mentioned above) using the configs files you trained with. LightningCLI is rather limiting in that sense.

cc @Borda @carmocca @mauvilsa

@mouroutzoglou mouroutzoglou added the needs triage Waiting to be triaged by maintainers label Oct 31, 2022
@awaelchli awaelchli added feature Is an improvement or enhancement discussion In a discussion stage lightningcli pl.cli.LightningCLI and removed needs triage Waiting to be triaged by maintainers labels Oct 31, 2022
@mauvilsa
Copy link
Contributor

mauvilsa commented Nov 1, 2022

Instead of using config.yaml (which is only intended for reproducibility) it might be better to extend save_hyperparameters and load_from_checkpoint so that they are compatible with LightningCLI. I think it wouldn't be complex. To avoid the save_hyperparameters logic to import from pytorch_lightning.cli, it could have a custom hyper-parameter provider (maybe context var?) so that the CLI can provide the hyper-parameters dict. Something similar could be done for load_from_checkpoint which would use a custom hyper-parameter loader.

@carmocca
Copy link
Contributor

carmocca commented Nov 2, 2022

@mauvilsa Wouldn't that hyper-parameter provider load the hyper-parameters from a YAML file anyways?

@mauvilsa
Copy link
Contributor

mauvilsa commented Nov 2, 2022

It wouldn't be loaded from a yaml file. This would happen on instantiation of the LightningModule class. The CLI would take it from self.config.model and that is what save_hyperparameters would get as a dict. At this point no config has been saved to disk. This will also mean that hparams.yaml would have the model's config.

@maxfreu
Copy link

maxfreu commented Jul 1, 2023

You can parse your config using e.g. cfg=Omegaconf.load(..) and then do ModelClass(**cfg.model)

@mauvilsa
Copy link
Contributor

I created pull request #18105 with an initial implementation for what I proposed in #15427 (comment). Note that this loads from a checkpoint, not from a config. @mouroutzoglou you had in mind instantiating from a config. Does loading from a checkpoint resolve your needs? or is it really necessary to only instantiate without loading any weights?

@leonmkim
Copy link

leonmkim commented Aug 2, 2023

Just ran into this issue today. Expected the pl_module to take in hyper_parameters from the checkpoint to initialize, but it instead used my default config values which were different and resulted in a different architecture and failure of loading of state dict. Would love to see this feature added!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion In a discussion stage feature Is an improvement or enhancement lightningcli pl.cli.LightningCLI
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants