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

Parse arguments with HfArgumentParser #3090

Closed
chocoded opened this issue Nov 27, 2024 · 3 comments · Fixed by #3178
Closed

Parse arguments with HfArgumentParser #3090

chocoded opened this issue Nov 27, 2024 · 3 comments · Fixed by #3178
Labels
bug Something isn't working

Comments

@chocoded
Copy link

I was using HfArgumentParser to parse SentenceTransformerTrainingArguments and met the following error:

parser = HfArgumentParser(SentenceTransformerTrainingArguments)`
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/admin/.local/lib/python3.10/site-packages/transformers/hf_argparser.py", line 137, in __init__
    self._add_dataclass_arguments(dtype)
  File "/home/admin/.local/lib/python3.10/site-packages/transformers/hf_argparser.py", line 264, in _add_dataclass_arguments
    self._parse_dataclass_field(parser, field)
  File "/home/admin/.local/lib/python3.10/site-packages/transformers/hf_argparser.py", line 172, in _parse_dataclass_field
    field.type.__args__[0] if isinstance(None, field.type.__args__[1]) else field.type.__args__[1]
  File "/opt/conda/envs/python3.10/lib/python3.10/typing.py", line 994, in __instancecheck__
    return self.__subclasscheck__(type(obj))
  File "/opt/conda/envs/python3.10/lib/python3.10/typing.py", line 997, in __subclasscheck__
    raise TypeError("Subscripted generics cannot be used with"
TypeError: Subscripted generics cannot be used with class and instance checks

This error is due to that SentenceTransformerTrainingArguments.prompts has a type of dict[str, dict[str, str]] | dict[str, str] | str | None, while in _parse_dataclass_field, field.type.__args__[1] got a generic type of dict[str, str], and isinstance(None, dict[str, str]) caused the error. Could you fix this? thanks.

@tomaarsen tomaarsen added the bug Something isn't working label Nov 27, 2024
@tomaarsen
Copy link
Collaborator

Hello!

Thanks for reporting.
Based on https://github.com/huggingface/transformers/blob/4c1388f48e72dde96b666f8fe24f74bf4056e410/src/transformers/hf_argparser.py#L167-L171
it seems like the HfArgumentParser only accepts one type per argument (and optionally None). The new prompts parameter has 3 types plus None, so it might be incompatible. I think this is a limitation by argparse itself. I'm also not sure if you can load dictionaries with argparse.

I'm not sure how to tackle this other than e.g. updating the type for prompts to str | None or something. Looking into this some more now.

  • Tom Aarsen

@Hypothesis-Z
Copy link

Same issue.

Overwriting the type seems to work.

@dataclass
class MySentenceTransformerTrainingArguments(SentenceTransformerTrainingArguments):
    prompts: Optional[str] = field(
        default_factory=dict
    )

@tomaarsen
Copy link
Collaborator

Thanks for providing a quick fix @Hypothesis-Z. Perhaps the best approach is to just use Optional[str] in the actual implementation. The other typings are also described in the docs, and I'd like to avoid errors like these.

I'll open a PR.

  • Tom Aarsen

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.

3 participants