zai-org/CogVideoX-2b
37617k
1---2license: apache-2.03language:4 - en5tags:6 - cogvideox7 - video-generation8 - thudm9 - text-to-video10inference: false11---12 13# CogVideoX-2B14 15<p style="text-align: center;">16 <div align="center">17 <img src=https://github.com/THUDM/CogVideo/raw/main/resources/logo.svg width="50%"/>18 </div>19 <p align="center">20 <a href="https://huggingface.co/THUDM/CogVideoX-2b/blob/main/README_zh.md">π δΈζι
θ―»</a> | 21 <a href="https://huggingface.co/spaces/THUDM/CogVideoX-2B-Space">π€ Huggingface Space</a> |22 <a href="https://github.com/THUDM/CogVideo">π Github </a> | 23 <a href="https://arxiv.org/pdf/2408.06072">π arxiv </a>24</p>25<p align="center">26π Visit <a href="https://chatglm.cn/video?lang=en?fr=osm_cogvideo">QingYing</a> and <a href="https://open.bigmodel.cn/?utm_campaign=open&_channel_track_key=OWTVNma9">API Platform</a> to experience commercial video generation models.27</p>28 29## Demo Show30 31<!DOCTYPE html>32<html lang="en">33<head>34 <meta charset="UTF-8">35 <meta name="viewport" content="width=device-width, initial-scale=1.0">36 <title>Video Gallery with Captions</title>37 <style>38 .video-container {39 display: flex;40 flex-wrap: wrap;41 justify-content: space-around;42 }43 .video-item {44 width: 45%;45 margin-bottom: 20px;46 transition: transform 0.3s;47 }48 .video-item:hover {49 transform: scale(1.1);50 }51 .caption {52 text-align: center;53 margin-top: 10px;54 font-size: 11px;55 }56 </style>57</head>58<body>59 <div class="video-container">60 <div class="video-item">61 <video width="100%" controls>62 <source src="https://github.com/THUDM/CogVideo/raw/main/resources/videos/1.mp4" type="video/mp4">63 </video>64 <div class="caption">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.</div>65 </div>66 <div class="video-item">67 <video width="100%" controls>68 <source src="https://github.com/THUDM/CogVideo/raw/main/resources/videos/2.mp4" type="video/mp4">69 </video>70 <div class="caption">The camera follows behind a white vintage SUV with a black roof rack as it speeds up a steep dirt road surrounded by pine trees on a steep mountain slope, dust kicks up from itβs tires, the sunlight shines on the SUV as it speeds along the dirt road, casting a warm glow over the scene. The dirt road curves gently into the distance, with no other cars or vehicles in sight. The trees on either side of the road are redwoods, with patches of greenery scattered throughout. The car is seen from the rear following the curve with ease, making it seem as if it is on a rugged drive through the rugged terrain. The dirt road itself is surrounded by steep hills and mountains, with a clear blue sky above with wispy clouds.</div>71 </div>72 <div class="video-item">73 <video width="100%" controls>74 <source src="https://github.com/THUDM/CogVideo/raw/main/resources/videos/3.mp4" type="video/mp4">75 </video>76 <div class="caption">A street artist, clad in a worn-out denim jacket and a colorful bandana, stands before a vast concrete wall in the heart, holding a can of spray paint, spray-painting a colorful bird on a mottled wall.</div>77 </div>78 <div class="video-item">79 <video width="100%" controls>80 <source src="https://github.com/THUDM/CogVideo/raw/main/resources/videos/4.mp4" type="video/mp4">81 </video>82 <div class="caption"> In the haunting backdrop of a war-torn city, where ruins and crumbled walls tell a story of devastation, a poignant close-up frames a young girl. Her face is smudged with ash, a silent testament to the chaos around her. Her eyes glistening with a mix of sorrow and resilience, capturing the raw emotion of a world that has lost its innocence to the ravages of conflict.</div>83 </div>84 </div>85</body>86</html>87 88## Model Introduction89 90CogVideoX is an open-source version of the video generation model originating91from [QingYing](https://chatglm.cn/video?lang=en?fr=osm_cogvideo). The table below displays the list of video generation92models we currently offer, along with their foundational information.93 94<table style="border-collapse: collapse; width: 100%;">95 <tr>96 <th style="text-align: center;">Model Name</th>97 <th style="text-align: center;">CogVideoX-2B (This Repository)</th>98 <th style="text-align: center;">CogVideoX-5B</th>99 </tr>100 <tr>101 <td style="text-align: center;">Model Description</td>102 <td style="text-align: center;">Entry-level model, balancing compatibility. Low cost for running and secondary development.</td>103 <td style="text-align: center;">Larger model with higher video generation quality and better visual effects.</td>104 </tr>105 <tr>106 <td style="text-align: center;">Inference Precision</td>107 <td style="text-align: center;"><b>FP16* (Recommended)</b>, BF16, FP32, FP8*, INT8, no support for INT4</td>108 <td style="text-align: center;"><b>BF16 (Recommended)</b>, FP16, FP32, FP8*, INT8, no support for INT4</td>109 </tr>110 <tr>111 <td style="text-align: center;">Single GPU VRAM Consumption<br></td>112 <td style="text-align: center;"><a href="https://github.com/THUDM/SwissArmyTransformer">SAT</a> FP16: 18GB <br><b>diffusers FP16: starting from 4GB*</b><br><b>diffusers INT8(torchao): starting from 3.6GB*</b></td>113 <td style="text-align: center;"><a href="https://github.com/THUDM/SwissArmyTransformer">SAT</a> BF16: 26GB <br><b>diffusers BF16: starting from 5GB*</b><br><b>diffusers INT8(torchao): starting from 4.4GB*</b></td>114 </tr>115 <tr>116 <td style="text-align: center;">Multi-GPU Inference VRAM Consumption</td>117 <td style="text-align: center;"><b>FP16: 10GB* using diffusers</b></td>118 <td style="text-align: center;"><b>BF16: 15GB* using diffusers</b></td>119 </tr>120 <tr>121 <td style="text-align: center;">Inference Speed<br>(Step = 50, FP/BF16)</td>122 <td style="text-align: center;">Single A100: ~90 seconds<br>Single H100: ~45 seconds</td>123 <td style="text-align: center;">Single A100: ~180 seconds<br>Single H100: ~90 seconds</td>124 </tr>125 <tr>126 <td style="text-align: center;">Fine-tuning Precision</td>127 <td style="text-align: center;"><b>FP16</b></td>128 <td style="text-align: center;"><b>BF16</b></td>129 </tr>130 <tr>131 <td style="text-align: center;">Fine-tuning VRAM Consumption (per GPU)</td>132 <td style="text-align: center;">47 GB (bs=1, LORA)<br> 61 GB (bs=2, LORA)<br> 62GB (bs=1, SFT)</td>133 <td style="text-align: center;">63 GB (bs=1, LORA)<br> 80 GB (bs=2, LORA)<br> 75GB (bs=1, SFT)</td>134 </tr>135 <tr>136 <td style="text-align: center;">Prompt Language</td>137 <td colspan="2" style="text-align: center;">English*</td>138 </tr>139 <tr>140 <td style="text-align: center;">Prompt Length Limit</td>141 <td colspan="2" style="text-align: center;">226 Tokens</td>142 </tr>143 <tr>144 <td style="text-align: center;">Video Length</td>145 <td colspan="2" style="text-align: center;">6 Seconds</td>146 </tr>147 <tr>148 <td style="text-align: center;">Frame Rate</td>149 <td colspan="2" style="text-align: center;">8 Frames per Second</td>150 </tr>151 <tr>152 <td style="text-align: center;">Video Resolution</td>153 <td colspan="2" style="text-align: center;">720 x 480, no support for other resolutions (including fine-tuning)</td>154 </tr>155 <tr>156 <td style="text-align: center;">Positional Encoding</td>157 <td style="text-align: center;">3d_sincos_pos_embed</td>158 <td style="text-align: center;">3d_rope_pos_embed</td>159 </tr>160</table>161 162**Data Explanation**163 164+ When testing using the `diffusers` library, all optimizations provided by the `diffusers` library were enabled. This165 solution has not been tested for actual VRAM/memory usage on devices other than **NVIDIA A100 / H100**. Generally,166 this solution can be adapted to all devices with **NVIDIA Ampere architecture** and above. If the optimizations are167 disabled, VRAM usage will increase significantly, with peak VRAM usage being about 3 times higher than the table168 shows. However, speed will increase by 3-4 times. You can selectively disable some optimizations, including:169 170```171pipe.enable_model_cpu_offload()172pipe.enable_sequential_cpu_offload()173pipe.vae.enable_slicing()174pipe.vae.enable_tiling()175``` 176 177+ When performing multi-GPU inference, the `enable_model_cpu_offload()` optimization needs to be disabled.178+ Using INT8 models will reduce inference speed. This is to ensure that GPUs with lower VRAM can perform inference179 normally while maintaining minimal video quality loss, though inference speed will decrease significantly.180+ The 2B model is trained with `FP16` precision, and the 5B model is trained with `BF16` precision. We recommend using181 the precision the model was trained with for inference.182+ [PytorchAO](https://github.com/pytorch/ao) and [Optimum-quanto](https://github.com/huggingface/optimum-quanto/) can be183 used to quantize the text encoder, Transformer, and VAE modules to reduce CogVideoX's memory requirements. This makes184 it possible to run the model on a free T4 Colab or GPUs with smaller VRAM! It is also worth noting that TorchAO185 quantization is fully compatible with `torch.compile`, which can significantly improve inference speed. `FP8`186 precision must be used on devices with `NVIDIA H100` or above, which requires installing187 the `torch`, `torchao`, `diffusers`, and `accelerate` Python packages from source. `CUDA 12.4` is recommended.188+ The inference speed test also used the above VRAM optimization scheme. Without VRAM optimization, inference speed189 increases by about 10%. Only the `diffusers` version of the model supports quantization.190+ The model only supports English input; other languages can be translated into English during refinement by a large191 model.192 193**Note**194 195+ Using [SAT](https://github.com/THUDM/SwissArmyTransformer) for inference and fine-tuning of SAT version196 models. Feel free to visit our GitHub for more information.197 198## Quick Start π€199 200This model supports deployment using the huggingface diffusers library. You can deploy it by following these steps.201 202**We recommend that you visit our [GitHub](https://github.com/THUDM/CogVideo) and check out the relevant prompt203optimizations and conversions to get a better experience.**204 2051. Install the required dependencies206 207```shell208# diffusers>=0.30.1209# transformers>=0.44.0210# accelerate>=0.33.0 (suggest install from source)211# imageio-ffmpeg>=0.5.1212pip install --upgrade transformers accelerate diffusers imageio-ffmpeg 213```214 2152. Run the code216 217```python218import torch219from diffusers import CogVideoXPipeline220from diffusers.utils import export_to_video221 222prompt = "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."223 224pipe = CogVideoXPipeline.from_pretrained(225 "THUDM/CogVideoX-2b",226 torch_dtype=torch.float16227)228 229pipe.enable_model_cpu_offload()230pipe.enable_sequential_cpu_offload()231pipe.vae.enable_slicing()232pipe.vae.enable_tiling()233video = pipe(234 prompt=prompt,235 num_videos_per_prompt=1,236 num_inference_steps=50,237 num_frames=49,238 guidance_scale=6,239 generator=torch.Generator(device="cuda").manual_seed(42),240).frames[0]241 242export_to_video(video, "output.mp4", fps=8)243```244 245## Quantized Inference246 247[PytorchAO](https://github.com/pytorch/ao) and [Optimum-quanto](https://github.com/huggingface/optimum-quanto/) can be248used to quantize the Text Encoder, Transformer and VAE modules to lower the memory requirement of CogVideoX. This makes249it possible to run the model on free-tier T4 Colab or smaller VRAM GPUs as well! It is also worth noting that TorchAO250quantization is fully compatible with `torch.compile`, which allows for much faster inference speed.251 252```diff253# To get started, PytorchAO needs to be installed from the GitHub source and PyTorch Nightly.254# Source and nightly installation is only required until next release.255 256import torch257from diffusers import AutoencoderKLCogVideoX, CogVideoXTransformer3DModel, CogVideoXPipeline258from diffusers.utils import export_to_video259+ from transformers import T5EncoderModel260+ from torchao.quantization import quantize_, int8_weight_only, int8_dynamic_activation_int8_weight261 262+ quantization = int8_weight_only263 264+ text_encoder = T5EncoderModel.from_pretrained("THUDM/CogVideoX-5b", subfolder="text_encoder", torch_dtype=torch.bfloat16)265+ quantize_(text_encoder, quantization())266 267+ transformer = CogVideoXTransformer3DModel.from_pretrained("THUDM/CogVideoX-5b", subfolder="transformer", torch_dtype=torch.bfloat16)268+ quantize_(transformer, quantization())269 270+ vae = AutoencoderKLCogVideoX.from_pretrained("THUDM/CogVideoX-2b", subfolder="vae", torch_dtype=torch.bfloat16)271+ quantize_(vae, quantization())272 273# Create pipeline and run inference274pipe = CogVideoXPipeline.from_pretrained(275 "THUDM/CogVideoX-2b",276+ text_encoder=text_encoder,277+ transformer=transformer,278+ vae=vae,279 torch_dtype=torch.bfloat16,280)281pipe.enable_model_cpu_offload()282pipe.vae.enable_tiling()283 284prompt = "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."285 286video = pipe(287 prompt=prompt,288 num_videos_per_prompt=1,289 num_inference_steps=50,290 num_frames=49,291 guidance_scale=6,292 generator=torch.Generator(device="cuda").manual_seed(42),293).frames[0]294 295export_to_video(video, "output.mp4", fps=8)296```297 298Additionally, the models can be serialized and stored in a quantized datatype to save disk space when using PytorchAO.299Find examples and benchmarks at these links:300 301- [torchao](https://gist.github.com/a-r-r-o-w/4d9732d17412888c885480c6521a9897)302- [quanto](https://gist.github.com/a-r-r-o-w/31be62828b00a9292821b85c1017effa)303 304## Explore the Model305 306Welcome to our [github](https://github.com/THUDM/CogVideo), where you will find:307 3081. More detailed technical details and code explanation.3092. Optimization and conversion of prompt words.3103. Reasoning and fine-tuning of SAT version models, and even pre-release.3114. Project update log dynamics, more interactive opportunities.3125. CogVideoX toolchain to help you better use the model.3136. INT8 model inference code support.314 315## Model License316 317The CogVideoX-2B model (including its corresponding Transformers module and VAE module) is released under318the [Apache 2.0 License](LICENSE).319 320The CogVideoX-5B model (Transformers module) is released under321the [CogVideoX LICENSE](https://huggingface.co/THUDM/CogVideoX-5b/blob/main/LICENSE).322 323## Citation324 325```326@article{yang2024cogvideox,327 title={CogVideoX: Text-to-Video Diffusion Models with An Expert Transformer},328 author={Yang, Zhuoyi and Teng, Jiayan and Zheng, Wendi and Ding, Ming and Huang, Shiyu and Xu, Jiazheng and Yang, Yuanming and Hong, Wenyi and Zhang, Xiaohan and Feng, Guanyu and others},329 journal={arXiv preprint arXiv:2408.06072},330 year={2024}331}332```