-
Notifications
You must be signed in to change notification settings - Fork 27.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use less RAM when creating models #11958
Conversation
class ReplaceHelper: | ||
def __init__(self): | ||
self.replaced = [] | ||
|
||
def replace(self, obj, field, func): | ||
original = getattr(obj, field, None) | ||
if original is None: | ||
return None | ||
|
||
self.replaced.append((obj, field, original)) | ||
setattr(obj, field, func) | ||
|
||
return original | ||
|
||
def restore(self): | ||
for obj, field, original in self.replaced: | ||
setattr(obj, field, original) | ||
|
||
self.replaced.clear() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be implemented using contextlib.ExitStack
too.
If someone who's low on system RAM can try this and report back it would help me. |
Tried it out with 24Gb of ram and i can finally load SDXL without the need of a swap file in windows. (before I had OOM errors on the cpu side.. loading was taking 15-20 GB of ram) so it seems to work better. |
Merged this into dev locally and it fixed my issue that was the same as #12081
Python version: 3.11.4 VAE: https://huggingface.co/madebyollin/sdxl-vae-fp16-fix |
Came here due to having only 16G RAM and wanted to try out SDXL. Ubuntu 20.04.6 LTS I was able to load SDXL on 1.5.1 as well but in my observation RAM usage is slightly better during and after the loading of the models. On 1.5.1 more RAM stayed occupied after the model was loaded. Total duration is random and depends on the amount of swapped out pages. Will run some more tests in a headless environment later, which should yield an additional 1.5G of free RAM on my system. EDIT: Made some tests in headless mode (meaning I have stopped the display-manager service of Ubuntu and access the webui from another machine). Seems that the additional RAM available in this mode is just enough to get the model loading quickly (🥳): First load: Second load: I also observe improved loading time for SD 1.5 models, especially right after starting the webui. Great PR! Will stay on this branch until it has been merged. |
I have just tried this on an EC2 with 16GB RAM and no swapfile (my usual environment). It crashes on switching to the SDXL 1.0 base model. On restart the model shows as selected. On then selecting the usual default model, it crashes with what appears to be a high memory use. Uncertain if more improvements are to come, or the expectation of more than 16GB RAM now needs to be made crystal clear. |
this def does not use less ram, takes me around 4 min to generate 1 image on 25 steps, using 14 gig ram when doing so and then once generation stops it still shows that it is using 14 gig. never had issues with any other versions. how can I fix this on my side? |
Description
meta
device when creating a model, and deleting them from the state_dict one by one as the parameters are loaded into model byload_state_dict
--disable-model-loading-ram-optimization
disables itChecklist: