"vscode:/vscode.git/clone" did not exist on "01ec4614d877cc133b3e519dbd9d399682b2fd47"
cogvideox.md 7.55 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!--Copyright 2024 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-->

Steven Liu's avatar
Steven Liu committed
16
17
18
19
20
21
<div style="float: right;">
  <div class="flex flex-wrap space-x-1">
    <a href="https://huggingface.co/docs/diffusers/main/en/tutorials/using_peft_for_inference" target="_blank" rel="noopener">
      <img alt="LoRA" src="https://img.shields.io/badge/LoRA-d8b4fe?style=flat"/>
    </a>
  </div>
Steven Liu's avatar
Steven Liu committed
22
23
</div>

Steven Liu's avatar
Steven Liu committed
24
# CogVideoX
25

Steven Liu's avatar
Steven Liu committed
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.
27

Steven Liu's avatar
Steven Liu committed
28
You can find all the original CogVideoX checkpoints under the [CogVideoX](https://huggingface.co/collections/THUDM/cogvideo-66c08e62f1685a3ade464cce) collection.
29

Steven Liu's avatar
Steven Liu committed
30
31
> [!TIP]
> Click on the CogVideoX models in the right sidebar for more examples of other video generation tasks.
32

Steven Liu's avatar
Steven Liu committed
33
The example below demonstrates how to generate a video optimized for memory or inference speed.
34

Steven Liu's avatar
Steven Liu committed
35
36
<hfoptions id="usage">
<hfoption id="memory">
Aryan's avatar
Aryan committed
37

Steven Liu's avatar
Steven Liu committed
38
Refer to the [Reduce memory usage](../../optimization/memory) guide for more details about the various memory saving techniques.
Yuxuan.Zhang's avatar
Yuxuan.Zhang committed
39

Steven Liu's avatar
Steven Liu committed
40
The quantized CogVideoX 5B model below requires ~16GB of VRAM.
Aryan's avatar
Aryan committed
41

Steven Liu's avatar
Steven Liu committed
42
43
44
45
46
47
```py
import torch
from diffusers import CogVideoXPipeline, AutoModel
from diffusers.quantizers import PipelineQuantizationConfig
from diffusers.hooks import apply_group_offloading
from diffusers.utils import export_to_video
Yuxuan.Zhang's avatar
Yuxuan.Zhang committed
48

Steven Liu's avatar
Steven Liu committed
49
50
51
52
53
54
# quantize weights to int8 with torchao
pipeline_quant_config = PipelineQuantizationConfig(
  quant_backend="torchao",
  quant_kwargs={"quant_type": "int8wo"},
  components_to_quantize=["transformer"]
)
Yuxuan.Zhang's avatar
Yuxuan.Zhang committed
55

Steven Liu's avatar
Steven Liu committed
56
57
58
59
60
61
62
63
64
# fp8 layerwise weight-casting
transformer = AutoModel.from_pretrained(
    "THUDM/CogVideoX-5b",
    subfolder="transformer",
    torch_dtype=torch.bfloat16
)
transformer.enable_layerwise_casting(
    storage_dtype=torch.float8_e4m3fn, compute_dtype=torch.bfloat16
)
Aryan's avatar
Aryan committed
65

Steven Liu's avatar
Steven Liu committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
pipeline = CogVideoXPipeline.from_pretrained(
    "THUDM/CogVideoX-5b",
    transformer=transformer,
    quantization_config=pipeline_quant_config,
    torch_dtype=torch.bfloat16
)
pipeline.to("cuda")

# model-offloading
pipeline.enable_model_cpu_offload()

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.
"""

video = pipeline(
    prompt=prompt,
    guidance_scale=6,
    num_inference_steps=50
).frames[0]
export_to_video(video, "output.mp4", fps=8)
```
91

Steven Liu's avatar
Steven Liu committed
92
93
</hfoption>
<hfoption id="inference speed">
94

Steven Liu's avatar
Steven Liu committed
95
[Compilation](../../optimization/fp16#torchcompile) is slow the first time but subsequent calls to the pipeline are faster.
96

Steven Liu's avatar
Steven Liu committed
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.
98

Steven Liu's avatar
Steven Liu committed
99
```py
100
import torch
Steven Liu's avatar
Steven Liu committed
101
102
from diffusers import CogVideoXPipeline
from diffusers.utils import export_to_video
103

Steven Liu's avatar
Steven Liu committed
104
105
106
107
pipeline = CogVideoXPipeline.from_pretrained(
    "THUDM/CogVideoX-2b",
    torch_dtype=torch.float16
).to("cuda")
108

Steven Liu's avatar
Steven Liu committed
109
110
111
112
113
# torch.compile
pipeline.transformer.to(memory_format=torch.channels_last)
pipeline.transformer = torch.compile(
    pipeline.transformer, mode="max-autotune", fullgraph=True
)
114

Steven Liu's avatar
Steven Liu committed
115
116
117
118
119
120
121
122
123
124
125
126
127
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.
"""

video = pipeline(
    prompt=prompt,
    guidance_scale=6,
    num_inference_steps=50
).frames[0]
export_to_video(video, "output.mp4", fps=8)
128
129
```

Steven Liu's avatar
Steven Liu committed
130
131
</hfoption>
</hfoptions>
132

Steven Liu's avatar
Steven Liu committed
133
## Notes
134

Steven Liu's avatar
Steven Liu committed
135
- CogVideoX supports LoRAs with [`~loaders.CogVideoXLoraLoaderMixin.load_lora_weights`].
136

Steven Liu's avatar
Steven Liu committed
137
138
  <details>
  <summary>Show example code</summary>
139

Steven Liu's avatar
Steven Liu committed
140
141
142
143
144
  ```py
  import torch
  from diffusers import CogVideoXPipeline
  from diffusers.hooks import apply_group_offloading
  from diffusers.utils import export_to_video
145

Steven Liu's avatar
Steven Liu committed
146
147
148
149
150
  pipeline = CogVideoXPipeline.from_pretrained(
      "THUDM/CogVideoX-5b",
      torch_dtype=torch.bfloat16
  )
  pipeline.to("cuda")
151

Steven Liu's avatar
Steven Liu committed
152
153
154
  # load LoRA weights
  pipeline.load_lora_weights("finetrainers/CogVideoX-1.5-crush-smol-v0", adapter_name="crush-lora")
  pipeline.set_adapters("crush-lora", 0.9)
155

Steven Liu's avatar
Steven Liu committed
156
157
  # model-offloading
  pipeline.enable_model_cpu_offload()
158

Steven Liu's avatar
Steven Liu committed
159
160
161
162
  prompt = """
  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.
  """
  negative_prompt = "inconsistent motion, blurry motion, worse quality, degenerate outputs, deformed outputs"
163

Steven Liu's avatar
Steven Liu committed
164
165
166
167
168
169
170
171
172
173
  video = pipeline(
      prompt=prompt, 
      negative_prompt=negative_prompt, 
      num_frames=81, 
      height=480,
      width=768,
      num_inference_steps=50
  ).frames[0]
  export_to_video(video, "output.mp4", fps=16)
  ```
174

Steven Liu's avatar
Steven Liu committed
175
  </details>
Steven Liu's avatar
Steven Liu committed
176

Steven Liu's avatar
Steven Liu committed
177
- The text-to-video (T2V) checkpoints work best with a resolution of 1360x768 because that was the resolution it was pretrained on.
Steven Liu's avatar
Steven Liu committed
178

Steven Liu's avatar
Steven Liu committed
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.
Steven Liu's avatar
Steven Liu committed
180

Steven Liu's avatar
Steven Liu committed
181
- Both T2V and I2V checkpoints work best with 81 and 161 frames. It is recommended to export the generated video at 16fps.
Steven Liu's avatar
Steven Liu committed
182

Steven Liu's avatar
Steven Liu committed
183
- Refer to the table below to view memory usage when various memory-saving techniques are enabled.
184

Steven Liu's avatar
Steven Liu committed
185
186
187
188
189
190
  | method | memory usage (enabled) | memory usage (disabled) |
  |---|---|---|
  | enable_model_cpu_offload | 19GB | 33GB |
  | enable_sequential_cpu_offload | <4GB | ~33GB (very slow inference speed) |
  | enable_tiling | 11GB (with enable_model_cpu_offload) | --- |
 
191
192
193
194
195
196
## CogVideoXPipeline

[[autodoc]] CogVideoXPipeline
  - all
  - __call__

197
198
199
200
201
202
## CogVideoXImageToVideoPipeline

[[autodoc]] CogVideoXImageToVideoPipeline
  - all
  - __call__

203
204
205
206
207
208
## CogVideoXVideoToVideoPipeline

[[autodoc]] CogVideoXVideoToVideoPipeline
  - all
  - __call__

209
210
211
212
213
214
## CogVideoXFunControlPipeline

[[autodoc]] CogVideoXFunControlPipeline
  - all
  - __call__

215
216
## CogVideoXPipelineOutput

217
[[autodoc]] pipelines.cogvideo.pipeline_output.CogVideoXPipelineOutput