Skip to content

Commit d002d24

Browse files
Merge branch 'main' into modify-apply-overlay
2 parents 849c993 + 7833ed9 commit d002d24

12 files changed

+121
-15
lines changed

examples/advanced_diffusion_training/train_dreambooth_lora_sd15_advanced.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,7 @@ def save_model_hook(models, weights, output_dir):
12901290
text_encoder_one_lora_layers_to_save = convert_state_dict_to_diffusers(
12911291
get_peft_model_state_dict(model)
12921292
)
1293+
else:
12931294
raise ValueError(f"unexpected save model: {model.__class__}")
12941295

12951296
# make sure to pop weight so that corresponding model is not saved again
@@ -1981,7 +1982,7 @@ def compute_text_embeddings(prompt, text_encoders, tokenizers):
19811982
lora_state_dict = load_file(f"{args.output_dir}/pytorch_lora_weights.safetensors")
19821983
peft_state_dict = convert_all_state_dict_to_peft(lora_state_dict)
19831984
kohya_state_dict = convert_state_dict_to_kohya(peft_state_dict)
1984-
save_file(kohya_state_dict, f"{args.output_dir}/{args.output_dir}.safetensors")
1985+
save_file(kohya_state_dict, f"{args.output_dir}/{Path(args.output_dir).name}.safetensors")
19851986

