From 0c4426fc2b28ecfae2b7f0421ddc6ad5e69baf52 Mon Sep 17 00:00:00 2001 From: kijai <40791699+kijai@users.noreply.github.com> Date: Thu, 14 Aug 2025 18:53:08 +0300 Subject: [PATCH] FantasyPortrait reqs and cleanup unsure if I want to add onnx in the requirements at this stage... --- fantasyportrait/face_utils.py | 7 +++- fantasyportrait/model.py | 77 ----------------------------------- requirements.txt | 4 +- wanvideo/wan_video_vae.py | 2 + 4 files changed, 10 insertions(+), 80 deletions(-) diff --git a/fantasyportrait/face_utils.py b/fantasyportrait/face_utils.py index ccfad08..4b78fbb 100644 --- a/fantasyportrait/face_utils.py +++ b/fantasyportrait/face_utils.py @@ -3,9 +3,12 @@ import time import cv2 import numpy as np -import onnx -import onnxruntime +try: + import onnx + import onnxruntime +except ImportError: + print("ONNX and ONNX Runtime are required for FantasyPortrait. Please install them using 'pip install onnx onnxruntime-gpu'") def create_onnx_session(onnx_path, gpu_id=None) -> onnxruntime.InferenceSession: start = time.perf_counter() diff --git a/fantasyportrait/model.py b/fantasyportrait/model.py index 492b38d..2ab6ca4 100644 --- a/fantasyportrait/model.py +++ b/fantasyportrait/model.py @@ -3,8 +3,6 @@ import torch import torch.nn as nn import torch.nn.functional as F -from ..wanvideo.modules.attention import attention - def FeedForward(dim, mult=4): inner_dim = int(dim * mult) return nn.Sequential( @@ -51,81 +49,6 @@ class MultiProjModel(nn.Module): return result_final - -class SingleStreamBlockProcessor(nn.Module): - def __init__(self, context_dim, hidden_dim): - super().__init__() - - self.context_dim = context_dim - self.hidden_dim = hidden_dim - - self.ip_adapter_single_stream_k_proj = nn.Linear( - context_dim, hidden_dim, bias=False - ) - self.ip_adapter_single_stream_v_proj = nn.Linear( - context_dim, hidden_dim, bias=False - ) - - nn.init.zeros_(self.ip_adapter_single_stream_k_proj.weight) - nn.init.zeros_(self.ip_adapter_single_stream_v_proj.weight) - - def __call__( - self, - attn: nn.Module, - x: torch.Tensor, - context: torch.Tensor, - context_lens: torch.Tensor, - adapter_proj: torch.Tensor, - adapter_context_lens: torch.Tensor, - latents_num_frames: int = 21, - ip_scale: float = 1.0, - adapter_attn_mask: torch.Tensor = None, - ) -> torch.Tensor: - context_img = context[:, :257] - context = context[:, 257:] - b, n, d = x.size(0), attn.num_heads, attn.head_dim - - # compute query, key, value - q = attn.norm_q(attn.q(x)).view(b, -1, n, d) - k = attn.norm_k(attn.k(context)).view(b, -1, n, d) - v = attn.v(context).view(b, -1, n, d) - k_img = attn.norm_k_img(attn.k_img(context_img)).view(b, -1, n, d) - v_img = attn.v_img(context_img).view(b, -1, n, d) - img_x = attention(q, k_img, v_img) - # compute attention - x = attention(q, k, v) - - x = x.flatten(2) - img_x = img_x.flatten(2) - - if len(adapter_proj.shape) == 4: - adapter_q = q.view(b * latents_num_frames, -1, n, d) - ip_key = self.ip_adapter_single_stream_k_proj(adapter_proj).view( - b * latents_num_frames, -1, n, d - ) - ip_value = self.ip_adapter_single_stream_v_proj(adapter_proj).view( - b * latents_num_frames, -1, n, d - ) - adapter_x = attention( - adapter_q, ip_key, ip_value, attn_mask=adapter_attn_mask - ) - adapter_x = adapter_x.view(b, q.size(1), n, d) - adapter_x = adapter_x.flatten(2) - elif len(adapter_proj.shape) == 3: - ip_key = self.ip_adapter_single_stream_k_proj(adapter_proj).view( - b, -1, n, d - ) - ip_value = self.ip_adapter_single_stream_v_proj(adapter_proj).view( - b, -1, n, d - ) - adapter_x = attention(q, ip_key, ip_value, attn_mask=adapter_attn_mask) - adapter_x = adapter_x.flatten(2) - - x = x + img_x + adapter_x * ip_scale - x = attn.o(x) - return x - - class PerceiverAttention(nn.Module): def __init__(self, *, dim, dim_head=64, heads=8): super().__init__() diff --git a/requirements.txt b/requirements.txt index 48f64e5..171c9de 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,4 +6,6 @@ peft>=0.15.0 sentencepiece>=0.2.0 protobuf pyloudnorm -gguf>=0.14.0 \ No newline at end of file +gguf>=0.14.0 +opencv-python +scipy \ No newline at end of file diff --git a/wanvideo/wan_video_vae.py b/wanvideo/wan_video_vae.py index 881d841..6b1101e 100644 --- a/wanvideo/wan_video_vae.py +++ b/wanvideo/wan_video_vae.py @@ -1022,6 +1022,7 @@ class VideoVAE_(nn.Module): def encode(self, x): self.clear_cache() ## cache + pbar = ProgressBar(z.shape[2]) t = x.shape[2] iter_ = 1 + (t - 1) // 4 @@ -1036,6 +1037,7 @@ class VideoVAE_(nn.Module): feat_cache=self._enc_feat_map, feat_idx=self._enc_conv_idx) out = torch.cat([out, out_], 2) + pbar.update(1) mu = self.conv1(out).chunk(2, dim=1)[0] mu = (mu - self.mean.to(mu)) * self.inv_std.to(mu)