From 7dee33e3343ab88c734fee003f4a97950b957f6d Mon Sep 17 00:00:00 2001 From: mikehu Date: Wed, 4 Nov 2026 01:54:33 +0800 Subject: [PATCH] Refactor: add skip-all checks, fix PIL image loading and cfg_scale parameter, and enable xformers attention --- config_template_ref2va.py | 6 ++++++ ref2va_step1.py | 19 ++++++++++++++++--- ref2va_step2.py | 8 ++++++++ ref2va_step3.py | 14 ++++++++++++-- run_refh3.sh | 1 + 5 files changed, 43 insertions(+), 5 deletions(-) diff --git a/config_template_ref2va.py b/config_template_ref2va.py index 7d42cbb..da12495 100644 --- a/config_template_ref2va.py +++ b/config_template_ref2va.py @@ -51,6 +51,12 @@ RUN_CONFIG = { "default_num_inference_steps": 12, } +# --- 額外 LoRA (NSFW anatomy) --- +EXTRA_LORAS = [ + ("Mini_Dick_Fix_V1.safetensors", 0.55), + ("MysticXXX_MMH3-V4.safetensors", 0.7), +] + # ------------------------------------------------------------------------------ # 🎯 任務配置 # ------------------------------------------------------------------------------ diff --git a/ref2va_step1.py b/ref2va_step1.py index db50ebb..3e9c152 100644 --- a/ref2va_step1.py +++ b/ref2va_step1.py @@ -38,6 +38,7 @@ import sys import gc import hashlib import importlib.util +from PIL import Image # ============================================================================== # 動態加載專案配置 (與 fl2va 一致: cd 到專案目錄後讀 ./config.py) @@ -58,11 +59,13 @@ RUN_CONFIG = config.RUN_CONFIG def get_enabled_tasks(): - return config.get_enabled_tasks() + return [idx for idx, task in TASK_REGISTRY.items() if task.enabled] def load_task_references(idx): - return config.load_task_references(idx) + if idx not in TASK_REGISTRY: + raise KeyError(f"Task index {idx} not found in TASK_REGISTRY") + return TASK_REGISTRY[idx].references import torch @@ -135,6 +138,14 @@ def main(): torch.cuda.empty_cache() gc.collect() + # 檢查所有 result1_ref_*.pt 是否已存在,若全部存在則跳過整個 Step 1 + all_ref_pts_exist = all(os.path.exists(f"result1_ref_{idx:02d}.pt") for idx in enabled_ids) + if all_ref_pts_exist: + print(">> [SKIP] 所有 result1_ref_*.pt 已存在,跳過 Step 1。") + print(f"\n========== [REF2VA STAGE 1] 完成 {len(enabled_ids)}/{len(enabled_ids)} 個任務 (全部跳過) ==========") + print(">> 下一步: ref2va_step2.py (text encoder)") + return + # 單一權重: video VAE (1.6GB) 全駐 GPU (1.6G 對 24GB 完全沒壓力, # 全 cuda 避免 layer-wise hotload 拖慢 N 張參考圖的編碼) vram_config = { @@ -237,7 +248,9 @@ def main(): short_edge = getattr(task, "ref_image_short_edge", 2048) v_shape, a_shape, nf = latent_shapes(task) - references = [{"type": "image", "image": img} for img in refs] + # 載入圖片為 PIL Image 物件 (pipeline 預期輸入) + refs_images = [Image.open(img_path).convert("RGB") for img_path in refs] + references = [{"type": "image", "image": img} for img in refs_images] print(f"\n>> [Task {idx}] 編碼 {len(refs)} 張參考圖 (短邊 {short_edge}, " f"目標 {task.width}x{task.height}, {nf}f)..." + (f" (原 {task.num_frames}f → snap {nf}f)" if nf != task.num_frames else "")) diff --git a/ref2va_step2.py b/ref2va_step2.py index 759853a..f3f0da8 100644 --- a/ref2va_step2.py +++ b/ref2va_step2.py @@ -107,6 +107,14 @@ def main(): print(f">> [Error] Task {idx} 缺少 result1_ref_{idx:02d}.pt, 請先執行 step1。") sys.exit(1) + # 檢查所有 result2_text_*.pt 是否已存在,若全部存在則跳過整個 Step 2 + all_text_pts_exist = all(os.path.exists(f"result2_text_{idx:02d}.pt") for idx in enabled_ids) + if all_text_pts_exist: + print(">> [SKIP] 所有 result2_text_*.pt 已存在,跳過 Step 2。") + print(f"\n========== [REF2VA STAGE 2] 完成 {len(enabled_ids)}/{len(enabled_ids)} 個任務 (全部跳過) ==========") + print(">> 下一步: ref2va_step3.py (DiT 去噪)") + return + torch.cuda.empty_cache() gc.collect() diff --git a/ref2va_step3.py b/ref2va_step3.py index ace0a6a..999b0d5 100644 --- a/ref2va_step3.py +++ b/ref2va_step3.py @@ -196,6 +196,16 @@ def main(): print(f">> [Error] Task {idx} 缺少前置 pt 檔案。") sys.exit(1) + # 檢查所有 result3_video/audio_*.pt 是否已存在,若全部存在則跳過整個 Step 3 + all_latents_exist = all( + os.path.exists(f"result3_video_{idx:02d}.pt") and os.path.exists(f"result3_audio_{idx:02d}.pt") + for idx in enabled_ids + ) + if all_latents_exist: + print(">> [SKIP] 所有 result3_video/audio_*.pt 已存在,跳過 Step 3。") + print(f"\n========== [REF2VA STAGE 3] 完成 {len(enabled_ids)}/{len(enabled_ids)} 個任務 (全部跳過) ==========") + return + torch.cuda.empty_cache() gc.collect() @@ -324,7 +334,7 @@ def main(): num_frames=task.num_frames, num_inference_steps=steps, seed=seed, - **({'guidance_scale': 1.0} if use_turbo_guidance else {}), + **({'cfg_scale': 1.0} if use_turbo_guidance else {}), ) # --- 任務完成:抓取本次任務的 VRAM 峰值 --- @@ -368,4 +378,4 @@ def main(): print(f"\n========== [REF2VA STAGE 3] 全部任務完成 ==========") if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/run_refh3.sh b/run_refh3.sh index 7d16c25..ea70724 100755 --- a/run_refh3.sh +++ b/run_refh3.sh @@ -32,6 +32,7 @@ BASE_DIR="/home/mikehu/data/AI/minimax-h3" PROJECT_DIR="$BASE_DIR/outputs/$PROJECT_NAME" VENV_PYTHON="$BASE_DIR/.venv/bin/python" export PYTORCH_CUDA_ALLOC_CONF="max_split_size_mb:256,garbage_collection_threshold:0.8" +export DIFFSYNTH_ATTENTION_IMPLEMENTATION=torch echo "----------------------------------------------------------------" echo "🚀 Initializing MiniMax-H3 REF2VA Project: $PROJECT_NAME"