Skip to content

Open-Sora: Democratizing Efficient Video Production for All

Python

[exp] update config

Zangwei Zheng5f6c9bfceba8
diff --git a/configs/opensora-v1-2/inference/pixart.py b/configs/opensora-v1-2/inference/pixart.py
deleted file mode 100644
index 1baf45d..0000000
--- a/configs/opensora-v1-2/inference/pixart.py
+++ /dev/null
@@ -1,37 +0,0 @@
-num_frames = 1
-fps = 1
-# image_size = (2560, 1536)
-image_size = (2048, 2048)
-
-model = dict(
- # type="PixArt-1B/2",
- type="PixArt-XL/2",
- from_pretrained="PixArt-Sigma-XL-2-2K-MS.pth",
- space_scale=4,
- no_temporal_pos_emb=True,
- enable_flashattn=True,
- enable_layernorm_kernel=True,
- base_size=2048 // 8,
-)
-vae = dict(
- type="VideoAutoencoderKL",
- from_pretrained="PixArt-alpha/pixart_sigma_sdxlvae_T5_diffusers",
- subfolder="vae",
-)
-text_encoder = dict(
- type="t5",
- from_pretrained="DeepFloyd/t5-v1_1-xxl",
- model_max_length=300,
-)
-scheduler = dict(
- type="dpm-solver",
- num_sampling_steps=14,
- cfg_scale=4.5,
-)
-dtype = "bf16"
-
-# Others
-batch_size = 1
-seed = 42
-prompt_path = "./assets/texts/t2i_sigma.txt"
-save_dir = "./samples/samples/"
diff --git a/configs/opensora-v1-2/inference/xl-rf.py b/configs/opensora-v1-2/inference/sample.py
similarity index 82%
rename from configs/opensora-v1-2/inference/xl-rf.py
rename to configs/opensora-v1-2/inference/sample.py
index 943a64b..2996592 100644
--- a/configs/opensora-v1-2/inference/xl-rf.py
+++ b/configs/opensora-v1-2/inference/sample.py
@@ -1,7 +1,6 @@
num_frames = 1
fps = 1
image_size = (2560, 1536)
-# image_size = (2048, 2048)
multi_resolution = "STDiT2"
model = dict(
@@ -11,13 +10,6 @@ model = dict(
enable_flashattn=True,
enable_layernorm_kernel=True,
)
-# vae = dict(
-# type="VideoAutoencoderKL",
-# micro_batch_size=4,
-# from_pretrained="PixArt-alpha/pixart_sigma_sdxlvae_T5_diffusers",
-# subfolder="vae",
-# local_files_only=True,
-# )
vae = dict(
type="VideoAutoencoderPipeline",
from_pretrained="pretrained_models/vae-v1",
diff --git a/configs/opensora-v1-2/inference/xl.py b/configs/opensora-v1-2/inference/xl.py
deleted file mode 100644
index 0e0d282..0000000
--- a/configs/opensora-v1-2/inference/xl.py
+++ /dev/null
@@ -1,38 +0,0 @@
-num_frames = 1
-fps = 1
-image_size = (2560, 1536)
-# image_size = (2048, 2048)
-multi_resolution = "STDiT2"
-
-model = dict(
- type="STDiT3-XL/2",
- from_pretrained="PixArt-Sigma-XL-2-2K-MS.pth",
- qk_norm=True,
- enable_flashattn=True,
- enable_layernorm_kernel=True,
-)
-vae = dict(
- type="VideoAutoencoderKL",
- micro_batch_size=4,
- from_pretrained="PixArt-alpha/pixart_sigma_sdxlvae_T5_diffusers",
- subfolder="vae",
- local_files_only=True,
-)
-text_encoder = dict(
- type="t5",
- from_pretrained="DeepFloyd/t5-v1_1-xxl",
- model_max_length=300,
- local_files_only=True,
-)
-scheduler = dict(
- type="dpm-solver",
- num_sampling_steps=50,
- cfg_scale=4.5,
-)
-dtype = "bf16"
-
-# Others
-batch_size = 1
-seed = 42
-prompt_path = "./assets/texts/t2i_sigma.txt"
-save_dir = "./samples/samples/"
diff --git a/configs/opensora-v1-2/train/3b.py b/configs/opensora-v1-2/train/3b.py
deleted file mode 100644
index d23dfd7..0000000
--- a/configs/opensora-v1-2/train/3b.py
+++ /dev/null
@@ -1,57 +0,0 @@
-# Define dataset
-dataset = dict(
- type="VariableVideoTextDataset",
- frame_interval=3,
- transform_name="resize_crop",
-)
-bucket_config = {
- "144p": {2: (1.0, 1)},
-}
-
-# Define acceleration
-num_workers = 4
-num_bucket_build_workers = 16
-dtype = "bf16"
-grad_checkpoint = True
-plugin = "zero2"
-sp_size = 1
-
-# Define model
-model = dict(
- type="STDiT3-3B/2",
- from_pretrained=None,
- enable_flashattn=True,
- enable_layernorm_kernel=True,
-)
-vae = dict(
- type="VideoAutoencoderKL",
- micro_batch_size=4,
- from_pretrained="PixArt-alpha/pixart_sigma_sdxlvae_T5_diffusers",
- subfolder="vae",
- local_files_only=True,
-)
-text_encoder = dict(
- type="t5",
- from_pretrained="DeepFloyd/t5-v1_1-xxl",
- model_max_length=300,
- shardformer=True,
- local_files_only=True,
-)
-scheduler = dict(
- type="iddpm",
- timestep_respacing="",
-)
-
-# Others
-seed = 42
-outputs = "outputs"
-wandb = False
-
-epochs = 1000
-log_every = 10
-ckpt_every = 500
-load = None
-
-batch_size = 10 # only for logging
-lr = 2e-5
-grad_clip = 1.0
diff --git a/configs/opensora-v1-2/train/adapt-rf.py b/configs/opensora-v1-2/train/adapt-rf.py
deleted file mode 100644
index 9c641ba..0000000
--- a/configs/opensora-v1-2/train/adapt-rf.py
+++ /dev/null
@@ -1,76 +0,0 @@
-# Define dataset
-dataset = dict(
- type="VariableVideoTextDataset",
- transform_name="resize_crop",
-)
-bucket_config = {
- "144p": {1: (1.0, 64)},
- # ---
- "256": {1: (0.5, 48)},
- "240p": {1: (0.5, 48)},
- # ---
- "360p": {1: (0.5, 18)},
- "512": {1: (0.5, 18)},
- # ---
- "480p": {1: (0.5, 8)},
- # ---
- "720p": {1: (0.2, 4)},
- "1024": {1: (0.2, 4)},
- # ---
- "1080p": {1: (0.3, 2)},
- # ---
- "2048": {1: (1.0, 1)},
-}
-
-# Define acceleration
-num_workers = 4
-num_bucket_build_workers = 16
-dtype = "bf16"
-grad_checkpoint = False
-plugin = "zero2"
-sp_size = 1
-
-# Define model
-model = dict(
- type="STDiT3-XL/2",
- from_pretrained=None,
- qk_norm=True,
- enable_flashattn=True,
- enable_layernorm_kernel=True,
-)
-vae = dict(
- type="VideoAutoencoderKL",
- micro_batch_size=4,
- from_pretrained="PixArt-alpha/pixart_sigma_sdxlvae_T5_diffusers",
- subfolder="vae",
- local_files_only=True,
-)
-text_encoder = dict(
- type="t5",
- from_pretrained="DeepFloyd/t5-v1_1-xxl",
- model_max_length=300,
- shardformer=True,
- local_files_only=True,
-)
-scheduler = dict(
- type="rflow",
- use_discrete_timesteps=False,
- use_timestep_transform=True,
- sample_method="logit-normal",
-)
-
-# Others
-seed = 42
-outputs = "outputs"
-wandb = False
-
-epochs = 1000
-log_every = 10
-ckpt_every = 500
-load = None
-
-batch_size = None
-lr = 1e-4
-grad_clip = 1.0
-ema_decay = 0.99
-adam_eps = 1e-15
diff --git a/configs/opensora-v1-2/train/adapt.py b/configs/opensora-v1-2/train/adapt.py
deleted file mode 100644
index 7396f21..0000000
--- a/configs/opensora-v1-2/train/adapt.py
+++ /dev/null
@@ -1,72 +0,0 @@
-# Define dataset
-dataset = dict(
- type="VariableVideoTextDataset",
- transform_name="resize_crop",
-)
-bucket_config = {
- "144p": {1: (1.0, 64)},
- # ---
- "256": {1: (0.5, 48)},
- "240p": {1: (0.5, 48)},
- # ---
- "360p": {1: (0.5, 18)},
- "512": {1: (0.5, 18)},
- # ---
- "480p": {1: (0.5, 8)},
- # ---
- "720p": {1: (0.2, 4)},
- "1024": {1: (0.2, 4)},
- # ---
- "1080p": {1: (0.3, 2)},
- # ---
- "2048": {1: (1.0, 1)},
-}
-
-# Define acceleration
-num_workers = 4
-num_bucket_build_workers = 16
-dtype = "bf16"
-grad_checkpoint = False
-plugin = "zero2"
-sp_size = 1
-
-# Define model
-model = dict(
- type="STDiT3-XL/2",
- from_pretrained=None,
- qk_norm=True,
- enable_flashattn=True,
- enable_layernorm_kernel=True,
-)
-vae = dict(
- type="VideoAutoencoderKL",
- micro_batch_size=4,
- from_pretrained="PixArt-alpha/pixart_sigma_sdxlvae_T5_diffusers",
- subfolder="vae",
- local_files_only=True,
-)
-text_encoder = dict(
- type="t5",
- from_pretrained="DeepFloyd/t5-v1_1-xxl",
- model_max_length=300,
- shardformer=True,
- local_files_only=True,
-)
-scheduler = dict(
- type="iddpm",
- timestep_respacing="",
-)
-
-# Others
-seed = 42
-outputs = "outputs"
-wandb = False
-
-epochs = 1000
-log_every = 10
-ckpt_every = 500
-load = None
-
-batch_size = 10 # only for logging
-lr = 2e-5
-grad_clip = 1.0
diff --git a/configs/opensora-v1-2/train/adapt-rf-gc.py b/configs/opensora-v1-2/train/stage1-gc.py
similarity index 54%
rename from configs/opensora-v1-2/train/adapt-rf-gc.py
rename to configs/opensora-v1-2/train/stage1-gc.py
index 8120ba2..6e31b0d 100644
--- a/configs/opensora-v1-2/train/adapt-rf-gc.py
+++ b/configs/opensora-v1-2/train/stage1-gc.py
@@ -2,31 +2,32 @@
dataset = dict(
type="VariableVideoTextDataset",
transform_name="resize_crop",
+ frame_interval=1,
)
bucket_config = {
- "144p": {1: (1.0, 200)},
+ "144p": {1: (1.0, 100)},
+ # # ---
+ "256": {1: (0.5, 100)},
+ "240p": {1: (0.5, 100)},
+ # # ---
+ "360p": {1: (0.5, 60)},
+ "512": {1: (0.5, 60)},
+ # # ---
+ "480p": {1: (0.5, 40)},
+ # # ---
+ "720p": {1: (0.2, 20)},
+ "1024": {1: (0.1, 20)},
+ # # ---
+ "1080p": {1: (0.1, 10)},
# ---
- "256": {1: (0.5, 200)},
- "240p": {1: (0.5, 200)},
- # ---
- "360p": {1: (0.5, 120)},
- "512": {1: (0.5, 120)},
- # ---
- "480p": {1: (0.5, 80)},
- # ---
- "720p": {1: (0.2, 40)},
- "1024": {1: (0.1, 40)},
- # ---
- "1080p": {1: (0.1, 20)},
- # ---
- "2048": {1: (0.1, 10)},
+ "2048": {1: (0.1, 5)},
}
+grad_checkpoint = True
# Acceleration settings
num_workers = 4
num_bucket_build_workers = 16
dtype = "bf16"
-grad_checkpoint = True
plugin = "zero2"
sp_size = 1
@@ -39,11 +40,20 @@ model = dict(
enable_layernorm_kernel=True,
)
vae = dict(
- type="VideoAutoencoderKL",
- micro_batch_size=4,
- from_pretrained="PixArt-alpha/pixart_sigma_sdxlvae_T5_diffusers",
- subfolder="vae",
- local_files_only=True,
+ type="VideoAutoencoderPipeline",
+ from_pretrained="pretrained_models/vae-v2",
+ micro_frame_size=16,
+ vae_2d=dict(
+ type="VideoAutoencoderKL",
+ from_pretrained="PixArt-alpha/pixart_sigma_sdxlvae_T5_diffusers",
+ subfolder="vae",
+ micro_batch_size=4,
+ local_files_only=True,
+ ),
+ vae_temporal=dict(
+ type="VAE_Temporal_SD",
+ from_pretrained=None,
+ ),
)
text_encoder = dict(
type="t5",
@@ -71,7 +81,6 @@ ckpt_every = 500
load = None
batch_size = None
grad_clip = 1.0
-# lr = 1e-4
-lr = 2e-5
+lr = 1e-4
ema_decay = 0.99
adam_eps = 1e-15
diff --git a/configs/opensora-v1-2/train/adapt-vae.py b/configs/opensora-v1-2/train/stage1.py
similarity index 94%
rename from configs/opensora-v1-2/train/adapt-vae.py
rename to configs/opensora-v1-2/train/stage1.py
index 20ea182..5d7ad54 100644
--- a/configs/opensora-v1-2/train/adapt-vae.py
+++ b/configs/opensora-v1-2/train/stage1.py
@@ -2,12 +2,13 @@
dataset = dict(
type="VariableVideoTextDataset",
transform_name="resize_crop",
+ frame_interval=1,
)
bucket_config = {
"144p": {1: (1.0, 64)},
# ---
- "256": {1: (0.5, 48)},
- "240p": {1: (0.5, 48), 16: (1.0, 8)},
+ "256": {1: (0.5, 48), 48: (1.0, 2)},
+ "240p": {1: (0.5, 48), 48: (0.5, 2)},
# ---
"360p": {1: (0.5, 18)},
"512": {1: (0.5, 18)},
diff --git a/configs/opensora-v1-2/train/xl.py b/configs/opensora-v1-2/train/xl.py
deleted file mode 100644
index adc379b..0000000
--- a/configs/opensora-v1-2/train/xl.py
+++ /dev/null
@@ -1,69 +0,0 @@
-# Define dataset
-dataset = dict(
- type="VariableVideoTextDataset",
- frame_interval=3,
- transform_name="resize_crop",
-)
-bucket_config = {
- "240p": {16: (1.0, 2)},
-}
-mask_ratios = { # 20%
- "mask_random": 0.10,
- "mask_quarter_random": 0.01,
- "mask_quarter_head": 0.01,
- "mask_quarter_tail": 0.01,
- "mask_quarter_head_tail": 0.01,
- "mask_image_random": 0.01,
- "mask_image_head": 0.02,
- "mask_image_tail": 0.01,
- "mask_image_head_tail": 0.01,
- "mask_intepolate": 0.01,
-}
-
-# Define acceleration
-num_workers = 4
-num_bucket_build_workers = 16
-dtype = "bf16"
-grad_checkpoint = True
-plugin = "zero2"
-sp_size = 1
-
-# Define model
-model = dict(
- type="STDiT3-XL/2",
- from_pretrained=None,
- enable_flashattn=True,
- enable_layernorm_kernel=True,
-)
-vae = dict(
- type="VideoAutoencoderKL",
- micro_batch_size=4,
- from_pretrained="PixArt-alpha/pixart_sigma_sdxlvae_T5_diffusers",
- subfolder="vae",
- local_files_only=True,
-)
-text_encoder = dict(
- type="t5",
- from_pretrained="DeepFloyd/t5-v1_1-xxl",
- model_max_length=300,
- shardformer=True,
- local_files_only=True,
-)
-scheduler = dict(
- type="iddpm",
- timestep_respacing="",
-)
-
-# Others
-seed = 42
-outputs = "outputs"
-wandb = False
-
-epochs = 1000
-log_every = 10
-ckpt_every = 500
-load = None
-
-batch_size = 10 # only for logging
-lr = 2e-5
-grad_clip = 1.0
diff --git a/opensora/models/stdit/stdit3.py b/opensora/models/stdit/stdit3.py
index 44e0a5c..cd182ab 100644
--- a/opensora/models/stdit/stdit3.py
+++ b/opensora/models/stdit/stdit3.py
@@ -2,6 +2,7 @@ import numpy as np
import torch
import torch.nn as nn
from einops import rearrange
+from rotary_embedding_torch import RotaryEmbedding
from timm.models.layers import DropPath
from timm.models.vision_transformer import Mlp
@@ -187,7 +188,7 @@ class STDiT3(nn.Module):
self.patch_size = patch_size
self.input_sq_size = input_sq_size
self.pos_embed = PositionEmbedding2D(hidden_size)
- # self.rope = RotaryEmbedding(dim=self.hidden_size // self.num_heads)
+ self.rope = RotaryEmbedding(dim=self.hidden_size // self.num_heads)
# embedding
self.x_embedder = PatchEmbed3D(patch_size, in_channels, hidden_size)
@@ -224,25 +225,25 @@ class STDiT3(nn.Module):
)
# temporal blocks
- # drop_path = [x.item() for x in torch.linspace(0, self.drop_path, depth)]
- # self.temporal_blocks = nn.ModuleList(
- # [
- # STDiT3Block(
- # hidden_size=hidden_size,
- # num_heads=num_heads,
- # mlp_ratio=mlp_ratio,
- # drop_path=drop_path[i],
- # qk_norm=qk_norm,
- # enable_flashattn=enable_flashattn,
- # enable_layernorm_kernel=enable_layernorm_kernel,
- # enable_sequence_parallelism=enable_sequence_parallelism,
- # # temporal
- # temporal=True,
- # rope=self.rope.rotate_queries_or_keys,
- # )
- # for i in range(depth)
- # ]
- # )
+ drop_path = [x.item() for x in torch.linspace(0, self.drop_path, depth)]
+ self.temporal_blocks = nn.ModuleList(
+ [
+ STDiT3Block(
+ hidden_size=hidden_size,
+ num_heads=num_heads,
+ mlp_ratio=mlp_ratio,
+ drop_path=drop_path[i],
+ qk_norm=qk_norm,
+ enable_flashattn=enable_flashattn,
+ enable_layernorm_kernel=enable_layernorm_kernel,
+ enable_sequence_parallelism=enable_sequence_parallelism,
+ # temporal
+ temporal=True,
+ rope=self.rope.rotate_queries_or_keys,
+ )
+ for i in range(depth)
+ ]
+ )
# final layer
self.final_layer = T2IFinalLayer(hidden_size, np.prod(self.patch_size), self.out_channels)
@@ -250,12 +251,27 @@ class STDiT3(nn.Module):
self.initialize_weights()
def initialize_weights(self):
+ # Initialize transformer layers:
+ def _basic_init(module):
+ if isinstance(module, nn.Linear):
+ torch.nn.init.xavier_uniform_(module.weight)
+ if module.bias is not None:
+ nn.init.constant_(module.bias, 0)
+
+ self.apply(_basic_init)
+
# Initialize fps_embedder
nn.init.normal_(self.fps_embedder.mlp[0].weight, std=0.02)
nn.init.constant_(self.fps_embedder.mlp[0].bias, 0)
nn.init.constant_(self.fps_embedder.mlp[2].weight, 0)
nn.init.constant_(self.fps_embedder.mlp[2].bias, 0)
+ # Initialize timporal blocks
+ for block in self.temporal_blocks:
+ nn.init.constant_(block.attn.proj.weight, 0)
+ nn.init.constant_(block.cross_attn.proj.weight, 0)
+ nn.init.constant_(block.mlp.fc2.weight, 0)
+
def get_dynamic_size(self, x):
_, _, T, H, W = x.size()
if T % self.patch_size[0] != 0:
@@ -315,10 +331,9 @@ class STDiT3(nn.Module):
x = rearrange(x, "B T S C -> B (T S) C", T=T, S=S)
# === blocks ===
- # for spatial_block, temporal_block in zip(self.spatial_blocks, self.temporal_blocks):
- for spatial_block in self.spatial_blocks:
+ for spatial_block, temporal_block in zip(self.spatial_blocks, self.temporal_blocks):
x = auto_grad_checkpoint(spatial_block, x, y, t_mlp, y_lens, x_mask, t0_mlp, T, S)
- # x = auto_grad_checkpoint(temporal_block, x, y, t_mlp, y_lens, x_mask, t0_mlp, T, S)
+ x = auto_grad_checkpoint(temporal_block, x, y, t_mlp, y_lens, x_mask, t0_mlp, T, S)
# === final layer ===
x = self.final_layer(x, t, x_mask, t0, T, S)
diff --git a/opensora/models/vae/vae.py b/opensora/models/vae/vae.py
index 2fd4d57..bf4e9a7 100644
--- a/opensora/models/vae/vae.py
+++ b/opensora/models/vae/vae.py
@@ -191,7 +191,7 @@ class VideoAutoencoderPipeline(nn.Module):
return x_rec, x_z_rec, z, posterior, x_z
def get_latent_size(self, input_size):
- if self.micro_frame_size is None:
+ if self.micro_frame_size is None or input_size[0] is None:
return self.temporal_vae.get_latent_size(self.spatial_vae.get_latent_size(input_size))
else:
sub_input_size = [self.micro_frame_size, input_size[1], input_size[2]]
diff --git a/opensora/schedulers/rf/rectified_flow.py b/opensora/schedulers/rf/rectified_flow.py
index 9570a23..6ff2348 100644
--- a/opensora/schedulers/rf/rectified_flow.py
+++ b/opensora/schedulers/rf/rectified_flow.py
@@ -7,12 +7,26 @@ from ..iddpm.gaussian_diffusion import _extract_into_tensor, mean_flat
# and https://github.com/magic-research/piecewise-rectified-flow/blob/main/src/scheduler_perflow.py
-def timestep_transform(t, model_kwargs, base_resolution=512 * 512, base_num_frames=1, scale=1.0, num_timesteps=1):
+def timestep_transform(
+ t,
+ model_kwargs,
+ base_resolution=512 * 512,
+ base_num_frames=1,
+ scale=1.0,
+ num_timesteps=1,
+ temporal_reduction=4,
+):
t = t / num_timesteps
+ # NOTE: temporal_reduction is hardcoded to 4, this should be equal to the temporal reduction factor of the vae
resolution = model_kwargs["height"] * model_kwargs["width"]
- num_frames = model_kwargs["num_frames"]
- ratio = (resolution / base_resolution).sqrt() * (num_frames / base_num_frames).sqrt() * scale
+ ratio_space = (resolution / base_resolution).sqrt()
+ # NOTE: currently, we do not take fps into account
+ num_frames = (model_kwargs["num_frames"] - 1) // temporal_reduction + 1
+ ratio_time = (num_frames / base_num_frames).sqrt()
+
+ ratio = ratio_space * ratio_time * scale
new_t = ratio * t / (1 + (ratio - 1) * t)
+
new_t = new_t * num_timesteps
return new_t