fix/workaround for potential memory leak (#2315)

unload old models, based on reference count <= 2
(in practise only noticed extra copies of JointTextEncoder, never KModel or IntegratedAutoencoderKL)
#2281 #2308 and others
This commit is contained in:
DenOfEquity 2024-11-14 22:05:54 +00:00 committed by GitHub
parent 98e0adcc78
commit 19a9a78c9b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 1 deletions

View File

@ -560,6 +560,11 @@ def unload_model_clones(model):
def free_memory(memory_required, device, keep_loaded=[], free_all=False):
# this check fully unloads any 'abandoned' models
for i in range(len(current_loaded_models) - 1, -1, -1):
if sys.getrefcount(current_loaded_models[i].model) <= 2:
current_loaded_models.pop(i).model_unload(avoid_model_moving=True)
if free_all:
memory_required = 1e30
print(f"[Unload] Trying to free all memory for {device} with {len(keep_loaded)} models keep loaded ... ", end="")

View File

@ -450,7 +450,7 @@ def reload_model_weights(sd_model=None, info=None, forced_reload=False):
def unload_model_weights(sd_model=None, info=None):
memory_management.unload_all_models()
pass
return
def apply_token_merging(sd_model, token_merging_ratio):