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

Adding mixin class for ease saving, uploading, downloading (as discussed in issue #9). #11

Merged
merged 20 commits into from
Mar 18, 2021

Conversation

thevasudevgupta
Copy link
Contributor

@thevasudevgupta thevasudevgupta commented Jan 19, 2021

This PR will add feature as discussed in issue #9.

Example

from huggingface_hub import ModelHubMixin

class MyModel(nn.Module, ModelHubMixin):
   def __init__(self, **kwargs):
      super().__init__()
      self.config = kwargs.pop("config", None)
      self.layer = ...
   def forward(self, ...):
      return ...

model = MyModel()

# saving model to local directory & pushing to hub
model.save_pretrained("mymodel", push_to_hub=True, config={"act": "gelu"})

# initiatizing model & loading it from trained-weights
model = MyModel.from_pretrained("username/mymodel@main")

Note

  • In case user want to save some specific layers as per his needs, he can simple overwrite ._save_pretrained() method.
  • Supporting all kind of PyTorch model.
  • tf2 not supported currently

@thevasudevgupta
Copy link
Contributor Author

Shall I integrate model uploading directly in .save_pretrained(path, model_id="username/mymodel", upload_to_hub=True) method instead of making separate method .upload_to_hub(model_id="username/mymodel")? I feel this looks better.

Please put your views on this.

@julien-c
Copy link
Member

Hi @vasudevgupta7 thanks for the PR!

I have no more bandwidth this week but plan on actively looking at this early next week!

@julien-c julien-c linked an issue Jan 25, 2021 that may be closed by this pull request
src/huggingface_hub/hub_mixin.py Outdated Show resolved Hide resolved
src/huggingface_hub/hub_mixin.py Outdated Show resolved Hide resolved
src/huggingface_hub/hub_mixin.py Outdated Show resolved Hide resolved
@julien-c
Copy link
Member

julien-c commented Mar 4, 2021

Ooops @vasudevgupta7 I wanted to reach out to let you know that we had merged the Repository PR. But I guess you saw that :)

Will take a look at your updated PR with @philschmid and others, soon. Thanks!

@julien-c
Copy link
Member

julien-c commented Mar 4, 2021

Shall I integrate model uploading directly in .save_pretrained(path, model_id="username/mymodel", upload_to_hub=True) method instead of making separate method .upload_to_hub(model_id="username/mymodel")? I feel this looks better.

Replying late to this but yeah I feel like it'd be nice to expose both.

Copy link
Contributor

@sgugger sgugger left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! I'm not entirely sure adding a complex logic in the default save_pretrained and from_pretrained is desirable as users that need such a logic will probable need to overload those methods anyway (for instance transformers will definitely need to, or the API assumes the model is initialized with a config when it exists but some library might use a class method instead). So I would limit those two methods to the base save of the model weights and document they can be overloaded to add more logic.

The push_to_hub on the other hand is absolutely awesome to have!

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.

This looks like a great addition!

src/huggingface_hub/hub_mixin.py Outdated Show resolved Hide resolved
@julien-c
Copy link
Member

julien-c commented Mar 6, 2021

@vasudevgupta7 let us know when you feel like this is ready to squash/merge and I'll make a final review. Thanks again

Copy link

@theo-m theo-m left a comment

Choose a reason for hiding this comment

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

Cool stuff! 🚀
First time reviewing on this repo, mostly shallow review; the rest looks sound to me, but I'm not confident approving on my own.

src/huggingface_hub/hub_mixin.py Outdated Show resolved Hide resolved
src/huggingface_hub/hub_mixin.py Outdated Show resolved Hide resolved
src/huggingface_hub/hub_mixin.py Outdated Show resolved Hide resolved
src/huggingface_hub/repository.py Outdated Show resolved Hide resolved
src/huggingface_hub/repository.py Outdated Show resolved Hide resolved
src/huggingface_hub/repository.py Outdated Show resolved Hide resolved
Copy link
Contributor

@patrickvonplaten patrickvonplaten left a comment

Choose a reason for hiding this comment

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

Great PR!

@thevasudevgupta
Copy link
Contributor Author

Hoping everything works now. Sorry for the delay.

@julien-c
Copy link
Member

Looks good!

Does anyone tagged here have any last comments? @LysandreJik and @philschmid maybe?

Otherwise I'll merge and push a release soon.

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.

LGTM!

Copy link
Member

@philschmid philschmid left a comment

Choose a reason for hiding this comment

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

LGTM too! Thank you for creating a test. Looking forward to using this!🚀

@julien-c
Copy link
Member

I've removed torch as a hard dependency, and please note that I don't think the tests can actually pass given that they are pushing to your userspace @vasudevgupta7 (not in a staging env) and the CI env of course doesn't have your git credentials.

I'll merge like this but would be nice to set up CI for this at some point

@LysandreJik By the way do you know how to activate GitHub Actions for pull requests from forks?

@julien-c julien-c merged commit 9e1d3d5 into huggingface:main Mar 18, 2021
@julien-c
Copy link
Member

Thanks for the contribution @vasudevgupta7 ❤️

@LysandreJik
Copy link
Member

Yes, setting it up can be done like it was done here: https://github.com/huggingface/transformers/pull/10383/files. I'm opening a PR with that proposal now!

@thevasudevgupta
Copy link
Contributor Author

Glad it's merged finally 🤩. Thank you @julien-c, @philschmid, @LysandreJik, @patrickvonplaten, @theo-m, @sgugger for reviewing and approving.

@thevasudevgupta
Copy link
Contributor Author

@julien-c there is small error in the example you shared in release. It should be push_to_hub instead of upload_to_hub. Sorry, I forgot to update PR description. I changed it now.

@julien-c
Copy link
Member

Oops, no big deal, but I will update the release notes: https://github.com/huggingface/huggingface_hub/releases/tag/v0.0.7

@julien-c
Copy link
Member

link to 🐦

@thevasudevgupta thevasudevgupta deleted the saving-utils branch March 18, 2021 22:01
julien-c pushed a commit that referenced this pull request May 21, 2021
* addition of new language: Mongolia

I am adding the new language templates for the hosted API.
Please review this for me. Thank you so much!

* update mn summaraization

I just fixed the wrong initialization for `mn` summarization
it was fault that i copied `english` version of what I am doing
commit imported from https://github.com/huggingface/widgets/commit/0c8b3e0ea843145623a865e8e7fe2f4653f168d9

* addition of new language: Mongolia

I am adding the new language templates for the hosted API.
Please review this for me. Thank you so much!

* update mn summaraization

I just fixed the wrong initialization for `mn` summarization
it was fault that i copied `english` version of what I am doing
julien-c pushed a commit that referenced this pull request May 21, 2021
* addition of new language: Mongolia

I am adding the new language templates for the hosted API.
Please review this for me. Thank you so much!

* update mn summaraization

I just fixed the wrong initialization for `mn` summarization
it was fault that i copied `english` version of what I am doing
commit imported from https://github.com/huggingface/widgets/commit/0c8b3e0ea843145623a865e8e7fe2f4653f168d9

* addition of new language: Mongolia

I am adding the new language templates for the hosted API.
Please review this for me. Thank you so much!

* update mn summaraization

I just fixed the wrong initialization for `mn` summarization
it was fault that i copied `english` version of what I am doing
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.

Add simple class to ease saving loading from HF hub
7 participants