-
Notifications
You must be signed in to change notification settings - Fork 182
[Feature] Add Cosmos2 i2v pipeline #837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
kevin314
wants to merge
24
commits into
main
Choose a base branch
from
klin/cosmos2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
2746bec
Init
kevin314 d763cc5
Fix t5 encoder tests
kevin314 1a96ad9
Fix dit test
kevin314 37204c4
Add fastvideo pipeline test
kevin314 baeea19
Init pipeline
kevin314 43437ef
Update denoising
kevin314 cc197e7
Running pipeline
kevin314 8fafbad
Update
kevin314 1edf638
Update
kevin314 ffb8b0a
Fix latent preparation
kevin314 adf49de
Fix latent prep mismatch
kevin314 6fa24b7
Add logging
kevin314 6599352
Add addt logging
kevin314 a8b8c78
Fix negative prompt embedding
kevin314 719af46
Fix conditioning mismatch
kevin314 fc5f77e
Use torch backend
kevin314 11aaaae
Clean up
kevin314 f747b40
Addt. logging
kevin314 83c5dc3
Fix norm
kevin314 2c78d9b
Fix rope scale
kevin314 56be69d
Uncond pred sum
kevin314 a10d0a6
Fix scheduling
kevin314 e45dab4
Fix vae decoding
kevin314 26c2f0f
Remove diffusers vae dependency
kevin314 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
from fastvideo.configs.models.dits.hunyuanvideo import HunyuanVideoConfig | ||
from fastvideo.configs.models.dits.stepvideo import StepVideoConfig | ||
from fastvideo.configs.models.dits.wanvideo import WanVideoConfig | ||
print("WOW") | ||
from fastvideo.configs.models.dits.cosmos import CosmosVideoConfig | ||
|
||
__all__ = ["HunyuanVideoConfig", "WanVideoConfig", "StepVideoConfig"] | ||
__all__ = ["HunyuanVideoConfig", "WanVideoConfig", "StepVideoConfig", "CosmosVideoConfig"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from dataclasses import dataclass, field | ||
|
||
from fastvideo.configs.models.dits.base import DiTArchConfig, DiTConfig | ||
|
||
|
||
def is_transformer_blocks(n: str, m) -> bool: | ||
return "transformer_blocks" in n and str.isdigit(n.split(".")[-1]) | ||
|
||
|
||
@dataclass | ||
class CosmosArchConfig(DiTArchConfig): | ||
_fsdp_shard_conditions: list = field( | ||
default_factory=lambda: [is_transformer_blocks]) | ||
|
||
param_names_mapping: dict = field( | ||
default_factory=lambda: { | ||
r"^patch_embed\.(.*)$": r"patch_embed.\1", | ||
r"^time_embed\.time_proj\.(.*)$": r"time_embed.time_proj.\1", | ||
r"^time_embed\.t_embedder\.(.*)$": r"time_embed.t_embedder.\1", | ||
r"^time_embed\.norm\.(.*)$": r"time_embed.norm.\1", | ||
r"^transformer_blocks\.(\d+)\.attn1\.to_q\.(.*)$": | ||
r"transformer_blocks.\1.attn1.to_q.\2", | ||
r"^transformer_blocks\.(\d+)\.attn1\.to_k\.(.*)$": | ||
r"transformer_blocks.\1.attn1.to_k.\2", | ||
r"^transformer_blocks\.(\d+)\.attn1\.to_v\.(.*)$": | ||
r"transformer_blocks.\1.attn1.to_v.\2", | ||
r"^transformer_blocks\.(\d+)\.attn1\.to_out\.0\.(.*)$": | ||
r"transformer_blocks.\1.attn1.to_out.\2", | ||
r"^transformer_blocks\.(\d+)\.attn1\.norm_q\.(.*)$": | ||
r"transformer_blocks.\1.attn1.norm_q.\2", | ||
r"^transformer_blocks\.(\d+)\.attn1\.norm_k\.(.*)$": | ||
r"transformer_blocks.\1.attn1.norm_k.\2", | ||
r"^transformer_blocks\.(\d+)\.attn2\.to_q\.(.*)$": | ||
r"transformer_blocks.\1.attn2.to_q.\2", | ||
r"^transformer_blocks\.(\d+)\.attn2\.to_k\.(.*)$": | ||
r"transformer_blocks.\1.attn2.to_k.\2", | ||
r"^transformer_blocks\.(\d+)\.attn2\.to_v\.(.*)$": | ||
r"transformer_blocks.\1.attn2.to_v.\2", | ||
r"^transformer_blocks\.(\d+)\.attn2\.to_out\.0\.(.*)$": | ||
r"transformer_blocks.\1.attn2.to_out.\2", | ||
r"^transformer_blocks\.(\d+)\.attn2\.norm_q\.(.*)$": | ||
r"transformer_blocks.\1.attn2.norm_q.\2", | ||
r"^transformer_blocks\.(\d+)\.attn2\.norm_k\.(.*)$": | ||
r"transformer_blocks.\1.attn2.norm_k.\2", | ||
r"^transformer_blocks\.(\d+)\.ff\.net\.0\.proj\.(.*)$": | ||
r"transformer_blocks.\1.ff.fc_in.\2", | ||
r"^transformer_blocks\.(\d+)\.ff\.net\.2\.(.*)$": | ||
r"transformer_blocks.\1.ff.fc_out.\2", | ||
r"^norm_out\.(.*)$": r"norm_out.\1", | ||
r"^proj_out\.(.*)$": r"proj_out.\1", | ||
}) | ||
|
||
lora_param_names_mapping: dict = field( | ||
default_factory=lambda: { | ||
r"^transformer_blocks\.(\d+)\.attn1\.to_q\.(.*)$": | ||
r"transformer_blocks.\1.attn1.to_q.\2", | ||
r"^transformer_blocks\.(\d+)\.attn1\.to_k\.(.*)$": | ||
r"transformer_blocks.\1.attn1.to_k.\2", | ||
r"^transformer_blocks\.(\d+)\.attn1\.to_v\.(.*)$": | ||
r"transformer_blocks.\1.attn1.to_v.\2", | ||
r"^transformer_blocks\.(\d+)\.attn1\.to_out\.(.*)$": | ||
r"transformer_blocks.\1.attn1.to_out.\2", | ||
r"^transformer_blocks\.(\d+)\.attn2\.to_q\.(.*)$": | ||
r"transformer_blocks.\1.attn2.to_q.\2", | ||
r"^transformer_blocks\.(\d+)\.attn2\.to_k\.(.*)$": | ||
r"transformer_blocks.\1.attn2.to_k.\2", | ||
r"^transformer_blocks\.(\d+)\.attn2\.to_v\.(.*)$": | ||
r"transformer_blocks.\1.attn2.to_v.\2", | ||
r"^transformer_blocks\.(\d+)\.attn2\.to_out\.(.*)$": | ||
r"transformer_blocks.\1.attn2.to_out.\2", | ||
r"^transformer_blocks\.(\d+)\.ff\.(.*)$": | ||
r"transformer_blocks.\1.ff.\2", | ||
}) | ||
|
||
# Cosmos-specific config parameters based on transformer_cosmos.py | ||
in_channels: int = 16 | ||
out_channels: int = 16 | ||
num_attention_heads: int = 16 | ||
attention_head_dim: int = 128 | ||
num_layers: int = 28 | ||
mlp_ratio: float = 4.0 | ||
text_embed_dim: int = 1024 | ||
adaln_lora_dim: int = 256 | ||
max_size: tuple[int, int, int] = (128, 240, 240) | ||
patch_size: tuple[int, int, int] = (1, 2, 2) | ||
rope_scale: tuple[float, float, float] = (1.0, 3.0, 3.0) | ||
concat_padding_mask: bool = True | ||
extra_pos_embed_type: str | None = None | ||
qk_norm: str = "rms_norm" | ||
eps: float = 1e-6 | ||
exclude_lora_layers: list[str] = field(default_factory=lambda: ["embedder"]) | ||
|
||
|
||
def __post_init__(self): | ||
super().__post_init__() | ||
self.out_channels = self.out_channels or self.in_channels | ||
self.hidden_size = self.num_attention_heads * self.attention_head_dim | ||
self.num_channels_latents = self.in_channels | ||
|
||
|
||
@dataclass | ||
class CosmosVideoConfig(DiTConfig): | ||
arch_config: DiTArchConfig = field(default_factory=CosmosArchConfig) | ||
prefix: str = "Cosmos" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
from fastvideo.configs.models.vaes.hunyuanvae import HunyuanVAEConfig | ||
from fastvideo.configs.models.vaes.stepvideovae import StepVideoVAEConfig | ||
from fastvideo.configs.models.vaes.wanvae import WanVAEConfig | ||
from fastvideo.configs.models.vaes.cosmosvae import CosmosVAEConfig | ||
|
||
__all__ = [ | ||
"HunyuanVAEConfig", | ||
"WanVAEConfig", | ||
"StepVideoVAEConfig", | ||
"CosmosVAEConfig", | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from dataclasses import dataclass, field | ||
|
||
import torch | ||
|
||
from fastvideo.configs.models.vaes.base import VAEArchConfig, VAEConfig | ||
|
||
|
||
@dataclass | ||
class CosmosVAEArchConfig(VAEArchConfig): | ||
_class_name: str = "AutoencoderKLWan" | ||
_diffusers_version: str = "0.34.0.dev0" | ||
_name_or_path: str = "" | ||
base_dim: int = 96 | ||
z_dim: int = 16 | ||
dim_mult: tuple[int, ...] = (1, 2, 4, 4) | ||
num_res_blocks: int = 2 | ||
attn_scales: tuple[float, ...] = () | ||
temperal_downsample: tuple[bool, ...] = (False, True, True) | ||
dropout: float = 0.0 | ||
decoder_base_dim: int | None = None | ||
is_residual: bool = False | ||
in_channels: int = 3 | ||
out_channels: int = 3 | ||
patch_size: int | None = None | ||
scale_factor_temporal: int = 4 | ||
scale_factor_spatial: int = 8 | ||
clip_output: bool = True | ||
latents_mean: tuple[float, ...] = ( | ||
-0.7571, | ||
-0.7089, | ||
-0.9113, | ||
0.1075, | ||
-0.1745, | ||
0.9653, | ||
-0.1517, | ||
1.5508, | ||
0.4134, | ||
-0.0715, | ||
0.5517, | ||
-0.3632, | ||
-0.1922, | ||
-0.9497, | ||
0.2503, | ||
-0.2921, | ||
) | ||
latents_std: tuple[float, ...] = ( | ||
2.8184, | ||
1.4541, | ||
2.3275, | ||
2.6558, | ||
1.2196, | ||
1.7708, | ||
2.6052, | ||
2.0743, | ||
3.2687, | ||
2.1526, | ||
2.8652, | ||
1.5579, | ||
1.6382, | ||
1.1253, | ||
2.8251, | ||
1.9160, | ||
) | ||
temporal_compression_ratio = 4 | ||
spatial_compression_ratio = 8 | ||
|
||
def __post_init__(self): | ||
self.scaling_factor: torch.Tensor = 1.0 / torch.tensor( | ||
self.latents_std).view(1, self.z_dim, 1, 1, 1) | ||
self.shift_factor: torch.Tensor = torch.tensor(self.latents_mean).view( | ||
1, self.z_dim, 1, 1, 1) | ||
self.temporal_compression_ratio = self.scale_factor_temporal | ||
self.spatial_compression_ratio = self.scale_factor_spatial | ||
|
||
|
||
@dataclass | ||
class CosmosVAEConfig(VAEConfig): | ||
arch_config: CosmosVAEArchConfig = field(default_factory=CosmosVAEArchConfig) | ||
use_feature_cache: bool = True | ||
|
||
use_tiling: bool = False | ||
use_temporal_tiling: bool = False | ||
use_parallel_tiling: bool = False | ||
|
||
def __post_init__(self): | ||
self.blend_num_frames = (self.tile_sample_min_num_frames - | ||
self.tile_sample_stride_num_frames) * 2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Newline char |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These fields should be pop/removed in the loader so can be removed. You can refer to how wan's vae config is defined