Skip to content

Commit

Permalink
feat: aggressive_unloading for more regular garbage collection
Browse files Browse the repository at this point in the history
ComfyUI's default mode of running (`main.py`) includes calls to `cleanup_models(...)` and `soft_empty_cache(...)` on a timer. I suspect issues that have arisen lately are rooted in the fact that horde-engine does not currently do something similar. I am adding this (default on) option to the `HordeLib` class to call these after every pipeline run.
  • Loading branch information
tazlin committed Aug 25, 2024
1 parent 3c106b4 commit 29b4e81
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
8 changes: 8 additions & 0 deletions hordelib/comfy_horde.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ def __init__(
self,
*,
comfyui_callback: typing.Callable[[str, dict, str], None] | None = None,
aggressive_unloading: bool = True,
) -> None:
"""Initialise the Comfy_Horde object.
Expand All @@ -453,6 +454,7 @@ def __init__(
self._load_custom_nodes()

self._comfyui_callback = comfyui_callback
self.aggressive_unloading = aggressive_unloading

def _set_comfyui_paths(self) -> None:
# These set the default paths for comfyui to look for models and embeddings. From within hordelib,
Expand Down Expand Up @@ -829,6 +831,12 @@ def _run_pipeline(
inference.execute(pipeline, self.client_id, {"client_id": self.client_id}, valid[2])
except Exception as e:
logger.exception(f"Exception during comfy execute: {e}")
finally:
if self.aggressive_unloading:
global _comfy_cleanup_models
logger.debug("Cleaning up models")
_comfy_cleanup_models(False)
_comfy_soft_empty_cache(True)

stdio.replay()

Expand Down
2 changes: 2 additions & 0 deletions hordelib/horde.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,12 @@ def __init__(
self,
*,
comfyui_callback: Callable[[str, dict, str], None] | None = None,
aggressive_unloading: bool = False,
):
if not self._initialised:
self.generator = Comfy_Horde(
comfyui_callback=comfyui_callback if comfyui_callback else self._comfyui_callback,
aggressive_unloading=aggressive_unloading,
)
self.__class__._initialised = True

Expand Down

0 comments on commit 29b4e81

Please sign in to comment.