-
Notifications
You must be signed in to change notification settings - Fork 581
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
Better config support in ModelHubMixin #2001
Conversation
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
The API looks good to me. Just wondering whether users could pass the config to the init of the superclass rather than adding it specifically as an attribute (similar to how this is done in the Transformers library): class MyModel(nn.Module, PyTorchModelHubMixin):
def __init__(self, config: Config):
+ super().__init__(config)
- self.config = config
self.param = nn.Parameter(torch.rand(config.hidden_size, config.vocab_size))
self.linear = nn.Linear(4, 5)
def forward(self, x):
return self.linear(x + self.param) It's great that users don't need to pass the config argument anymore when using |
But how would we be assured that the superclass accepts What we could do in EDIT: made the change in 26e1c57. It's now possible to do: from dataclasses import dataclass, asdict
import torch.nn as nn
from huggingface_hub import PyTorchModelHubMixin
@dataclass
class Config:
...
class MyModel(nn.Module, PyTorchModelHubMixin):
def __init__(self, config: Config):
...
# MyModel.from_pretrained(...).config should be set (except if config doesn't exist) |
Ok, that looks good to me.
If you mean the And I assume this still works if your config is a regular Python dictionary, rather than a Dataclass? Hence we will support 2 use cases of the config, being Dataclass + Python dictionary? For now, I've always used a regular Python dictionary, but then it wasn't that convenient as I couldn't do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sounds like a good idea to me. I would document it with examples of how to work with it though.
Linking to the related doc PR: huggingface/hub-docs#1196 Just to reiterate, we don't want to push the python mixin as the preferred way of uploading models given our preferred way is to have native library support (i.e., natively supported libraries are listed in hf.co/models filters) – but i agree this mixin is useful in some cases. Let's try to get it more widely used @NielsRogge? This PR looks good to me conceptually ✅ |
"library-less models" i guess we could call them? |
Hey there 👋 Thanks for the feedback. I came back to this PR to clean it + improve documentation + add tests. PR is in its final state and ready to be reviewed IMO @LysandreJik @NielsRogge mind having a second look at it? Thanks in advance! 🙏
Yes that's the case. It works both if your class expects a dataclass or a dictionary (and in a backward-compatible way). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Played with it locally, works very well. Very extensive test suite!
Also I got to learn that a dataclass with non-typed parameters doesn't behave the same at all 😀
Co-authored-by: Lysandre Debut <hi@lysand.re>
Co-authored-by: Lysandre Debut <hi@lysand.re>
…ggingface/huggingface_hub into 1750-access-config-in-model-hub-mixin
Thanks for the thorough review @LysandreJik! Clearly a TIL for me as well (still can't believe it totally...). |
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## main #2001 +/- ##
==========================================
+ Coverage 82.23% 82.28% +0.04%
==========================================
Files 66 66
Lines 8309 8347 +38
==========================================
+ Hits 6833 6868 +35
- Misses 1476 1479 +3 ☔ View full report in Codecov by Sentry. |
Fix #1750 following plan described in #1750 (comment).
In particular:
config
either from input parameter orself.config
(if exists). We support serialization both if config is adict
or adataclass
.__init__
expects a config value. If it's the case, we check if the expected type from the annotation is a dataclass type, in which case we instantiate it correctly. Otherwise, we pass the rawdict
.cc @NielsRogge @LysandreJik could you have a look at the API? Once I have feedback, I'll move forward with the tests + docs. Thanks in advance! 🙏
With this PR, it is possible to do:
TODO:
This PR introduces a few breaking changes. We could be more conservative but given the low usage of
ModelHubMixin
I don't think we should be too conservative (IMO). Here is a list I can think of:self.config
is an attribute of the model that cannot be serialized, it will fail. Prior to the PR, the config wouldn't be saved. With this PR, we'll try to serialize it to json which might fail. Alternative: try/except + warn on failure + ignore config._from_pretrained
need theconfig
object but not__init__
, we won't pass it anymore. Alternative: check if_from_pretrained
expects it? (not sure it's worth it)__init__
does not expect aconfig
input value in its declared parameters but only with a**kwargs
, we won't forward it which means model instantiation might be different. Alternative: pass the config dict if__init__
accepts**kwargs
. I feel that we would encourage a bad behavior by doing so.Otherwise, I think that's it.