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

Defining class_path before init_args does not work, but switching the order does work. #161

Closed
basveeling opened this issue Sep 23, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@basveeling
Copy link

🐛 Bug report

Using LightningCLI, when setting the model property in a yaml file, the order of class_path and init_args matters. class_path before init_args causes a ParsingError.

To reproduce

Setup a LightingCLI with a base class BaseModelClass(LightningModule)
train.py would be something along the lines of

LightningCLI(model_class=BaseModelClass, ...)

Config file a.yaml:

model:
   class_path: SubclassedModelClass
   init_args:
     a: b

Config file b.yaml

model:
   init_args:
     a: b
   class_path: SubclassedModelClass

Run it as follows
python train.py fit -c a.yaml

jsonargparse.util.ParserError: Configuration check failed :: Parser key "model": Type <class 'BaseModelClass'> expects: a class path (str); or a dict with a class_path entry; or a dict with init_args (if class path given previously). Got "Namespace(init_args=Namespace(a="b"))".

(Notice the missing class_path)

Then run with the alternative order
python train.py fit -c b.yaml
Which runs successfully.

Expected behavior

Both orders should work

Environment

  • jsonargparse version 4.14.0
  • pytorch_lightning 1.7.4
  • python 3.8.14
  • pip install jsonargparse[signatures]
  • Ubuntu
@basveeling basveeling added the bug Something isn't working label Sep 23, 2022
@mauvilsa
Copy link
Member

Thank you for reporting. I tried a few things, but unfortunately I am unable to reproduce it. From the description I wrote:

from pytorch_lightning import LightningModule
from pytorch_lightning.cli import LightningCLI

class BaseModelClass(LightningModule):
    def __init__(self, a: str = '-'):
        pass

class SubclassedModelClass(BaseModelClass):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

LightningCLI(
    model_class=BaseModelClass,
    subclass_mode_model=True,
)

If I run this with the two config files you posted, both ./cli.py fit -c a.yaml --print_config and ./cli.py fit -c b.yaml --print_config work correctly (--print_config added just to stop after parsing).

If by mistake subclass_mode_model=True is missing, it doesn't fail when it should. This is a bug which I will fix, but it is not the problem you reported. Please check again and complete the code in the description so that it can be reproduced.

@basveeling
Copy link
Author

basveeling commented Sep 24, 2022 via email

@mauvilsa
Copy link
Member

mauvilsa commented Oct 7, 2022

Any update on this?

@mauvilsa
Copy link
Member

I assume you were not able to reproduce the issue. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants