Open-Sora Plan
repository·main·Indexed 11 days ago
https://github.com/pku-yuangroup/open-sora-planAn open-source initiative to reproduce OpenAI's Sora, providing scalable architectures for high-quality video generation. The project features advanced components like WFVAE and SUV (Sparse 3D), with version v1.5.0 optimized for Huawei Ascend NPUs using the MindSpeed-MM framework. It includes tools for frame interpolation using AMT, a LLaMA 3.1-based Prompt Refiner with LoRA training support, and CausalVideoVAE with tile convolution for high-resolution inference.
What's inside Open-Sora Plan
- Open-Sora Plan is an open-source project aimed at reproducing OpenAI's Sora. It provides a scalable repository for high-quality video generation. The project has evolved through several versions, with the latest version (v1.5.0) being optimized for Huawei Ascend (NPU) accelerators using the MindSpeed-MM framework. It features advanced architectures like SUV (Sparse 3D) and WFVAE for high-performance video synthesis.
Key improvements in Open-Sora-Plan v1.1.0
mainOpen-Sora-Plan v1.1.0 introduces significant improvements over v1.0.0 in video generation quality and duration:
- Optimized CausalVideoVAE: Features better compressed visual representations, stronger performance, and higher inference efficiency.
- Higher Quality Data: Utilizes higher quality visual data and captions from ShareGPT4Video to improve world understanding and video quality.
Key Features of Open-Sora-Plan v1.3.0
mainVersion 1.3.0 introduces several architectural improvements over v1.2.0:
- WF-VAE: A wavelet-transform based VAE that decomposes video into sub-bands, providing higher performance and better compression by focusing on low-frequency energy.
- Prompt Refiner: An LLM-based component to refine short text inputs into more descriptive prompts.
- High-quality Data Cleaning: A strategy that reduced the panda70m dataset to 27% of its original size while maintaining quality.
- Sparse DiT: A Diffusion Transformer (DiT) utilizing new sparse attention for improved cost-efficiency.
- Dynamic Resolution and Duration: Support for varying video lengths and resolutions, treating single frames as images for efficient utilization.
Key updates in Open-Sora Plan v1.5.0
mainOpen-Sora Plan v1.5.0 introduces several architectural and scaling improvements over v1.3.0:
- SUV (Sparse DiT with U-shaped structure): An evolution of the
skiparse attentionmechanism. It extends sparse DiT to a U-shaped sparse structure, achieving performance comparable to dense DiT while maintaining speed advantages. - High-Compression WFVAE: Introduces a WFVAE with an 8x8x8 downsampling rate. This achieves performance comparable to the common 4x8x8 downsampling rate while halving the latent shape, which significantly reduces attention sequence length.
- Data and Model Scaling: The release utilizes 1.1B high-quality images and 40m high-quality videos, with model parameters scaled up to 8.5B.
- Adaptive Grad Clipping: Replaces the complex 'tainted batch discarding' strategy from v1.3.0 with a simpler adaptive gradient norm thresholding and clipping mechanism, making it more compatible with various parallel training strategies.
- SUV (Sparse DiT with U-shaped structure): An evolution of the
Inference Trick: Temporal Rollback Tiled Convolution
mainTo optimize inference forCausalVideoVAE, a tiling approach called temporal rollback tiled convolution is used. In this method, all windows except the first one discard their first frame. This is because the first frame in a window is treated as a static image, while subsequent frames are treated as video frames, ensuring temporal consistency during tiling.Understand CausalVideoVAE Architecture
mainThe
CausalVideoVAEis designed for efficient spatial-temporal compression (4×8×8) and supports joint image-video training. It inherits from the Stable-Diffusion Image VAE structure with two key modifications:- CausalConv3D: Replaces Conv2D with
CausalConv3Dto enable joint training. It applies special treatment to the first frame (which lacks preceding frames) to allow seamless encoding of both single images and video sequences. - Tail Initialization: Instead of standard average or center initialization, the model uses a specific 'tail initialization' method. This ensures the model can reconstruct images and videos effectively even before training begins.
Inference Tip: To handle high-resolution or long-duration videos without exceeding GPU memory (e.g., on an 80GB GPU), use tile convolution. This allows for inference with nearly constant memory usage regardless of resolution or duration.
- CausalConv3D: Replaces Conv2D with
Implement Skiparse (Skip-Sparse) Attention
mainSkiparse Attention is a method to accelerate training of Full 3D Attention models by reducing the sequence length in the attention operation by a sparse ratio $k$, while maintaining global spatiotemporal modeling capabilities. It modifies only the Attention component within the Transformer Block.
Single Skip Mode
Organizes elements into $k$ sub-sequences where each token performs attention with tokens spaced $k-1$ apart.
Group Skip Mode
Groups adjacent tokens in segments of length $k$, then bundles these groups with other groups spaced $k-1$ groups apart into a single scope. This introduces a degree of locality while preserving a large receptive field.
Implementation Note: Always apply RoPE (Rotary Positional Embedding) before the Skiparse operation, as the rearrangement causes the sequence to lose its original spatial positions.
# Single Skip Implementation # x.shape: (B,N,C) def single_skip_rearrange(x, sparse_k): return rearrange(x, 'b (g k) d -> (k b) g d', k=sparse_k) def reverse_sparse(x, sparse_k): return rearrange(x, '(k b) g d -> b (g k) d', k=sparse_k) q, k, v = Q(x), K(x), V(x) q = add_rope(q) k = add_rope(k) q = single_skip_rearrange(q) k = single_skip_rearrange(k) v = single_skip_rearrange(v) hidden_states = F.scaled_dot_product_attention(q=q,k=k,v=v) output = reverse_sparse(hidden_states) # Group Skip Implementation # x.shape: (B,N,C) def group_skip_rearrange(x, sparse_k): return rearrange(x, ' b (n m k) d -> (m b) (n k) d', m=sparse_k, k=sparse_k) def reverse_sparse(x, sparse_k): return rearrange(x, '(m b) (n k) d -> b (n m k) d', m=sparse_k, k=sparse_k) q, k, v = Q(x), K(x), V(x) q = add_rope(q) k = add_rope(k) q = group_skip_rearrange(q) k = group_skip_rearrange(k) v = group_skip_rearrange(v) hidden_states = F.scaled_dot_product_attention(q=q,k=k,v=v) output = reverse_sparse(hidden_states)Implement Dynamic Training using the Bucket Strategy
mainOpen-Sora Plan uses a Bucket strategy for dynamic training to support arbitrary video lengths and resolutions without the inefficiencies of padding or the complexity of Patch n' Pack. This strategy is decoupled from the model code and acts as a plug-and-play video sampling strategy.
How the Bucket Strategy Works
- Sort by frame: Count frames in all video data and sort them to group similar data.
- Group megabatch: Divide sorted data into groups called 'megabatches'. Most videos in a megabatch will have similar frame counts.
- Re-organize megabatch: For boundary cases (e.g., a 1-frame video in a 61-frame megabatch), randomly replace the minority data with majority data to ensure uniform frame counts within the megabatch.
- Shuffle megabatch: Shuffle data both within and between megabatches to maintain randomness.
By replacing each sample's frame sequence with
(frame × height × width), the data dimension processed by each GPU remains identical every step, preventing synchronization delays where one GPU waits for another processing a longer video.Understanding Skiparse-1D vs Skiparse-2D for Sparse Attention
mainOpen-Sora Plan utilizes a sparse attention method called Skiparse.
- Skiparse-1D: Treats video data as a one-dimensional sequence. This is the approach currently used for training in Open-Sora Plan v1.3 because it is more flexible to implement and performs comparably to 2D methods in current experiments.
- Skiparse-2D: Extends sparsity to the 2D spatial dimensions ($h$ or $w$). A sparse ratio of $k$ in Skiparse-2D represents sparsity along the height or width direction. In terms of attention computation tokens, Skiparse-2D is equivalent to the square of the sparse ratio used in Skiparse-1D. While Skiparse-2D aligns better with spatial visual characteristics and approximates 2+1D approaches as $k$ increases, it is currently less flexible to implement.
Preview of CausalVideoVAE improvements
mainThe upcoming 'preview version' of
CausalVideoVAEis designed to address two primary limitations found in v1.0.0: motion blurring and the gridding effect. The preview version aims to reduce inference cost while enhancing reconstruction performance.Performance Comparison (Kinetics-400 validation set)
Metric v1.0.0 Preview SSIM↑ 0.829 0.877 LPIPS↓ 0.106 0.064 PSNR↑ 27.171 29.695 FLOLPIPS↓ 0.119 0.070 CausalVideoVAE Model Structure and Training
mainThe CausalVideoVAE in version 1.2.0 uses a unified spatial-temporal downsampling approach. Unlike version 1.1.0 which performed spatial and temporal downsampling sequentially, v1.2.0 performs both simultaneously with a stride of
(2, 2, 2). The decoder uses spatial-temporal upsampling with aninterpolate_factorof(2, 2, 2). This architecture allows for more seamless weight inheritance from the SD2.1 VAE.Training Workflow
- Initialization: Initialize from SD2.1 VAE using tail initialization.
- Phase 1: Train on the Kinetic400 (K400) dataset for 200,000 steps.
- Phase 2: Use EMA weights from Phase 1 to initialize fine-tuning on high-quality data for 450,000 steps.
All training is conducted on 25-frame 256×256 videos using one A100 node.
Text-to-Video Diffusion Model Architecture
mainThe Text-to-Video model in v1.2.0 features several key architectural updates:
- 3D Full Attention: Replaces all 2+1D Transformer blocks with 3D full attention blocks.
- Patch Embedding: A layer that downsamples spatial dimensions by a factor of 2.
- Sequence Flattening: Videos are flattened into a 1D sequence across frame, width, and height dimensions.
- Multilingual Support: Replaces T5-XXL with mT5-XXL to enhance multilingual adaptation.
- RoPE: Incorporates Rotary Positional Embeddings.