How to instantiate plugins with LightningCLI? #8561
-
I'm looking at the following example: import torch
from torch.utils.data import DataLoader, Dataset
from pytorch_lightning import LightningModule
from pytorch_lightning.utilities.cli import LightningCLI
class RandomDataset(Dataset):
def __init__(self, size, length):
self.len = length
self.data = torch.randn(length, size)
def __getitem__(self, index):
return self.data[index]
def __len__(self):
return self.len
class BoringModel(LightningModule):
def __init__(self):
super().__init__()
self.layer = torch.nn.Linear(32, 2)
def forward(self, x):
return self.layer(x)
def training_step(self, batch, batch_idx):
loss = self(batch).sum()
self.log("train_loss", loss)
return {"loss": loss}
def configure_optimizers(self):
return torch.optim.SGD(self.layer.parameters(), lr=0.1)
def train_dataloader(self):
return DataLoader(RandomDataset(32, 64), batch_size=2)
def run():
LightningCLI(model_class=BoringModel)
if __name__ == "__main__":
run() with config file:
In Lightning 1.4 I am getting the following error. bug_report_model.py: error: Parser key "trainer.plugins": Value "[{'class_path': 'pytorch_lightning.plugins.training_type.ddp.DDPPlugin', 'init_args': {'find_unused_parameters': True}}]" does not validate against any of the types in typing.Union[typing.List[typing.Union[pytorch_lightning.plugins.base_plugin.Plugin, pytorch_lightning.plugins.environments.cluster_environment.ClusterEnvironment, str]], pytorch_lightning.plugins.base_plugin.Plugin, pytorch_lightning.plugins.environments.cluster_environment.ClusterEnvironment, str, NoneType]:
- Value "{'class_path': 'pytorch_lightning.plugins.training_type.ddp.DDPPlugin', 'init_args': {'find_unused_parameters': True}}" does not validate against any of the types in typing.Union[pytorch_lightning.plugins.base_plugin.Plugin, pytorch_lightning.plugins.environments.cluster_environment.ClusterEnvironment, str]:
- Problem with given class_path "pytorch_lightning.plugins.training_type.ddp.DDPPlugin":
- 'Configuration check failed :: No action for key "find_unused_parameters" to check its value.'
- "pytorch_lightning.plugins.training_type.ddp.DDPPlugin" is not a subclass of ClusterEnvironment
- Expected a <class 'str'> but got "{'class_path': 'pytorch_lightning.plugins.training_type.ddp.DDPPlugin', 'init_args': {'find_unused_parameters': True}}"
- Type <class 'pytorch_lightning.plugins.base_plugin.Plugin'> expects an str or a Dict with a class_path entry but got "[{'class_path': 'pytorch_lightning.plugins.training_type.ddp.DDPPlugin', 'init_args': {'find_unused_parameters': True}}]"
- Type <class 'pytorch_lightning.plugins.environments.cluster_environment.ClusterEnvironment'> expects an str or a Dict with a class_path entry but got "[{'class_path': 'pytorch_lightning.plugins.training_type.ddp.DDPPlugin', 'init_args': {'find_unused_parameters': True}}]"
- Expected a <class 'str'> but got "[{'class_path': 'pytorch_lightning.plugins.training_type.ddp.DDPPlugin', 'init_args': {'find_unused_parameters': True}}]"
- Expected a <class 'NoneType'> but got "[{'class_path': 'pytorch_lightning.plugins.training_type.ddp.DDPPlugin', 'init_args': {'find_unused_parameters': True}}]" The relevant error is: - Problem with given class_path "pytorch_lightning.plugins.training_type.ddp.DDPPlugin":
- 'Configuration check failed :: No action for key "find_unused_parameters" to check its value.' The DDPPlugin signature has kwargs defined and I am not sure if jsonargparse is supposed to be able to handle them, I couldn't find any explanation in their docs. I would like to pass in Current workaround:
cc @mauvilsa @carmocca who might be pleased to hear that I started to really like the CLI <3 EDIT
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 9 replies
-
There are many possible ways in which people could use Is there any class or function that defines the accepted parameters and type hints for the |
Beta Was this translation helpful? Give feedback.
-
Hi @awaelchli @mauvilsa @carmocca, what about implementing
That is to regard |
Beta Was this translation helpful? Give feedback.
-
The issues related to plugins instantiation have been resolved with #13283. |
Beta Was this translation helpful? Give feedback.
The issues related to plugins instantiation have been resolved with #13283.