Search

Search papers and lessons

CogVideoX: Text-to-Video Diffusion Models with An Expert Transformer

Zhuoyi Yang, Jiayan Teng, Wendi Zheng et al. · 2024

CogVideoX makes 10-second, 768 by 1360 text-to-video clips by compressing video in 3D and letting text and video tokens share one transformer with per-modality normalisation.

On this page

  • Colour = topic mix
  • Probability
  • Architectures
  • Neural network basics
  • Calculus
  • Linear algebra
  • Discrete math
  • ReadReady to readNot yet

TL;DR

CogVideoX is a text-to-video model. It turns a sentence into a clip of up to 10 seconds, at 16 frames per second and 768 by 1360 pixels, with motion that stays coherent from start to end. It does this in three moves. A 3D autoencoder squeezes the video in space and time, a diffusion transformer denoises the squeezed version while reading the text, and a special normalisation step gives text tokens and video tokens their own scale and shift so they can share one sequence.

Builds on: Latent Diffusion Model, Diffusion Transformer (DiT), Adaptive Layer Normalization, Rotary Position Embedding, Variational Autoencoder · Leads to: Stable Video Diffusion: Scaling Latent Video Diffusion Models to Large Datasets (a later video diffusion model, useful for comparison)

Why it mattered

Text-to-video had been limited to short clips with little movement. CogVideoX showed that a diffusion transformer, trained with careful data work and staged resolution increases, could produce longer clips with large, consistent motion. The authors release 2B and 5B parameter versions with code on GitHub, which made the recipe available to other groups. In the paper's own comparisons, the 5B model scores best on five of seven automated metrics and beats Kling, a closed commercial model, on every axis of a human study.

The Diffusion Transformer (DiT) idea comes from Peebles and Xie (2023), who showed that a transformer could serve as the denoising backbone of a diffusion model. Earlier text-to-video systems, such as CogVideo and Phenaki (both 2022), used transformers over discrete video tokens rather than diffusion. CogVideoX is one of the first open releases to bring the diffusion approach to clips of this length and resolution.

The problem

Before this work, video models faced three difficulties at once.

First, a video is very large. A 10-second clip at 768 by 1360 pixels has far more values than an image, and running a transformer over every pixel is out of reach. Earlier systems fine-tuned a 2D image autoencoder frame by frame, which does not model motion across frames and lets flicker through.

Second, text and video are different kinds of data. Their embeddings can have different scales, so putting them in one sequence without care makes the network struggle to fuse them.

Third, the training data is hard to use. Most online video has no good captions, and clips come in many lengths and shapes. Forcing them to one length throws data away, and mixing images with videos in the same batch often makes the model split into two generation modes.

How it works

Here is the generation path, traced in order. The training path uses the same pieces, with a real video in place of the noise in step 2.

  1. Read the prompt. The text goes through a T5 encoder, which turns it into a sequence of text embeddings ztextz_{\text{text}}. The paper's training setting pads text to 226 tokens.
  2. Start from noise in latent space. The video is not generated in pixels. It is generated as a compressed block of numbers, the latent. The model starts from random noise with the latent's shape.
  3. Patchify the latent. The latent block is cut into small patches. Each patch becomes one token, so the video becomes a sequence zvisionz_{\text{vision}}.
  4. Concatenate. The text tokens and the video tokens are joined along the sequence dimension into one list.
  5. Run the expert transformer. Each block applies attention over all tokens at once, which is the paper's 3D full attention. Each token's position is encoded with 3D rotary embeddings. Each block normalises text and video tokens with separate scales and shifts, both set by the diffusion timestep tt.
  6. Denoise, step by step. The network predicts what to remove from the current latent. A diffusion sampler moves the latent toward a clean one, and steps 5 and 6 repeat. The paper's timing numbers use 50 inference steps.
  7. Unpatchify and decode. The clean latent is reshaped back to its video shape and passed through the 3D causal VAE decoder, which returns pixels.
flowchart LR
  A[Prompt] --> B[T5 encoder]
  B --> F[Join text and video tokens]
  N[Noise in latent shape] --> P[Patchify]
  P --> F
  F --> T[Expert transformer blocks]
  T --> D[Denoised latent]
  D --> U[Unpatchify and 3D VAE decoder]
  U --> V[Video]

