Skip to content

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.

Delay dynamic pin cleanup until model destruction (#15183)

rattusde5625a64aba
diff --git a/comfy/model_patcher.py b/comfy/model_patcher.py
index e44322e72..6b698f767 100644
--- a/comfy/model_patcher.py
+++ b/comfy/model_patcher.py
@@ -558,12 +558,9 @@ class ModelPatcher:
new_multigpu_models = []
for mm in multigpu_models:
# clone main model, but bring over relevant props from existing multigpu clone
- n = self.clone()
+ n = self.clone(model_override=mm.get_clone_model_override())
n.load_device = mm.load_device
- n.backup = mm.backup
- n.object_patches_backup = mm.object_patches_backup
n.hook_backup = mm.hook_backup
- n.model = mm.model
n.is_multigpu_base_clone = mm.is_multigpu_base_clone
n.remove_additional_models("multigpu")
orig_additional_models: dict[str, list[ModelPatcher]] = comfy.patcher_extension.copy_nested_dicts(n.additional_models)
@@ -1758,6 +1755,9 @@ class ModelPatcherDynamic(ModelPatcher):
self.register_load_device(self.load_device)
self.non_dynamic_delegate_model = None
assert load_device is not None
+ if not hasattr(self.model, "dynamic_patchers"):
+ self.model.dynamic_patchers = set()
+ self.model.dynamic_patchers.add(id(self))
def register_load_device(self, device):
"""Ensure dynamic_pins has an entry for *device*.
@@ -1813,6 +1813,18 @@ class ModelPatcherDynamic(ModelPatcher):
def unpin_all_weights(self):
self.partially_unload_ram(1e32)
+ def __del__(self):
+ model = getattr(self, "model", None)
+ dynamic_patchers = getattr(model, "dynamic_patchers", None)
+ if dynamic_patchers is None or id(self) not in dynamic_patchers:
+ return
+ dynamic_patchers.discard(id(self))
+ try:
+ if not dynamic_patchers:
+ self.unpin_all_weights()
+ finally:
+ self.detach(unpatch_all=False)
+
def memory_required(self, input_shape):
#Pad this significantly. We are trying to get away from precise estimates. This
#estimate is only used when using the ModelPatcherDynamic after ModelPatcher. If you