Skip to content

Commit 53e5995

Browse files
authored
Merge branch 'main' into safety_checker
2 parents 567f77a + 298ab6e commit 53e5995

29 files changed

+1809
-149
lines changed

.github/workflows/push_tests.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ jobs:
8181
- name: Environment
8282
run: |
8383
python utils/print_env.py
84-
- name: Slow PyTorch CUDA checkpoint tests on Ubuntu
84+
- name: PyTorch CUDA checkpoint tests on Ubuntu
8585
env:
8686
HF_TOKEN: ${{ secrets.HF_TOKEN }}
8787
# https://pytorch.org/docs/stable/notes/randomness.html#avoiding-nondeterministic-algorithms
@@ -184,7 +184,7 @@ jobs:
184184
run: |
185185
python utils/print_env.py
186186
187-
- name: Run slow Flax TPU tests
187+
- name: Run Flax TPU tests
188188
env:
189189
HF_TOKEN: ${{ secrets.HF_TOKEN }}
190190
run: |
@@ -232,7 +232,7 @@ jobs:
232232
run: |
233233
python utils/print_env.py
234234
235-
- name: Run slow ONNXRuntime CUDA tests
235+
- name: Run ONNXRuntime CUDA tests
236236
env:
237237
HF_TOKEN: ${{ secrets.HF_TOKEN }}
238238
run: |