A tiny clip, counted by hand

Take a clip of 8 frames, each 64 by 64 pixels, with 3 colour channels. Use the paper's 8 by 8 by 4 compression and a latent with 16 channels, and treat each group of 4 frames as one latent frame.

  1. Count the pixel values: 8×64×64×3=98,3048 \times 64 \times 64 \times 3 = 98{,}304.
  2. Count the latent frames: 8/4=28 / 4 = 2.
  3. Count the latent spatial positions: 64/8=864 / 8 = 8 in each direction, so 8×8=648 \times 8 = 64 positions per frame.
  4. Count the latent positions in total: 2×64=1282 \times 64 = 128.
  5. Count the latent values with 16 channels each: 128×16=2,048128 \times 16 = 2{,}048.
  6. Compare: 98,304/2,048=4898{,}304 / 2{,}048 = 48. The latent is 48 times smaller than the pixels.

The transformer sees 2,048 numbers instead of 98,304 before patchifying.

The math

The 3D causal VAE (step 2)

A video has TT frames, height HH, width WW and 33 colour channels. The VAE compresses it by a factor 44 in time and 88 in each spatial direction, which the paper writes as 8 by 8 by 4. It encodes the video into a latent of shape T4×H8×W8×C\frac{T}{4} \times \frac{H}{8} \times \frac{W}{8} \times C, where CC is the number of latent channels. The causal part means the convolution pads only at the start of time, so frame tt never sees frames after tt.

The VAE is trained with a reconstruction loss, a perceptual loss (a distance computed on features from a pretrained image network), a Kullback–Leibler Divergence term that keeps the latent close to a simple distribution, and, after a few thousand steps, a GAN loss from a 3D discriminator. The Variational Autoencoder lesson covers the reconstruction and KL parts.

Patchify (step 3)

If the latent has shape T′×H′×W′×CT' \times H' \times W' \times C and a patch covers qq frames and pp by pp spatial positions, the number of tokens is

