Skip to content
Merged
74 changes: 39 additions & 35 deletions README.md

Large diffs are not rendered by default.

74 changes: 39 additions & 35 deletions README_zh.md

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions diffsynth/configs/model_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@
]

wan_series = [
{
# Example: ModelConfig(model_id="Wan-AI/Wan2.2-Animate-2-14B", origin_file_pattern="wan_animate_2/wan_animate_2_bf16.safetensors")
"model_hash": "4536c21ad8740ba78367af4216ae85bf",
"model_name": "wan_video_dit",
"model_class": "diffsynth.models.wan_animate_2_dit.WanAnimate2Transformer",
"extra_kwargs": {},
},
{
# Example: ModelConfig(model_id="krea/krea-realtime-video", origin_file_pattern="krea-realtime-video-14b.safetensors")
"model_hash": "5ec04e02b42d2580483ad69f4e76346a",
Expand Down
9 changes: 9 additions & 0 deletions diffsynth/configs/vram_management_module_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@
"diffsynth.models.wan_video_dit.RMSNorm": "diffsynth.core.vram.layers.AutoWrappedModule",
"torch.nn.Conv2d": "diffsynth.core.vram.layers.AutoWrappedModule",
},
"diffsynth.models.wan_animate_2_dit.WanAnimate2Transformer": {
"diffsynth.models.wan_video_dit.MLP": "diffsynth.core.vram.layers.AutoWrappedModule",
"diffsynth.models.wan_animate_2_dit.AttentionBlock": "diffsynth.core.vram.layers.AutoWrappedNonRecurseModule",
"diffsynth.models.wan_animate_2_dit.Head": "diffsynth.core.vram.layers.AutoWrappedModule",
"torch.nn.Linear": "diffsynth.core.vram.layers.AutoWrappedLinear",
"torch.nn.Conv3d": "diffsynth.core.vram.layers.AutoWrappedModule",
"torch.nn.LayerNorm": "diffsynth.core.vram.layers.AutoWrappedModule",
"diffsynth.models.wan_video_dit.RMSNorm": "diffsynth.core.vram.layers.AutoWrappedModule",
},
"diffsynth.models.wan_video_dit.WanModel": {
"diffsynth.models.wan_video_dit.MLP": "diffsynth.core.vram.layers.AutoWrappedModule",
"diffsynth.models.wan_video_dit.DiTBlock": "diffsynth.core.vram.layers.AutoWrappedNonRecurseModule",
Expand Down
22 changes: 20 additions & 2 deletions diffsynth/core/attention/attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
except ModuleNotFoundError:
XFORMERS_AVAILABLE = False

try:
from torch.nn.attention.flex_attention import flex_attention as flex_attention_func
flex_attention_func = torch.compile(flex_attention_func, dynamic=False, mode="max-autotune-no-cudagraphs", fullgraph=True, backend="inductor")
FLEX_ATTN_AVAILABLE = True
except (ModuleNotFoundError, ImportError):
FLEX_ATTN_AVAILABLE = False

try:
if "enable_gqa" in inspect.signature(torch.nn.functional.scaled_dot_product_attention).parameters:
TORCH_SUPPORT_GQA = True
Expand Down Expand Up @@ -169,9 +176,20 @@ def xformers_attention(q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, q_patt
return out


def attention_forward(q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, q_pattern="b n s d", k_pattern="b n s d", v_pattern="b n s d", out_pattern="b n s d", dims=None, attn_mask=None, scale=None, is_causal=False, compatibility_mode=False, window_size=None):
def flex_attention(q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, q_pattern="b n s d", k_pattern="b n s d", v_pattern="b n s d", out_pattern="b n s d", dims=None, attn_mask=None, scale=None, score_mod=None):
assert FLEX_ATTN_AVAILABLE, "Flex Attention is not available. Please upgrade torch to 2.5.0 or later."
required_in_pattern, required_out_pattern = "b n s d", "b n s d"
q, k, v = rearrange_qkv(q, k, v, q_pattern, k_pattern, v_pattern, required_in_pattern, dims)
out = flex_attention_func(query=q, key=k, value=v, block_mask=attn_mask, scale=scale, score_mod=score_mod)
out = rearrange_out(out, out_pattern, required_out_pattern, dims)
return out


def attention_forward(q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, q_pattern="b n s d", k_pattern="b n s d", v_pattern="b n s d", out_pattern="b n s d", dims=None, attn_mask=None, scale=None, is_causal=False, compatibility_mode=False, window_size=None, use_flex=False, score_mod=None):
if compatibility_mode or (attn_mask is not None) or ATTENTION_IMPLEMENTATION == "torch":
if window_size is None:
if use_flex or score_mod is not None:
return flex_attention(q, k, v, q_pattern, k_pattern, v_pattern, out_pattern, dims, attn_mask=attn_mask, scale=scale, score_mod=score_mod)
elif window_size is None:
return torch_sdpa(q, k, v, q_pattern, k_pattern, v_pattern, out_pattern, dims, attn_mask=attn_mask, scale=scale, is_causal=is_causal)
else:
# Sliding Window Attention is not compatible with `is_causal` and `attn_mask`.
Expand Down
Loading