KohakuBlueleaf / a1111-sd-webui-lycoris

An extension for stable-diffusion-webui to load lycoris models.
Apache License 2.0
862 stars 117 forks source link

fix memory leak #20

Closed vladmandic closed 1 year ago

vladmandic commented 1 year ago

if lyco is NOT used, it will still try to allocate memory due to self.lyco_weights_backup = weights_backup, but those are then never released as there is no lyco applied.

fix is simple early-exit if no lyco model is loaded.

memray before patch shows (must run on cpu so memray can capture memory allocations):

390.238MB (0.71 %) lyco_apply_weights extensions/a1111-sd-webui-lycoris/lycoris.py:671

KohakuBlueleaf commented 1 year ago

This method has a problem

if you have: using lycos -> disable all lycos

this return will let the extension cannot restore the backup weights

vladmandic commented 1 year ago

how do you suggest to fix it? as it is, memory leak is pretty big, 390MB on each generate?

KohakuBlueleaf commented 1 year ago

@vladmandic How about this

KohakuBlueleaf commented 1 year ago

Wait a min I do something wrong

vladmandic commented 1 year ago

thanks for quick action. btw, this code is pretty much exactly the same as in lora which is built-in in a1111, so it leaks in both places. once you have your fix in place, i'll modify lora code in my repo to follow the same logic.

KohakuBlueleaf commented 1 year ago

@vladmandic I think the code now can resolve it

catboxanon commented 1 year ago

I'm pretty sure this change caused inference to become at least twice as slow now when a LoRA is in the prompt, as there's a brief pause that keeps occurring. If I disable this extension then speed returns to normal.

Edit: Can confirm as well that rolling back to ce584a0ff863de98233ee135dcc17f2fb44703c3 fixes the speed.

vladmandic commented 1 year ago

i was about to say the same - this drops the speed by ~40%, even with empty prompt. @KohakuBlueleaf ?

TingTingin commented 1 year ago

can confirm I had the same issue

RayHell commented 1 year ago

+1