Skip to content

Commit d9869be

Browse files
authored
Merge pull request #252 from mrhan1993:patch
After running for a long time, due to the loading problem of the model, the GPU memory and memory footprint will continue to increase, which will lead to the emergence of OOM. Add manual release logic while avoiding repeated loading of the model as much as possible. thanks for @PeakLee and his code: #245 (comment)
2 parents b2c2377 + 858eed6 commit d9869be

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

fooocusapi/worker.py

+12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from fooocusapi.task_queue import QueueTask, TaskQueue, TaskOutputs
1313

1414
worker_queue: TaskQueue = None
15+
last_model_name = None
1516

1617
def process_top():
1718
import ldm_patched.modules.model_management
@@ -118,6 +119,17 @@ def yield_result(_, imgs, tasks, extension='png'):
118119

119120
try:
120121
print(f"[Task Queue] Task queue start task, job_id={async_task.job_id}")
122+
# clear memory
123+
global last_model_name
124+
125+
if last_model_name is None:
126+
last_model_name = async_task.req_param.base_model_name
127+
if last_model_name != async_task.req_param.base_model_name:
128+
model_management.cleanup_models() # key1
129+
model_management.unload_all_models()
130+
model_management.soft_empty_cache() # key2
131+
last_model_name = async_task.req_param.base_model_name
132+
121133
worker_queue.start_task(async_task.job_id)
122134

123135
execution_start_time = time.perf_counter()

0 commit comments

Comments
 (0)