19861987
save_model_card(
19871988
model_id if not args.push_to_hub else repo_id,

examples/advanced_diffusion_training/train_dreambooth_lora_sdxl_advanced.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2425,7 +2425,7 @@ def get_sigmas(timesteps, n_dim=4, dtype=torch.float32):
24252425
lora_state_dict = load_file(f"{args.output_dir}/pytorch_lora_weights.safetensors")
24262426
peft_state_dict = convert_all_state_dict_to_peft(lora_state_dict)
24272427
kohya_state_dict = convert_state_dict_to_kohya(peft_state_dict)
2428-
save_file(kohya_state_dict, f"{args.output_dir}/{args.output_dir}.safetensors")
2428+
save_file(kohya_state_dict, f"{args.output_dir}/{Path(args.output_dir).name}.safetensors")
24292429

24302430
save_model_card(
24312431
model_id if not args.push_to_hub else repo_id,

examples/dreambooth/train_dreambooth_lora_sd3.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,37 @@ def save_model_card(
101101
102102
## Model description
103103
104-
These are {repo_id} DreamBooth weights for {base_model}.
104+
These are {repo_id} DreamBooth LoRA weights for {base_model}.
105105
106-
The weights were trained using [DreamBooth](https://dreambooth.github.io/).
106+
The weights were trained using [DreamBooth](https://dreambooth.github.io/) with the [SD3 diffusers trainer](https://github.com/huggingface/diffusers/blob/main/examples/dreambooth/README_sd3.md).
107107
108-
LoRA for the text encoder was enabled: {train_text_encoder}.
108+
Was LoRA for the text encoder enabled? {train_text_encoder}.
109109
110110
## Trigger words
111111
112-
You should use {instance_prompt} to trigger the image generation.
112+
You should use `{instance_prompt}` to trigger the image generation.
113113
114114
## Download model
115115
116-
[Download]({repo_id}/tree/main) them in the Files & versions tab.
116+
[Download the *.safetensors LoRA]({repo_id}/tree/main) in the Files & versions tab.
117+
118+
## Use it with the [🧨 diffusers library](https://github.com/huggingface/diffusers)
119+
120+
```py
121+
from diffusers import AutoPipelineForText2Image
122+
import torch
123+
pipeline = AutoPipelineForText2Image.from_pretrained('stabilityai/stable-diffusion-3-medium-diffusers', torch_dtype=torch.float16).to('cuda')
124+
pipeline.load_lora_weights('{repo_id}', weight_name='pytorch_lora_weights.safetensors')
125+
image = pipeline('{validation_prompt if validation_prompt else instance_prompt}').images[0]
126+
```
127+
128+
### Use it with UIs such as AUTOMATIC1111, Comfy UI, SD.Next, Invoke
129+
130+
- **LoRA**: download **[`diffusers_lora_weights.safetensors` here 💾](/{repo_id}/blob/main/diffusers_lora_weights.safetensors)**.
131+
- Rename it and place it on your `models/Lora` folder.
132+
- On AUTOMATIC1111, load the LoRA by adding `<lora:your_new_name:1>` to your prompt. On ComfyUI just [load it as a regular LoRA](https://comfyanonymous.github.io/ComfyUI_examples/lora/).
133+
134+
For more details, including weighting, merging and fusing LoRAs, check the [documentation on loading LoRAs in diffusers](https://huggingface.co/docs/diffusers/main/en/using-diffusers/loading_adapters)
117135
118136
## License
119137

examples/dreambooth/train_dreambooth_sd3.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,22 @@ def save_model_card(
9595
9696
These are {repo_id} DreamBooth weights for {base_model}.
9797
98-
The weights were trained using [DreamBooth](https://dreambooth.github.io/).
98+
The weights were trained using [DreamBooth](https://dreambooth.github.io/) with the [SD3 diffusers trainer](https://github.com/huggingface/diffusers/blob/main/examples/dreambooth/README_sd3.md).
9999
100-
Text encoder was fine-tuned: {train_text_encoder}.
100+
Was the text encoder fine-tuned? {train_text_encoder}.
101101
102102
## Trigger words
103103
104-
You should use {instance_prompt} to trigger the image generation.
104+
You should use `{instance_prompt}` to trigger the image generation.
105105
106-
## Download model
106+
## Use it with the [🧨 diffusers library](https://github.com/huggingface/diffusers)
107107
108-
[Download]({repo_id}/tree/main) them in the Files & versions tab.
108+
```py
109+
from diffusers import AutoPipelineForText2Image
110+
import torch
111+
pipeline = AutoPipelineForText2Image.from_pretrained('{repo_id}', torch_dtype=torch.float16).to('cuda')
112+
image = pipeline('{validation_prompt if validation_prompt else instance_prompt}').images[0]
113+
```
109114
110115
## License
111116

src/diffusers/loaders/single_file.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,4 @@ def load_module(name, value):
555555

556556
pipe = pipeline_class(**init_kwargs)
557557

558-
if torch_dtype is not None:
559-
pipe.to(dtype=torch_dtype)
560-
561558
return pipe

src/diffusers/loaders/single_file_utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,4 +1808,17 @@ def create_diffusers_t5_model_from_checkpoint(
18081808

18091809
else:
18101810
model.load_state_dict(diffusers_format_checkpoint)
1811+
1812+
use_keep_in_fp32_modules = (cls._keep_in_fp32_modules is not None) and (torch_dtype == torch.float16)
1813+
if use_keep_in_fp32_modules:
1814+
keep_in_fp32_modules = model._keep_in_fp32_modules
1815+
else:
1816+
keep_in_fp32_modules = []
1817+
1818+
if keep_in_fp32_modules is not None:
1819+
for name, param in model.named_parameters():
1820+
if any(module_to_keep_in_fp32 in name.split(".") for module_to_keep_in_fp32 in keep_in_fp32_modules):
1821+
# param = param.to(torch.float32) does not work here as only in the local scope.
1822+
param.data = param.data.to(torch.float32)
1823+
18111824
return model

tests/single_file/single_file_testing_utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,20 @@ def test_single_file_components_with_diffusers_config_local_files_only(
201201

202202
self._compare_component_configs(pipe, single_file_pipe)
203203

204+
def test_single_file_setting_pipeline_dtype_to_fp16(
205+
self,
206+
single_file_pipe=None,
207+
):
208+
single_file_pipe = single_file_pipe or self.pipeline_class.from_single_file(
209+
self.ckpt_path, torch_dtype=torch.float16
210+
)
211+
212+
for component_name, component in single_file_pipe.components.items():
213+
if not isinstance(component, torch.nn.Module):
214+
continue
215+
216+
assert component.dtype == torch.float16
217+
204218

205219
class SDXLSingleFileTesterMixin:
206220
def _compare_component_configs(self, pipe, single_file_pipe):
@@ -378,3 +392,17 @@ def test_single_file_format_inference_is_same_as_pretrained(self, expected_max_d
378392
max_diff = numpy_cosine_similarity_distance(image.flatten(), image_single_file.flatten())
379393

380394
assert max_diff < expected_max_diff
395+
396+
def test_single_file_setting_pipeline_dtype_to_fp16(
397+
self,
398+
single_file_pipe=None,
399+
):
400+
single_file_pipe = single_file_pipe or self.pipeline_class.from_single_file(
401+
self.ckpt_path, torch_dtype=torch.float16
402+
)
403+
404+
for component_name, component in single_file_pipe.components.items():
405+
if not isinstance(component, torch.nn.Module):
406+
continue
407+
408+
assert component.dtype == torch.float16

tests/single_file/test_stable_diffusion_controlnet_img2img_single_file.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,12 @@ def test_single_file_components_with_diffusers_config_local_files_only(self):
180180
local_files_only=True,
181181
)
182182
super()._compare_component_configs(pipe, pipe_single_file)
183+
184+
def test_single_file_setting_pipeline_dtype_to_fp16(self):
185+
controlnet = ControlNetModel.from_pretrained(
186+
"lllyasviel/control_v11p_sd15_canny", torch_dtype=torch.float16, variant="fp16"
187+
)
188+
single_file_pipe = self.pipeline_class.from_single_file(
189+
self.ckpt_path, controlnet=controlnet, safety_checker=None, torch_dtype=torch.float16
190+
)
191+
super().test_single_file_setting_pipeline_dtype_to_fp16(single_file_pipe)

tests/single_file/test_stable_diffusion_controlnet_inpaint_single_file.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,12 @@ def test_single_file_components_with_diffusers_config_local_files_only(self):
181181
local_files_only=True,
182182
)
183183
super()._compare_component_configs(pipe, pipe_single_file)
184+
185+
def test_single_file_setting_pipeline_dtype_to_fp16(self):
186+
controlnet = ControlNetModel.from_pretrained(
187+
"lllyasviel/control_v11p_sd15_canny", torch_dtype=torch.float16, variant="fp16"
188+
)
189+
single_file_pipe = self.pipeline_class.from_single_file(
190+
self.ckpt_path, controlnet=controlnet, safety_checker=None, torch_dtype=torch.float16
191+
)
192+
super().test_single_file_setting_pipeline_dtype_to_fp16(single_file_pipe)

tests/single_file/test_stable_diffusion_controlnet_single_file.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,12 @@ def test_single_file_components_with_diffusers_config_local_files_only(self):
169169
local_files_only=True,
170170
)
171171
super()._compare_component_configs(pipe, pipe_single_file)
172+
173+
def test_single_file_setting_pipeline_dtype_to_fp16(self):
174+
controlnet = ControlNetModel.from_pretrained(
175+
"lllyasviel/control_v11p_sd15_canny", torch_dtype=torch.float16, variant="fp16"
176+
)
177+
single_file_pipe = self.pipeline_class.from_single_file(
178+
self.ckpt_path, controlnet=controlnet, safety_checker=None, torch_dtype=torch.float16
179+
)
180+
super().test_single_file_setting_pipeline_dtype_to_fp16(single_file_pipe)

0 commit comments

Comments
 (0)