CogVideo & CogVideoX

repository·main·Indexed 12 days ago

https://github.com/zai-org/cogvideo

A repository for high-performance video generation models, including the CogVideoX series (2B and 5B parameters). It supports text-to-video (T2V), image-to-video (I2V), and video continuation tasks. The project provides tools for LoRA and SFT fine-tuning, a Gradio demo for CogVideoX-5B, and integration with the Hugging Face diffusers ecosystem.

Tokens
20.7K
Snippets
57
Records
97
Agent score
94%

What's inside CogVideo

  1. Overview of CogVideoX Models

    main

    CogVideoX is an open-source video generation model series compatible with QingYing. The available models include versions for Text-to-Video (T2V) and Image-to-Video (I2V), with varying parameter counts (2B and 5B) and precision recommendations.

    Model Comparison Summary

    Model NameResolutionFrame CountRecommended Precision
    CogVideoX1.5-5B1360 * 76816N + 1 (N <= 10, default 81)BF16
    CogVideoX1.5-5B-I2VMin(W, H)=768, Max(W, H) $\le$ 1360, Max % 16 = 016N + 1 (N <= 10, default 81)BF16
    CogVideoX-2B720 * 4808N + 1 (N <= 6, default 49)FP16
    CogVideoX-5B720 * 4808N + 1 (N <= 6, default 49)BF16
    CogVideoX-5B-I2V720 * 4808N + 1 (N <= 6, default 49)BF16

    Key Constraints

    • Prompt Language: English only. Use LLMs to translate other languages to English before input.
    • Prompt Length: ~224-226 tokens.
    • Video Length: 5 or 10 seconds for 1.5 series; 6 seconds for older models.
    • Frame Rate: 16 fps for 1.5 series; 8 fps for older models.
  2. Overview of CogVideoX Model Variants

    main

    CogVideoX is an open-source video generation model family. The available models include versions optimized for different resolutions, frame counts, and capabilities (Text-to-Video vs. Image-to-Video).

    Model Comparison Summary

    Model NameResolutionFrame Count RequirementRecommended PrecisionDiffusers BF16 VRAM (Single GPU)
    CogVideoX1.5-5B1360 * 768$16N + 1$ ($N \le 10$, default 81)BF16~10GB
    CogVideoX1.5-5B-I2V1360 * 768$16N + 1$ ($N \le 10$, default 81)BF16~10GB
    CogVideoX-2B720 * 480$8N + 1$ ($N \le 6$, default 49)FP16~4GB
    CogVideoX-5B720 * 480$8N + 1$ ($N \le 6$, default 49)BF16~5GB
    CogVideoX-5B-I2V720 * 480$8N + 1$ ($N \le 6$, default 49)BF16~5GB

    Key Technical Constraints

    • Prompt Language: English only.
    • Prompt Length: ~224-226 tokens.
    • Video Length: 5-10 seconds (depending on model).
    • Frame Rate: 16 fps (1.5 models) or 8 fps (original models).
    • Hardware Compatibility: Optimized for NVIDIA Ampere architecture or newer (A100, H100).
  3. What is VEnhancer?

    main

    VEnhancer is a unified framework designed to enhance videos generated by CogVideoX. It provides three main capabilities:

    1. Spatial Super-resolution: Increasing the resolution of the video.
    2. Temporal Super-resolution: Frame interpolation to increase frame rates.
    3. Video Refinement: Reducing artifacts and improving visual quality.

    Technical Architecture: VEnhancer utilizes a design similar to ControlNet. It copies the architecture and weights of the multi-frame encoder and middle block from a pre-trained video diffusion model to create a trainable conditional network. This network accepts low-resolution keyframes and noisy full-frame latents as inputs. Conditioning is provided via the time step t, the text prompt, the noise augmentation level σ, and the downscaling factor s.

  4. Perform quantized inference with Diffusers and TorchAO

    main
    For memory-efficient inference or potential speed improvements during compilation, you can use quantized inference by combining diffusers with diffusers-torchao. Detailed benchmarks for various settings on A100 and H100 GPUs are available in the diffusers-torchao repository.
  5. Best practices for CogVideoX fine-tuning

    main

    To achieve better results during fine-tuning, follow these recommendations:

    • Concept Training: Use videos with 25 or more frames when training new concepts or styles.
    • Identifier Tokens: Use an identifier token (via --id_token) for better results, similar to Dreambooth training.
    • LoRA Rank: Use a rank of 64 or higher.
    • LoRA Alpha: Instead of the default lora_alpha = 1, set lora_alpha to be equal to the rank or rank // 2 for better performance.
    • Data Caching: The system automatically encodes and caches videos to disk. If you modify your dataset after a training run, delete the latent directory under your video directory to ensure the cache is refreshed.
  6. Organize Model Weight Directory Structure

    main

    After downloading the weights, ensure your directory structure matches the following pattern for the inference engine to locate files correctly:

    .
    ├── transformer
    │   ├── 1000 (or 1)
    │   │   └── mp_rank_00_model_states.pt
    │   └── latest
    └── vae
        └── 3d-vae.pt
  7. Quantization for Low-VRAM Inference

    main

    To run CogVideoX on hardware with limited memory (e.g., NVIDIA T4 Colab), you can use quantization for the text encoder, transformer, and VAE modules.

    Supported Tools

    • PytorchAO and Optimum-quanto: Used to quantize modules to reduce memory requirements.
    • TorchAO Quantization: Fully compatible with torch.compile, which can significantly improve inference speed.

    Precision Recommendations

    • CogVideoX-2B: Trained in FP16; use FP16 for inference.
    • CogVideoX-5B: Trained in BF16; use BF16 for inference.
    • NVIDIA H100 and newer: Use FP8 precision. This requires installing torch and torchao from source and is recommended for CUDA 12.4.
  8. Load and use fine-tuned models

    main

    For LoRA models

    Refer to ../inference/cli_demo.py for instructions on how to load and run inference with LoRA weights.

    For SFT models

    SFT training produces checkpoints that need to be merged into a single weight file. Use the zero_to_fp32.py script located in the checkpoint-* directory to integrate the weights.

  9. Use a fine-tuned model for inference

    main

    Fine-tuned models cannot be merged. To use them, modify the run_cmd in inference.sh to point to the correct base and inference configs.

    Example command structure:

    run_cmd="$environs python sample_video.py --base configs/cogvideox_<model parameters>_lora.yaml configs/inference.yaml --seed 42"

    Execution:

    bash inference.sh
    bash inference.sh
  10. Fine-tuning CogVideoX

    main

    There are two primary paths for fine-tuning:

    1. Diffusers-based LoRA: Use finetune/train_cogvideox_lora for fine-tuning CogVideoX models using the diffusers framework.
    2. SAT-based: Use sat/README_zh.md for inference and fine-tuning using SAT weights. This is recommended for researchers looking to improve upon the CogVideoX architecture.
    3. Low-cost Fine-tuning: The cogvideox-factory framework supports low-cost fine-tuning of the diffusers version and can run on a single RTX 4090 for CogVideoX-5B.
  11. Prepare a Dataset for Fine-tuning

    main

    Datasets for fine-tuning must follow a specific directory structure where videos and their corresponding text labels are paired by filename:

    .
    ├── labels
    │   ├── 1.txt
    │   ├── 2.txt
    │   └── ...
    └── videos
        ├── 1.mp4
        ├── 2.mp4
        └── ...

    Requirements:

    • Each .txt file must have the same name as its corresponding .mp4 file.
    • The .txt file contains the label/prompt for that video.
    • Maintain a one-to-one mapping between videos and labels.
    • Style Fine-tuning: It is recommended to prepare at least 50 videos and labels with a similar style to improve fitting quality.