Repository navigation
Open
Description
Describe the bug
When using enable_model_cpu_offload on StableDiffusionXLPipeline, each consecutive call takes more and more RAM. Also, after deleting pipe not all memory is freed
Reproduction
import gc
import torch
from diffusers import StableDiffusionXLPipeline
import psutil
def print_memory_usage(step):
print(f"{step} - Memory usage: {psutil.virtual_memory().used / (1024 ** 3):.2f} GB")
def clear_memory():
torch.cuda.empty_cache()
gc.collect()
def inference():
print_memory_usage("Before loading pipeline")
# Load the pipeline
pipe = StableDiffusionXLPipeline.from_pretrained(
"4spaces/RealVisXL_V4.0",
torch_dtype=torch.float16,
variant="fp16",
)
pipe.enable_model_cpu_offload()
print_memory_usage("After loading pipeline")
# Move the model to CPU
pipe.to("cpu")
print_memory_usage("After moving model to CPU")
# Generate an image and clear memory
for i in range(3):
_ = pipe("horse")
print_memory_usage(f"After generating {i + 1}")
clear_memory()
# Delete the pipeline
del pipe
clear_memory()
print_memory_usage("After deleting pipeline")
inference()
clear_memory()
print_memory_usage("After inference")Logs
Before loading pipeline - Memory usage: 0.71 GB
Loading pipeline components...: 100%|██████████████████████████████████| 7/7 [00:03<00:00, 1.98it/s]
After loading pipeline - Memory usage: 0.80 GB
After moving model to CPU - Memory usage: 0.80 GB
100%|████████████████████████████████████████████████████████████████| 50/50 [00:39<00:00, 1.27it/s]
After generating 1 - Memory usage: 7.87 GB
100%|████████████████████████████████████████████████████████████████| 50/50 [00:37<00:00, 1.34it/s]
After generating 2 - Memory usage: 9.62 GB
100%|████████████████████████████████████████████████████████████████| 50/50 [00:37<00:00, 1.35it/s]
After generating 3 - Memory usage: 9.87 GB
After deleting pipeline - Memory usage: 9.37 GB
After inference - Memory usage: 7.35 GBSystem Info
- 🤗 Diffusers version: 0.28.0.dev0
- Platform: Clear Linux OS - Linux-6.1.71-427.aws-x86_64-with-glibc2.38
- Running on a notebook?: No
- Running on Google Colab?: No
- Python version: 3.11.0
- PyTorch version (GPU?): 2.1.1+cu121 (True)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Huggingface_hub version: 0.23.0
- Transformers version: 4.40.2
- Accelerate version: 0.30.1
- PEFT version: 0.11.0
- Bitsandbytes version: 0.42.0
- Safetensors version: 0.4.3
- xFormers version: 0.0.23
- Accelerator: Tesla T4, 15360 MiB VRAM
- Using GPU in script?: yes
- Using distributed or parallel set-up in script?: no
Who can help?
Reacted by max-fofanov, anisimovaanfisa, gazdovsky, Aleksei Balchunas, Andrey Pagin, Sergey Zelenovsky and Aryan V S
Activity
sayakpaul commentedon May 17, 2024
I would think that would be the case because them modules are loaded and offloaded to CPU as in when needed. Ccing @pcuenca @SunMarc too as they might have additional insights into this.
max-fofanov commentedon May 17, 2024
I should also mention, that after the first few calls the RAM starts raising very slowly, but still fails after 30-40 calls
sayakpaul commentedon May 17, 2024
On a shared machine and A100, I get this:
I extended the number of runs to 50 to get a more reasonable estimate and also commented the
to("cpu")call (not sure why it's there). The numbers seem reasonable to me. The initial spike that we see in the logs above could very likely be because of the shared nature of the machine I am using.This is interesting. The increase is small but nonetheless it's there and it has no reason to be there.
max-fofanov commentedon May 17, 2024
So this is intended behavior? I can see, that you've also experienced an increase, and although it looks small on A100, this is very painful when using a single T4 and 16 GB RAM
mtolgacangoz commentedon May 17, 2024
I tried in Colab T4 free. ~1 GB fluctuation during 50 inference 🤔:
Also, I couldn't clear all the RAM either at the end.
bleshik commentedon May 20, 2024
Is there any workaround for that by any chance?
gazdovsky commentedon May 21, 2024
As a workaround you can try this before
torch.cuda.empty_cache():github-actions commentedon Sep 14, 2024
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.
Please note that issues that do not follow the contributing guidelines are likely to be ignored.
github-actions commentedon Dec 14, 2024
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.
Please note that issues that do not follow the contributing guidelines are likely to be ignored.