huggingface / huggingface_hub

The official Python client for the Huggingface Hub.
https://huggingface.co/docs/huggingface_hub
Apache License 2.0
1.83k stars 471 forks source link

Metadata is missed in child classes for models with HubMixinModel #2300

Closed qubvel closed 1 month ago

qubvel commented 1 month ago

Describe the bug

If ModelHubMixin is added to the model's base class, then metadata (such as repo_url, docs_url, ...) is missed in any class that inherits a class with mixin (its easier to show with an example, see below).

import torch
from huggingface_hub import PyTorchModelHubMixin

class BaseModel(
    torch.nn.Module,
    PyTorchModelHubMixin,
    repo_url="my-repo",
):
    pass

class DummyModel(BaseModel):  # <--- inherit BaseModel
    pass

base_model = BaseModel()
print(base_model._hub_mixin_info.repo_url)
# my-repo

model = DummyModel()
print(model._hub_mixin_info.repo_url)
# None

Is it supposed to be that way or is it unexpected behavior?

This happens because in__init_subclass__ we override cls._hub_mixin_info. https://github.com/huggingface/huggingface_hub/blob/main/src/huggingface_hub/hub_mixin.py#L215

Would it be better to update parameters for _hub_mixin_info instead of overriding them?

P.S.

Arguments can be provided for DummyModel, and that would be correctly handled

class DummyModel(BaseModel, repo_url="new-repo"):
    pass

model = DummyModel()
print(model._hub_mixin_info.repo_url)
# new-repo

System info

- huggingface_hub version: 0.24.0.dev0
- Platform: Linux-5.15.0-1056-aws-x86_64-with-glibc2.31
- Python version: 3.10.9
Wauplin commented 1 month ago

Thanks for the report @qubvel! This is indeed a bug. Fixed it in https://github.com/huggingface/huggingface_hub/pull/2305.