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

Correctly inject config in PytorchModelHubMixin #2079

Merged
merged 9 commits into from
Mar 6, 2024

Conversation

Wauplin
Copy link
Contributor

@Wauplin Wauplin commented Mar 1, 2024

Solves #2079.

With this PR, users don't have to rely on a config parameter to configure their model. We read default+passed values automatically to build the config file ourselves. This way, we are able to save a correct config.json file so that reloaded model have exact same config. Only "jsonable" fields are saved in config.json.

And ofc it's backward compatible so users that prefer to pass config.json directly are still supported.

PR is inspired by Inspired by facebookresearch/hiera#26. cc @dbolya with this change, you won't need the @has_config decorator anymore 🤗

Here is an example to showcase it:

>>> import torch
>>> import torch.nn as nn
>>> from huggingface_hub import PyTorchModelHubMixin

>>> class MyModel(nn.Module, PyTorchModelHubMixin):
...     def __init__(self, hidden_size: int = 512, vocab_size: int = 30000, output_size: int = 4):
...         super().__init__()
...         self.param = nn.Parameter(torch.rand(hidden_size, vocab_size))
...         self.linear = nn.Linear(output_size, vocab_size)

...     def forward(self, x):
...         return self.linear(x + self.param)
>>> model = MyModel(hidden_size=256)

# Save model weights to local directory
>>> model.save_pretrained("my-awesome-model")

# Push model weights to the Hub
>>> model.push_to_hub("my-awesome-model")

# Download and initialize weights from the Hub
>>> model = MyModel.from_pretrained("username/my-awesome-model")
>>> model.hidden_size
256

cc @mfuntowicz as well. This shouldn't impact you except that the generated config.json files should be more complete.

@HuggingFaceDocBuilderDev

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.

Copy link
Contributor

@NielsRogge NielsRogge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! This is going to be very useful

@Wauplin
Copy link
Contributor Author

Wauplin commented Mar 4, 2024

That looks great. However, in your PR description, do you mean self.hidden_size instead of config.hidden_size etc. in order to access the attributes?

Yes sorry, I've updated the example.

Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com>
@codecov-commenter
Copy link

codecov-commenter commented Mar 5, 2024

Codecov Report

Attention: Patch coverage is 89.18919% with 4 lines in your changes are missing coverage. Please review.

Project coverage is 82.91%. Comparing base (05d715f) to head (0c900cd).
Report is 1 commits behind head on main.

Files Patch % Lines
src/huggingface_hub/hub_mixin.py 89.18% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2079      +/-   ##
==========================================
+ Coverage   82.90%   82.91%   +0.01%     
==========================================
  Files         102      102              
  Lines        9480     9499      +19     
==========================================
+ Hits         7859     7876      +17     
- Misses       1621     1623       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! That is sure to come in handy with model serialization, no need to handle a config on the side anymore. Awesome!

Comment on lines +217 to +219
def _save_pretrained(self, save_directory: Path) -> None:
"""Save weights from a Pytorch model to a local directory."""
save_model_as_safetensor(self.module, str(save_directory / SAFETENSORS_SINGLE_FILE))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yess 🎉

src/huggingface_hub/hub_mixin.py Outdated Show resolved Hide resolved
src/huggingface_hub/utils/_typing.py Outdated Show resolved Hide resolved
Co-authored-by: Lysandre Debut <hi@lysand.re>
@Wauplin
Copy link
Contributor Author

Wauplin commented Mar 6, 2024

Thanks for the review @LysandreJik 🤗 Yes I also expect it to make integrations even more straightforward now :)

@Wauplin Wauplin merged commit 290b4cc into main Mar 6, 2024
15 checks passed
@Wauplin Wauplin deleted the correctly-inject-config-in-model-hub-mixin branch March 6, 2024 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants