model_sharing.md 9.98 KB
Newer Older
1
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
Sylvain Gugger's avatar
Sylvain Gugger committed
2
3
4
5
6
7
8
9
10

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
11
12
13
14

鈿狅笍 Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.

Sylvain Gugger's avatar
Sylvain Gugger committed
15
16
-->

17
# Share a model
Sylvain Gugger's avatar
Sylvain Gugger committed
18

19
20
21
22
23
24
The last two tutorials showed how you can fine-tune a model with PyTorch, Keras, and 馃 Accelerate for distributed setups. The next step is to share your model with the community! At Hugging Face, we believe in openly sharing knowledge and resources to democratize artificial intelligence for everyone. We encourage you to consider sharing your model with the community to help others save time and resources.

In this tutorial, you will learn two methods for sharing a trained or fine-tuned model on the [Model Hub](https://huggingface.co/models):

- Programmatically push your files to the Hub.
- Drag-and-drop your files to the Hub with the web interface.
Sylvain Gugger's avatar
Sylvain Gugger committed
25
26
27
28
29
30
31

<iframe width="560" height="315" src="https://www.youtube.com/embed/XvSGPZFEjDY" title="YouTube video player"
frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope;
picture-in-picture" allowfullscreen></iframe>

<Tip>

32
To share a model with the community, you need an account on [huggingface.co](https://huggingface.co/join). You can also join an existing organization or create a new one.
Sylvain Gugger's avatar
Sylvain Gugger committed
33
34
35

</Tip>

36
## Repository features
Sylvain Gugger's avatar
Sylvain Gugger committed
37

38
Each repository on the Model Hub behaves like a typical GitHub repository. Our repositories offer versioning, commit history, and the ability to visualize differences.
Sylvain Gugger's avatar
Sylvain Gugger committed
39

40
The Model Hub's built-in versioning is based on git and [git-lfs](https://git-lfs.github.com/). In other words, you can treat one model as one repository, enabling greater access control and scalability. Version control allows *revisions*, a method for pinning a specific version of a model with a commit hash, tag or branch.
Sylvain Gugger's avatar
Sylvain Gugger committed
41

42
As a result, you can load a specific model version with the `revision` parameter:
Sylvain Gugger's avatar
Sylvain Gugger committed
43

44
```py
Sylvain Gugger's avatar
Sylvain Gugger committed
45
>>> model = AutoModel.from_pretrained(
Sylvain Gugger's avatar
Sylvain Gugger committed
46
47
...     "julien-c/EsperBERTo-small", revision="v2.0.1"  # tag name, or branch name, or commit hash
... )
Sylvain Gugger's avatar
Sylvain Gugger committed
48
49
```

50
51
52
Files are also easily edited in a repository, and you can view the commit history as well as the difference:

![vis_diff](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/vis_diff.png)
Sylvain Gugger's avatar
Sylvain Gugger committed
53

54
## Setup
Sylvain Gugger's avatar
Sylvain Gugger committed
55

56
Before sharing a model to the Hub, you will need your Hugging Face credentials. If you have access to a terminal, run the following command in the virtual environment where 馃 Transformers is installed. This will store your access token in your Hugging Face cache folder (`~/.cache/` by default):
Sylvain Gugger's avatar
Sylvain Gugger committed
57
58
59
60
61

```bash
huggingface-cli login
```

62
If you are using a notebook like Jupyter or Colaboratory, make sure you have the [`huggingface_hub`](https://huggingface.co/docs/hub/adding-a-library) library installed. This library allows you to programmatically interact with the Hub.
Sylvain Gugger's avatar
Sylvain Gugger committed
63

64
65
```bash
pip install huggingface_hub
Sylvain Gugger's avatar
Sylvain Gugger committed
66
67
```

68
Then use `notebook_login` to sign-in to the Hub, and follow the link [here](https://huggingface.co/settings/token) to generate a token to login with:
Sylvain Gugger's avatar
Sylvain Gugger committed
69

70
71
```py
>>> from huggingface_hub import notebook_login
Sylvain Gugger's avatar
Sylvain Gugger committed
72

73
>>> notebook_login()
Sylvain Gugger's avatar
Sylvain Gugger committed
74
75
```

76
## Convert a model for all frameworks
Sylvain Gugger's avatar
Sylvain Gugger committed
77

78
To ensure your model can be used by someone working with a different framework, we recommend you convert and upload your model with both PyTorch and TensorFlow checkpoints. While users are still able to load your model from a different framework if you skip this step, it will be slower because 馃 Transformers will need to convert the checkpoint on-the-fly.
Sylvain Gugger's avatar
Sylvain Gugger committed
79

80
Converting a checkpoint for another framework is easy. Make sure you have PyTorch and TensorFlow installed (see [here](installation) for installation instructions), and then find the specific model for your task in the other framework. 
Sylvain Gugger's avatar
Sylvain Gugger committed
81

82
83
84
<frameworkcontent>
<pt>
Specify `from_tf=True` to convert a checkpoint from TensorFlow to PyTorch:
Sylvain Gugger's avatar
Sylvain Gugger committed
85

86
```py
87
88
>>> pt_model = DistilBertForSequenceClassification.from_pretrained("path/to/awesome-name-you-picked", from_tf=True)
>>> pt_model.save_pretrained("path/to/awesome-name-you-picked")
Sylvain Gugger's avatar
Sylvain Gugger committed
89
```
90
91
92
</pt>
<tf>
Specify `from_pt=True` to convert a checkpoint from PyTorch to TensorFlow:
Sylvain Gugger's avatar
Sylvain Gugger committed
93

94
```py
95
>>> tf_model = TFDistilBertForSequenceClassification.from_pretrained("path/to/awesome-name-you-picked", from_pt=True)
Sylvain Gugger's avatar
Sylvain Gugger committed
96
97
```

omahs's avatar
omahs committed
98
Then you can save your new TensorFlow model with its new checkpoint:
Sylvain Gugger's avatar
Sylvain Gugger committed
99

100
```py
101
>>> tf_model.save_pretrained("path/to/awesome-name-you-picked")
Sylvain Gugger's avatar
Sylvain Gugger committed
102
```
103
104
</tf>
<jax>
105
If a model is available in Flax, you can also convert a checkpoint from PyTorch to Flax:
Sylvain Gugger's avatar
Sylvain Gugger committed
106

107
108
109
110
```py
>>> flax_model = FlaxDistilBertForSequenceClassification.from_pretrained(
...     "path/to/awesome-name-you-picked", from_pt=True
... )
Sylvain Gugger's avatar
Sylvain Gugger committed
111
```
112
113
</jax>
</frameworkcontent>
Sylvain Gugger's avatar
Sylvain Gugger committed
114

115
## Push a model during training
Sylvain Gugger's avatar
Sylvain Gugger committed
116

117
118
<frameworkcontent>
<pt>
119
<Youtube id="Z1-XMy-GNLQ"/>
Sylvain Gugger's avatar
Sylvain Gugger committed
120

121
Sharing a model to the Hub is as simple as adding an extra parameter or callback. Remember from the [fine-tuning tutorial](training), the [`TrainingArguments`] class is where you specify hyperparameters and additional training options. One of these training options includes the ability to push a model directly to the Hub. Set `push_to_hub=True` in your [`TrainingArguments`]:
Sylvain Gugger's avatar
Sylvain Gugger committed
122

123
124
```py
>>> training_args = TrainingArguments(output_dir="my-awesome-model", push_to_hub=True)
Sylvain Gugger's avatar
Sylvain Gugger committed
125
126
```

127
Pass your training arguments as usual to [`Trainer`]:
Sylvain Gugger's avatar
Sylvain Gugger committed
128

129
130
131
132
133
134
135
136
```py
>>> trainer = Trainer(
...     model=model,
...     args=training_args,
...     train_dataset=small_train_dataset,
...     eval_dataset=small_eval_dataset,
...     compute_metrics=compute_metrics,
... )
Sylvain Gugger's avatar
Sylvain Gugger committed
137
138
```

139
After you fine-tune your model, call [`~transformers.Trainer.push_to_hub`] on [`Trainer`] to push the trained model to the Hub. 馃 Transformers will even automatically add training hyperparameters, training results and framework versions to your model card!
Sylvain Gugger's avatar
Sylvain Gugger committed
140

141
142
```py
>>> trainer.push_to_hub()
Sylvain Gugger's avatar
Sylvain Gugger committed
143
```
144
145
146
</pt>
<tf>
Share a model to the Hub with [`PushToHubCallback`]. In the [`PushToHubCallback`] function, add:
Sylvain Gugger's avatar
Sylvain Gugger committed
147

148
149
150
- An output directory for your model.
- A tokenizer.
- The `hub_model_id`, which is your Hub username and model name.
Sylvain Gugger's avatar
Sylvain Gugger committed
151

152
```py
153
>>> from transformers import PushToHubCallback
Sylvain Gugger's avatar
Sylvain Gugger committed
154

155
156
157
>>> push_to_hub_callback = PushToHubCallback(
...     output_dir="./your_model_save_path", tokenizer=tokenizer, hub_model_id="your-username/my-awesome-model"
... )
Sylvain Gugger's avatar
Sylvain Gugger committed
158
159
```

160
Add the callback to [`fit`](https://keras.io/api/models/model_training_apis/), and 馃 Transformers will push the trained model to the Hub:
Sylvain Gugger's avatar
Sylvain Gugger committed
161

162
163
```py
>>> model.fit(tf_train_dataset, validation_data=tf_validation_dataset, epochs=3, callbacks=push_to_hub_callback)
Sylvain Gugger's avatar
Sylvain Gugger committed
164
```
165
166
</tf>
</frameworkcontent>
Sylvain Gugger's avatar
Sylvain Gugger committed
167

168
## Use the `push_to_hub` function
Sylvain Gugger's avatar
Sylvain Gugger committed
169

170
You can also call `push_to_hub` directly on your model to upload it to the Hub.
Sylvain Gugger's avatar
Sylvain Gugger committed
171

172
Specify your model name in `push_to_hub`:
Sylvain Gugger's avatar
Sylvain Gugger committed
173

174
175
```py
>>> pt_model.push_to_hub("my-awesome-model")
Sylvain Gugger's avatar
Sylvain Gugger committed
176
177
```

178
This creates a repository under your username with the model name `my-awesome-model`. Users can now load your model with the `from_pretrained` function:
Sylvain Gugger's avatar
Sylvain Gugger committed
179

180
181
```py
>>> from transformers import AutoModel
Sylvain Gugger's avatar
Sylvain Gugger committed
182

183
>>> model = AutoModel.from_pretrained("your_username/my-awesome-model")
Sylvain Gugger's avatar
Sylvain Gugger committed
184
185
```

186
If you belong to an organization and want to push your model under the organization name instead, just add it to the `repo_id`:
Sylvain Gugger's avatar
Sylvain Gugger committed
187

188
```py
189
>>> pt_model.push_to_hub("my-awesome-org/my-awesome-model")
Sylvain Gugger's avatar
Sylvain Gugger committed
190
191
```

192
The `push_to_hub` function can also be used to add other files to a model repository. For example, add a tokenizer to a model repository:
Sylvain Gugger's avatar
Sylvain Gugger committed
193

194
195
```py
>>> tokenizer.push_to_hub("my-awesome-model")
Sylvain Gugger's avatar
Sylvain Gugger committed
196
197
```

198
Or perhaps you'd like to add the TensorFlow version of your fine-tuned PyTorch model:
Sylvain Gugger's avatar
Sylvain Gugger committed
199

200
201
```py
>>> tf_model.push_to_hub("my-awesome-model")
Sylvain Gugger's avatar
Sylvain Gugger committed
202
203
```

omahs's avatar
omahs committed
204
Now when you navigate to your Hugging Face profile, you should see your newly created model repository. Clicking on the **Files** tab will display all the files you've uploaded to the repository.
Sylvain Gugger's avatar
Sylvain Gugger committed
205

206
For more details on how to create and upload files to a repository, refer to the Hub documentation [here](https://huggingface.co/docs/hub/how-to-upstream).
Sylvain Gugger's avatar
Sylvain Gugger committed
207

208
## Upload with the web interface
Sylvain Gugger's avatar
Sylvain Gugger committed
209

210
Users who prefer a no-code approach are able to upload a model through the Hub's web interface. Visit [huggingface.co/new](https://huggingface.co/new) to create a new repository:
Sylvain Gugger's avatar
Sylvain Gugger committed
211

212
![new_model_repo](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/new_model_repo.png)
Sylvain Gugger's avatar
Sylvain Gugger committed
213

214
From here, add some information about your model:
Sylvain Gugger's avatar
Sylvain Gugger committed
215

216
217
218
219
- Select the **owner** of the repository. This can be yourself or any of the organizations you belong to.
- Pick a name for your model, which will also be the repository name.
- Choose whether your model is public or private.
- Specify the license usage for your model.
Sylvain Gugger's avatar
Sylvain Gugger committed
220

221
Now click on the **Files** tab and click on the **Add file** button to upload a new file to your repository. Then drag-and-drop a file to upload and add a commit message.
Sylvain Gugger's avatar
Sylvain Gugger committed
222

223
![upload_file](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/upload_file.png)
Sylvain Gugger's avatar
Sylvain Gugger committed
224

225
## Add a model card
Sylvain Gugger's avatar
Sylvain Gugger committed
226

227
To make sure users understand your model's capabilities, limitations, potential biases and ethical considerations, please add a model card to your repository. The model card is defined in the `README.md` file. You can add a model card by:
Sylvain Gugger's avatar
Sylvain Gugger committed
228

229
230
* Manually creating and uploading a `README.md` file.
* Clicking on the **Edit model card** button in your model repository.
Sylvain Gugger's avatar
Sylvain Gugger committed
231

232
Take a look at the DistilBert [model card](https://huggingface.co/distilbert-base-uncased) for a good example of the type of information a model card should include. For more details about other options you can control in the `README.md` file such as a model's carbon footprint or widget examples, refer to the documentation [here](https://huggingface.co/docs/hub/models-cards).