Skip to content

Commit c934720

Browse files
authored
[docs] Model cards (#11112)
* initial * update * hunyuanvideo * ltx * fix * wan * gen guide * feedback * feedback * pipeline-level quant config * feedback * ltx
1 parent 9f48394 commit c934720

File tree

8 files changed

+1088
-1458
lines changed

8 files changed

+1088
-1458
lines changed

docs/source/en/_toctree.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@
9292
title: API Reference
9393
title: Hybrid Inference
9494
- sections:
95-
- local: using-diffusers/cogvideox
96-
title: CogVideoX
9795
- local: using-diffusers/consisid
9896
title: ConsisID
9997
- local: using-diffusers/sdxl

docs/source/en/api/loaders/lora.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,8 @@ To learn more about how to load LoRA weights, see the [LoRA](../../using-diffuse
9898

9999
## LoraBaseMixin
100100

101-
[[autodoc]] loaders.lora_base.LoraBaseMixin
101+
[[autodoc]] loaders.lora_base.LoraBaseMixin
102+
103+
## WanLoraLoaderMixin
104+
105+
[[autodoc]] loaders.lora_pipeline.WanLoraLoaderMixin

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

Lines changed: 137 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -13,150 +13,181 @@
1313
# limitations under the License.
1414
-->
1515

16-
# CogVideoX
17-
18-
<div class="flex flex-wrap space-x-1">
19-
<img alt="LoRA" src="https://img.shields.io/badge/LoRA-d8b4fe?style=flat"/>
16+
<div style="float: right;">
17+
<div class="flex flex-wrap space-x-1">
18+
<a href="https://huggingface.co/docs/diffusers/main/en/tutorials/using_peft_for_inference" target="_blank" rel="noopener">
19+
<img alt="LoRA" src="https://img.shields.io/badge/LoRA-d8b4fe?style=flat"/>
20+
</a>
21+
</div>
2022
</div>
2123

22-
[CogVideoX: Text-to-Video Diffusion Models with An Expert Transformer](https://huggingface.co/papers/2408.06072) from Tsinghua University & ZhipuAI, by Zhuoyi Yang, Jiayan Teng, Wendi Zheng, Ming Ding, Shiyu Huang, Jiazheng Xu, Yuanming Yang, Wenyi Hong, Xiaohan Zhang, Guanyu Feng, Da Yin, Xiaotao Gu, Yuxuan Zhang, Weihan Wang, Yean Cheng, Ting Liu, Bin Xu, Yuxiao Dong, Jie Tang.
23-
24-
The abstract from the paper is:
25-
26-
*We introduce CogVideoX, a large-scale diffusion transformer model designed for generating videos based on text prompts. To efficently model video data, we propose to levearge a 3D Variational Autoencoder (VAE) to compresses videos along both spatial and temporal dimensions. To improve the text-video alignment, we propose an expert transformer with the expert adaptive LayerNorm to facilitate the deep fusion between the two modalities. By employing a progressive training technique, CogVideoX is adept at producing coherent, long-duration videos characterized by significant motion. In addition, we develop an effectively text-video data processing pipeline that includes various data preprocessing strategies and a video captioning method. It significantly helps enhance the performance of CogVideoX, improving both generation quality and semantic alignment. Results show that CogVideoX demonstrates state-of-the-art performance across both multiple machine metrics and human evaluations. The model weight of CogVideoX-2B is publicly available at https://github.com/THUDM/CogVideo.*
24+
# CogVideoX
2725

28-
<Tip>
26+
[CogVideoX](https://huggingface.co/papers/2408.06072) is a large diffusion transformer model - available in 2B and 5B parameters - designed to generate longer and more consistent videos from text. This model uses a 3D causal variational autoencoder to more efficiently process video data by reducing sequence length (and associated training compute) and preventing flickering in generated videos. An "expert" transformer with adaptive LayerNorm improves alignment between text and video, and 3D full attention helps accurately capture motion and time in generated videos.
2927

30-
Make sure to check out the Schedulers [guide](../../using-diffusers/schedulers) to learn how to explore the tradeoff between scheduler speed and quality, and see the [reuse components across pipelines](../../using-diffusers/loading#reuse-a-pipeline) section to learn how to efficiently load the same components into multiple pipelines.
28+
You can find all the original CogVideoX checkpoints under the [CogVideoX](https://huggingface.co/collections/THUDM/cogvideo-66c08e62f1685a3ade464cce) collection.
3129

32-
</Tip>
30+
> [!TIP]
31+
> Click on the CogVideoX models in the right sidebar for more examples of other video generation tasks.
3332
34-
This pipeline was contributed by [zRzRzRzRzRzRzR](https://github.com/zRzRzRzRzRzRzR). The original codebase can be found [here](https://huggingface.co/THUDM). The original weights can be found under [hf.co/THUDM](https://huggingface.co/THUDM).
33+
The example below demonstrates how to generate a video optimized for memory or inference speed.
3534

36-
There are three official CogVideoX checkpoints for text-to-video and video-to-video.
35+
<hfoptions id="usage">
36+
<hfoption id="memory">
3737

38-
| checkpoints | recommended inference dtype |
39-
|:---:|:---:|
40-
| [`THUDM/CogVideoX-2b`](https://huggingface.co/THUDM/CogVideoX-2b) | torch.float16 |
41-
| [`THUDM/CogVideoX-5b`](https://huggingface.co/THUDM/CogVideoX-5b) | torch.bfloat16 |
42-
| [`THUDM/CogVideoX1.5-5b`](https://huggingface.co/THUDM/CogVideoX1.5-5b) | torch.bfloat16 |
38+
Refer to the [Reduce memory usage](../../optimization/memory) guide for more details about the various memory saving techniques.
4339

44-
There are two official CogVideoX checkpoints available for image-to-video.
40+
The quantized CogVideoX 5B model below requires ~16GB of VRAM.
4541

46-
| checkpoints | recommended inference dtype |
47-
|:---:|:---:|
48-
| [`THUDM/CogVideoX-5b-I2V`](https://huggingface.co/THUDM/CogVideoX-5b-I2V) | torch.bfloat16 |
49-
| [`THUDM/CogVideoX-1.5-5b-I2V`](https://huggingface.co/THUDM/CogVideoX-1.5-5b-I2V) | torch.bfloat16 |
42+
```py
43+
import torch
44+
from diffusers import CogVideoXPipeline, AutoModel
45+
from diffusers.quantizers import PipelineQuantizationConfig
46+
from diffusers.hooks import apply_group_offloading
47+
from diffusers.utils import export_to_video
5048

51-
For the CogVideoX 1.5 series:
52-
- Text-to-video (T2V) works best at a resolution of 1360x768 because it was trained with that specific resolution.
53-
- Image-to-video (I2V) works for multiple resolutions. The width can vary from 768 to 1360, but the height must be 768. The height/width must be divisible by 16.
54-
- Both T2V and I2V models support generation with 81 and 161 frames and work best at this value. Exporting videos at 16 FPS is recommended.
49+
# quantize weights to int8 with torchao
50+
pipeline_quant_config = PipelineQuantizationConfig(
51+
quant_backend="torchao",
52+
quant_kwargs={"quant_type": "int8wo"},
53+
components_to_quantize=["transformer"]
54+
)
5555

56-
There are two official CogVideoX checkpoints that support pose controllable generation (by the [Alibaba-PAI](https://huggingface.co/alibaba-pai) team).
56+
# fp8 layerwise weight-casting
57+
transformer = AutoModel.from_pretrained(
58+
"THUDM/CogVideoX-5b",
59+
subfolder="transformer",
60+
torch_dtype=torch.bfloat16
61+
)
62+
transformer.enable_layerwise_casting(
63+
storage_dtype=torch.float8_e4m3fn, compute_dtype=torch.bfloat16
64+
)
5765

58-
| checkpoints | recommended inference dtype |
59-
|:---:|:---:|
60-
| [`alibaba-pai/CogVideoX-Fun-V1.1-2b-Pose`](https://huggingface.co/alibaba-pai/CogVideoX-Fun-V1.1-2b-Pose) | torch.bfloat16 |
61-
| [`alibaba-pai/CogVideoX-Fun-V1.1-5b-Pose`](https://huggingface.co/alibaba-pai/CogVideoX-Fun-V1.1-5b-Pose) | torch.bfloat16 |
66+
pipeline = CogVideoXPipeline.from_pretrained(
67+
"THUDM/CogVideoX-5b",
68+
transformer=transformer,
69+
quantization_config=pipeline_quant_config,
70+
torch_dtype=torch.bfloat16
71+
)
72+
pipeline.to("cuda")
73+
74+
# model-offloading
75+
pipeline.enable_model_cpu_offload()
76+
77+
prompt = """
78+
A detailed wooden toy ship with intricately carved masts and sails is seen gliding smoothly over a plush, blue carpet that mimics the waves of the sea.
79+
The ship's hull is painted a rich brown, with tiny windows. The carpet, soft and textured, provides a perfect backdrop, resembling an oceanic expanse.
80+
Surrounding the ship are various other toys and children's items, hinting at a playful environment. The scene captures the innocence and imagination of childhood,
81+
with the toy ship's journey symbolizing endless adventures in a whimsical, indoor setting.
82+
"""
83+
84+
video = pipeline(
85+
prompt=prompt,
86+
guidance_scale=6,
87+
num_inference_steps=50
88+
).frames[0]
89+
export_to_video(video, "output.mp4", fps=8)
90+
```
6291

63-
## Inference
92+
</hfoption>
93+
<hfoption id="inference speed">
6494

65-
Use [`torch.compile`](https://huggingface.co/docs/diffusers/main/en/tutorials/fast_diffusion#torchcompile) to reduce the inference latency.
95+
[Compilation](../../optimization/fp16#torchcompile) is slow the first time but subsequent calls to the pipeline are faster.
6696

67-
First, load the pipeline:
97+
The average inference time with torch.compile on a 80GB A100 is 76.27 seconds compared to 96.89 seconds for an uncompiled model.
6898

69-
```python
99+
```py
70100
import torch
71-
from diffusers import CogVideoXPipeline, CogVideoXImageToVideoPipeline
72-
from diffusers.utils import export_to_video,load_image
73-
pipe = CogVideoXPipeline.from_pretrained("THUDM/CogVideoX-5b").to("cuda") # or "THUDM/CogVideoX-2b"
74-
```
75-
76-
If you are using the image-to-video pipeline, load it as follows:
101+
from diffusers import CogVideoXPipeline
102+
from diffusers.utils import export_to_video
77103

78-
```python
79-
pipe = CogVideoXImageToVideoPipeline.from_pretrained("THUDM/CogVideoX-5b-I2V").to("cuda")
80-
```
104+
pipeline = CogVideoXPipeline.from_pretrained(
105+
"THUDM/CogVideoX-2b",
106+
torch_dtype=torch.float16
107+
).to("cuda")
81108

82-
Then change the memory layout of the pipelines `transformer` component to `torch.channels_last`:
109+
# torch.compile
110+
pipeline.transformer.to(memory_format=torch.channels_last)
111+
pipeline.transformer = torch.compile(
112+
pipeline.transformer, mode="max-autotune", fullgraph=True
113+
)
83114

84-
```python
85-
pipe.transformer.to(memory_format=torch.channels_last)
115+
prompt = """
116+
A detailed wooden toy ship with intricately carved masts and sails is seen gliding smoothly over a plush, blue carpet that mimics the waves of the sea.
117+
The ship's hull is painted a rich brown, with tiny windows. The carpet, soft and textured, provides a perfect backdrop, resembling an oceanic expanse.
118+
Surrounding the ship are various other toys and children's items, hinting at a playful environment. The scene captures the innocence and imagination of childhood,
119+
with the toy ship's journey symbolizing endless adventures in a whimsical, indoor setting.
120+
"""
121+
122+
video = pipeline(
123+
prompt=prompt,
124+
guidance_scale=6,
125+
num_inference_steps=50
126+
).frames[0]
127+
export_to_video(video, "output.mp4", fps=8)
86128
```
87129

88-
Compile the components and run inference:
130+
</hfoption>
131+
</hfoptions>
89132

90-
```python
91-
pipe.transformer = torch.compile(pipeline.transformer, mode="max-autotune", fullgraph=True)
133+
## Notes
92134

93-
# CogVideoX works well with long and well-described prompts
94-
prompt = "A panda, dressed in a small, red jacket and a tiny hat, sits on a wooden stool in a serene bamboo forest. The panda's fluffy paws strum a miniature acoustic guitar, producing soft, melodic tunes. Nearby, a few other pandas gather, watching curiously and some clapping in rhythm. Sunlight filters through the tall bamboo, casting a gentle glow on the scene. The panda's face is expressive, showing concentration and joy as it plays. The background includes a small, flowing stream and vibrant green foliage, enhancing the peaceful and magical atmosphere of this unique musical performance."
95-
video = pipe(prompt=prompt, guidance_scale=6, num_inference_steps=50).frames[0]
96-
```
135+
- CogVideoX supports LoRAs with [`~loaders.CogVideoXLoraLoaderMixin.load_lora_weights`].
97136

98-
The [T2V benchmark](https://gist.github.com/a-r-r-o-w/5183d75e452a368fd17448fcc810bd3f) results on an 80GB A100 machine are:
137+
<details>
138+
<summary>Show example code</summary>
99139

100-
```
101-
Without torch.compile(): Average inference time: 96.89 seconds.
102-
With torch.compile(): Average inference time: 76.27 seconds.
103-
```
140+
```py
141+
import torch
142+
from diffusers import CogVideoXPipeline
143+
from diffusers.hooks import apply_group_offloading
144+
from diffusers.utils import export_to_video
104145

105-
### Memory optimization
146+
pipeline = CogVideoXPipeline.from_pretrained(
147+
"THUDM/CogVideoX-5b",
148+
torch_dtype=torch.bfloat16
149+
)
150+
pipeline.to("cuda")
106151

107-
CogVideoX-2b requires about 19 GB of GPU memory to decode 49 frames (6 seconds of video at 8 FPS) with output resolution 720x480 (W x H), which makes it not possible to run on consumer GPUs or free-tier T4 Colab. The following memory optimizations could be used to reduce the memory footprint. For replication, you can refer to [this](https://gist.github.com/a-r-r-o-w/3959a03f15be5c9bd1fe545b09dfcc93) script.
152+
# load LoRA weights
153+
pipeline.load_lora_weights("finetrainers/CogVideoX-1.5-crush-smol-v0", adapter_name="crush-lora")
154+
pipeline.set_adapters("crush-lora", 0.9)
108155

109-
- `pipe.enable_model_cpu_offload()`:
110-
- Without enabling cpu offloading, memory usage is `33 GB`
111-
- With enabling cpu offloading, memory usage is `19 GB`
112-
- `pipe.enable_sequential_cpu_offload()`:
113-
- Similar to `enable_model_cpu_offload` but can significantly reduce memory usage at the cost of slow inference
114-
- When enabled, memory usage is under `4 GB`
115-
- `pipe.vae.enable_tiling()`:
116-
- With enabling cpu offloading and tiling, memory usage is `11 GB`
117-
- `pipe.vae.enable_slicing()`
156+
# model-offloading
157+
pipeline.enable_model_cpu_offload()
118158

119-
## Quantization
159+
prompt = """
160+
PIKA_CRUSH A large metal cylinder is seen pressing down on a pile of Oreo cookies, flattening them as if they were under a hydraulic press.
161+
"""
162+
negative_prompt = "inconsistent motion, blurry motion, worse quality, degenerate outputs, deformed outputs"
120163

121-
Quantization helps reduce the memory requirements of very large models by storing model weights in a lower precision data type. However, quantization may have varying impact on video quality depending on the video model.
164+
video = pipeline(
165+
prompt=prompt,
166+
negative_prompt=negative_prompt,
167+
num_frames=81,
168+
height=480,
169+
width=768,
170+
num_inference_steps=50
171+
).frames[0]
172+
export_to_video(video, "output.mp4", fps=16)
173+
```
122174

123-
Refer to the [Quantization](../../quantization/overview) overview to learn more about supported quantization backends and selecting a quantization backend that supports your use case. The example below demonstrates how to load a quantized [`CogVideoXPipeline`] for inference with bitsandbytes.
175+
</details>
124176

125-
```py
126-
import torch
127-
from diffusers import BitsAndBytesConfig as DiffusersBitsAndBytesConfig, CogVideoXTransformer3DModel, CogVideoXPipeline
128-
from diffusers.utils import export_to_video
129-
from transformers import BitsAndBytesConfig as BitsAndBytesConfig, T5EncoderModel
177+
- The text-to-video (T2V) checkpoints work best with a resolution of 1360x768 because that was the resolution it was pretrained on.
130178

131-
quant_config = BitsAndBytesConfig(load_in_8bit=True)
132-
text_encoder_8bit = T5EncoderModel.from_pretrained(
133-
"THUDM/CogVideoX-2b",
134-
subfolder="text_encoder",
135-
quantization_config=quant_config,
136-
torch_dtype=torch.float16,
137-
)
179+
- The image-to-video (I2V) checkpoints work with multiple resolutions. The width can vary from 768 to 1360, but the height must be 758. Both height and width must be divisible by 16.
138180

139-
quant_config = DiffusersBitsAndBytesConfig(load_in_8bit=True)
140-
transformer_8bit = CogVideoXTransformer3DModel.from_pretrained(
141-
"THUDM/CogVideoX-2b",
142-
subfolder="transformer",
143-
quantization_config=quant_config,
144-
torch_dtype=torch.float16,
145-
)
181+
- Both T2V and I2V checkpoints work best with 81 and 161 frames. It is recommended to export the generated video at 16fps.
146182

147-
pipeline = CogVideoXPipeline.from_pretrained(
148-
"THUDM/CogVideoX-2b",
149-
text_encoder=text_encoder_8bit,
150-
transformer=transformer_8bit,
151-
torch_dtype=torch.float16,
152-
device_map="balanced",
153-
)
154-
155-
prompt = "A detailed wooden toy ship with intricately carved masts and sails is seen gliding smoothly over a plush, blue carpet that mimics the waves of the sea. The ship's hull is painted a rich brown, with tiny windows. The carpet, soft and textured, provides a perfect backdrop, resembling an oceanic expanse. Surrounding the ship are various other toys and children's items, hinting at a playful environment. The scene captures the innocence and imagination of childhood, with the toy ship's journey symbolizing endless adventures in a whimsical, indoor setting."
156-
video = pipeline(prompt=prompt, guidance_scale=6, num_inference_steps=50).frames[0]
157-
export_to_video(video, "ship.mp4", fps=8)
158-
```
183+
- Refer to the table below to view memory usage when various memory-saving techniques are enabled.
159184

185+
| method | memory usage (enabled) | memory usage (disabled) |
186+
|---|---|---|
187+
| enable_model_cpu_offload | 19GB | 33GB |
188+
| enable_sequential_cpu_offload | <4GB | ~33GB (very slow inference speed) |
189+
| enable_tiling | 11GB (with enable_model_cpu_offload) | --- |
190+
160191
## CogVideoXPipeline
161192

162193
[[autodoc]] CogVideoXPipeline

0 commit comments

Comments
 (0)