Fix PreTrainedConfig as Pydantic field type after dataclass conversion#45080
Closed
joaquinhuigomez wants to merge 1 commit intohuggingface:mainfrom
Closed
Fix PreTrainedConfig as Pydantic field type after dataclass conversion#45080joaquinhuigomez wants to merge 1 commit intohuggingface:mainfrom
joaquinhuigomez wants to merge 1 commit intohuggingface:mainfrom
Conversation
The v5.4.0 conversion of PreTrainedConfig to a dataclass causes Pydantic to introspect its field annotations when used as a field type in a BaseModel. This fails because the dtype field uses a forward reference to torch.dtype that is only importable under TYPE_CHECKING. Add __get_pydantic_core_schema__ to return an is-instance schema, which tells Pydantic to validate instances by type check rather than trying to resolve the dataclass fields. Fixes huggingface#45070
Contributor
|
View the CircleCI Test Summary for this PR: https://huggingface.co/spaces/transformers-community/circle-ci-viz?pr=45080&sha=7aa9f5 |
Contributor
Author
|
Going to close this — needs more investigation than I initially thought. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root cause
The v5.4.0 release converted
PreTrainedConfigfrom a regular class to a@dataclass. This changes how Pydantic handles it when used as a field type in aBaseModel: instead of treating it as an opaque arbitrary type, Pydantic now introspects the dataclass fields and attempts to resolve all type annotations.The
dtypefield is annotated asUnion[str, "torch.dtype"] | None, where"torch.dtype"is a forward reference that depends ontorchbeing in the namespace. Sincetorchis only imported underTYPE_CHECKING, Pydantic raisesPydanticUndefinedAnnotation: name 'torch' is not defined.Fix
Add
__get_pydantic_core_schema__toPreTrainedConfigthat returns acore_schema.is_instance_schema(cls). This tells Pydantic to validate values by type-checking rather than introspecting the dataclass fields, restoring the pre-v5.4.0 behavior.The method is inherited by all subclasses (e.g.
BertConfig), so they also work as Pydantic field types.Reproduction
After this fix, the above passes.
Fixes #45070