Skip to content

Commit 4a8c0ed

Browse files
authored
Merge branch 'main' into torchao-error-on-sharded-device-map
2 parents 05276c4 + d8825e7 commit 4a8c0ed

40 files changed

+2387
-93
lines changed

docs/source/en/_toctree.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@
400400
title: DiT
401401
- local: api/pipelines/flux
402402
title: Flux
403+
- local: api/pipelines/control_flux_inpaint
404+
title: FluxControlInpaint
403405
- local: api/pipelines/hunyuandit
404406
title: Hunyuan-DiT
405407
- local: api/pipelines/hunyuan_video
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<!--Copyright 2024 The HuggingFace Team, The Black Forest 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+
# FluxControlInpaint
14+
15+
FluxControlInpaintPipeline is an implementation of Inpainting for Flux.1 Depth/Canny models. It is a pipeline that allows you to inpaint images using the Flux.1 Depth/Canny models. The pipeline takes an image and a mask as input and returns the inpainted image.
16+
17+
FLUX.1 Depth and Canny [dev] is a 12 billion parameter rectified flow transformer capable of generating an image based on a text description while following the structure of a given input image. **This is not a ControlNet model**.
18+
19+
| Control type | Developer | Link |
20+
| -------- | ---------- | ---- |
21+
| Depth | [Black Forest Labs](https://huggingface.co/black-forest-labs) | [Link](https://huggingface.co/black-forest-labs/FLUX.1-Depth-dev) |
22+
| Canny | [Black Forest Labs](https://huggingface.co/black-forest-labs) | [Link](https://huggingface.co/black-forest-labs/FLUX.1-Canny-dev) |
23+
24+
25+
<Tip>
26+
27+
Flux can be quite expensive to run on consumer hardware devices. However, you can perform a suite of optimizations to run it faster and in a more memory-friendly manner. Check out [this section](https://huggingface.co/blog/sd3#memory-optimizations-for-sd3) for more details. Additionally, Flux can benefit from quantization for memory efficiency with a trade-off in inference latency. Refer to [this blog post](https://huggingface.co/blog/quanto-diffusers) to learn more. For an exhaustive list of resources, check out [this gist](https://gist.github.com/sayakpaul/b664605caf0aa3bf8585ab109dd5ac9c).
28+
29+
</Tip>
30+
31+
```python
32+
import torch
33+
from diffusers import FluxControlInpaintPipeline
34+
from diffusers.models.transformers import FluxTransformer2DModel
35+
from transformers import T5EncoderModel
36+
from diffusers.utils import load_image, make_image_grid
37+
from image_gen_aux import DepthPreprocessor # https://github.com/huggingface/image_gen_aux
38+
from PIL import Image
39+
import numpy as np
40+
41+
pipe = FluxControlInpaintPipeline.from_pretrained(
42+
"black-forest-labs/FLUX.1-Depth-dev",
43+
torch_dtype=torch.bfloat16,
44+
)
45+
# use following lines if you have GPU constraints
46+
# ---------------------------------------------------------------
47+
transformer = FluxTransformer2DModel.from_pretrained(
48+
"sayakpaul/FLUX.1-Depth-dev-nf4", subfolder="transformer", torch_dtype=torch.bfloat16
49+
)
50+
text_encoder_2 = T5EncoderModel.from_pretrained(
51+
"sayakpaul/FLUX.1-Depth-dev-nf4", subfolder="text_encoder_2", torch_dtype=torch.bfloat16
52+
)
53+
pipe.transformer = transformer
54+
pipe.text_encoder_2 = text_encoder_2
55+
pipe.enable_model_cpu_offload()
56+
# ---------------------------------------------------------------
57+
pipe.to("cuda")
58+
59+
prompt = "a blue robot singing opera with human-like expressions"
60+
image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/robot.png")
61+
62+
head_mask = np.zeros_like(image)
63+
head_mask[65:580,300:642] = 255
64+
mask_image = Image.fromarray(head_mask)
65+
66+
processor = DepthPreprocessor.from_pretrained("LiheYoung/depth-anything-large-hf")
67+
control_image = processor(image)[0].convert("RGB")
68+
69+
output = pipe(
70+
prompt=prompt,
71+
image=image,
72+
control_image=control_image,
73+
mask_image=mask_image,
74+
num_inference_steps=30,
75+
strength=0.9,
76+
guidance_scale=10.0,
77+
generator=torch.Generator().manual_seed(42),
78+
).images[0]
79+
make_image_grid([image, control_image, mask_image, output.resize(image.size)], rows=1, cols=4).save("output.png")
80+
```
81+
82+
## FluxControlInpaintPipeline
83+
[[autodoc]] FluxControlInpaintPipeline
84+
- all
85+
- __call__
86+
87+
88+
## FluxPipelineOutput
89+
[[autodoc]] pipelines.flux.pipeline_output.FluxPipelineOutput

docs/source/en/quantization/gguf.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ pip install -U gguf
2525

2626
Since GGUF is a single file format, use [`~FromSingleFileMixin.from_single_file`] to load the model and pass in the [`GGUFQuantizationConfig`].
2727

28-
When using GGUF checkpoints, the quantized weights remain in a low memory `dtype`(typically `torch.unint8`) and are dynamically dequantized and cast to the configured `compute_dtype` during each module's forward pass through the model. The `GGUFQuantizationConfig` allows you to set the `compute_dtype`.
28+
When using GGUF checkpoints, the quantized weights remain in a low memory `dtype`(typically `torch.uint8`) and are dynamically dequantized and cast to the configured `compute_dtype` during each module's forward pass through the model. The `GGUFQuantizationConfig` allows you to set the `compute_dtype`.
2929

30-
The functions used for dynamic dequantizatation are based on the great work done by [city96](https://github.com/city96/ComfyUI-GGUF), who created the Pytorch ports of the original (`numpy`)[https://github.com/ggerganov/llama.cpp/blob/master/gguf-py/gguf/quants.py] implementation by [compilade](https://github.com/compilade).
30+
The functions used for dynamic dequantizatation are based on the great work done by [city96](https://github.com/city96/ComfyUI-GGUF), who created the Pytorch ports of the original [`numpy`](https://github.com/ggerganov/llama.cpp/blob/master/gguf-py/gguf/quants.py) implementation by [compilade](https://github.com/compilade).
3131

3232
```python
3333
import torch

docs/source/en/quantization/overview.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ If you are new to the quantization field, we recommend you to check out these be
3333
## When to use what?
3434

3535
Diffusers currently supports the following quantization methods.
36-
- [BitsandBytes]()
37-
- [TorchAO]()
38-
- [GGUF]()
36+
- [BitsandBytes](./bitsandbytes.md)
37+
- [TorchAO](./torchao.md)
38+
- [GGUF](./gguf.md)
3939

4040
[This resource](https://huggingface.co/docs/transformers/main/en/quantization/overview#when-to-use-what) provides a good overview of the pros and cons of different quantization techniques.

examples/dreambooth/README_sana.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ This will also allow us to push the trained LoRA parameters to the Hugging Face
7373
Now, we can launch training using:
7474

7575
```bash
76-
export MODEL_NAME="Efficient-Large-Model/Sana_1600M_1024px_diffusers"
76+
export MODEL_NAME="Efficient-Large-Model/Sana_1600M_1024px_BF16_diffusers"
7777
export INSTANCE_DIR="dog"
7878
export OUTPUT_DIR="trained-sana-lora"
7979

@@ -124,4 +124,4 @@ We provide several options for optimizing memory optimization:
124124
* `cache_latents`: When enabled, we will pre-compute the latents from the input images with the VAE and remove the VAE from memory once done.
125125
* `--use_8bit_adam`: When enabled, we will use the 8bit version of AdamW provided by the `bitsandbytes` library.
126126

127-
Refer to the [official documentation](https://huggingface.co/docs/diffusers/main/en/api/pipelines/sana) of the `SanaPipeline` to know more about the models available under the SANA family and their preferred dtypes during inference.
127+
Refer to the [official documentation](https://huggingface.co/docs/diffusers/main/en/api/pipelines/sana) of the `SanaPipeline` to know more about the models available under the SANA family and their preferred dtypes during inference.

src/diffusers/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@
277277
"CogView3PlusPipeline",
278278
"CycleDiffusionPipeline",
279279
"FluxControlImg2ImgPipeline",
280+
"FluxControlInpaintPipeline",
280281
"FluxControlNetImg2ImgPipeline",
281282
"FluxControlNetInpaintPipeline",
282283
"FluxControlNetPipeline",
@@ -765,6 +766,7 @@
765766
CogView3PlusPipeline,
766767
CycleDiffusionPipeline,
767768
FluxControlImg2ImgPipeline,
769+
FluxControlInpaintPipeline,
768770
FluxControlNetImg2ImgPipeline,
769771
FluxControlNetInpaintPipeline,
770772
FluxControlNetPipeline,

src/diffusers/loaders/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def text_encoder_attn_modules(text_encoder):
7070
"FluxLoraLoaderMixin",
7171
"CogVideoXLoraLoaderMixin",
7272
"Mochi1LoraLoaderMixin",
73+
"HunyuanVideoLoraLoaderMixin",
7374
"SanaLoraLoaderMixin",
7475
]
7576
_import_structure["textual_inversion"] = ["TextualInversionLoaderMixin"]
@@ -90,6 +91,7 @@ def text_encoder_attn_modules(text_encoder):
9091
AmusedLoraLoaderMixin,
9192
CogVideoXLoraLoaderMixin,
9293
FluxLoraLoaderMixin,
94+
HunyuanVideoLoraLoaderMixin,
9395
LoraLoaderMixin,
9496
LTXVideoLoraLoaderMixin,
9597
Mochi1LoraLoaderMixin,

src/diffusers/loaders/lora_conversion_utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,11 @@ def handle_qkv(sds_sd, ait_sd, sds_key, ait_keys, dims=None):
643643
old_state_dict,
644644
new_state_dict,
645645
old_key,
646-
[f"transformer.single_transformer_blocks.{block_num}.norm.linear"],
646+
[
647+
f"transformer.single_transformer_blocks.{block_num}.attn.to_q",
648+
f"transformer.single_transformer_blocks.{block_num}.attn.to_k",
649+
f"transformer.single_transformer_blocks.{block_num}.attn.to_v",
650+
],
647651
)
648652

649653
if "down" in old_key:

0 commit comments

Comments
 (0)