Skip to content

Commit 656a19f

Browse files
authored
Merge branch 'main' into refactor-pkg-imports
2 parents 6bb876f + de6a88c commit 656a19f

File tree

19 files changed

+650
-77
lines changed

19 files changed

+650
-77
lines changed

docs/source/en/api/pipelines/hunyuan_video.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ The following models are available for the image-to-video pipeline:
5050
| Model name | Description |
5151
|:---|:---|
5252
| [`Skywork/SkyReels-V1-Hunyuan-I2V`](https://huggingface.co/Skywork/SkyReels-V1-Hunyuan-I2V) | Skywork's custom finetune of HunyuanVideo (de-distilled). Performs best with `97x544x960` resolution. Performs best at `97x544x960` resolution, `guidance_scale=1.0`, `true_cfg_scale=6.0` and a negative prompt. |
53-
| [`hunyuanvideo-community/HunyuanVideo-I2V`](https://huggingface.co/hunyuanvideo-community/HunyuanVideo-I2V) | Tecent's official HunyuanVideo I2V model. Performs best at resolutions of 480, 720, 960, 1280. A higher `shift` value when initializing the scheduler is recommended (good values are between 7 and 20) |
53+
| [`hunyuanvideo-community/HunyuanVideo-I2V-33ch`](https://huggingface.co/hunyuanvideo-community/HunyuanVideo-I2V) | Tecent's official HunyuanVideo 33-channel I2V model. Performs best at resolutions of 480, 720, 960, 1280. A higher `shift` value when initializing the scheduler is recommended (good values are between 7 and 20). |
54+
| [`hunyuanvideo-community/HunyuanVideo-I2V`](https://huggingface.co/hunyuanvideo-community/HunyuanVideo-I2V) | Tecent's official HunyuanVideo 16-channel I2V model. Performs best at resolutions of 480, 720, 960, 1280. A higher `shift` value when initializing the scheduler is recommended (good values are between 7 and 20) |
5455

5556
## Quantization
5657

docs/source/en/optimization/memory.md

+20
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,18 @@ export_to_video(video, "output.mp4", fps=8)
198198

199199
Group offloading (for CUDA devices with support for asynchronous data transfer streams) overlaps data transfer and computation to reduce the overall execution time compared to sequential offloading. This is enabled using layer prefetching with CUDA streams. The next layer to be executed is loaded onto the accelerator device while the current layer is being executed - this increases the memory requirements slightly. Group offloading also supports leaf-level offloading (equivalent to sequential CPU offloading) but can be made much faster when using streams.
200200

201+
<Tip>
202+
203+
- Group offloading may not work with all models out-of-the-box. If the forward implementations of the model contain weight-dependent device-casting of inputs, it may clash with the offloading mechanism's handling of device-casting.
204+
- The `offload_type` parameter can be set to either `block_level` or `leaf_level`. `block_level` offloads groups of `torch::nn::ModuleList` or `torch::nn:Sequential` modules based on a configurable attribute `num_blocks_per_group`. For example, if you set `num_blocks_per_group=2` on a standard transformer model containing 40 layers, it will onload/offload 2 layers at a time for a total of 20 onload/offloads. This drastically reduces the VRAM requirements. `leaf_level` offloads individual layers at the lowest level, which is equivalent to sequential offloading. However, unlike sequential offloading, group offloading can be made much faster when using streams, with minimal compromise to end-to-end generation time.
205+
- The `use_stream` parameter can be used with CUDA devices to enable prefetching layers for onload. It defaults to `False`. Layer prefetching allows overlapping computation and data transfer of model weights, which drastically reduces the overall execution time compared to other offloading methods. However, it can increase the CPU RAM usage significantly. Ensure that available CPU RAM that is at least twice the size of the model when setting `use_stream=True`. You can find more information about CUDA streams [here](https://pytorch.org/docs/stable/generated/torch.cuda.Stream.html)
206+
- If specifying `use_stream=True` on VAEs with tiling enabled, make sure to do a dummy forward pass (possibly with dummy inputs) before the actual inference to avoid device-mismatch errors. This may not work on all implementations. Please open an issue if you encounter any problems.
207+
- The parameter `low_cpu_mem_usage` can be set to `True` to reduce CPU memory usage when using streams for group offloading. This is useful when the CPU memory is the bottleneck, but it may counteract the benefits of using streams and increase the overall execution time. The CPU memory savings come from creating pinned-tensors on-the-fly instead of pre-pinning them. This parameter is better suited for using `leaf_level` offloading.
208+
209+
For more information about available parameters and an explanation of how group offloading works, refer to [`~hooks.group_offloading.apply_group_offloading`].
210+
211+
</Tip>
212+
201213
## FP8 layerwise weight-casting
202214

203215
PyTorch supports `torch.float8_e4m3fn` and `torch.float8_e5m2` as weight storage dtypes, but they can't be used for computation in many different tensor operations due to unimplemented kernel support. However, you can use these dtypes to store model weights in fp8 precision and upcast them on-the-fly when the layers are used in the forward pass. This is known as layerwise weight-casting.
@@ -235,6 +247,14 @@ In the above example, layerwise casting is enabled on the transformer component
235247

236248
However, you gain more control and flexibility by directly utilizing the [`~hooks.layerwise_casting.apply_layerwise_casting`] function instead of [`~ModelMixin.enable_layerwise_casting`].
237249

250+
<Tip>
251+
252+
- Layerwise casting may not work with all models out-of-the-box. Sometimes, the forward implementations of the model might contain internal typecasting of weight values. Such implementations are not supported due to the currently simplistic implementation of layerwise casting, which assumes that the forward pass is independent of the weight precision and that the input dtypes are always in `compute_dtype`. An example of an incompatible implementation can be found [here](https://github.com/huggingface/transformers/blob/7f5077e53682ca855afc826162b204ebf809f1f9/src/transformers/models/t5/modeling_t5.py#L294-L299).
253+
- Layerwise casting may fail on custom modeling implementations that make use of [PEFT](https://github.com/huggingface/peft) layers. Some minimal checks to handle this case is implemented but is not extensively tested or guaranteed to work in all cases.
254+
- It can be also be applied partially to specific layers of a model. Partially applying layerwise casting can either be done manually by calling the `apply_layerwise_casting` function on specific internal modules, or by specifying the `skip_modules_pattern` and `skip_modules_classes` parameters for a root module. These parameters are particularly useful for layers such as normalization and modulation.
255+
256+
</Tip>
257+
238258
## Channels-last memory format
239259

240260
The channels-last memory format is an alternative way of ordering NCHW tensors in memory to preserve dimension ordering. Channels-last tensors are ordered in such a way that the channels become the densest dimension (storing images pixel-per-pixel). Since not all operators currently support the channels-last format, it may result in worst performance but you should still try and see if it works for your model.

docs/source/ko/training/controlnet.md

-6
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,6 @@ from accelerate.utils import write_basic_config
6666
write_basic_config()
6767
```
6868

69-
## 원을 채우는 데이터셋
70-
71-
원본 데이터셋은 ControlNet [repo](https://huggingface.co/lllyasviel/ControlNet/blob/main/training/fill50k.zip)에 올라와있지만, 우리는 [여기](https://huggingface.co/datasets/fusing/fill50k)에 새롭게 다시 올려서 🤗 Datasets 과 호환가능합니다. 그래서 학습 스크립트 상에서 데이터 불러오기를 다룰 수 있습니다.
72-
73-
우리의 학습 예시는 원래 ControlNet의 학습에 쓰였던 [`stable-diffusion-v1-5/stable-diffusion-v1-5`](https://huggingface.co/stable-diffusion-v1-5/stable-diffusion-v1-5)을 사용합니다. 그렇지만 ControlNet은 대응되는 어느 Stable Diffusion 모델([`CompVis/stable-diffusion-v1-4`](https://huggingface.co/CompVis/stable-diffusion-v1-4)) 혹은 [`stabilityai/stable-diffusion-2-1`](https://huggingface.co/stabilityai/stable-diffusion-2-1)의 증가를 위해 학습될 수 있습니다.
74-
7569
자체 데이터셋을 사용하기 위해서는 [학습을 위한 데이터셋 생성하기](create_dataset) 가이드를 확인하세요.
7670

7771
## 학습

examples/research_projects/anytext/README.md

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
# AnyTextPipeline Pipeline
1+
# AnyTextPipeline
22

33
Project page: https://aigcdesigngroup.github.io/homepage_anytext
44

55
"AnyText comprises a diffusion pipeline with two primary elements: an auxiliary latent module and a text embedding module. The former uses inputs like text glyph, position, and masked image to generate latent features for text generation or editing. The latter employs an OCR model for encoding stroke data as embeddings, which blend with image caption embeddings from the tokenizer to generate texts that seamlessly integrate with the background. We employed text-control diffusion loss and text perceptual loss for training to further enhance writing accuracy."
66

7-
Each text line that needs to be generated should be enclosed in double quotes. For any usage questions, please refer to the [paper](https://arxiv.org/abs/2311.03054).
7+
> **Note:** Each text line that needs to be generated should be enclosed in double quotes.
88
9+
For any usage questions, please refer to the [paper](https://arxiv.org/abs/2311.03054).
10+
11+
[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/gist/tolgacangoz/b87ec9d2f265b448dd947c9d4a0da389/anytext.ipynb)
912

1013
```py
14+
# This example requires the `anytext_controlnet.py` file:
15+
# !git clone --depth 1 https://github.com/huggingface/diffusers.git
16+
# %cd diffusers/examples/research_projects/anytext
17+
# Let's choose a font file shared by an HF staff:
18+
# !wget https://huggingface.co/spaces/ysharma/TranslateQuotesInImageForwards/resolve/main/arial-unicode-ms.ttf
19+
1120
import torch
1221
from diffusers import DiffusionPipeline
1322
from anytext_controlnet import AnyTextControlNetModel
1423
from diffusers.utils import load_image
1524

16-
# I chose a font file shared by an HF staff:
17-
# !wget https://huggingface.co/spaces/ysharma/TranslateQuotesInImageForwards/resolve/main/arial-unicode-ms.ttf
1825

1926
anytext_controlnet = AnyTextControlNetModel.from_pretrained("tolgacangoz/anytext-controlnet", torch_dtype=torch.float16,
2027
variant="fp16",)
@@ -26,6 +33,7 @@ pipe = DiffusionPipeline.from_pretrained("tolgacangoz/anytext", font_path="arial
2633
# generate image
2734
prompt = 'photo of caramel macchiato coffee on the table, top-down perspective, with "Any" "Text" written on it using cream'
2835
draw_pos = load_image("https://raw.githubusercontent.com/tyxsspa/AnyText/refs/heads/main/example_images/gen9.png")
36+
# There are two modes: "generate" and "edit". "edit" mode requires `ori_image` parameter for the image to be edited.
2937
image = pipe(prompt, num_inference_steps=20, mode="generate", draw_pos=draw_pos,
3038
).images[0]
3139
image

examples/research_projects/anytext/anytext.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,17 @@ def _is_whitespace(self, char):
146146
EXAMPLE_DOC_STRING = """
147147
Examples:
148148
```py
149+
>>> # This example requires the `anytext_controlnet.py` file:
150+
>>> # !git clone --depth 1 https://github.com/huggingface/diffusers.git
151+
>>> # %cd diffusers/examples/research_projects/anytext
152+
>>> # Let's choose a font file shared by an HF staff:
153+
>>> # !wget https://huggingface.co/spaces/ysharma/TranslateQuotesInImageForwards/resolve/main/arial-unicode-ms.ttf
154+
149155
>>> import torch
150156
>>> from diffusers import DiffusionPipeline
151157
>>> from anytext_controlnet import AnyTextControlNetModel
152158
>>> from diffusers.utils import load_image
153159
154-
>>> # I chose a font file shared by an HF staff:
155-
>>> !wget https://huggingface.co/spaces/ysharma/TranslateQuotesInImageForwards/resolve/main/arial-unicode-ms.ttf
156-
157160
>>> anytext_controlnet = AnyTextControlNetModel.from_pretrained("tolgacangoz/anytext-controlnet", torch_dtype=torch.float16,
158161
... variant="fp16",)
159162
>>> pipe = DiffusionPipeline.from_pretrained("tolgacangoz/anytext", font_path="arial-unicode-ms.ttf",
@@ -165,6 +168,7 @@ def _is_whitespace(self, char):
165168
>>> # generate image
166169
>>> prompt = 'photo of caramel macchiato coffee on the table, top-down perspective, with "Any" "Text" written on it using cream'
167170
>>> draw_pos = load_image("https://raw.githubusercontent.com/tyxsspa/AnyText/refs/heads/main/example_images/gen9.png")
171+
>>> # There are two modes: "generate" and "edit". "edit" mode requires `ori_image` parameter for the image to be edited.
168172
>>> image = pipe(prompt, num_inference_steps=20, mode="generate", draw_pos=draw_pos,
169173
... ).images[0]
170174
>>> image
@@ -257,11 +261,11 @@ def forward(
257261
idx = tokenized_text[i] == self.placeholder_token.to(device)
258262
if sum(idx) > 0:
259263
if i >= len(self.text_embs_all):
260-
print("truncation for log images...")
264+
logger.warning("truncation for log images...")
261265
break
262266
text_emb = torch.cat(self.text_embs_all[i], dim=0)
263267
if sum(idx) != len(text_emb):
264-
print("truncation for long caption...")
268+
logger.warning("truncation for long caption...")
265269
text_emb = text_emb.to(embedded_text.device)
266270
embedded_text[i][idx] = text_emb[: sum(idx)]
267271
return embedded_text
@@ -1058,6 +1062,8 @@ def forward(
10581062
raise ValueError(f"Can't read ori_image image from {ori_image}!")
10591063
elif isinstance(ori_image, torch.Tensor):
10601064
ori_image = ori_image.cpu().numpy()
1065+
elif isinstance(ori_image, PIL.Image.Image):
1066+
ori_image = np.array(ori_image.convert("RGB"))
10611067
else:
10621068
if not isinstance(ori_image, np.ndarray):
10631069
raise ValueError(f"Unknown format of ori_image: {type(ori_image)}")

scripts/convert_hunyuan_video_to_diffusers.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,9 @@ def remap_single_transformer_blocks_(key, state_dict):
160160
"pooled_projection_dim": 768,
161161
"rope_theta": 256.0,
162162
"rope_axes_dim": (16, 56, 56),
163+
"image_condition_type": None,
163164
},
164-
"HYVideo-T/2-I2V": {
165+
"HYVideo-T/2-I2V-33ch": {
165166
"in_channels": 16 * 2 + 1,
166167
"out_channels": 16,
167168
"num_attention_heads": 24,
@@ -178,6 +179,26 @@ def remap_single_transformer_blocks_(key, state_dict):
178179
"pooled_projection_dim": 768,
179180
"rope_theta": 256.0,
180181
"rope_axes_dim": (16, 56, 56),
182+
"image_condition_type": "latent_concat",
183+
},
184+
"HYVideo-T/2-I2V-16ch": {
185+
"in_channels": 16,
186+
"out_channels": 16,
187+
"num_attention_heads": 24,
188+
"attention_head_dim": 128,
189+
"num_layers": 20,
190+
"num_single_layers": 40,
191+
"num_refiner_layers": 2,
192+
"mlp_ratio": 4.0,
193+
"patch_size": 2,
194+
"patch_size_t": 1,
195+
"qk_norm": "rms_norm",
196+
"guidance_embeds": True,
197+
"text_embed_dim": 4096,
198+
"pooled_projection_dim": 768,
199+
"rope_theta": 256.0,
200+
"rope_axes_dim": (16, 56, 56),
201+
"image_condition_type": "token_replace",
181202
},
182203
}
183204

scripts/convert_sana_to_diffusers.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
CTX = init_empty_weights if is_accelerate_available else nullcontext
2828

2929
ckpt_ids = [
30+
"Efficient-Large-Model/Sana_Sprint_0.6B_1024px/checkpoints/Sana_Sprint_0.6B_1024px.pth"
31+
"Efficient-Large-Model/Sana_Sprint_1.6B_1024px/checkpoints/Sana_Sprint_1.6B_1024px.pth"
3032
"Efficient-Large-Model/SANA1.5_4.8B_1024px/checkpoints/SANA1.5_4.8B_1024px.pth",
33+
"Efficient-Large-Model/SANA1.5_1.6B_1024px/checkpoints/SANA1.5_1.6B_1024px.pth",
3134
"Efficient-Large-Model/Sana_1600M_4Kpx_BF16/checkpoints/Sana_1600M_4Kpx_BF16.pth",
3235
"Efficient-Large-Model/Sana_1600M_2Kpx_BF16/checkpoints/Sana_1600M_2Kpx_BF16.pth",
3336
"Efficient-Large-Model/Sana_1600M_1024px_MultiLing/checkpoints/Sana_1600M_1024px_MultiLing.pth",
@@ -314,7 +317,6 @@ def main(args):
314317

315318
# SCM Scheduler for Sana Sprint
316319
scheduler_config = {
317-
"num_train_timesteps": 1000,
318320
"prediction_type": "trigflow",
319321
"sigma_data": 0.5,
320322
}
@@ -378,7 +380,8 @@ def main(args):
378380
choices=[
379381
"SanaMS_1600M_P1_D20",
380382
"SanaMS_600M_P1_D28",
381-
"SanaMS_4800M_P1_D60",
383+
"SanaMS1.5_1600M_P1_D20",
384+
"SanaMS1.5_4800M_P1_D60",
382385
"SanaSprint_1600M_P1_D20",
383386
"SanaSprint_600M_P1_D28",
384387
],
@@ -421,7 +424,7 @@ def main(args):
421424
"cross_attention_dim": 2240,
422425
"num_layers": 20,
423426
},
424-
"SanaMS1.5__4800M_P1_D60": {
427+
"SanaMS1.5_4800M_P1_D60": {
425428
"num_attention_heads": 70,
426429
"attention_head_dim": 32,
427430
"num_cross_attention_heads": 20,

src/diffusers/hooks/group_offloading.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ def apply_group_offloading(
331331
num_blocks_per_group: Optional[int] = None,
332332
non_blocking: bool = False,
333333
use_stream: bool = False,
334-
low_cpu_mem_usage=False,
334+
low_cpu_mem_usage: bool = False,
335335
) -> None:
336336
r"""
337337
Applies group offloading to the internal layers of a torch.nn.Module. To understand what group offloading is, and
@@ -378,6 +378,10 @@ def apply_group_offloading(
378378
use_stream (`bool`, defaults to `False`):
379379
If True, offloading and onloading is done asynchronously using a CUDA stream. This can be useful for
380380
overlapping computation and data transfer.
381+
low_cpu_mem_usage (`bool`, defaults to `False`):
382+
If True, the CPU memory usage is minimized by pinning tensors on-the-fly instead of pre-pinning them. This
383+
option only matters when using streamed CPU offloading (i.e. `use_stream=True`). This can be useful when
384+
the CPU memory is a bottleneck but may counteract the benefits of using streams.
381385
382386
Example:
383387
```python

src/diffusers/loaders/peft.py

+3
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,9 @@ def load_lora_adapter(self, pretrained_model_name_or_path_or_dict, prefix="trans
307307
try:
308308
inject_adapter_in_model(lora_config, self, adapter_name=adapter_name, **peft_kwargs)
309309
incompatible_keys = set_peft_model_state_dict(self, state_dict, adapter_name, **peft_kwargs)
310+
# Set peft config loaded flag to True if module has been successfully injected and incompatible keys retrieved
311+
if not self._hf_peft_config_loaded:
312+
self._hf_peft_config_loaded = True
310313
except Exception as e:
311314
# In case `inject_adapter_in_model()` was unsuccessful even before injecting the `peft_config`.
312315
if hasattr(self, "peft_config"):

src/diffusers/loaders/single_file_model.py

+1
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ def from_single_file(cls, pretrained_model_link_or_path_or_dict: Optional[str] =
282282
if quantization_config is not None:
283283
hf_quantizer = DiffusersAutoQuantizer.from_config(quantization_config)
284284
hf_quantizer.validate_environment()
285+
torch_dtype = hf_quantizer.update_torch_dtype(torch_dtype)
285286

286287
else:
287288
hf_quantizer = None

src/diffusers/models/transformers/sana_transformer.py

+5
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ class SanaTransformer2DModel(ModelMixin, ConfigMixin, PeftAdapterMixin, FromOrig
326326
Whether to use elementwise affinity in the normalization layer.
327327
norm_eps (`float`, defaults to `1e-6`):
328328
The epsilon value for the normalization layer.
329+
qk_norm (`str`, *optional*, defaults to `None`):
330+
The normalization to use for the query and key.
331+
timestep_scale (`float`, defaults to `1.0`):
332+
The scale to use for the timesteps.
329333
"""
330334

331335
_supports_gradient_checkpointing = True
@@ -355,6 +359,7 @@ def __init__(
355359
guidance_embeds: bool = False,
356360
guidance_embeds_scale: float = 0.1,
357361
qk_norm: Optional[str] = None,
362+
timestep_scale: float = 1.0,
358363
) -> None:
359364
super().__init__()
360365

0 commit comments

Comments
 (0)