Skip to content

Commit c72daf0

Browse files
NielsRoggepcuencajulien-cWauplin
authored
[Upload models] Add docs for mixin (huggingface#1196)
* Add docs * Add formatting * Apply suggestions from code review Co-authored-by: Pedro Cuenca <pedro@huggingface.co> * More improvements * Update docs * Address comments * Add link to upload files * Apply suggestions from code review Co-authored-by: Julien Chaumond <julien@huggingface.co> * web interface first --------- Co-authored-by: Pedro Cuenca <pedro@huggingface.co> Co-authored-by: Julien Chaumond <julien@huggingface.co> Co-authored-by: Wauplin <lucainp@gmail.com>
1 parent a6c5fd9 commit c72daf0

File tree

1 file changed

+77
-5
lines changed

1 file changed

+77
-5
lines changed

docs/hub/models-uploading.md

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ To upload models to the Hub, you'll need to create an account at [Hugging Face](
44

55
You can link repositories with an individual user, such as [osanseviero/fashion_brands_patterns](https://huggingface.co/osanseviero/fashion_brands_patterns), or with an organization, such as [facebook/bart-large-xsum](https://huggingface.co/facebook/bart-large-xsum). Organizations can collect models related to a company, community, or library! If you choose an organization, the model will be featured on the organization’s page, and every member of the organization will have the ability to contribute to the repository. You can create a new organization [here](https://huggingface.co/organizations/new).
66

7-
There are several ways to upload models to the Hub, described below. We suggest adding a [Model Card](./model-cards) to your repo to document your model.
7+
There are several ways to upload models to the Hub, described below.
8+
9+
- In case your model comes from a library that has [built-in support](#upload-from-a-library-with-built-in-support), one can use the existing methods.
10+
- In case your model is a custom PyTorch model, the recommended way is to leverage the [huggingface_hub](#using-the-huggingface_hub-client-library) Python library as it allows to add `from_pretrained`, `push_to_hub` and [automated download metrics](https://huggingface.co/docs/hub/models-download-stats) capabilities to your models, just like models in the Transformers, Diffusers and Timm libraries.
11+
- In addition to programmatic uploads, you can always use the [web interface](#using-the-web-interface).
12+
13+
Once your model is uploaded, we suggest adding a [Model Card](./model-cards) to your repo to document your model.
814

915
## Using the web interface
1016

@@ -66,10 +72,76 @@ Any repository that contains TensorBoard traces (filenames that contain `tfevent
6672

6773
Models trained with 🤗 Transformers will generate [TensorBoard traces](https://huggingface.co/docs/transformers/main_classes/callback#transformers.integrations.TensorBoardCallback) by default if [`tensorboard`](https://pypi.org/project/tensorboard/) is installed.
6874

69-
## Using Git
7075

71-
Since model repos are just Git repositories, you can use Git to push your model files to the Hub. Follow the guide on [Getting Started with Repositories](repositories-getting-started) to learn about using the `git` CLI to commit and push your models.
76+
## Upload from a library with built-in support
77+
78+
First check if your model is from a library that has built-in support to push to/load from the Hub, like Transformers, Diffusers, Timm, Asteroid, etc.: https://huggingface.co/docs/hub/models-libraries. Below we'll show how easy this is for a library like Transformers:
79+
80+
```python
81+
from transformers import BertConfig, BertModel
82+
83+
config = BertConfig()
84+
model = BertModel(config)
85+
86+
model.push_to_hub("nielsr/my-awesome-bert-model")
87+
88+
# reload
89+
model = BertModel.from_pretrained("nielsr/my-awesome-bert-model")
90+
```
91+
92+
## Upload a PyTorch model using `huggingface_hub`
93+
94+
In case your model is a (custom) PyTorch model, you can leverage the `PyTorchModelHubMixin` [class](https://huggingface.co/docs/huggingface_hub/package_reference/mixins#huggingface_hub.PyTorchModelHubMixin) available in the [huggingface_hub](https://github.com/huggingface/huggingface_hub) Python library. It is a minimal class which adds `from_pretrained` and `push_to_hub` capabilities to any `nn.Module`, along with download metrics.
95+
96+
Here is how to use it (assuming you have run `pip install huggingface_hub`):
97+
98+
```python
99+
import torch
100+
import torch.nn as nn
101+
from huggingface_hub import PyTorchModelHubMixin
102+
103+
104+
class MyModel(nn.Module, PyTorchModelHubMixin):
105+
def __init__(self, config: dict):
106+
super().__init__()
107+
self.param = nn.Parameter(torch.rand(config["num_channels"], config["hidden_size"]))
108+
self.linear = nn.Linear(config["hidden_size"], config["num_classes"])
72109

73-
## Using the `huggingface_hub` client library
110+
def forward(self, x):
111+
return self.linear(x + self.param)
112+
113+
# create model
114+
config = {"num_channels": 3, "hidden_size": 32, "num_classes": 10}
115+
model = MyModel(config=config)
116+
117+
# save locally
118+
model.save_pretrained("my-awesome-model", config=config)
119+
120+
# push to the hub
121+
model.push_to_hub("my-awesome-model", config=config)
122+
123+
# reload
124+
model = MyModel.from_pretrained("username/my-awesome-model")
125+
```
126+
127+
As can be seen, the only thing required is to define all hyperparameters regarding the model architecture (such as hidden size, number of classes, dropout probability, etc.) in a Python dictionary often called the `config`. Next, you can define a class which takes the `config` as keyword argument in its init.
128+
129+
This comes with automated download metrics, meaning that you'll be able to see how many times the model is downloaded, the same way they are available for models integrated natively in the Transformers, Diffusers or Timm libraries. With this mixin class, each separate checkpoint is stored on the Hub in a single repository consisting of 2 files:
130+
131+
- a `pytorch_model.bin` or `model.safetensors` file containing the weights
132+
- a `config.json` file which is a serialized version of the model configuration. This class is used for counting download metrics: everytime a user calls `from_pretrained` to load a `config.json`, the count goes up by one. See [this guide](https://huggingface.co/docs/hub/models-download-stats) regarding automated download metrics.
133+
134+
It's recommended to add a model card to each checkpoint so that people can read what the model is about, have a link to the paper, etc.
135+
136+
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/huggingface_hub/mixin_example_bis.png"
137+
alt="drawing" width="600"/>
138+
139+
<small> Example [repository](https://huggingface.co/LiheYoung/depth_anything_vitl14) that leverages PyTorchModelHubMixin. Downloads are shown on the right.</small>
140+
141+
Visit [the huggingface_hub's documentation](https://huggingface.co/docs/huggingface_hub/guides/integrations) to learn more.
142+
143+
Alternatively, one can also simply programmatically upload files or folders to the hub: https://huggingface.co/docs/huggingface_hub/guides/upload.
144+
145+
## Using Git
74146

75-
The rich feature set in the `huggingface_hub` library allows you to manage repositories, including creating repos and uploading models to the Model Hub. Visit [the client library's documentation](https://huggingface.co/docs/huggingface_hub/index) to learn more.
147+
Finally, since model repos are just Git repositories, you can also use Git to push your model files to the Hub. Follow the guide on [Getting Started with Repositories](repositories-getting-started#adding-files-to-a-repository-terminalterminal) to learn about using the `git` CLI to commit and push your models.

0 commit comments

Comments
 (0)