CogView4 & CogView3 & CogView-3Plus

March 29, 2025 · View on GitHub

Read this in English 日本語で読む

🤗 CogView4 HuggingFace Space 🤗 CogView4-Control HuggingFace Space 🤖 CogView4 魔搭社区空间
🛠️ CogView4 智谱MaaS平台 🛠️ CogView4-Control 智谱MaaS平台
👋 微信社区 📚 CogView3 论文

showcase.png

项目更新

  • 🔥🔥 2025/03/24: 我们推出了 CogKit 工具,这是一个微调CogView4, CogVideoX 系列的微调和推理框架,一个工具包,玩转我们的多模态生成模型。
  • 2025/03/04: 我们适配和开源了 diffusers 版本的 CogView-4 模型,该模型具有6B权重,支持原生中文输入,支持中文文字绘画。你可以前往在线体验
  • 2024/10/13: 我们适配和开源了 diffusers 版本的 CogView-3Plus-3B 模型。你可以前往在线体验
  • 2024/9/29: 我们已经开源了 CogView3 以及 CogView-3Plus-3BCogView3 是一个基于级联扩散的文本生成图像系统,采用了接力扩散框架。 CogView-3Plus 是一系列新开发的基 Diffusion Transformer 的文本生成图像模型。

项目计划

  • diffusers 工作流适配
  • Cog系列微调套件
  • ControlNet模型和训练代码

社区工作

我们将一些和本仓库相关的社区工作收录在这里。这些代码由社区成员维护,我们感谢他们的贡献。

模型介绍

模型对比

模型名称 CogView4 CogView3-Plus-3B
分辨率 512 <= H, W <= 2048
H * W <= 2^{21}
H, W \mod 32 = 0
推理精度 仅支持BF16, FP32
编码器 GLM-4-9B T5-XXL
提示词语言 中文,English English
提示词长度上限 1024 Tokens 224 Tokens
下载链接 🤗 HuggingFace
🤖 ModelScope
🟣 WiseModel
🤗 HuggingFace
🤖 ModelScope
🟣 WiseModel

显存占用

DIT模型均使用 BF16 精度, batchsize=4 进行测试,测试结果如下表所示:

分辨率enable_model_cpu_offload OFFenable_model_cpu_offload ONenable_model_cpu_offload ON
Text Encoder 4bit
512 * 51233GB20GB13G
1280 * 72035GB20GB13G
1024 * 102435GB20GB13G
1920 * 128039GB20GB14G

此外, 建议您的设备至少拥有32GB内存,以防止进程被杀。

模型指标

我们在多个榜单上进行了测试, 并得到了如下的成绩:

DPG-Bench

ModelOverallGlobalEntityAttributeRelationOther
SDXL74.6583.2782.4380.9186.7680.41
PixArt-alpha71.1174.9779.3278.6082.5776.96
SD3-Medium84.0887.9091.0188.8380.7088.68
DALL-E 383.5090.9789.6188.3990.5889.83
Flux.1-dev83.7985.8086.7989.9890.0489.90
Janus-Pro-7B84.1986.9088.9089.4089.3289.48
CogView4-6B85.1383.8590.3591.1791.1487.29

GenEval

ModelOverallSingle Obj.Two Obj.CountingColorsPositionColor attribution
SDXL0.550.980.740.390.850.150.23
PixArt-alpha0.480.980.500.440.800.080.07
SD3-Medium0.740.990.940.720.890.330.60
DALL-E 30.670.960.870.470.830.430.45
Flux.1-dev0.660.980.790.730.770.220.45
Janus-Pro-7B0.800.990.890.590.900.790.66
CogView4-6B0.730.990.860.660.790.480.58

T2I-CompBench

ModelColorShapeTexture2D-Spatial3D-SpatialNumeracyNon-spatial ClipComplex 3-in-1
SDXL0.58790.46870.52990.21330.35660.49880.31190.3237
PixArt-alpha0.66900.49270.64770.20640.39010.50580.31970.3433
SD3-Medium0.81320.58850.73340.32000.40840.61740.31400.3771
DALL-E 30.77850.62050.70360.28650.37440.58800.30030.3773
Flux.1-dev0.75720.50660.63000.27000.39920.61650.30650.3628
Janus-Pro-7B0.51450.33230.40690.15660.27530.44060.31370.3806
CogView4-6B0.77860.58800.69830.30750.37080.66260.30560.3869

中文文字准确率评测

ModelPrecisionRecallF1 ScorePick@4
Kolors0.60940.18860.28800.1633
CogView4-6B0.69690.55320.61680.3265

推理模型

提示词优化

虽然 CogView4 系列模型都是通过长篇合成图像描述进行训练的,但我们强烈建议在文本生成图像之前,基于大语言模型进行提示词的重写操作,这将大大提高生成质量。

我们提供了一个 示例脚本。我们建议您运行这个脚本,以实现对提示词对润色。请注意,CogView4CogView3 模型的提示词优化使用的few shot不同。需要区分。

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"

推理模型

BF16 的精度运行CogView4-6B模型:

from diffusers import CogView4Pipeline
import torch

pipe = CogView4Pipeline.from_pretrained("THUDM/CogView4-6B", torch_dtype=torch.bfloat16).to("cuda")

# Open it for reduce GPU memory usage
pipe.enable_model_cpu_offload()
pipe.vae.enable_slicing()
pipe.vae.enable_tiling()

prompt = "A vibrant cherry red sports car sits proudly under the gleaming sun, its polished exterior smooth and flawless, casting a mirror-like reflection. The car features a low, aerodynamic body, angular headlights that gaze forward like predatory eyes, and a set of black, high-gloss racing rims that contrast starkly with the red. A subtle hint of chrome embellishes the grille and exhaust, while the tinted windows suggest a luxurious and private interior. The scene conveys a sense of speed and elegance, the car appearing as if it's about to burst into a sprint along a coastal road, with the ocean's azure waves crashing in the background."
image = pipe(
    prompt=prompt,
    guidance_scale=3.5,
    num_images_per_prompt=1,
    num_inference_steps=50,
    width=1024,
    height=1024,
).images[0]

image.save("cogview4.png")

更多推理代码,可以参考:

  1. BNB int4 加载 text encoder 代码,参考这里
  2. TorchAO int8 or int4 加载 text encoder & transformer 代码,参考这里
  3. 使用 gradio 界面运行CogView4-6B-Control, 参考这里

微调模型

本仓库没有存放微调代码,你可以通过两个方案进行微调,包括 Lora 和 SFT。

  1. CogKit, 由我们提出的系统微调框架,支持 CogView4,CogVideoX 微调,由我们进行维护。
  2. finetrainers, 框架采用低显存的解决方案,在4090上即可进行微调。
  3. 如果你想直接训练 ControlNet模型,可以参考 训练代码 自行训练。

开源协议

本仓库代码和 CogView3 模型均采用 Apache 2.0 开源协议。

我们欢迎和感谢你贡献代码,你可以在 这里 查看贡献指南。