MCPcopy Create free account
hub / github.com/OpenImagingLab/FlashVSR / model_fn_wan_video

Function model_fn_wan_video

diffsynth/pipelines/flashvsr_tiny_long.py:490–575  ·  view source on GitHub ↗
(
    dit: WanModel,
    x: torch.Tensor,
    timestep: torch.Tensor,
    context: torch.Tensor,
    tea_cache: Optional[TeaCache] = None,
    use_unified_sequence_parallel: bool = False,
    LQ_latents: Optional[torch.Tensor] = None,
    is_full_block: bool = False,
    is_stream: bool = False,
    pre_cache_k: Optional[list[torch.Tensor]] = None,
    pre_cache_v: Optional[list[torch.Tensor]] = None,
    topk_ratio: float = 2.0,
    kv_ratio: float = 3.0,
    cur_process_idx: int = 0,
    t_mod : torch.Tensor = None,
    t : torch.Tensor = None,
    local_range: int = 9,
    **kwargs,
)

Source from the content-addressed store, hash-verified

488# 简化版模型前向封装(无 vace / 无 motion_controller)
489# -----------------------------
490def model_fn_wan_video(
491 dit: WanModel,
492 x: torch.Tensor,
493 timestep: torch.Tensor,
494 context: torch.Tensor,
495 tea_cache: Optional[TeaCache] = None,
496 use_unified_sequence_parallel: bool = False,
497 LQ_latents: Optional[torch.Tensor] = None,
498 is_full_block: bool = False,
499 is_stream: bool = False,
500 pre_cache_k: Optional[list[torch.Tensor]] = None,
501 pre_cache_v: Optional[list[torch.Tensor]] = None,
502 topk_ratio: float = 2.0,
503 kv_ratio: float = 3.0,
504 cur_process_idx: int = 0,
505 t_mod : torch.Tensor = None,
506 t : torch.Tensor = None,
507 local_range: int = 9,
508 **kwargs,
509):
510 # patchify
511 x, (f, h, w) = dit.patchify(x)
512
513 win = (2, 8, 8)
514 seqlen = f // win[0]
515 local_num = seqlen
516 window_size = win[0] * h * w // 128
517 square_num = window_size * window_size
518 topk = int(square_num * topk_ratio) - 1
519 kv_len = int(kv_ratio)
520
521 # RoPE 位置(分段)
522 if cur_process_idx == 0:
523 freqs = torch.cat([
524 dit.freqs[0][:f].view(f, 1, 1, -1).expand(f, h, w, -1),
525 dit.freqs[1][:h].view(1, h, 1, -1).expand(f, h, w, -1),
526 dit.freqs[2][:w].view(1, 1, w, -1).expand(f, h, w, -1)
527 ], dim=-1).reshape(f * h * w, 1, -1).to(x.device)
528 else:
529 freqs = torch.cat([
530 dit.freqs[0][4 + cur_process_idx*2:4 + cur_process_idx*2 + f].view(f, 1, 1, -1).expand(f, h, w, -1),
531 dit.freqs[1][:h].view(1, h, 1, -1).expand(f, h, w, -1),
532 dit.freqs[2][:w].view(1, 1, w, -1).expand(f, h, w, -1)
533 ], dim=-1).reshape(f * h * w, 1, -1).to(x.device)
534
535 # TeaCache(默认不启用)
536 tea_cache_update = tea_cache.check(dit, x, t_mod) if tea_cache is not None else False
537
538 # 统一序列并行(此处默认关闭)
539 if use_unified_sequence_parallel:
540 import torch.distributed as dist
541 from xfuser.core.distributed import (get_sequence_parallel_rank,
542 get_sequence_parallel_world_size,
543 get_sp_group)
544 if dist.is_initialized() and dist.get_world_size() > 1:
545 x = torch.chunk(x, get_sequence_parallel_world_size(), dim=1)[get_sequence_parallel_rank()]
546
547 # Block 堆叠

Callers 1

__call__Method · 0.70

Calls 5

patchifyMethod · 0.45
toMethod · 0.45
checkMethod · 0.45
updateMethod · 0.45
unpatchifyMethod · 0.45

Tested by

no test coverage detected