docs/source/en/_toctree.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@
188188
title: Metal Performance Shaders (MPS)
189189
- local: optimization/habana
190190
title: Habana Gaudi
191+
- local: optimization/neuron
192+
title: AWS Neuron
191193
title: Optimized hardware
192194
title: Accelerate inference and reduce memory
193195
- sections:
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<!--Copyright 2024 The HuggingFace Team. All rights reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
4+
the License. You may obtain a copy of the License at
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9+
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
specific language governing permissions and limitations under the License.
11+
-->
12+
13+
# AWS Neuron
14+
15+
Diffusers functionalities are available on [AWS Inf2 instances](https://aws.amazon.com/ec2/instance-types/inf2/), which are EC2 instances powered by [Neuron machine learning accelerators](https://aws.amazon.com/machine-learning/inferentia/). These instances aim to provide better compute performance (higher throughput, lower latency) with good cost-efficiency, making them good candidates for AWS users to deploy diffusion models to production.
16+
17+
[Optimum Neuron](https://huggingface.co/docs/optimum-neuron/en/index) is the interface between Hugging Face libraries and AWS Accelerators, including AWS [Trainium](https://aws.amazon.com/machine-learning/trainium/) and AWS [Inferentia](https://aws.amazon.com/machine-learning/inferentia/). It supports many of the features in Diffusers with similar APIs, so it is easier to learn if you're already familiar with Diffusers. Once you have created an AWS Inf2 instance, install Optimum Neuron.
18+
19+
```bash
20+
python -m pip install --upgrade-strategy eager optimum[neuronx]
21+
```
22+
23+
<Tip>
24+
25+
We provide pre-built [Hugging Face Neuron Deep Learning AMI](https://aws.amazon.com/marketplace/pp/prodview-gr3e6yiscria2) (DLAMI) and Optimum Neuron containers for Amazon SageMaker. It's recommended to correctly set up your environment.
26+
27+
</Tip>
28+
29+
The example below demonstrates how to generate images with the Stable Diffusion XL model on an inf2.8xlarge instance (you can switch to cheaper inf2.xlarge instances once the model is compiled). To generate some images, use the [`~optimum.neuron.NeuronStableDiffusionXLPipeline`] class, which is similar to the [`StableDiffusionXLPipeline`] class in Diffusers.
30+
31+
Unlike Diffusers, you need to compile models in the pipeline to the Neuron format, `.neuron`. Launch the following command to export the model to the `.neuron` format.
32+
33+
```bash
34+
optimum-cli export neuron --model stabilityai/stable-diffusion-xl-base-1.0 \
35+
--batch_size 1 \
36+
--height 1024 `# height in pixels of generated image, eg. 768, 1024` \
37+
--width 1024 `# width in pixels of generated image, eg. 768, 1024` \
38+
--num_images_per_prompt 1 `# number of images to generate per prompt, defaults to 1` \
39+
--auto_cast matmul `# cast only matrix multiplication operations` \
40+
--auto_cast_type bf16 `# cast operations from FP32 to BF16` \
41+
sd_neuron_xl/
42+
```
43+
44+
Now generate some images with the pre-compiled SDXL model.
45+
46+
```python
47+
>>> from optimum.neuron import NeuronStableDiffusionXLPipeline
48+
49+
>>> stable_diffusion_xl = NeuronStableDiffusionXLPipeline.from_pretrained("sd_neuron_xl/")
50+
>>> prompt = "a pig with wings flying in floating US dollar banknotes in the air, skyscrapers behind, warm color palette, muted colors, detailed, 8k"
51+
>>> image = stable_diffusion_xl(prompt).images[0]
52+
```
53+
54+
<img
55+
src="https://huggingface.co/datasets/Jingya/document_images/resolve/main/optimum/neuron/sdxl_pig.png"
56+
width="256"
57+
height="256"
58+
alt="peggy generated by sdxl on inf2"
59+
/>
60+
61+
Feel free to check out more guides and examples on different use cases from the Optimum Neuron [documentation](https://huggingface.co/docs/optimum-neuron/en/inference_tutorials/stable_diffusion#generate-images-with-stable-diffusion-models-on-aws-inferentia)!

examples/advanced_diffusion_training/train_dreambooth_lora_flux_advanced.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,8 +2198,8 @@ def get_sigmas(timesteps, n_dim=4, dtype=torch.float32):
21982198

21992199
latent_image_ids = FluxPipeline._prepare_latent_image_ids(
22002200
model_input.shape[0],
2201-
model_input.shape[2],
2202-
model_input.shape[3],
2201+
model_input.shape[2] // 2,
2202+
model_input.shape[3] // 2,
22032203
accelerator.device,
22042204
weight_dtype,
22052205
)
@@ -2253,8 +2253,8 @@ def get_sigmas(timesteps, n_dim=4, dtype=torch.float32):
22532253
)[0]
22542254
model_pred = FluxPipeline._unpack_latents(
22552255
model_pred,
2256-
height=int(model_input.shape[2] * vae_scale_factor / 2),
2257-
width=int(model_input.shape[3] * vae_scale_factor / 2),
2256+
height=model_input.shape[2] * vae_scale_factor,
2257+
width=model_input.shape[3] * vae_scale_factor,
22582258
vae_scale_factor=vae_scale_factor,
22592259
)
22602260

examples/controlnet/train_controlnet_flux.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,8 +1256,8 @@ def get_sigmas(timesteps, n_dim=4, dtype=torch.float32):
12561256

12571257
latent_image_ids = FluxControlNetPipeline._prepare_latent_image_ids(
12581258
batch_size=pixel_latents_tmp.shape[0],
1259-
height=pixel_latents_tmp.shape[2],
1260-
width=pixel_latents_tmp.shape[3],
1259+
height=pixel_latents_tmp.shape[2] // 2,
1260+
width=pixel_latents_tmp.shape[3] // 2,
12611261
device=pixel_values.device,
12621262
dtype=pixel_values.dtype,
12631263
)

examples/dreambooth/train_dreambooth_flux.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,12 +1540,12 @@ def get_sigmas(timesteps, n_dim=4, dtype=torch.float32):
15401540
model_input = (model_input - vae.config.shift_factor) * vae.config.scaling_factor
15411541
model_input = model_input.to(dtype=weight_dtype)
15421542

1543-
vae_scale_factor = 2 ** (len(vae.config.block_out_channels))
1543+
vae_scale_factor = 2 ** (len(vae.config.block_out_channels) - 1)
15441544

15451545
latent_image_ids = FluxPipeline._prepare_latent_image_ids(
15461546
model_input.shape[0],
1547-
model_input.shape[2],
1548-
model_input.shape[3],
1547+
model_input.shape[2] // 2,
1548+
model_input.shape[3] // 2,
15491549
accelerator.device,
15501550
weight_dtype,
15511551
)
@@ -1601,8 +1601,8 @@ def get_sigmas(timesteps, n_dim=4, dtype=torch.float32):
16011601
# upscaling height & width as discussed in https://github.com/huggingface/diffusers/pull/9257#discussion_r1731108042
16021602
model_pred = FluxPipeline._unpack_latents(
16031603
model_pred,
1604-
height=int(model_input.shape[2] * vae_scale_factor / 2),
1605-
width=int(model_input.shape[3] * vae_scale_factor / 2),
1604+
height=model_input.shape[2] * vae_scale_factor,
1605+
width=model_input.shape[3] * vae_scale_factor,
16061606
vae_scale_factor=vae_scale_factor,
16071607
)
16081608

examples/dreambooth/train_dreambooth_lora_flux.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,12 +1645,12 @@ def get_sigmas(timesteps, n_dim=4, dtype=torch.float32):
16451645
model_input = (model_input - vae_config_shift_factor) * vae_config_scaling_factor
16461646
model_input = model_input.to(dtype=weight_dtype)
16471647

1648-
vae_scale_factor = 2 ** (len(vae_config_block_out_channels))
1648+
vae_scale_factor = 2 ** (len(vae_config_block_out_channels) - 1)
16491649

16501650
latent_image_ids = FluxPipeline._prepare_latent_image_ids(
16511651
model_input.shape[0],
1652-
model_input.shape[2],
1653-
model_input.shape[3],
1652+
model_input.shape[2] // 2,
1653+
model_input.shape[3] // 2,
16541654
accelerator.device,
16551655
weight_dtype,
16561656
)
@@ -1704,8 +1704,8 @@ def get_sigmas(timesteps, n_dim=4, dtype=torch.float32):
17041704
)[0]
17051705
model_pred = FluxPipeline._unpack_latents(
17061706
model_pred,
1707-
height=int(model_input.shape[2] * vae_scale_factor / 2),
1708-
width=int(model_input.shape[3] * vae_scale_factor / 2),
1707+
height=model_input.shape[2] * vae_scale_factor,
1708+
width=model_input.shape[3] * vae_scale_factor,
17091709
vae_scale_factor=vae_scale_factor,
17101710
)
17111711

examples/dreambooth/train_dreambooth_lora_sd3.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ def save_model_card(
8686
validation_prompt=None,
8787
repo_folder=None,
8888
):
89+
if "large" in base_model:
90+
model_variant = "SD3.5-Large"
91+
license_url = "https://huggingface.co/stabilityai/stable-diffusion-3.5-large/blob/main/LICENSE.md"
92+
variant_tags = ["sd3.5-large", "sd3.5", "sd3.5-diffusers"]
93+
else:
94+
model_variant = "SD3"
95+
license_url = "https://huggingface.co/stabilityai/stable-diffusion-3-medium/blob/main/LICENSE.md"
96+
variant_tags = ["sd3", "sd3-diffusers"]
97+
8998
widget_dict = []
9099
if images is not None:
91100
for i, image in enumerate(images):
@@ -95,7 +104,7 @@ def save_model_card(
95104
)
96105

97106
model_description = f"""
98-
# SD3 DreamBooth LoRA - {repo_id}
107+
# {model_variant} DreamBooth LoRA - {repo_id}
99108
100109
<Gallery />
101110
@@ -120,7 +129,7 @@ def save_model_card(
120129
```py
121130
from diffusers import AutoPipelineForText2Image
122131
import torch
123-
pipeline = AutoPipelineForText2Image.from_pretrained('stabilityai/stable-diffusion-3-medium-diffusers', torch_dtype=torch.float16).to('cuda')
132+
pipeline = AutoPipelineForText2Image.from_pretrained({base_model}, torch_dtype=torch.float16).to('cuda')
124133
pipeline.load_lora_weights('{repo_id}', weight_name='pytorch_lora_weights.safetensors')
125134
image = pipeline('{validation_prompt if validation_prompt else instance_prompt}').images[0]
126135
```
@@ -135,7 +144,7 @@ def save_model_card(
135144
136145
## License
137146
138-
Please adhere to the licensing terms as described [here](https://huggingface.co/stabilityai/stable-diffusion-3-medium/blob/main/LICENSE).
147+
Please adhere to the licensing terms as described [here]({license_url}).
139148
"""
140149
model_card = load_or_create_model_card(
141150
repo_id_or_path=repo_id,
@@ -151,11 +160,11 @@ def save_model_card(
151160
"diffusers-training",
152161
"diffusers",
153162
"lora",
154-
"sd3",
155-
"sd3-diffusers",
156163
"template:sd-lora",
157164
]
158165

166+
tags += variant_tags
167+
159168
model_card = populate_model_card(model_card, tags=tags)
160169
model_card.save(os.path.join(repo_folder, "README.md"))
161170

examples/dreambooth/train_dreambooth_sd3.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ def save_model_card(
7777
validation_prompt=None,
7878
repo_folder=None,
7979
):
80+
if "large" in base_model:
81+
model_variant = "SD3.5-Large"
82+
license_url = "https://huggingface.co/stabilityai/stable-diffusion-3.5-large/blob/main/LICENSE.md"
83+
variant_tags = ["sd3.5-large", "sd3.5", "sd3.5-diffusers"]
84+
else:
85+
model_variant = "SD3"
86+
license_url = "https://huggingface.co/stabilityai/stable-diffusion-3-medium/blob/main/LICENSE.md"
87+
variant_tags = ["sd3", "sd3-diffusers"]
88+
8089
widget_dict = []
8190
if images is not None:
8291
for i, image in enumerate(images):
@@ -86,7 +95,7 @@ def save_model_card(
8695
)
8796

8897
model_description = f"""
89-
# SD3 DreamBooth - {repo_id}
98+
# {model_variant} DreamBooth - {repo_id}
9099
91100
<Gallery />
92101
@@ -113,7 +122,7 @@ def save_model_card(
113122
114123
## License
115124
116-
Please adhere to the licensing terms as described `[here](https://huggingface.co/stabilityai/stable-diffusion-3-medium/blob/main/LICENSE)`.
125+
Please adhere to the licensing terms as described `[here]({license_url})`.
117126
"""
118127
model_card = load_or_create_model_card(
119128
repo_id_or_path=repo_id,
@@ -128,10 +137,9 @@ def save_model_card(
128137
"text-to-image",
129138
"diffusers-training",
130139
"diffusers",
131-
"sd3",
132-
"sd3-diffusers",
133140
"template:sd-lora",
134141
]
142+
tags += variant_tags
135143

136144
model_card = populate_model_card(model_card, tags=tags)
137145
model_card.save(os.path.join(repo_folder, "README.md"))

0 commit comments

Comments
 (0)