CogView4 Documentation

repository·main·Indexed 22 days ago

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

A high-performance text-to-image generation model series, including CogView4, CogView3, and CogView-3Plus, supporting native Chinese and English input and high-resolution image synthesis. The documentation covers inference using the diffusers library, memory optimization techniques (such as CPU offloading and 4-bit quantization), prompt optimization via LLMs, and guidelines for fine-tuning and training ControlNet models.

Tokens
5.7K
Snippets
11
Records
24
Agent score
74%

What's inside CogView4

  1. Compare CogView4 and CogView3-Plus-3B models

    main

    Use the following comparison to choose the appropriate model for your requirements:

    FeatureCogView4CogView3-Plus-3B
    Resolution512 $\le$ H, W $\le$ 2048; H * W $\le$ $2^{21}$; H, W $\mod$ 32 = 0512 $\le$ H, W $\le$ 2048; H * W $\le$ $2^{21}$; H, W $\mod$ 32 = 0
    Inference PrecisionBF16, FP32 onlyBF16, FP32 only
    EncoderGLM-4-9BT5-XXL
    Prompt LanguagesChinese, EnglishEnglish
    Prompt Length Limit1024 tokens224 tokens
    Download LinksHuggingFace, ModelScope, WiseModelHuggingFace, ModelScope, WiseModel
  2. Estimate memory usage for CogView4

    main

    When running CogView4 (DIT models) with BF16 precision and batchsize=4, use the following memory requirements as a guide. It is recommended to have at least 32GB of RAM to prevent process termination.

    Resolutionenable_model_cpu_offload OFFenable_model_cpu_offload ONenable_model_cpu_offload ON + Text Encoder 4bit
    512 * 51233GB20GB13G
    1280 * 72035GB20GB13G
    1024 * 102435GB20GB13G
    1920 * 128039GB20GB14G
  3. How CogView4 ControlNet fine-tuning works

    main
    CogView4 ControlNet fine-tuning works by increasing the number of channels in the latent from 16 to 32. The additional 16 channels are dedicated to the control model. These channel values are treated as part of the latent and concatenated with it, allowing the generator's output to be controlled by adjusting these specific channel values.
  4. Configure Accelerate and DeepSpeed for training

    main

    Create an accelerate_ds.yaml configuration file to set up the deepspeed and accelerate environments. Below is a standard configuration using zero2 for a single machine with 8 processes:

    compute_environment: LOCAL_MACHINE
    debug: false
    deepspeed_config:
      # deepspeed_hostfile: hostfile # If using multi-machine multi-card training, prepare the hostfile configuration
      gradient_accumulation_steps: 1
      gradient_clipping: 1.0
      offload_optimizer_device: cpu
      offload_param_device: cpu
      zero3_init_flag: false
      zero_stage: 2
    num_machines: 1
    num_processes: 8 # 8 processes in total, write 16 if using two machines
    distributed_type: DEEPSPEED
    downcast_bf16: 'no'
    enable_cpu_affinity: false
    gpu_ids: all
    machine_rank: 0
    main_training_function: main
    mixed_precision: bf16
    rdzv_backend: static
    same_network: true
    tpu_env: [ ]
    tpu_use_cluster: false
    tpu_use_sudo: false
    use_cpu: false
  5. Optimize prompts using prompt_optimize.py

    main

    The CogView4 series models are trained on long, descriptive synthetic image captions. To significantly improve generation quality, it is strongly recommended to rewrite your initial prompts using a Large Language Model (LLM) before generating images.

    The repository provides a script inference/prompt_optimize.py to automate this refinement. Note that CogView4 and CogView3 require different few-shot examples for optimization, so you must specify the correct version using the --cogview_version flag.

    cd inference
    python prompt_optimize.py --api_key "Zhipu AI API Key" --prompt {your prompt} --base_url "https://open.bigmodel.cn/api/paas/v4" --model "glm-4-plus" --cogview_version "cogview4"
  6. Optimize prompts for CogView4

    main

    While CogView4 is trained on long descriptive prompts, it is strongly recommended to use a Large Language Model (LLM) to rewrite/polish your prompts before generation to significantly improve quality.

    A script inference/prompt_optimize.py is provided for this purpose. Note that the few-shot prompting logic differs between CogView4 and CogView3, so ensure you specify the correct --cogview_version.

    cd inference
    python prompt_optimize.py --api_key "智谱AI API Key" --prompt {你的提示词} --base_url "https://open.bigmodel.cn/api/paas/v4" --model "glm-4-plus" --cogview_version "cogview4"
  7. Setup CogView4 ControlNet training environment

    main

    To begin training, you must install diffusers from source and navigate to the CogView4 control training directory.

    1. Clone and install diffusers:
    git clone https://github.com/huggingface/diffusers.git
    cd diffusers
    pip install -e .
    cd examples/cogview4-control
    1. Configure accelerate with DeepSpeed (e.g., zero2). Save the configuration as accelerate_ds.yaml.
    compute_environment: LOCAL_MACHINE
    debug: false
    deepspeed_config:
      gradient_accumulation_steps: 1
      gradient_clipping: 1.0
      offload_optimizer_device: cpu
      offload_param_device: cpu
      zero3_init_flag: false
      zero_stage: 2
    num_machines: 1
    num_processes: 8
    distributed_type: DEEPSPEED
    downcast_bf16: 'no'
    enable_cpu_affinity: false
    gpu_ids: all
    machine_rank: 0
    main_training_function: main
    mixed_precision: bf16
    rdzv_backend: static
    same_network: true
    tpu_env: []
    tpu_use_cluster: false
    tpu_use_sudo: false
    use_cpu: false
  8. Optimize memory usage for CogView4 DIT models

    main

    When running DIT models with BF16 precision and batchsize=4, you can manage VRAM usage using enable_model_cpu_offload and 4-bit Text Encoder quantization.

    Recommended Hardware: A device with at least 32GB of RAM is recommended to prevent process termination.

    Resolutionenable_model_cpu_offload OFFenable_model_cpu_offload ONenable_model_cpu_offload ON + Text Encoder 4bit
    512 * 51233GB20GB13GB
    1280 * 72035GB20GB13GB
    1024 * 102435GB20GB13GB
    1920 * 128039GB20GB14GB
  9. Prepare the dataset for ControlNet fine-tuning

    main

    You can use the open_pose_controlnet dataset or your own. If using a custom dataset, it must follow the open_pose_controlnet format or you must modify the dataloader accordingly.

    Constraints:

    • Resolution: All images will be resized to a fixed size; dynamic resolutions are not supported.
    • Tokens: Dynamic-length tokens are not supported. Tokens in each batch will be padded to the maximum length.