Skip to content

Open-Sora: Democratizing Efficient Video Production for All

Python

converted model to hf format (#366)

Frank Lee19911b99b886
diff --git a/configs/opensora-v1-1/inference/sample-ref.py b/configs/opensora-v1-1/inference/sample-ref.py
index 735c01b..c896cae 100644
--- a/configs/opensora-v1-1/inference/sample-ref.py
+++ b/configs/opensora-v1-1/inference/sample-ref.py
@@ -38,10 +38,10 @@ reference_path = [
# Define model
model = dict(
type="STDiT2-XL/2",
- from_pretrained=None,
+ from_pretrained="hpcai-tech/OpenSora-STDiT-v2-stage3",
input_sq_size=512,
qk_norm=True,
- enable_flashattn=True,
+ enable_flash_attn=True,
enable_layernorm_kernel=True,
)
vae = dict(
diff --git a/configs/opensora-v1-1/inference/sample.py b/configs/opensora-v1-1/inference/sample.py
index cec8073..29e63ca 100644
--- a/configs/opensora-v1-1/inference/sample.py
+++ b/configs/opensora-v1-1/inference/sample.py
@@ -7,10 +7,10 @@ multi_resolution = "STDiT2"
# Define model
model = dict(
type="STDiT2-XL/2",
- from_pretrained=None,
+ from_pretrained="hpcai-tech/OpenSora-STDiT-v2-stage3",
input_sq_size=512,
qk_norm=True,
- enable_flashattn=True,
+ enable_flash_attn=True,
enable_layernorm_kernel=True,
)
vae = dict(
diff --git a/gradio/app.py b/gradio/app.py
index 181c059..726b244 100644
--- a/gradio/app.py
+++ b/gradio/app.py
@@ -255,9 +255,9 @@ def build_models(model_type, config, enable_optimization=False):
# build stdit
# we load model from HuggingFace directly so that we don't need to
# handle model download logic in HuggingFace Space
- from transformers import AutoModel
+ from opensora.models.stdit.stdit2 import STDiT2
- stdit = AutoModel.from_pretrained(
+ stdit = STDiT2.from_pretrained(
HF_STDIT_MAP[model_type],
enable_flash_attn=enable_optimization,
trust_remote_code=True,
@@ -410,7 +410,6 @@ def run_inference(mode, prompt_text, resolution, aspect_ratio, length, reference
# save image to disk
from PIL import Image
im = Image.fromarray(reference_image)
- idx = os.environ['CUDA_VISIBLE_DEVICES']
with NamedTemporaryFile(suffix=".jpg") as temp_file:
im.save(temp_file.name)
diff --git a/opensora/models/layers/blocks.py b/opensora/models/layers/blocks.py
index 55d874a..c4dd5c3 100644
--- a/opensora/models/layers/blocks.py
+++ b/opensora/models/layers/blocks.py
@@ -139,7 +139,7 @@ class Attention(nn.Module):
attn_drop: float = 0.0,
proj_drop: float = 0.0,
norm_layer: nn.Module = LlamaRMSNorm,
- enable_flashattn: bool = False,
+ enable_flash_attn: bool = False,
rope=None,
) -> None:
super().__init__()
@@ -148,7 +148,7 @@ class Attention(nn.Module):
self.num_heads = num_heads
self.head_dim = dim // num_heads
self.scale = self.head_dim**-0.5
- self.enable_flashattn = enable_flashattn
+ self.enable_flash_attn = enable_flash_attn
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
self.q_norm = norm_layer(self.head_dim) if qk_norm else nn.Identity()
@@ -165,7 +165,7 @@ class Attention(nn.Module):
def forward(self, x: torch.Tensor) -> torch.Tensor:
B, N, C = x.shape
# flash attn is not memory efficient for small sequences, this is empirical
- enable_flashattn = self.enable_flashattn and (N > B)
+ enable_flash_attn = self.enable_flash_attn and (N > B)
qkv = self.qkv(x)
qkv_shape = (B, N, 3, self.num_heads, self.head_dim)
@@ -177,7 +177,7 @@ class Attention(nn.Module):
k = self.rotary_emb(k)
q, k = self.q_norm(q), self.k_norm(k)
- if enable_flashattn:
+ if enable_flash_attn:
from flash_attn import flash_attn_func
# (B, #heads, N, #dim) -> (B, N, #heads, #dim)
@@ -202,7 +202,7 @@ class Attention(nn.Module):
x = attn @ v
x_output_shape = (B, N, C)
- if not enable_flashattn:
+ if not enable_flash_attn:
x = x.transpose(1, 2)
x = x.reshape(x_output_shape)
x = self.proj(x)
@@ -220,7 +220,7 @@ class SeqParallelAttention(Attention):
attn_drop: float = 0.0,
proj_drop: float = 0.0,
norm_layer: nn.Module = LlamaRMSNorm,
- enable_flashattn: bool = False,
+ enable_flash_attn: bool = False,
rope=None,
) -> None:
assert rope is None, "Rope is not supported in SeqParallelAttention"
@@ -232,7 +232,7 @@ class SeqParallelAttention(Attention):
attn_drop=attn_drop,
proj_drop=proj_drop,
norm_layer=norm_layer,
- enable_flashattn=enable_flashattn,
+ enable_flash_attn=enable_flash_attn,
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
@@ -248,7 +248,7 @@ class SeqParallelAttention(Attention):
# [B, SUB_N, 3, NUM_HEAD, HEAD_DIM] -> [B, N, 3, NUM_HEAD_PER_DEVICE, HEAD_DIM]
qkv = all_to_all(qkv, sp_group, scatter_dim=3, gather_dim=1)
- if self.enable_flashattn:
+ if self.enable_flash_attn:
qkv_permute_shape = (
2,
0,
@@ -269,7 +269,7 @@ class SeqParallelAttention(Attention):
# ERROR: Should qk_norm first
q, k, v = qkv.unbind(0)
q, k = self.q_norm(q), self.k_norm(k)
- if self.enable_flashattn:
+ if self.enable_flash_attn:
from flash_attn import flash_attn_func
x = flash_attn_func(
@@ -289,7 +289,7 @@ class SeqParallelAttention(Attention):
attn = self.attn_drop(attn)
x = attn @ v
- if not self.enable_flashattn:
+ if not self.enable_flash_attn:
x = x.transpose(1, 2)
# apply all to all to gather back attention heads and split sequence
diff --git a/opensora/models/stdit/stdit2.py b/opensora/models/stdit/stdit2.py
index 73fe276..afcc0c1 100644
--- a/opensora/models/stdit/stdit2.py
+++ b/opensora/models/stdit/stdit2.py
@@ -1,6 +1,5 @@
import numpy as np
import torch
-import torch.distributed as dist
import torch.nn as nn
from einops import rearrange
from rotary_embedding_torch import RotaryEmbedding
@@ -8,16 +7,12 @@ from timm.models.layers import DropPath
from timm.models.vision_transformer import Mlp
from opensora.acceleration.checkpoint import auto_grad_checkpoint
-from opensora.acceleration.communications import gather_forward_split_backward, split_forward_gather_backward
-from opensora.acceleration.parallel_states import get_sequence_parallel_group
from opensora.models.layers.blocks import (
Attention,
CaptionEmbedder,
MultiHeadCrossAttention,
PatchEmbed3D,
PositionEmbedding2D,
- SeqParallelAttention,
- SeqParallelMultiHeadCrossAttention,
SizeEmbedder,
T2IFinalLayer,
TimestepEmbedder,
@@ -27,7 +22,7 @@ from opensora.models.layers.blocks import (
t2i_modulate,
)
from opensora.registry import MODELS
-from opensora.utils.ckpt_utils import load_checkpoint
+from transformers import PretrainedConfig, PreTrainedModel
class STDiT2Block(nn.Module):
@@ -37,7 +32,7 @@ class STDiT2Block(nn.Module):
num_heads,
mlp_ratio=4.0,
drop_path=0.0,
- enable_flashattn=False,
+ enable_flash_attn=False,
enable_layernorm_kernel=False,
enable_sequence_parallelism=False,
rope=None,
@@ -45,30 +40,22 @@ class STDiT2Block(nn.Module):
):
super().__init__()
self.hidden_size = hidden_size
- self.enable_flashattn = enable_flashattn
+ self.enable_flash_attn = enable_flash_attn
self._enable_sequence_parallelism = enable_sequence_parallelism
- assert not self._enable_sequence_parallelism, "Sequence parallelism is not supported."
- if enable_sequence_parallelism:
- self.attn_cls = SeqParallelAttention
- self.mha_cls = SeqParallelMultiHeadCrossAttention
- else:
- self.attn_cls = Attention
- self.mha_cls = MultiHeadCrossAttention
-
# spatial branch
self.norm1 = get_layernorm(hidden_size, eps=1e-6, affine=False, use_kernel=enable_layernorm_kernel)
- self.attn = self.attn_cls(
+ self.attn = Attention(
hidden_size,
num_heads=num_heads,
qkv_bias=True,
- enable_flashattn=enable_flashattn,
+ enable_flash_attn=enable_flash_attn,
qk_norm=qk_norm,
)
self.scale_shift_table = nn.Parameter(torch.randn(6, hidden_size) / hidden_size**0.5)
# cross attn
- self.cross_attn = self.mha_cls(hidden_size, num_heads)
+ self.cross_attn = MultiHeadCrossAttention(hidden_size, num_heads)
# mlp branch
self.norm2 = get_layernorm(hidden_size, eps=1e-6, affine=False, use_kernel=enable_layernorm_kernel)
@@ -79,11 +66,11 @@ class STDiT2Block(nn.Module):
# temporal branch
self.norm_temp = get_layernorm(hidden_size, eps=1e-6, affine=False, use_kernel=enable_layernorm_kernel) # new
- self.attn_temp = self.attn_cls(
+ self.attn_temp = Attention(
hidden_size,
num_heads=num_heads,
qkv_bias=True,
- enable_flashattn=self.enable_flashattn,
+ enable_flash_attn=self.enable_flash_attn,
rope=rope,
qk_norm=qk_norm,
)
@@ -174,8 +161,10 @@ class STDiT2Block(nn.Module):
return x
-class STDiT2(nn.Module):
+class STDiT2Config(PretrainedConfig):
+
+ model_type = "STDiT2"
+
def __init__(
self,
input_size=(None, None, None),
@@ -192,45 +181,73 @@ class STDiT2(nn.Module):
no_temporal_pos_emb=False,
caption_channels=4096,
model_max_length=120,
- dtype=torch.float32,
freeze=None,
qk_norm=False,
- enable_flashattn=False,
+ enable_flash_attn=False,
enable_layernorm_kernel=False,
- enable_sequence_parallelism=False,
+ **kwargs,
):
- super().__init__()
- self.pred_sigma = pred_sigma
+ self.input_size = input_size
+ self.input_sq_size = input_sq_size
self.in_channels = in_channels
- self.out_channels = in_channels * 2 if pred_sigma else in_channels
+ self.patch_size = patch_size
self.hidden_size = hidden_size
- self.num_heads = num_heads
- self.dtype = dtype
- self.no_temporal_pos_emb = no_temporal_pos_emb
self.depth = depth
+ self.num_heads = num_heads
self.mlp_ratio = mlp_ratio
- self.enable_flashattn = enable_flashattn
+ self.class_dropout_prob = class_dropout_prob
+ self.pred_sigma = pred_sigma
+ self.drop_path = drop_path
+ self.no_temporal_pos_emb = no_temporal_pos_emb
+ self.caption_channels = caption_channels
+ self.model_max_length = model_max_length
+ self.freeze = freeze
+ self.qk_norm = qk_norm
+ self.enable_flash_attn = enable_flash_attn
self.enable_layernorm_kernel = enable_layernorm_kernel
+ super().__init__(**kwargs)
- # support dynamic input
- self.patch_size = patch_size
- self.input_size = input_size
- self.input_sq_size = input_sq_size
- self.pos_embed = PositionEmbedding2D(hidden_size)
- self.x_embedder = PatchEmbed3D(patch_size, in_channels, hidden_size)
- self.t_embedder = TimestepEmbedder(hidden_size)
- self.t_block = nn.Sequential(nn.SiLU(), nn.Linear(hidden_size, 6 * hidden_size, bias=True))
- self.t_block_temp = nn.Sequential(nn.SiLU(), nn.Linear(hidden_size, 3 * hidden_size, bias=True)) # new
+class STDiT2(PreTrainedModel):
+
+ config_class = STDiT2Config
+
+ def __init__(
+ self,
+ config
+ ):
+ super().__init__(config)
+ self.pred_sigma = config.pred_sigma
+ self.in_channels = config.in_channels
+ self.out_channels = config.in_channels * 2 if config.pred_sigma else config.in_channels
+ self.hidden_size = config.hidden_size
+ self.num_heads = config.num_heads
+ self.no_temporal_pos_emb = config.no_temporal_pos_emb
+ self.depth = config.depth
+ self.mlp_ratio = config.mlp_ratio
+ self.enable_flash_attn = config.enable_flash_attn
+ self.enable_layernorm_kernel = config.enable_layernorm_kernel
+
+ # support dynamic input
+ self.patch_size = config.patch_size
+ self.input_size = config.input_size
+ self.input_sq_size = config.input_sq_size
+ self.pos_embed = PositionEmbedding2D(config.hidden_size)
+
+ self.x_embedder = PatchEmbed3D(config.patch_size, config.in_channels, config.hidden_size)
+ self.t_embedder = TimestepEmbedder(config.hidden_size)
+ self.t_block = nn.Sequential(nn.SiLU(), nn.Linear(config.hidden_size, 6 * config.hidden_size, bias=True))
+ self.t_block_temp = nn.Sequential(nn.SiLU(), nn.Linear(config.hidden_size, 3 * config.hidden_size, bias=True)) # new
self.y_embedder = CaptionEmbedder(
- in_channels=caption_channels,
- hidden_size=hidden_size,
- uncond_prob=class_dropout_prob,
+ in_channels=config.caption_channels,
+ hidden_size=config.hidden_size,
+ uncond_prob=config.class_dropout_prob,
act_layer=approx_gelu,
- token_num=model_max_length,
+ token_num=config.model_max_length,
)
- drop_path = [x.item() for x in torch.linspace(0, drop_path, depth)]
+ drop_path = [x.item() for x in torch.linspace(0, config.drop_path, config.depth)]
self.rope = RotaryEmbedding(dim=self.hidden_size // self.num_heads) # new
self.blocks = nn.ModuleList(
[
@@ -239,16 +256,15 @@ class STDiT2(nn.Module):
self.num_heads,
mlp_ratio=self.mlp_ratio,
drop_path=drop_path[i],
- enable_flashattn=self.enable_flashattn,
+ enable_flash_attn=self.enable_flash_attn,
enable_layernorm_kernel=self.enable_layernorm_kernel,
- enable_sequence_parallelism=enable_sequence_parallelism,
rope=self.rope.rotate_queries_or_keys,
- qk_norm=qk_norm,
+ qk_norm=config.qk_norm,
)
for i in range(self.depth)
]
)
- self.final_layer = T2IFinalLayer(hidden_size, np.prod(self.patch_size), self.out_channels)
+ self.final_layer = T2IFinalLayer(config.hidden_size, np.prod(self.patch_size), self.out_channels)
# multi_res
assert self.hidden_size % 3 == 0, "hidden_size must be divisible by 3"
@@ -260,20 +276,13 @@ class STDiT2(nn.Module):
# init model
self.initialize_weights()
self.initialize_temporal()
- if freeze is not None:
- assert freeze in ["not_temporal", "text"]
- if freeze == "not_temporal":
+ if config.freeze is not None:
+ assert config.freeze in ["not_temporal", "text"]
+ if config.freeze == "not_temporal":
self.freeze_not_temporal()
- elif freeze == "text":
+ elif config.freeze == "text":
self.freeze_text()
- # sequence parallel related configs
- self.enable_sequence_parallelism = enable_sequence_parallelism
- if enable_sequence_parallelism:
- self.sp_rank = dist.get_rank(get_sequence_parallel_group())
- else:
- self.sp_rank = None
-
def get_dynamic_size(self, x):
_, _, T, H, W = x.size()
if T % self.patch_size[0] != 0:
@@ -302,9 +311,10 @@ class STDiT2(nn.Module):
x (torch.Tensor): output latent representation; of shape [B, C, T, H, W]
"""
B = x.shape[0]
- x = x.to(self.dtype)
- timestep = timestep.to(self.dtype)
- y = y.to(self.dtype)
+ dtype = self.x_embedder.proj.weight.dtype
+ x = x.to(dtype)
+ timestep = timestep.to(dtype)
+ y = y.to(dtype)
# === process data info ===
# 1. get dynamic size
@@ -337,10 +347,6 @@ class STDiT2(nn.Module):
x = x + pos_emb
x = rearrange(x, "B T S C -> B (T S) C")
- # shard over the sequence dim if sp is enabled
- if self.enable_sequence_parallelism:
- x = split_forward_gather_backward(x, get_sequence_parallel_group(), dim=1, grad_scale="down")
-
# prepare adaIN
t = self.t_embedder(timestep, dtype=x.dtype) # [B, C]
t_spc = t + data_info # [B, C]
@@ -388,10 +394,7 @@ class STDiT2(nn.Module):
T,
S,
)
-
- if self.enable_sequence_parallelism:
- x = gather_forward_split_backward(x, get_sequence_parallel_group(), dim=1, grad_scale="up")
- # x.shape: [B, N, C]
+ # x.shape: [B, N, C]
# final process
x = self.final_layer(x, t, x_mask, t0_spc, T, S) # [B, N, C=T_p * H_p * W_p * C_out]
@@ -498,7 +501,14 @@ class STDiT2(nn.Module):
@MODELS.register_module("STDiT2-XL/2")
def STDiT2_XL_2(from_pretrained=None, **kwargs):
- model = STDiT2(depth=28, hidden_size=1152, patch_size=(1, 2, 2), num_heads=16, **kwargs)
if from_pretrained is not None:
- load_checkpoint(model, from_pretrained)
+ model = STDiT2.from_pretrained(from_pretrained, **kwargs)
+ else:
+ config = STDiT2Config(
+ depth=28,
+ hidden_size=1152,
+ patch_size=(1, 2, 2),
+ num_heads=16, **kwargs
+ )
+ model = STDiT2(config)
return model
diff --git a/requirements.txt b/requirements.txt
index e8031a8..f8d7948 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -7,7 +7,7 @@ mmengine
pandas
pre-commit
pyarrow
-pyav
+av
tensorboard
timm
tqdm
diff --git a/scripts/inference-long.py b/scripts/inference-long.py
index eec19cf..f77ebad 100644
--- a/scripts/inference-long.py
+++ b/scripts/inference-long.py
@@ -168,7 +168,6 @@ def main():
in_channels=vae.out_channels,
caption_channels=text_encoder.output_dim,
model_max_length=text_encoder.model_max_length,
- dtype=dtype,
enable_sequence_parallelism=enable_sequence_parallelism,
)
text_encoder.y_embedder = model.y_embedder # hack for classifier-free guidance
diff --git a/scripts/inference.py b/scripts/inference.py
index 7a83ead..369379e 100644
--- a/scripts/inference.py
+++ b/scripts/inference.py
@@ -55,6 +55,7 @@ def main():
vae = build_module(cfg.vae, MODELS)
latent_size = vae.get_latent_size(input_size)
text_encoder = build_module(cfg.text_encoder, MODELS, device=device) # T5 must be fp32
+
model = build_module(
cfg.model,
MODELS,
@@ -62,7 +63,6 @@ def main():
in_channels=vae.out_channels,
caption_channels=text_encoder.output_dim,
model_max_length=text_encoder.model_max_length,
- dtype=dtype,
enable_sequence_parallelism=enable_sequence_parallelism,
)
text_encoder.y_embedder = model.y_embedder # hack for classifier-free guidance