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

Support for Weights and Biases logger (and possibly others) in Lightning CLI #7774

Closed
ohayonguy opened this issue May 31, 2021 · 13 comments
Closed
Labels
argparse (removed) Related to argument parsing (argparse, Hydra, ...) feature Is an improvement or enhancement help wanted Open to be worked on won't fix This will not be worked on

Comments

@ohayonguy
Copy link

🚀 Feature

Adding support for weights and biases logger in lightning CLI

Motivation

Currently, instantiating a weights and biases logger from lightning CLI is not supported, and causes an error:

trainer.py: error: Parser key “trainer.logger”: Value “{‘class_path’: ‘pytorch_lightning.loggers.WandbLogger’, ‘init_args’: {‘entity’: ‘gip’, ‘project’: ‘solver’, ‘notes’: ‘notes’}}” does not validate against any of the types in typing.Union[pytorch_lightning.loggers.base.LightningLoggerBase, typing.Iterable[pytorch_lightning.loggers.base.LightningLoggerBase], bool] :: Problem with given class_path “pytorch_lightning.loggers.WandbLogger” :: ‘Configuration check failed :: No action for key “entity” to check its value.’ :: Expected a List but got “{‘class_path’: ‘pytorch_lightning.loggers.WandbLogger’, ‘init_args’: {‘entity’: ‘gip’, ‘project’: ‘solver’, ‘notes’: ‘notes’}}” :: Expected a <class ‘bool’> but got “{‘class_path’: ‘pytorch_lightning.loggers.WandbLogger’, ‘init_args’: {‘entity’: ‘gip’, ‘project’: ‘solver’, ‘notes’: ‘notes’}}”

Pitch

I want to be able to initialize a WandbLogger from the yaml file, and also be able to run wandb sweeps.

@ohayonguy ohayonguy added feature Is an improvement or enhancement help wanted Open to be worked on labels May 31, 2021
@ohayonguy
Copy link
Author

@mauvilsa cc

@mauvilsa
Copy link
Contributor

mauvilsa commented May 31, 2021

From a quick look at this the problem seems to be only for the cases in which a WandbLogger is given values that should go to its init **kwargs. I will look into this.

@awaelchli awaelchli added the argparse (removed) Related to argument parsing (argparse, Hydra, ...) label May 31, 2021
@oplatek
Copy link
Contributor

oplatek commented May 31, 2021

Maybe I am misunderstanding the question,
but for me using instantiating WandBLogger in the config with init_args in the field trainer: logger: class_path works fine.

@ohayonguy
Copy link
Author

@oplatek Did you try passing arguments to the wandb logger? For example, passing an 'entity' kwarg.

@oplatek
Copy link
Contributor

oplatek commented May 31, 2021

@ohayonguy oh... I see, I just used the name, save_dir.

Now, I understand the problem, sorry I misunderstood it.

I experienced this problem with LightningCLI too. I solved it by subclassing the parent and moving the argument from kwargs to regular argument in the inherited __init__ function.

@ohayonguy
Copy link
Author

Ahh yup :) that's exactly what we did!
@oplatek

@ohayonguy
Copy link
Author

@oplatek We still have a very annoying problem though, when we have nested pl.module classes that takes nn.Module as arguments. These can't be instantiated :(

@mauvilsa
Copy link
Contributor

mauvilsa commented Jun 1, 2021

when we have nested pl.module classes that takes nn.Module as arguments. These can't be instantiated :(

This is happening because some init arguments in nn.Module classes don't have type hints. Currently in LightningCLI it is implemented that arguments without a type get the Any type. However looking at this I have noticed that this option is not enabled for some cases. I know why this is and will implement it soon.

@mauvilsa
Copy link
Contributor

mauvilsa commented Jun 1, 2021

From a quick look at this the problem seems to be only for the cases in which a WandbLogger is given values that should go to its init **kwargs. I will look into this.

The issue is that WandbLogger receives **kwargs (wandb.py#L102) which is used for running wandb.init(). jsonargparse is not aware of the arguments that wandb.init() receives. The way jsonargparse works is via class inheritance. If the init of a class has **kwargs then it will look for more arguments in the next class it inherits from. If the following class also receives **kwargs it will continue looking in the next class and so on.

I would propose that we improve the implementation of WandbLogger such that it inherits from a class WandbInitOptions which would include all the arguments that can be accepted. This way the parser can be aware of them and validate against its types. The WandbInitOptions class would not be written manually. It would be a class dynamically generated using some function that inspects the signature of wandb.init(). This way if something changes in wandb it becomes automatically available in lightning.

Please analyze and comment to see if the idea makes sense and to figure out what other issues there could be.

@ohayonguy
Copy link
Author

@mauvilsa Your solution seems okay to me, although it's a "workaround". Is there a way to inherit form jsonargparse and change its behavior to our needs? I am not very familiar with it. Changing jsonargparse might be a better solution in the long term, and could give more flexibility (for example, instantiate nn.Modules in subclasses and stuff).

@mauvilsa
Copy link
Contributor

mauvilsa commented Jun 1, 2021

@ohayonguy the nn.Module instantiation is different. This a missing feature that will be implemented in jsonargparse except for maybe a minor modification in LightningCLI.

The proposal for **kwargs is not a workaround. Why do you say it is a workaround? jsonargparse needs to know what arguments it can accept and how to validate them. This is important so that when incorrect values are given it fails early and not after a whole lot of processing has been done. The way to tell jsonargparse what are valid arguments are with the type hints and in the case of classes possibly with class inheritance. This is a change that would be done in pytorch-lightning. From what I understand in the description this will fit your needs.

@stale
Copy link

stale bot commented Jul 1, 2021

This issue has been automatically marked as stale because it hasn't had any recent activity. This issue will be closed in 7 days if no further activity occurs. Thank you for your contributions, Pytorch Lightning Team!

@stale stale bot added the won't fix This will not be worked on label Jul 1, 2021
@stale stale bot closed this as completed Jul 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
argparse (removed) Related to argument parsing (argparse, Hydra, ...) feature Is an improvement or enhancement help wanted Open to be worked on won't fix This will not be worked on
Projects
None yet
Development

No branches or pull requests

4 participants