Skip to content

Commit 4ff7264

Browse files
authored
[docs] PushToHubMixin (huggingface#4622)
* push to hub docs * fix typo * feedback * make style
1 parent 5049599 commit 4ff7264

File tree

5 files changed

+182
-8
lines changed

5 files changed

+182
-8
lines changed

docs/source/en/_toctree.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
title: Load safetensors
3333
- local: using-diffusers/other-formats
3434
title: Load different Stable Diffusion formats
35+
- local: using-diffusers/push_to_hub
36+
title: Push files to the Hub
3537
title: Loading & Hub
3638
- sections:
3739
- local: using-diffusers/pipeline_overview

docs/source/en/api/models/overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ All models are built from the base [`ModelMixin`] class which is a [`torch.nn.mo
1111

1212
[[autodoc]] FlaxModelMixin
1313

14-
## Pushing to the Hub
14+
## PushToHubMixin
1515

1616
[[autodoc]] utils.PushToHubMixin
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# Push files to the Hub
2+
3+
[[open-in-colab]]
4+
5+
🤗 Diffusers provides a [`~diffusers.utils.PushToHubMixin`] for uploading your model, scheduler, or pipeline to the Hub. It is an easy way to store your files on the Hub, and also allows you to share your work with others. Under the hood, the [`~diffusers.utils.PushToHubMixin`]:
6+
7+
1. creates a repository on the Hub
8+
2. saves your model, scheduler, or pipeline files so they can be reloaded later
9+
3. uploads folder containing these files to the Hub
10+
11+
This guide will show you how to use the [`~diffusers.utils.PushToHubMixin`] to upload your files to the Hub.
12+
13+
You'll need to log in to your Hub account with your access [token](https://huggingface.co/settings/tokens) first:
14+
15+
```py
16+
from huggingface_hub import notebook_login
17+
18+
notebook_login()
19+
```
20+
21+
## Models
22+
23+
To push a model to the Hub, call [`~diffusers.utils.PushToHubMixin.push_to_hub`] and specfiy the repository id of the model to be stored on the Hub:
24+
25+
```py
26+
from diffusers import ControlNetModel
27+
28+
controlnet = ControlNetModel(
29+
block_out_channels=(32, 64),
30+
layers_per_block=2,
31+
in_channels=4,
32+
down_block_types=("DownBlock2D", "CrossAttnDownBlock2D"),
33+
cross_attention_dim=32,
34+
conditioning_embedding_out_channels=(16, 32),
35+
)
36+
controlnet.push_to_hub("my-controlnet-model")
37+
```
38+
39+
For model's, you can also specify the [*variant*](loading#checkpoint-variants) of the weights to push to the Hub. For example, to push `fp16` weights:
40+
41+
```py
42+
controlnet.push_to_hub("my-controlnet-model", variant="fp16")
43+
```
44+
45+
The [`~diffusers.utils.PushToHubMixin.push_to_hub`] function saves the model's `config.json` file and the weights are automatically saved in the `safetensors` format.
46+
47+
Now you can reload the model from your repository on the Hub:
48+
49+
```py
50+
model = ControlNetModel.from_pretrained("your-namespace/my-controlnet-model")
51+
```
52+
53+
## Scheduler
54+
55+
To push a scheduler to the Hub, call [`~diffusers.utils.PushToHubMixin.push_to_hub`] and specfiy the repository id of the scheduler to be stored on the Hub:
56+
57+
```py
58+
from diffusers import DDIMScheduler
59+
60+
scheduler = DDIMScheduler(
61+
beta_start=0.00085,
62+
beta_end=0.012,
63+
beta_schedule="scaled_linear",
64+
clip_sample=False,
65+
set_alpha_to_one=False,
66+
)
67+
scheduler.push_to_hub("my-controlnet-scheduler")
68+
```
69+
70+
The [`~diffusers.utils.PushToHubMixin.push_to_hub`] function saves the scheduler's `scheduler_config.json` file to the specified repository.
71+
72+
Now you can reload the scheduler from your repository on the Hub:
73+
74+
```py
75+
scheduler = DDIMScheduler.from_pretrained("your-namepsace/my-controlnet-scheduler")
76+
```
77+
78+
## Pipeline
79+
80+
You can also push an entire pipeline with all it's components to the Hub. For example, initialize the components of a [`StableDiffusionPipeline`] with the parameters you want:
81+
82+
```py
83+
from diffusers import (
84+
UNet2DConditionModel,
85+
AutoencoderKL,
86+
DDIMScheduler,
87+
StableDiffusionPipeline,
88+
)
89+
from transformers import CLIPTextModel, CLIPTextConfig, CLIPTokenizer
90+
91+
unet = UNet2DConditionModel(
92+
block_out_channels=(32, 64),
93+
layers_per_block=2,
94+
sample_size=32,
95+
in_channels=4,
96+
out_channels=4,
97+
down_block_types=("DownBlock2D", "CrossAttnDownBlock2D"),
98+
up_block_types=("CrossAttnUpBlock2D", "UpBlock2D"),
99+
cross_attention_dim=32,
100+
)
101+
102+
scheduler = DDIMScheduler(
103+
beta_start=0.00085,
104+
beta_end=0.012,
105+
beta_schedule="scaled_linear",
106+
clip_sample=False,
107+
set_alpha_to_one=False,
108+
)
109+
110+
vae = AutoencoderKL(
111+
block_out_channels=[32, 64],
112+
in_channels=3,
113+
out_channels=3,
114+
down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"],
115+
up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"],
116+
latent_channels=4,
117+
)
118+
119+
text_encoder_config = CLIPTextConfig(
120+
bos_token_id=0,
121+
eos_token_id=2,
122+
hidden_size=32,
123+
intermediate_size=37,
124+
layer_norm_eps=1e-05,
125+
num_attention_heads=4,
126+
num_hidden_layers=5,
127+
pad_token_id=1,
128+
vocab_size=1000,
129+
)
130+
text_encoder = CLIPTextModel(text_encoder_config)
131+
tokenizer = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip")
132+
```
133+
134+
Pass all of the components to the [`StableDiffusionPipeline`] and call [`~diffusers.utils.PushToHubMixin.push_to_hub`] to push the pipeline to the Hub:
135+
136+
```py
137+
components = {
138+
"unet": unet,
139+
"scheduler": scheduler,
140+
"vae": vae,
141+
"text_encoder": text_encoder,
142+
"tokenizer": tokenizer,
143+
"safety_checker": None,
144+
"feature_extractor": None,
145+
}
146+
147+
pipeline = StableDiffusionPipeline(**components)
148+
pipeline.push_to_hub("my-pipeline")
149+
```
150+
151+
The [`~diffusers.utils.PushToHubMixin.push_to_hub`] function saves each component to a subfolder in the repository. Now you can reload the pipeline from your repository on the Hub:
152+
153+
```py
154+
pipeline = StableDiffusionPipeline.from_pretrained("your-namespace/my-pipeline")
155+
```
156+
157+
## Privacy
158+
159+
Set `private=True` in the [`~diffusers.utils.PushToHubMixin.push_to_hub`] function to keep your model, scheduler, or pipeline files private:
160+
161+
```py
162+
controlnet.push_to_hub("my-controlnet-model", private=True)
163+
```
164+
165+
Private repositories are only visible to you, and other users won't be able to clone the repository and your repository won't appear in search results. Even if a user has the URL to your private repository, they'll receive a `404 - Repo not found error.`
166+
167+
To load a model, scheduler, or pipeline from a private or gated repositories, set `use_auth_token=True`:
168+
169+
```py
170+
model = ControlNet.from_pretrained("your-namespace/my-controlnet-model", use_auth_token=True)
171+
```

examples/research_projects/controlnet/train_controlnet_webdataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1370,7 +1370,7 @@ def get_sigmas(timesteps, n_dim=4, dtype=torch.float32):
13701370

13711371
# Get the target for loss depending on the prediction type
13721372
if noise_scheduler.config.prediction_type == "epsilon":
1373-
target = latents # compute loss against the denoised latents
1373+
target = latents # compute loss against the denoised latents
13741374
elif noise_scheduler.config.prediction_type == "v_prediction":
13751375
target = noise_scheduler.get_velocity(latents, noise, timesteps)
13761376
else:

src/diffusers/utils/hub_utils.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,23 +410,24 @@ def push_to_hub(
410410
variant: Optional[str] = None,
411411
) -> str:
412412
"""
413-
Upload the {object_files} to the 🤗 Hugging Face Hub.
413+
Upload model, scheduler, or pipeline files to the 🤗 Hugging Face Hub.
414414
415415
Parameters:
416416
repo_id (`str`):
417-
The name of the repository you want to push your {object} to. It should contain your organization name
418-
when pushing to a given organization. `repo_id` can also be a path to a local directory.
417+
The name of the repository you want to push your model, scheduler, or pipeline files to. It should
418+
contain your organization name when pushing to an organization. `repo_id` can also be a path to a local
419+
directory.
419420
commit_message (`str`, *optional*):
420-
Message to commit while pushing. Will default to `"Upload {object}"`.
421+
Message to commit while pushing. Default to `"Upload {object}"`.
421422
private (`bool`, *optional*):
422423
Whether or not the repository created should be private.
423424
token (`str`, *optional*):
424425
The token to use as HTTP bearer authorization for remote files. The token generated when running
425426
`huggingface-cli login` (stored in `~/.huggingface`).
426427
create_pr (`bool`, *optional*, defaults to `False`):
427428
Whether or not to create a PR with the uploaded files or directly commit.
428-
safe_serialization (`bool`, *optional*, defaults to `False`):
429-
Whether or not to convert the model weights in safetensors format for safer serialization.
429+
safe_serialization (`bool`, *optional*, defaults to `True`):
430+
Whether or not to convert the model weights to the `safetensors` format.
430431
variant (`str`, *optional*):
431432
If specified, weights are saved in the format `pytorch_model.<variant>.bin`.
432433

0 commit comments

Comments
 (0)