RioShiina's picture
fix(zerogpu): patch comfy/model_management.py for is_integrated compatibility in setup.py
849a9be verified
Raw History Blame Contribute Delete
8.1 kB
import os
import sys
import shutil
from core.settings import *
def move_and_overwrite(src, dst):
if os.path.isdir(src):
if os.path.exists(dst):
shutil.rmtree(dst)
shutil.move(src, dst)
elif os.path.isfile(src):
if os.path.exists(dst):
os.remove(dst)
shutil.move(src, dst)
def initialize_comfyui():
APP_DIR = sys.path[0]
COMFYUI_TEMP_DIR = "ComfyUI_temp"
print("--- Cloning ComfyUI Repository ---")
if not os.path.exists(COMFYUI_TEMP_DIR):
os.system(f"git clone --depth 1 https://github.com/comfy-Org/ComfyUI {COMFYUI_TEMP_DIR}")
print("βœ… ComfyUI repository cloned.")
else:
print("βœ… ComfyUI repository already exists.")
print(f"--- Merging ComfyUI from '{COMFYUI_TEMP_DIR}' to '{APP_DIR}' ---")
for item in os.listdir(COMFYUI_TEMP_DIR):
src_path = os.path.join(COMFYUI_TEMP_DIR, item)
dst_path = os.path.join(APP_DIR, item)
if item == '.git':
continue
move_and_overwrite(src_path, dst_path)
try:
shutil.rmtree(COMFYUI_TEMP_DIR)
print("βœ… ComfyUI merged and temporary directory removed.")
except OSError as e:
print(f"⚠️ Could not remove temporary directory '{COMFYUI_TEMP_DIR}': {e}")
print("--- Cloning third-party extensions for ComfyUI ---")
# 1. ComfyUI_IPAdapter_plus
ipadapter_plus_path = os.path.join(APP_DIR, "custom_nodes", "ComfyUI_IPAdapter_plus")
if not os.path.exists(ipadapter_plus_path):
os.system(f"git clone --depth 1 https://github.com/cubiq/ComfyUI_IPAdapter_plus.git {ipadapter_plus_path}")
print("βœ… ComfyUI_IPAdapter_plus extension cloned.")
else:
print("βœ… ComfyUI_IPAdapter_plus extension already exists.")
# 2. ComfyUI-InstantX-IPAdapter-SD3
ipadapter_plus_path = os.path.join(APP_DIR, "custom_nodes", "ComfyUI-InstantX-IPAdapter-SD3")
if not os.path.exists(ipadapter_plus_path):
os.system(f"git clone --depth 1 https://github.com/Slickytail/ComfyUI-InstantX-IPAdapter-SD3.git {ipadapter_plus_path}")
print("βœ… ComfyUI-InstantX-IPAdapter-SD3 extension cloned.")
else:
print("βœ… ComfyUI-InstantX-IPAdapter-SD3 extension already exists.")
# 3. ComfyUI-IPAdapter-Flux
ipadapter_flux_path = os.path.join(APP_DIR, "custom_nodes", "ComfyUI-IPAdapter-Flux")
if not os.path.exists(ipadapter_flux_path):
os.system(f"git clone --depth 1 https://github.com/Shakker-Labs/ComfyUI-IPAdapter-Flux.git {ipadapter_flux_path}")
print("βœ… ComfyUI-IPAdapter-Flux extension cloned.")
else:
print("βœ… ComfyUI-IPAdapter-Flux extension already exists.")
try:
print("--- [Setup] Applying PR #108 compatibility patch for ComfyUI-IPAdapter-Flux ---")
os.system(f"git -C {ipadapter_flux_path} fetch --depth 1 origin pull/108/head && git -C {ipadapter_flux_path} checkout -f FETCH_HEAD")
print("βœ… Successfully applied PR #108 compatibility patch.")
except Exception as e:
print(f"⚠️ Warning: Could not apply PR #108 compatibility patch for ComfyUI-IPAdapter-Flux: {e}")
# 4. comfyui-krea2-controlnet
krea2_controlnet_nodes_path = os.path.join(APP_DIR, "custom_nodes", "comfyui-krea2-controlnet")
if not os.path.exists(krea2_controlnet_nodes_path):
os.system(f"git clone --depth 1 https://github.com/facok/comfyui-krea2-controlnet.git {krea2_controlnet_nodes_path}")
print("βœ… comfyui-krea2-controlnet extension cloned.")
else:
print("βœ… comfyui-krea2-controlnet extension already exists.")
try:
print("--- [Setup] Applying PR #12 compatibility patch for comfyui-krea2-controlnet ---")
os.system(f"git -C {krea2_controlnet_nodes_path} fetch --depth 1 origin pull/12/head && git -C {krea2_controlnet_nodes_path} checkout -f FETCH_HEAD")
print("βœ… Successfully applied PR #12 compatibility patch.")
except Exception as e:
print(f"⚠️ Warning: Could not apply PR #12 compatibility patch for comfyui-krea2-controlnet: {e}")
# 5. comfyui-krea2edit
krea2edit_nodes_path = os.path.join(APP_DIR, "custom_nodes", "comfyui-krea2edit")
if not os.path.exists(krea2edit_nodes_path):
os.system(f"git clone --depth 1 https://github.com/lbouaraba/comfyui-krea2edit.git {krea2edit_nodes_path}")
print("βœ… comfyui-krea2edit extension cloned.")
else:
print("βœ… comfyui-krea2edit extension already exists.")
# 6. comfyui-anima-3-8B
anima_38b_nodes_path = os.path.join(APP_DIR, "custom_nodes", "comfyui-anima-3-8B")
if not os.path.exists(anima_38b_nodes_path):
os.system(f"git clone --depth 1 https://github.com/GumGum10/comfyui-anima-3-8B.git {anima_38b_nodes_path}")
print("βœ… comfyui-anima-3-8B extension cloned.")
else:
print("βœ… comfyui-anima-3-8B extension already exists.")
# 7. ComfyUI-Anima-2.9B-loraPatch
anima_29b_lora_path = os.path.join(APP_DIR, "custom_nodes", "ComfyUI-Anima-2.9B-loraPatch")
if not os.path.exists(anima_29b_lora_path):
os.system(f"git clone --depth 1 https://github.com/sparklingcoffee777/ComfyUI-Anima-2.9B-loraPatch.git {anima_29b_lora_path}")
print("βœ… ComfyUI-Anima-2.9B-loraPatch extension cloned.")
else:
print("βœ… ComfyUI-Anima-2.9B-loraPatch extension already exists.")
# 8. ComfyUI-Anima-3.8B-loraPatch
anima_38b_lora_path = os.path.join(APP_DIR, "custom_nodes", "ComfyUI-Anima-3.8B-loraPatch")
if not os.path.exists(anima_38b_lora_path):
os.system(f"git clone --depth 1 https://github.com/KitagawaCoding/ComfyUI-Anima-3.8B-loraPatch.git {anima_38b_lora_path}")
print("βœ… ComfyUI-Anima-3.8B-loraPatch extension cloned.")
else:
print("βœ… ComfyUI-Anima-3.8B-loraPatch extension already exists.")
print(f"βœ… Current working directory is: {os.getcwd()}")
# [ZeroGPU / Upstream ComfyUI Compatibility]
# Guard against ComfyUI PR #16746 is_integrated attribute check crash under ZeroGPU SimpleNamespace
try:
import torch
if hasattr(torch, "cuda") and hasattr(torch.cuda, "get_device_properties"):
_orig_gdp = torch.cuda.get_device_properties
def _safe_gdp(*args, **kwargs):
p = _orig_gdp(*args, **kwargs)
if not hasattr(p, "is_integrated"):
try:
p.is_integrated = False
except Exception:
pass
return p
torch.cuda.get_device_properties = _safe_gdp
except Exception:
pass
mm_path = os.path.join(APP_DIR, "comfy", "model_management.py")
if os.path.exists(mm_path):
try:
with open(mm_path, "r", encoding="utf-8") as f:
mm_code = f.read()
unsafe_check = "bool(torch.cuda.get_device_properties(device).is_integrated)"
safe_check = "bool(getattr(torch.cuda.get_device_properties(device), 'is_integrated', False))"
if unsafe_check in mm_code:
with open(mm_path, "w", encoding="utf-8") as f:
f.write(mm_code.replace(unsafe_check, safe_check))
print("πŸ›‘οΈ [Setup] Patched comfy/model_management.py for safe is_integrated attribute access.")
except Exception as e:
print(f"⚠️ [Setup] Failed to patch comfy/model_management.py: {e}")
import comfy.model_management
print("--- Environment Ready ---")
print("βœ… ComfyUI initialized with default attention mechanism.")
for dir_path in CATEGORY_TO_DIR_MAP.values():
os.makedirs(os.path.join(APP_DIR, dir_path), exist_ok=True)
os.makedirs(os.path.join(APP_DIR, INPUT_DIR), exist_ok=True)
os.makedirs(os.path.join(APP_DIR, OUTPUT_DIR), exist_ok=True)
print("βœ… All required model directories are present.")