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

Instantiation fails with TypeError only when fail_untyped=False #362

Closed
cristianregep opened this issue Aug 29, 2023 · 3 comments · Fixed by #364
Closed

Instantiation fails with TypeError only when fail_untyped=False #362

cristianregep opened this issue Aug 29, 2023 · 3 comments · Fixed by #364
Labels
bug Something isn't working

Comments

@cristianregep
Copy link

cristianregep commented Aug 29, 2023

🐛 Bug report

I'm trying to instantiate a config class from Huggingface with CLI and it fails when fail_untyped=False. The fail_untyped is important because I'm doing this as part of a project that is written within the lightning framework, where we make heavy use of lightning cli, which sets fail_untyped to False and you can't change it. Most of our project is written internally by us and is not HuggingFace based, but we want to make use from time to time of some external models in the open source community (like for example HuggingFace).

I created an isolated example just with jsonargparse (see below). It only fails when fail_untyped=False . I stepped through the instantiation and I'm quite confused of what's happening. num_labels is not set, and through kwargs.pop("num_labels", 2) it should become 2, which is an int.

File "/opt/pyenv/versions/3.10.12/lib/python3.10/site-packages/transformers/configuration_utils.py", line 331, in __init__
    self.num_labels = kwargs.pop("num_labels", 2)

File "/opt/pyenv/versions/3.10.12/lib/python3.10/site-packages/transformers/configuration_utils.py", line 256, in __setattr__
    super().__setattr__(key, value)

File "/opt/pyenv/versions/3.10.12/lib/python3.10/site-packages/transformers/configuration_utils.py", line 420, in num_labels
    self.id2label = {i: f"LABEL_{i}" for i in range(num_labels)}

TypeError: 'NoneType' object cannot be interpreted as an integer

As a side note I am planning to submit a PR soon to allow people to set fail_untyped in lightning Lightning-AI/pytorch-lightning#18285 .

To reproduce

from jsonargparse import CLI
from dataclasses import dataclass
from transformers.models.perceiver.configuration_perceiver import PerceiverConfig


@dataclass
class Test:
    cfg: PerceiverConfig


print(CLI(Test, fail_untyped=False))

Config

cfg:
  class_path: transformers.models.perceiver.configuration_perceiver.PerceiverConfig 

Command

python perceiver.py --config perceiver_config.yaml

Expected behavior

No TypeError

Environment

  • jsonargparse version: 4.23.1
  • Python version: 3.10.12
  • How jsonargparse was installed: pip install jsonargparse[signatures,typing-extensions]==4.23.1
  • Huggingface transformers package: transformers==4.31.0
  • OS: Ubuntu 22.04.2 LTS
@cristianregep cristianregep added the bug Something isn't working label Aug 29, 2023
@mauvilsa
Copy link
Member

Thank you for reporting!

From what I see, the conditional defaults from the AST resolver is not working correctly for the case of kwargs.pop() (i.e. configuration_utils.py#L322-L331). I will fix it.

For more details about the expected behavior, search for conditional_calls(**kwargs) in the docs.

@mauvilsa
Copy link
Member

I implemented a fix in #364. @cristianregep please test it out.

@cristianregep
Copy link
Author

Thanks for the quick turnaround. It works

mauvilsa added a commit that referenced this issue Aug 31, 2023
mauvilsa added a commit that referenced this issue Aug 31, 2023
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

Successfully merging a pull request may close this issue.

2 participants