Spaces:
Runtime error
Runtime error
File size: 15,294 Bytes
cf2f35c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
import os
import warnings
import torch
import torch.nn as nn
import torch.nn.functional as F
from einops import rearrange
from wan.models.vocal_projector_fantasy import split_audio_sequence, split_tensor_with_padding
try:
import flash_attn_interface
FLASH_ATTN_3_AVAILABLE = True
except ModuleNotFoundError:
FLASH_ATTN_3_AVAILABLE = False
try:
import flash_attn
FLASH_ATTN_2_AVAILABLE = True
except ModuleNotFoundError:
FLASH_ATTN_2_AVAILABLE = False
def flash_attention(
q,
k,
v,
q_lens=None,
k_lens=None,
dropout_p=0.,
softmax_scale=None,
q_scale=None,
causal=False,
window_size=(-1, -1),
deterministic=False,
dtype=torch.bfloat16,
version=None,
):
"""
q: [B, Lq, Nq, C1].
k: [B, Lk, Nk, C1].
v: [B, Lk, Nk, C2]. Nq must be divisible by Nk.
q_lens: [B].
k_lens: [B].
dropout_p: float. Dropout probability.
softmax_scale: float. The scaling of QK^T before applying softmax.
causal: bool. Whether to apply causal attention mask.
window_size: (left right). If not (-1, -1), apply sliding window local attention.
deterministic: bool. If True, slightly slower and uses more memory.
dtype: torch.dtype. Apply when dtype of q/k/v is not float16/bfloat16.
"""
half_dtypes = (torch.float16, torch.bfloat16)
assert dtype in half_dtypes
assert q.device.type == 'cuda' and q.size(-1) <= 256
# params
b, lq, lk, out_dtype = q.size(0), q.size(1), k.size(1), q.dtype
def half(x):
return x if x.dtype in half_dtypes else x.to(dtype)
# preprocess query
if q_lens is None:
q = half(q.flatten(0, 1))
q_lens = torch.tensor(
[lq] * b, dtype=torch.int32).to(
device=q.device, non_blocking=True)
else:
q = half(torch.cat([u[:v] for u, v in zip(q, q_lens)]))
# preprocess key, value
if k_lens is None:
k = half(k.flatten(0, 1))
v = half(v.flatten(0, 1))
k_lens = torch.tensor(
[lk] * b, dtype=torch.int32).to(
device=k.device, non_blocking=True)
else:
k = half(torch.cat([u[:v] for u, v in zip(k, k_lens)]))
v = half(torch.cat([u[:v] for u, v in zip(v, k_lens)]))
q = q.to(v.dtype)
k = k.to(v.dtype)
if q_scale is not None:
q = q * q_scale
if version is not None and version == 3 and not FLASH_ATTN_3_AVAILABLE:
warnings.warn(
'Flash attention 3 is not available, use flash attention 2 instead.'
)
# apply attention
if (version is None or version == 3) and FLASH_ATTN_3_AVAILABLE:
# Note: dropout_p, window_size are not supported in FA3 now.
x = flash_attn_interface.flash_attn_varlen_func(
q=q,
k=k,
v=v,
cu_seqlens_q=torch.cat([q_lens.new_zeros([1]), q_lens]).cumsum(
0, dtype=torch.int32).to(q.device, non_blocking=True),
cu_seqlens_k=torch.cat([k_lens.new_zeros([1]), k_lens]).cumsum(
0, dtype=torch.int32).to(q.device, non_blocking=True),
seqused_q=None,
seqused_k=None,
max_seqlen_q=lq,
max_seqlen_k=lk,
softmax_scale=softmax_scale,
causal=causal,
deterministic=deterministic)[0].unflatten(0, (b, lq))
else:
assert FLASH_ATTN_2_AVAILABLE
x = flash_attn.flash_attn_varlen_func(
q=q,
k=k,
v=v,
cu_seqlens_q=torch.cat([q_lens.new_zeros([1]), q_lens]).cumsum(
0, dtype=torch.int32).to(q.device, non_blocking=True),
cu_seqlens_k=torch.cat([k_lens.new_zeros([1]), k_lens]).cumsum(
0, dtype=torch.int32).to(q.device, non_blocking=True),
max_seqlen_q=lq,
max_seqlen_k=lk,
dropout_p=dropout_p,
softmax_scale=softmax_scale,
causal=causal,
window_size=window_size,
deterministic=deterministic).unflatten(0, (b, lq))
# output
return x.type(out_dtype)
def attention(
q,
k,
v,
q_lens=None,
k_lens=None,
dropout_p=0.,
softmax_scale=None,
q_scale=None,
causal=False,
window_size=(-1, -1),
deterministic=False,
dtype=torch.bfloat16,
fa_version=None,
):
if FLASH_ATTN_2_AVAILABLE or FLASH_ATTN_3_AVAILABLE:
return flash_attention(
q=q,
k=k,
v=v,
q_lens=q_lens,
k_lens=k_lens,
dropout_p=dropout_p,
softmax_scale=softmax_scale,
q_scale=q_scale,
causal=causal,
window_size=window_size,
deterministic=deterministic,
dtype=dtype,
version=fa_version,
)
else:
if q_lens is not None or k_lens is not None:
warnings.warn(
'Padding mask is disabled when using scaled_dot_product_attention. It can have a significant impact on performance.'
)
attn_mask = None
q = q.transpose(1, 2)
k = k.transpose(1, 2)
v = v.transpose(1, 2)
if torch.backends.cuda.flash_sdp_enabled() is False or torch.backends.cuda.enable_flash_sdp is False:
print(1/0)
out = torch.nn.functional.scaled_dot_product_attention(
q, k, v, attn_mask=attn_mask, is_causal=causal, dropout_p=dropout_p)
out = out.transpose(1, 2).contiguous()
return out
class WanRMSNorm(nn.Module):
def __init__(self, dim, eps=1e-5):
super().__init__()
self.dim = dim
self.eps = eps
self.weight = nn.Parameter(torch.ones(dim))
def forward(self, x):
r"""
Args:
x(Tensor): Shape [B, L, C]
"""
return self._norm(x.float()).type_as(x) * self.weight
def _norm(self, x):
return x * torch.rsqrt(x.pow(2).mean(dim=-1, keepdim=True) + self.eps)
class WanLayerNorm(nn.LayerNorm):
def __init__(self, dim, eps=1e-6, elementwise_affine=False):
super().__init__(dim, elementwise_affine=elementwise_affine, eps=eps)
def forward(self, x):
r"""
Args:
x(Tensor): Shape [B, L, C]
"""
return super().forward(x.float()).type_as(x)
class VocalCrossAttention(nn.Module):
def __init__(self,
vocal_dim,
dit_dim,
num_heads,
window_size=(-1, -1),
qk_norm=True,
eps=1e-6,
):
assert vocal_dim % num_heads == 0
super().__init__()
self.vocal_dim = vocal_dim
self.dit_dim = dit_dim
self.num_heads = num_heads
self.head_dim = vocal_dim // num_heads
self.window_size = window_size
self.qk_norm = qk_norm
self.eps = eps
# layers
self.q = nn.Linear(vocal_dim, vocal_dim)
self.k = nn.Linear(dit_dim, vocal_dim)
self.v = nn.Linear(dit_dim, vocal_dim)
self.o = nn.Linear(vocal_dim, vocal_dim)
self.norm_q = WanRMSNorm(vocal_dim, eps=eps) if qk_norm else nn.Identity()
self.norm_k = WanRMSNorm(vocal_dim, eps=eps) if qk_norm else nn.Identity()
def forward(self, x, context, q_lens, dtype):
r"""
Args:
x(Tensor): Shape [B, L1, C]
context(Tensor): Shape [B, L2, C]
context_lens(Tensor): Shape [B]
"""
b, n, d = x.size(0), self.num_heads, self.head_dim
latents_num_frames = 21
q = self.norm_q(self.q(x.to(dtype))).view(b * latents_num_frames, -1, n, d)
k = self.norm_k(self.k(context.to(dtype))).view(b * latents_num_frames, -1, n, d)
v = self.v(context.to(dtype)).view(b * latents_num_frames, -1, n, d)
# compute attention
x = attention(
q.to(dtype),
k.to(dtype),
v.to(dtype),
q_lens=None,
k_lens=None,
)
x = x.to(dtype)
x = x.view(b, -1, n, d)
# output
x = x.flatten(2)
x = self.o(x)
return x
class VocalAttentionBlock(nn.Module):
def __init__(self,
vocal_dim,
dit_dim,
ffn_dim,
num_heads,
window_size=(-1, -1),
qk_norm=True,
cross_attn_norm=True,
eps=1e-6):
super().__init__()
self.vocal_dim = vocal_dim
self.ffn_dim = ffn_dim
self.num_heads = num_heads
self.window_size = window_size
self.qk_norm = qk_norm
self.cross_attn_norm = cross_attn_norm
self.eps = eps
# layers
self.norm1 = WanLayerNorm(vocal_dim, eps)
self.norm3 = WanLayerNorm(
vocal_dim, eps,
elementwise_affine=True) if cross_attn_norm else nn.Identity()
self.cross_attn = VocalCrossAttention(vocal_dim=vocal_dim,
dit_dim=dit_dim,
num_heads=num_heads,
window_size=(-1, -1),
qk_norm=qk_norm,
eps=eps)
self.norm2 = WanLayerNorm(vocal_dim, eps)
self.ffn = nn.Sequential(
nn.Linear(vocal_dim, ffn_dim),
nn.GELU(approximate='tanh'),
nn.Linear(ffn_dim, vocal_dim))
# modulation
self.modulation = nn.Parameter(torch.randn(1, 6, vocal_dim) / vocal_dim ** 0.5)
def forward(
self,
x,
e,
context,
q_lens,
dtype=torch.float32,
):
r"""
Args:
x(Tensor): Shape [B, L, C]
e(Tensor): Shape [B, 6, C]
seq_lens(Tensor): Shape [B], length of each sequence in batch
grid_sizes(Tensor): Shape [B, 3], the second dimension contains (F, H, W)
freqs(Tensor): Rope freqs, shape [1024, C / num_heads / 2]
"""
e = (self.modulation + e).chunk(6, dim=1)
# self-attention
if len(x.shape) == 4:
b, t, n, d = x.size()
x = rearrange(x, "b t n d -> b (t n) d", t=t)
temp_x = self.norm1(x) * (1 + e[1]) + e[0]
temp_x = temp_x.to(dtype)
x = x + temp_x * e[2]
# cross-attention & ffn function
def cross_attn_ffn(x, context, q_lens, e):
# cross-attention
x = x + self.cross_attn(self.norm3(x), context, q_lens, dtype)
# ffn function
temp_x = self.norm2(x) * (1 + e[4]) + e[3]
temp_x = temp_x.to(dtype)
y = self.ffn(temp_x)
x = x + y * e[5]
return x
x = cross_attn_ffn(x, context, q_lens, e)
return x
class Final_Head(nn.Module):
def __init__(self, dim, out_dim, eps=1e-6):
super().__init__()
self.dim = dim
self.eps = eps
# layers
self.norm = WanLayerNorm(dim, eps)
self.final_proj = nn.Linear(dim, out_dim)
# modulation
self.modulation = nn.Parameter(torch.randn(1, 2, dim) / dim**0.5)
def forward(self, x, e):
r"""
Args:
x(Tensor): Shape [B, L1, C]
e(Tensor): Shape [B, C]
"""
e = (self.modulation + e.unsqueeze(1)).chunk(2, dim=1)
x = (self.final_proj(self.norm(x) * (1 + e[1]) + e[0]))
return x
class VocalProjModel(nn.Module):
def __init__(self, audio_in_dim=1024, cross_attention_dim=1024):
super().__init__()
self.cross_attention_dim = cross_attention_dim
self.proj_1 = torch.nn.Linear(audio_in_dim, 2048, bias=False)
self.norm_1 = torch.nn.LayerNorm(2048)
self.proj_2 = torch.nn.Linear(2048, cross_attention_dim, bias=False)
self.norm_2 = torch.nn.LayerNorm(cross_attention_dim)
def forward(self, audio_embeds):
context_tokens = self.proj_1(audio_embeds)
context_tokens = self.norm_1(context_tokens)
context_tokens = self.proj_2(context_tokens)
context_tokens = self.norm_2(context_tokens)
return context_tokens # [B,L,C]
class FantasyTalkingVocalCondition14BModel(nn.Module):
def __init__(self, audio_in_dim: int, audio_proj_dim: int, dit_dim: int):
super().__init__()
self.audio_in_dim = audio_in_dim
self.audio_proj_dim = audio_proj_dim
# audio proj model
self.proj_model = self.init_proj(self.audio_proj_dim)
num_layers = 2
self.blocks = nn.ModuleList([
VocalAttentionBlock(
vocal_dim=audio_proj_dim,
dit_dim=dit_dim,
ffn_dim=audio_proj_dim * 2,
num_heads=8,
window_size=(-1, -1),
qk_norm=True,
cross_attn_norm=True,
)
for _ in range(num_layers)
])
self.final_head = Final_Head(dim=audio_proj_dim, out_dim=audio_proj_dim)
def init_proj(self, cross_attention_dim=5120):
proj_model = VocalProjModel(
audio_in_dim=self.audio_in_dim, cross_attention_dim=cross_attention_dim
)
return proj_model
def forward(self, vocal_embeddings=None, video_sample_n_frames=81, latents=None, e0=None, e=None):
vocal_proj_feature = self.proj_model(vocal_embeddings)
pos_idx_ranges = split_audio_sequence(vocal_proj_feature.size(1), num_frames=video_sample_n_frames)
vocal_proj_split, vocal_context_lens = split_tensor_with_padding(vocal_proj_feature, pos_idx_ranges, expand_length=4)
latents_num_frames = vocal_proj_split.size()[1]
for block in self.blocks:
vocal_proj_split = block(
x=vocal_proj_split,
e=e0,
context=latents,
q_lens=vocal_context_lens,
)
context_tokens = self.final_head(vocal_proj_split, e)
context_tokens = rearrange(context_tokens, "b (f n) c -> b f n c", f=latents_num_frames)
if vocal_embeddings.size()[0] > 1:
vocal_context_lens = torch.cat([vocal_context_lens] * 3)
return context_tokens, vocal_context_lens
if __name__ == "__main__":
model = FantasyTalkingVocalCondition14BModel(audio_in_dim=768, audio_proj_dim=5120, dit_dim=5120)
vocal_embeddings = torch.randn(1, 134, 768)
latents = torch.randn(1, 21504, 5120)
e0 = torch.randn(1, 6, 5120)
e = torch.randn(1, 5120)
out, _ = model(vocal_embeddings=vocal_embeddings, video_sample_n_frames=81, latents=latents, e0=e0, e=e)
print(out.size()) |