huggingface / huggingface_hub

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

Should we use functions instead of mix-ins to define integration helpers? #327

Open nateraw opened 3 years ago

nateraw commented 3 years ago

Many 3rd party libraries use class factory functions to load/create models. This means users are only working with instances, not classes themselves. For example...

Timm:

import timm

model = timm.create_model('resnet18', pretrained=True)
# User fine tunes...

Torchhub:

import torch

model = torch.hub.load('pytorch/vision', 'resnet18', pretrained=True)
# User fine tunes...

Now, if we want to let users push these up to the hub, they either have to do all the saving/pushing logic manually, or they have to wrap these model instances in another class that mixes in PyTorchModelHubMixin. Wouldn't it be easier to just call a function like save_pretrained_pytorch(model, save_dir)? Then we can reuse that function in the mixin, so we're only writing the code in one place.

Keras is another great example of this. Users usually build models with the Functional or Sequential APIs instead of defining classes themselves, so a mixin doesn't help them much.

Additional context We explore using functions for Keras here - #284
Additional inspiration - #310

LysandreJik commented 3 years ago

Hi @nateraw, this definitely makes sense. The mixins are here for convenience for some frameworks - but I agree they don't make sense for others!