Knowledge Base · Learning paths
After Sora: OpenAI's Lilian Weng Teaches You How to Design a Video Generation Diffusion Model from Scratch
OpenAI's Head of Safety Lilian Weng has published a detailed guide on designing video generation diffusion models from the ground up. The article covers key techniques including v-parameterization, 3D U-Net and DiT architectures, cascaded diffusion, and reconstruction guidance, while explaining why video generation is significantly more challenging than image generation and how to solve these issues.
Translated from Chinese by AI. Spotted an issue? Use the feedback button.
From Lil’Log
Author: Lilian Weng
Compiled and edited by Machine Heart
Editor: Panda
Over the past few years, the powerful image synthesis capabilities of diffusion models have been thoroughly demonstrated. The research community is now tackling a much harder task: video generation. Recently, Lilian Weng, Head of Safety Systems at OpenAI, published a blog post on diffusion models for video generation.
Lilian Weng
Machine Heart has compiled and organized this blog post without altering its original meaning. The following is the original content of the blog:
The video generation task is itself a superset of image synthesis, since an image is simply a single-frame video. However, video synthesis is significantly more difficult for two main reasons:
-
Video synthesis requires maintaining temporal consistency across different frames. Naturally, this demands that far more world knowledge be encoded into the model.
-
Compared to text or images, collecting large amounts of high-quality, high-dimensional video data is much harder—let alone paired text-video data.
If you want to learn about the application of diffusion models in image generation, please refer to the author Lilian Weng’s previous blog post What are Diffusion Models?: https://lilianweng.github.io/posts/2021-07-11-diffusion-models/
Modeling Video Generation from Scratch
First, let’s look at how to design and train a diffusion video model from scratch—i.e., without using any pre-trained image generator.
Parameterization and Sampling
The variable definitions used here differ slightly from those in the previous article, but the mathematical form remains consistent. Let 𝐱~𝑞_real be a data point sampled from the real data distribution. Now, by adding a small amount of Gaussian noise over time, we create a noisy version sequence of 𝐱, denoted as {𝐳_𝑡 | 𝑡 = 1..., 𝑇}, where the noise increases with 𝑡, and the final 𝑞(𝐳_𝑇)~𝒩(𝟎,𝐈). This forward noising process is a Gaussian process. Let 𝛼_𝑡 and 𝜎_𝑡 be the differentiable noise schedule of this Gaussian process:
To express 𝑞(𝐳_𝑡|𝐳_𝑠) where 0 ≤ 𝑠 < 𝑡 ≤ 𝑇, we have:
Let the log signal-to-noise ratio be
. The DDIM update can then be written as:
The paper Progressive Distillation for Fast Sampling of Diffusion Models by Salimans & Ho (2022) introduces a special 𝐯-prediction parameterization here:
. Studies have shown that, compared to the 𝝐 parameterization, the 𝐯 parameterization helps avoid color shift issues in video generation.
The parameterization of 𝐯 is derived using a trick in angular coordinates. First, define 𝜙_𝑡 = arctan(𝜎_𝑡 / 𝛼_𝑡). From this we obtain 𝛼_𝜙 = cos 𝜙, 𝜎_𝑡 = sin 𝜙, and 𝐳_𝜙 = cos 𝜙 𝐱 + sin 𝜙 𝝐. The velocity of 𝐳_𝜙 can be written as:
We can then derive:
The DDIM update rule can be updated accordingly as:
Figure 1: Illustration of the diffusion update step in angular coordinates, where DDIM evolves z_{𝜙_𝑠} by moving it along the
direction.
For the model, the parameterization of 𝐯 corresponds to predicting
For video generation tasks, extending video length or increasing frame rate requires the diffusion model to perform multiple upsampling steps. This necessitates the ability to sample a second video 𝐱^𝑏 conditioned on the first video 𝐱^𝑎,
, where 𝐱^𝑏 may be an autoregressive extension of 𝐱^𝑎 or the missing frames in a low-frame-rate video.
In addition to its own corresponding noisy variable, sampling of 𝐱^𝑏 also needs to be conditioned on 𝐱^𝑎. The 2022 Video Diffusion Models (VDM) by Ho & Salimans et al. proposed using an adjusted denoising model to implement reconstruction guidance, which enables effective sampling of 𝐱^𝑏 based on 𝐱^𝑎:
where denotes the reconstructions of 𝐱^𝑎 and 𝐱^𝑏 according to the provided denoising model. Here, 𝑤_𝑟 is a weighting factor; choosing a larger 𝑤_𝑟 > 1 can improve sampling quality. Note that the same reconstruction guidance method can also be used to upscale samples from low-resolution videos to high-resolution ones.
Model Architectures: 3D U-Net and DiT
Similar to text-to-image diffusion models, U-Net and Transformer remain the primary architectural choices. Google has developed a series of diffusion-based video modeling papers using U-Net architectures, while OpenAI’s recent Sora model adopts a Transformer architecture.
VDM adopts the standard diffusion model setup but introduces several architectural modifications to better suit video modeling tasks. It extends the 2D U-Net to handle 3D data, where each feature map is a 4D tensor: number of frames × height × width × channels. This 3D U-Net is factorized along spatial and temporal dimensions — that is, each layer operates on either the spatial or temporal dimension exclusively, but not both simultaneously.
-
Spatial processing: The original 2D convolution layers from the 2D U-Net are extended into spatially-only 3D convolutions — specifically, 3×3 convolutions become 1×3×3 convolutions. Each spatial attention module remains spatially focused, treating the first axis (frames) as the batch dimension.
-
Temporal processing: A temporal attention module is added after each spatial attention module. It attends to the first axis (frames) while treating the spatial axes as the batch dimension. Relative positional embeddings are used to track frame order. This temporal attention module enables the model to achieve strong temporal consistency.
Figure 2: 3D U-Net architecture. The network takes as input a noisy video z_𝑡, conditioning information c, and log-signal-to-noise ratio (log-SNR) λ_𝑡. The channel multipliers M_1,...,M_𝐾 denote the number of channels at each layer.
Ho et al. (2022) proposed Imagen Video, which is based on a cascade of diffusion models. It significantly improves video generation quality and produces 1280×768 resolution videos at 24 fps. The Imagen Video architecture consists of the following components, totaling seven diffusion models:
- A frozen T5 text encoder that provides text embeddings as conditioning input.
- A base video diffusion model.
- A cascade of interleaved spatial and temporal super-resolution diffusion models, comprising 3 TSR (Temporal Super-Resolution) and 3 SSR (Spatial Super-Resolution) components.
Figure 3: Imagen Video’s cascaded sampling pipeline. In practice, text embeddings are injected into all components, not just the base model.
The base denoising model applies spatial operations across all frames using shared parameters, after which temporal layers mix the activations of individual frames to achieve better temporal consistency. This approach has proven more effective than frame-autoregressive methods.
Figure 4: Architecture of a space-time separable module in the Imagen Video diffusion model.
Both the SSR and TSR models are based on upsampled inputs concatenated in the channel dimension with the noisy data 𝐳_𝑡. SSR performs upsampling via bilinear resizing, while TSR upsamples by repeating frames or inserting blank frames.
Imagen Video also applies progressive distillation to accelerate sampling. Each distillation iteration can halve the required number of sampling steps. In their experiments, the team was able to distill all seven video diffusion models down to just 8 sampling steps per model without any noticeable loss in perceptual quality.
To better scale up the model, Sora adopts the DiT (Diffusion Transformer) architecture, which operates on spacetime patches of video and image latent codes. It represents the visual input as a sequence of spacetime patches and uses these patches as input tokens to the Transformer.
Figure 5: Sora is a Diffusion Transformer model.
Adapting Image Models to Generate Video
Another important approach in diffusion-based video modeling is to “inflate” a pretrained text-to-image diffusion model by inserting temporal layers. The new temporal layers can then be fine-tuned solely on video data, or in some cases, no additional training is needed at all. The resulting model inherits the rich prior knowledge from text-image pairs, which helps alleviate the demand for large-scale text-video paired data.
Fine-tuning on Video Data
Make-A-Video, proposed by Singer et al. in 2022, extends a pretrained diffusion image model by adding a temporal dimension. The system consists of three key components:
- A base text-to-image model trained on text-image paired data.
- Spatiotemporal convolution and attention layers that enable the network to handle the temporal dimension.
- A frame interpolation network for high-frame-rate generation.
Figure 6: Schematic overview of the Make-A-Video pipeline.
The mathematical formulation of the final video inference process can be written as:
where:
- 𝐱 is the input text
- is the BPE-encoded text
- is the CLIP text encoder
- P (.) is the prior that, given the text embedding 𝐱_𝑒 and the BPE-encoded text
- , generates the image embedding 𝐲_𝑒:
- This component is trained on text-image paired data and is not fine-tuned on video data.
- is the spatiotemporal decoder, which generates a sequence of 16 frames, each being a low-resolution 64×64 RGB image
- .
- is the frame interpolation network, which effectively increases the frame rate by interpolating between the generated frames. It is a fine-tuned model used to predict masked frames for video upsampling tasks.
- denotes the spatial and spatiotemporal super-resolution models, which upsample the image resolution to 256×256 and 768×768, respectively.
- is the final generated video.
The spatiotemporal super-resolution layers consist of pseudo-3D convolutional layers and pseudo-3D attention layers:
- Pseudo-3D convolution layer: Each spatial 2D convolution layer (initialized from the pretrained image model) is followed by a temporal 1D layer (initialized with an identity function). Conceptually, the 2D convolution first generates multiple frames, which are then reshaped into a video sequence.
- Pseudo-3D attention layer: A temporal attention layer is stacked after each (pretrained) spatial attention layer, approximating a full spatiotemporal attention layer.
Figure 7: How pseudo-3D convolution (left) and attention (right) layers work.
They can be expressed as:
Here, the input tensor is 𝐡 ∈ ℝ^{𝛣×𝐶×𝐹×𝐻×𝑊} (corresponding to batch size, number of channels, number of frames, height, and width).
The permute(2,1) operation swaps the temporal and spatial dimensions. The flatten(.) operator is a matrix operation that reshapes 𝐡 into 𝐡' ∈ ℝ^{𝛣×𝐶×𝐹×𝐻𝑊}, while flatten⁻¹(.) performs the inverse operation.
During training, the different components in the Make-A-Video workflow are trained separately.
-
The decoder D^𝑡, the prior P, and the two super-resolution modules are first trained individually on images without using paired text.
-
New temporal layers are then added. These layers are initialized as identity functions and subsequently fine-tuned on unlabeled video data.
Tune-A-Video, proposed by Wu et al. in 2023, extends a pretrained image diffusion model to enable single-sample video fine-tuning. Given a video 𝒱 = { 𝑣_𝑖 | 𝑖=1,...,𝑚 } containing 𝑚 frames paired with a descriptive prompt 𝜏, the objective is to generate a new video 𝒱* based on a slightly edited but semantically related text prompt 𝜏*. For example, 𝜏 = "A man is skiing" can be extended to 𝜏 = "Spiderman is skiing on the beach"*. Tune-A-Video is designed for object editing, background modification, and style transfer.
In addition to extending the 2D convolution layers, the U-Net architecture of Tune-A-Video integrates an ST-Attention (Spatio-Temporal Attention) module. This module achieves temporal consistency by querying relevant positions in previous frames. Given the latent features of the current frame 𝑣_𝑖, the previous frame 𝑣_{𝑖-1}, and the first frame 𝑣_1 (projected into queries 𝐐, keys 𝐊, and values 𝐕), ST-Attention is defined as:
Figure 8: Overview of the Tune-A-Video architecture. Before the sampling stage, it first runs a lightweight fine-tuning phase on a single video. Note that the entire temporal self-attention (T-Attn) layers are fine-tuned because they are newly added. During fine-tuning, only the query projections in ST-Attn and Cross-Attn are updated in order to preserve the prior text-to-image knowledge. ST-Attn improves spatio-temporal consistency, while Cross-Attn enhances text-video alignment.
Gen-1, proposed by Esser et al. in 2023 (Runway), is designed for the task of editing a given video based on text input. When considering generation conditions, it separates the structure and content of the video: p(𝐱 | 𝑠, c). However, clearly disentangling these two aspects is non-trivial.
- Content 𝑐 refers to the appearance and semantics of the video, which can be conditioned through sampling from text. CLIP embeddings of video frames provide a strong representation of content and are largely orthogonal to structural features.
- Structure 𝑠 describes geometric properties and dynamics, including shape, position, and temporal changes of objects. 𝑠 is sampled from the input video. Depth estimation or other task-specific auxiliary information (such as human pose or facial landmark information for human video synthesis) can be used.
The architectural modifications in Gen-1 are relatively standard: a 1D temporal convolution layer is added after each 2D spatial convolution layer in its residual blocks, and a 1D temporal attention module is added after each 2D spatial attention module in its attention blocks. During training, the structure variable 𝑠 is concatenated with the diffusion latent 𝐳, while the content variable 𝑐 is provided in the cross-attention layers. At inference time, a prior is used to transform the CLIP embedding — converting it from a CLIP text embedding to a CLIP image embedding.
Figure 9: Overview of the Gen-1 model training pipeline.
Video LDM, proposed by Blattmann et al. in 2023, begins by training an LDM (Latent Diffusion Model) image generator. The model is then fine-tuned to incorporate the temporal dimension for video. This fine-tuning process is applied only to the newly added temporal layers on top of the encoded image sequences. The temporal layers in Video LDM (see Figure 10) interact with the existing spatial layers.
Here is the translated English version:
These layers are interleaved, while the spatial layers remain frozen during fine-tuning. In other words, only the new parameters 𝜙 are fine-tuned, while the parameters 𝜃 of the pretrained image backbone remain untouched. The workflow of Video LDM first generates low-frame-rate keyframes, which are then upsampled to a higher frame rate through a two-step latent frame interpolation process.
An input sequence of length 𝑇 is interpreted as a batch of images (i.e., 𝐵・𝑇) for the base image model 𝜃, and then reshaped into video format for the temporal layers.
A skip connection combines the temporal layer output 𝐳' with the spatial output 𝐳 through a learned fusion parameter 𝛼. In practice, two types of temporal mixing layers are implemented: (1) temporal attention, and (2) 3D convolution-based residual blocks.
Figure 10: A pretrained LDM for image synthesis is extended into a video generator. 𝐵, 𝑇, 𝐶, 𝐻, 𝑊 denote batch size, sequence length, number of channels, height, and width, respectively. 𝐜_S is an optional conditioning/context frame.
However, the pretrained autoencoder of the LDM still has a limitation: it has only ever seen images and never videos. Using it directly for video generation tends to produce flickering artifacts and poor temporal consistency. To address this, Video LDM adds extra temporal layers to the decoder and fine-tunes it on video data using a 3D convolution-based patch-wise temporal discriminator, while keeping the encoder frozen. This allows the pretrained LDM to be reused. During temporal decoder fine-tuning, the frozen encoder processes each frame of the video independently, and a video-aware discriminator is used to enforce temporally consistent reconstructions across frames.
Figure 11: Training workflow of the autoencoder in a video latent diffusion model. The fine-tuning objective for the decoder is to achieve temporal consistency through the new cross-frame discriminator, while the encoder remains unchanged.
Similar to Video LDM, the architecture of Stable Video Diffusion (SVD) proposed by Blattmann et al. in 2023 is also based on LDM. Temporal layers are inserted after every spatial convolution and attention layer, but SVD performs fine-tuning at the full model level. Training a video LDM is divided into three stages:
- Text-to-image pretraining is crucial, as it improves quality and prompt adherence.
- It is beneficial to separate video pretraining, which should ideally be conducted on a much larger, carefully curated dataset.
- High-quality video fine-tuning is performed on a smaller, high-visual-fidelity dataset with pre-generated captions.
SVD particularly emphasizes the critical role of dataset curation in model performance. The authors used a clip detection pipeline to extract more clips from each video, then applied three different captioning models: (1) CoCa for captioning middle frames, (2) V-BLIP for video captioning, and (3) an LLM that generates final captions based on the first two annotations. They further improved the video dataset by removing clips with minimal motion (filtered by computing low optical flow scores at 2 fps), eliminating videos with excessive text (detected via optical character recognition), and removing visually unappealing videos (by computing aesthetic scores and text-image similarity using CLIP embeddings on the first, middle, and last frames of each video). Experiments showed that using a filtered, higher-quality dataset leads to better model performance, even when the dataset is significantly smaller.
For approaches that first generate distant keyframes and then interpolate them using temporal super-resolution, a key challenge is maintaining high-quality temporal consistency. Lumiere, proposed by Bar-Tal et al. in 2024, adopts a Space-Time U-Net (STUNet) architecture that generates a temporally coherent video in a single forward pass, eliminating the need for a separate TSR (temporal super-resolution) component. STUNet downsamples the video in both temporal and spatial dimensions, resulting in high computational cost within a compact spatio-temporal latent space.
Figure 12: Lumiere does not require a TSR (temporal super-resolution) model. Due to memory constraints, the expanded SSR network can only process short video segments. Therefore, the SSR model uses a set of shorter but overlapping video clips.
After being extended from a pretrained text-to-image U-Net, STUNet is able to downsample and upsample videos in both temporal and spatial dimensions. The convolution-based modules consist of pretrained text-to-image layers followed by factorized spatio-temporal convolutions. The attention-based modules at the coarsest level of the U-Net contain the pretrained text-to-image modules followed by temporal attention. Only the newly added layers require further training.
Figure 13: Architecture diagram: (a) Space-Time U-Net (STUNet), (b) convolution-based module, (c) attention-based module.
Zero-Shot Adaptation (No Training Required)
It is also possible to make a pretrained text-to-image model generate videos without any training at all — which is quite surprising.
If we simply randomly sample a sequence of latent codes and then construct a video from the corresponding decoded images, there is no guarantee of temporal consistency in objects or semantics. The Text2Video-Zero method proposed by Khachatryan et al. in 2023 enables zero-shot, training-free video generation by equipping a pretrained image diffusion model with two key mechanisms for temporal consistency.
-
Sampling a sequence of latent codes that carry motion dynamics to ensure temporal consistency of the global scene and background.
-
Using a novel cross-frame attention mechanism (where each frame attends to the first frame) to reprogram the frame-level self-attention, thereby preserving contextual, appearance, and identity information of foreground objects.
Figure 14: Schematic overview of the Text2Video-Zero pipeline.
The process of sampling a sequence of latent variables with motion information can be described mathematically as follows:
-
Define a direction 𝜹 = (𝛿ₓ, 𝛿ᵧ) ∈ ℝ² to control global scene and camera motion. By default, 𝜹 = (1, 1). Also define a hyperparameter λ > 0 that controls the magnitude of global motion.
-
First, randomly sample the latent code for the first frame:
- Perform Δ𝑡 ≥ 0 DDIM backward update steps using a pretrained image diffusion model (e.g., Stable Diffusion as used in the paper) to obtain the corresponding latent code:
where 𝑇' = 𝑇 − Δ𝑡.
- For each frame in this latent sequence, apply a warping operation defined by 𝜹ᵏ = λ(𝑘−1)𝜹 to perform the corresponding motion translation, yielding:
- Finally, apply the DDIM forward process to all warped latent codes
to obtain:
In addition, Text2Video-Zero replaces the self-attention layers in the pretrained Stable Diffusion model with a new cross-frame attention mechanism that references the first frame. The goal is to preserve the context, appearance, and identity information of foreground objects throughout the generated video.
An optional background mask can also be used to achieve smoother background transitions and further improve background consistency. Assuming we have obtained the foreground mask 𝐌ₖ for the 𝑘-th frame using some method, the background smoothing operation blends the actual latent code with the motion-warped latent code at diffusion timestep 𝑡 according to the following background matrix:
where
is the actual latent code,
is the motion-warped latent code on the background, and 𝛼 is a hyperparameter set to 0.6 in the paper’s experiments.
Text2Video-Zero can be combined with ControlNet. At each diffusion timestep 𝑡 = 𝑇, ..., 1, ControlNet’s pretrained copy branch is applied to every frame
(𝑘 = 1, ..., 𝑚), and the output of this ControlNet branch is added to the skip connections of the main U-Net.
ControlVideo, proposed by Zhang et al. in 2023, aims to generate videos conditioned on both a text prompt and a motion sequence (such as depth or edge maps)
. The model is built upon ControlNet with three additional mechanisms:
-
Cross-frame attention: It adds full cross-frame interaction inside the self-attention module. It introduces interactions across all frames by mapping latent frames from all timesteps into the 𝐐, 𝐊, 𝐕 matrices. This differs from Text2Video-Zero, which makes all frames attend only to the first frame.
-
The interleaved-frame smoother mechanism reduces flickering artifacts by applying frame interpolation on alternating frames. At each time step 𝑡, the smoother interpolates either even or odd frames to smooth their corresponding three-frame clips. Please note that the number of frames will gradually decrease over time after the smoothing step.
-
The hierarchical sampler ensures temporal consistency for long videos under memory constraints. A long video is divided into multiple short video segments, with one key frame selected from each segment. The model uses full cross-frame attention to pre-generate these key frames to achieve long-term consistency, while the corresponding short video segments are synthesized sequentially based on these key frames.
Figure 15: Overview of ControlVideo.
Original link: https://lilianweng.github.io/posts/2024-04-12-diffusion-video/