N=T′q⋅H′p⋅W′p.N = \frac{T'}{q} \cdot \frac{H'}{p} \cdot \frac{W'}{p}.

The count grows with video length and resolution, which is why memory is the bottleneck.

3D rotary positions (step 5)

Each latent token has coordinates (x,y,t)(x, y, t). A Rotary Position Embedding of size dd is split into three parts of sizes 38d\tfrac{3}{8}d, 38d\tfrac{3}{8}d and 28d\tfrac{2}{8}d. Each part is rotated by its own coordinate, and the three results are joined back into one vector of length dd.

Expert adaptive normalisation (step 5)

For each modality mm, either text or vision, with hidden states hmh_m, the block computes

h~m=(1+γm(t))⊙LN(hm)+βm(t),\tilde{h}_m = \bigl(1 + \gamma_m(t)\bigr) \odot \mathrm{LN}(h_m) + \beta_m(t),

where LN\mathrm{LN} is Layer Normalization, ⊙\odot is elementwise multiplication, and γm(t)\gamma_m(t), βm(t)\beta_m(t) are the scale and shift that a small network produces from the timestep. This follows the modulation used in Diffusion Transformer (DiT) models. The "expert" part is that each modality gets its own γ\gamma and β\beta, while the attention weights are shared.

Training loss and explicit uniform sampling (step 6)

The diffusion objective, as the paper writes it following Diffusion Model and DDPM, is

L(θ)=Et,x0,ϵ ∥ϵ−ϵθ(αˉt x0+1−αˉt ϵ, t)∥2,L(\theta) = \mathbb{E}_{t, x_0, \epsilon}\,\bigl\| \epsilon - \epsilon_\theta\bigl(\sqrt{\bar\alpha_t}\, x_0 + \sqrt{1 - \bar\alpha_t}\, \epsilon,\ t\bigr) \bigr\|^2,

where x0x_0 is a clean latent, ϵ\epsilon is Gaussian noise, αˉt\bar\alpha_t is the noise schedule's cumulative signal factor, and ϵθ\epsilon_\theta is the network. The timestep tt is drawn from {1,…,Tmax}\{1, \dots, T_{\text{max}}\}. With nn data-parallel ranks, explicit uniform sampling gives rank kk its own interval Ik=[(k−1)Tmax/n, kTmax/n]I_k = [(k-1)T_{\text{max}}/n,\ kT_{\text{max}}/n] and draws tt uniformly inside it. Independent random draws on each rank can cluster and make the loss jump; this scheme spreads them out.

The paper also uses v-prediction and zero terminal signal-to-noise ratio, which change what the network is asked to output at the final noise level. They are named in the paper's training section and are not re-derived here.

Results

Automated metrics. On the VBench-style suite the paper reports, CogVideoX-5B is best on five of seven metrics and competitive on the other two. Its Dynamic Quality score is 69.5, against 43.6 for VideoCrafter-2.0 and 57.7 for Show-1 in the same table. The 2B model has the highest Dynamic Degree (66.39), above the 5B model's 62.22. Dynamic metrics matter here because some scores reward nearly static clips, so the authors add them on purpose.

Human study. Evaluators scored Sensory Quality, Instruction Following, Physics Simulation and Cover Quality on a 0, 0.5 or 1 scale. CogVideoX-5B beats Kling (2024.7) on all four, with a total of 2.74 against 2.17. VAE. On 256 by 256, 17-frame WebVid validation clips, the paper's 3D VAE has the lowest flicker (85.5) and the highest PSNR (29.1) of the three compared, against 92.4 and 28.5 for Open-Sora.

Cost. For 768 by 1360 pixels and 5 seconds on an H800 with 50 steps, the table reports 500 seconds and 76 GB for the 5B model, and 220 seconds and 53 GB for the 2B model. That is about 2.3 times the time and 1.4 times the memory. Try it: how many GB more does the 5B model use at this setting? Answer: 76 minus 53, which is 23 GB.

Ablations. Full 3D attention beat 2D plus 1D attention, which was less stable and collapsed more often as the model grew. Expert normalisation matched or beat MMDiT with fewer parameters. Explicit uniform sampling gave a steadier loss at every timestep. A 16 by 16 by 8 VAE compression made training very hard to converge, so the authors kept 8 by 8 by 4.

Limits. The main comparisons are the authors' own, the human study uses one closed competitor, and the paper says more compression and larger models are future work. The scaling claim in the introduction is a hope based on the two sizes, not a measured law. Choosing 5B over 2B is a budget decision: the 5B model costs more time and memory for most, though not all, axes.

Common mistakes the paper's choices avoid. A more compressed video is not automatically better, because the reconstruction and training cost rise. Full attention is not automatically too expensive: FlashAttention (see FlashAttention: Fast and Memory-Efficient Exact Attention) makes it affordable in this setting. Text and video do not automatically need separate transformers, as in MMDiT, because the expert normalisation does the job with fewer parameters. The recipe pays off for clips of several seconds with real motion, and is overkill for short or low-resolution clips, where a smaller model or an image-based method is cheaper. See Flow Matching for a different training objective.

Reading guide

Check yourself

QuestionWhat does the 3D causal VAE compress a video by, in space and in time?Show answer
It compresses 8 by 8 in space and 4 in time, so a latent position covers an 8 by 8 pixel patch over 4 frames.
QuestionA clip has 16 frames and the VAE compresses 4 times in time. How many latent frames are there?Show answer
Sixteen divided by four is four latent frames.
QuestionWhy does the paper use explicit uniform sampling of timesteps?Show answer
Independent random draws across data-parallel ranks can cluster, so the average loss jumps from step to step. Splitting the timestep range into one interval per rank makes the draws cover the range evenly, which gives a steadier loss at every timestep.
QuestionWhy does the paper prefer 3D full attention to 2D plus 1D attention, even though full attention costs more?Show answer
In the paper's ablations, the 2D plus 1D version (attention within each frame, then across time) was less stable and collapsed more often as the model grew. Full 3D attention lets every token attend to every other token in space and time at once, so it costs more, and FlashAttention makes that extra cost acceptable.
QuestionA 10-second clip at 16 fps is generated. How many frames is that, and how many latent frames after 4 times temporal compression?Show answer
160 frames, since 10 times 16 is 160. After dividing by 4, there are 40 latent frames.

Sources

Read the full paper

Zhuoyi Yang, Jiayan Teng, Wendi Zheng et al., 2024. arXiv:2408.06072