diff --git a/ref2va_step2.py b/ref2va_step2.py index 7bb33c6..759853a 100644 --- a/ref2va_step2.py +++ b/ref2va_step2.py @@ -80,6 +80,18 @@ def move_to_cpu(obj): return obj +def drop_file_cache(filepath): + """ + 告知內核不需要快取該檔案的內容,釋放 Page Cache。 + """ + try: + with open(filepath, 'rb') as f: + fd = f.fileno() + os.posix_fadvise(fd, 0, 0, os.POSIX_FADV_DONTNEED) + print(f">> [Cache] Dropped page cache for {filepath}") + except Exception as e: + print(f">> [Cache] Failed to drop cache for {filepath}: {e}") + def main(): print("\n========== [REF2VA STAGE 2] Text Encoder 編碼 Prompt (單權重版) ==========") print(f">> [Config] 專案配置: {config_path}") @@ -128,6 +140,10 @@ def main(): vram_limit=18.0, ) + # 釋放權重檔的 Page Cache,避免 DRAM 雙倍佔用 + text_encoder_path = os.path.join(MODELS_DIR, "text_encoders/minimax-h3-text-encoder-nf4.safetensors") + drop_file_cache(text_encoder_path) + # pipeline 內建的 PromptEmbedder (ref2va 主路徑邏輯與官方完全一致) text_unit = MiniMaxH3Unit_PromptEmbedder() diff --git a/ref2va_step3.py b/ref2va_step3.py index d674099..a8c2462 100644 --- a/ref2va_step3.py +++ b/ref2va_step3.py @@ -171,6 +171,18 @@ class SaveLatentAudioVAE(torch.nn.Module): def decode(self, *args, **kwargs): return self.decode_audio(*args, **kwargs) +def drop_file_cache(filepath): + """ + 告知內核不需要快取該檔案的內容,釋放 Page Cache。 + """ + try: + with open(filepath, 'rb') as f: + fd = f.fileno() + os.posix_fadvise(fd, 0, 0, os.POSIX_FADV_DONTNEED) + print(f">> [Cache] Dropped page cache for {filepath}") + except Exception as e: + print(f">> [Cache] Failed to drop cache for {filepath}: {e}") + def main(): print("\n========== [REF2VA STAGE 3] DiT 去噪生成 (ref2va 權重, 單權重版) ==========") @@ -213,6 +225,10 @@ def main(): vram_limit=14.0, ) + # 釋放權重檔的 Page Cache,避免 DRAM 雙倍佔用 + dit_path = os.path.join(MODELS_DIR, "diffusion_models/minimax-h3-ref2va-nf4.safetensors") + drop_file_cache(dit_path) + seed = RUN_CONFIG.get("default_seed", 42) # 根據每個任務的 steps 決定是否載入 Turbo LoRA @@ -349,4 +365,4 @@ def main(): print(f"\n========== [REF2VA STAGE 3] 全部任務完成 ==========") if __name__ == "__main__": - main() \ No newline at end of file + main()