173 lines
5.2 KiB
Python
173 lines
5.2 KiB
Python
import os
|
|
import sys
|
|
import warnings
|
|
from typing import Any, Mapping, Sequence, Union
|
|
|
|
|
|
def find_path(name: str, path: str = None) -> str:
|
|
"""Recursively search parent folders until the named entry is found."""
|
|
if path is None:
|
|
path = os.getcwd()
|
|
|
|
if name in os.listdir(path):
|
|
path_name = os.path.join(path, name)
|
|
print(f"{name} found: {path_name}")
|
|
return path_name
|
|
|
|
parent_directory = os.path.dirname(path)
|
|
if parent_directory == path:
|
|
return None
|
|
|
|
return find_path(name, parent_directory)
|
|
|
|
|
|
def get_comfyui_path() -> str:
|
|
"""Return the configured ComfyUI path, preferring COMFYUI_PATH when set."""
|
|
comfyui_path = os.environ.get("COMFYUI_PATH")
|
|
if comfyui_path:
|
|
return comfyui_path
|
|
return find_path("ComfyUI")
|
|
|
|
|
|
def add_comfyui_directory_to_sys_path() -> None:
|
|
"""Add the ComfyUI checkout to sys.path."""
|
|
comfyui_path = get_comfyui_path()
|
|
if comfyui_path is not None and os.path.isdir(comfyui_path):
|
|
if comfyui_path in sys.path:
|
|
sys.path.remove(comfyui_path)
|
|
sys.path.insert(0, comfyui_path)
|
|
print(f"'{comfyui_path}' added to sys.path")
|
|
|
|
|
|
def add_extra_model_paths() -> None:
|
|
"""Load ComfyUI extra model paths configuration when available."""
|
|
try:
|
|
from main import load_extra_path_config
|
|
except ImportError:
|
|
print(
|
|
"Could not import load_extra_path_config from main.py. Looking in utils.extra_config instead."
|
|
)
|
|
from utils.extra_config import load_extra_path_config
|
|
|
|
extra_model_paths = find_path("extra_model_paths.yaml")
|
|
if extra_model_paths is not None:
|
|
load_extra_path_config(extra_model_paths)
|
|
else:
|
|
print("Could not find the extra_model_paths config file.")
|
|
|
|
|
|
def bootstrap_comfyui_runtime() -> None:
|
|
"""Mirror the allocator-related ComfyUI startup steps before torch import."""
|
|
add_comfyui_directory_to_sys_path()
|
|
|
|
import comfy.options
|
|
|
|
comfy.options.enable_args_parsing()
|
|
|
|
from comfy.cli_args import args
|
|
|
|
if os.name == "nt":
|
|
os.environ["MIMALLOC_PURGE_DELAY"] = "0"
|
|
|
|
if args.default_device is not None:
|
|
default_dev = args.default_device
|
|
devices = list(range(32))
|
|
devices.remove(default_dev)
|
|
devices.insert(0, default_dev)
|
|
devices = ",".join(map(str, devices))
|
|
os.environ["CUDA_VISIBLE_DEVICES"] = str(devices)
|
|
os.environ["HIP_VISIBLE_DEVICES"] = str(devices)
|
|
|
|
if args.cuda_device is not None:
|
|
os.environ["CUDA_VISIBLE_DEVICES"] = str(args.cuda_device)
|
|
os.environ["HIP_VISIBLE_DEVICES"] = str(args.cuda_device)
|
|
os.environ["ASCEND_RT_VISIBLE_DEVICES"] = str(args.cuda_device)
|
|
|
|
if args.oneapi_device_selector is not None:
|
|
os.environ["ONEAPI_DEVICE_SELECTOR"] = args.oneapi_device_selector
|
|
|
|
if args.deterministic and "CUBLAS_WORKSPACE_CONFIG" not in os.environ:
|
|
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8"
|
|
|
|
import cuda_malloc
|
|
|
|
if "rocm" in cuda_malloc.get_torch_version_noimport():
|
|
os.environ["OCL_SET_SVM_SIZE"] = "262144"
|
|
|
|
|
|
def cleanup_comfyui_runtime(unload_models: bool | None = None) -> None:
|
|
"""Best-effort cleanup for embedded or repeated generated-script execution."""
|
|
import gc
|
|
|
|
def run_cleanup_hook(name: str, should_run: bool = True) -> None:
|
|
if not should_run or not hasattr(model_management, name):
|
|
return
|
|
cleanup_fn = getattr(model_management, name)
|
|
try:
|
|
cleanup_fn()
|
|
except Exception as exc:
|
|
warnings.warn(
|
|
f"ComfyUI cleanup hook {name} failed during teardown: {exc}",
|
|
RuntimeWarning,
|
|
stacklevel=2,
|
|
)
|
|
|
|
should_unload = unload_models
|
|
if should_unload is None:
|
|
should_unload = os.environ.get("COMFYUI_TOPYTHON_UNLOAD_MODELS", "").lower() in {
|
|
"1",
|
|
"true",
|
|
"yes",
|
|
"on",
|
|
}
|
|
|
|
try:
|
|
import comfy.model_management as model_management
|
|
except ModuleNotFoundError:
|
|
gc.collect()
|
|
return
|
|
|
|
run_cleanup_hook("cleanup_models_gc")
|
|
run_cleanup_hook("unload_all_models", should_run=should_unload)
|
|
run_cleanup_hook("soft_empty_cache")
|
|
gc.collect()
|
|
|
|
|
|
def import_custom_nodes() -> None:
|
|
"""Initialize ComfyUI custom nodes in the exporter runtime."""
|
|
comfyui_path = get_comfyui_path()
|
|
if comfyui_path and comfyui_path not in sys.path:
|
|
sys.path.insert(0, comfyui_path)
|
|
|
|
import asyncio
|
|
import execution
|
|
from nodes import init_extra_nodes
|
|
|
|
if comfyui_path in sys.path:
|
|
sys.path.remove(comfyui_path)
|
|
sys.path.insert(0, comfyui_path)
|
|
|
|
import server
|
|
|
|
loop = asyncio.new_event_loop()
|
|
asyncio.set_event_loop(loop)
|
|
server_instance = server.PromptServer(loop)
|
|
execution.PromptQueue(server_instance)
|
|
asyncio.run(init_extra_nodes())
|
|
|
|
|
|
def get_node_class_mappings() -> dict:
|
|
"""Load ComfyUI node mappings on demand."""
|
|
add_comfyui_directory_to_sys_path()
|
|
from nodes import NODE_CLASS_MAPPINGS
|
|
|
|
return NODE_CLASS_MAPPINGS
|
|
|
|
|
|
def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:
|
|
"""Return a sequence or mapping result item by index."""
|
|
try:
|
|
return obj[index]
|
|
except KeyError:
|
|
return obj["result"][index]
|