InvokeAI icon indicating copy to clipboard operation
InvokeAI copied to clipboard

img2img does not unload VRAM once images are finished generating and it continues to climb?

Open benedlore opened this issue 4 years ago • 15 comments

Describe your environment

  • GPU: cuda RTX 3090
  • VRAM: 24GB
  • CPU arch: AMD 5800x
  • OS: Windows 10
  • Python: Anaconda run through powershell
  • Branch: main

Describe the bug I've based a bot of mine on this repo and noticed over time eventually the bot will start running out of memory for operations that previously it could do just fine (running on a 3090). I took a look and it was idling at ~23GB/24GB used when no image generation was happening. I restarted it and did a couple basic tests and noticed that if I continually to text to image generations, the VRAM increases to a point and then stays there indefinitely, but with img2img it increases much further and does not unload after it finishes generating the images. I would expect the VRAM to go up during the generation, but then go back down after. Right now it goes up but never comes back down.

I was hoping someone could help me figure out how to make it unload whatever memory it needed for that specific operation once it finishes generating images, but while keeping the model loaded.

Very basic observations:

  • GPU Idling no bot loaded: 0.1GB/24GB VRAM used
  • Model loaded (but no generations made): 4.9GB/24GB VRAM used
  • After any number of text to image generations: 7.9GB/24GB VRAM used

Then, img2img causes it to additive load more and more until it is sitting there at almost 100% VRAM usage even when it is not generating anything. I would like to figure out how to get it back down to that 7.9GB or 4.9GB, because I do need a small amount of VRAM off the top for other bot functionality, and when it is idling at 99% VRAM usage, it makes that impossible. While it is generating an img2img operation, I don't mind if it is using all available memory, but it seems like something is wrong since it never goes back down, even after the image generation is completed.

To Reproduce For me, I just use img2img and/or other operations while monitoring VRAM in Task Manager.

Expected behavior I would expect the model to load, and the VRAM usage increases during the img2img operation, but once finished generating the images, it goes back down to a base level with just the model loaded again. Right now, it stays very high, until it is essentially using all available VRAM, even when not generating images.

benedlore avatar Sep 08 '22 16:09 benedlore

I am noticing the same thing happening on an M1 Max 32gb, and just watching it increase over time in the Activity Monitor. Eventually it hits the swap file and things tend to slow down quite a bit.

milezzz avatar Sep 08 '22 17:09 milezzz

I imagine for most people who use it a little a bit and then close it, they may never run into or notice this issue (or for people who only use txt2img), but for me this is a dealbreaker since the purpose of my bot is to remain online 24/7 and with light usage of img2img it quickly ends up using all of the 3090's VRAM.

benedlore avatar Sep 08 '22 18:09 benedlore

This seems to be the case with me as well. Was not sure what was eating the ram but leaving dream open and generating multiple batches of images without closing does lead to paging on my 64 gig m1 max.

crs117 avatar Sep 09 '22 14:09 crs117

Also happening to me. I max out my 3080's 12G fast and have to kill the program and restart. Is there no way to clear memory without also clearing the model from VRAM?

joshfaust avatar Sep 10 '22 01:09 joshfaust

same problem here

cvar66 avatar Sep 10 '22 10:09 cvar66

I'll see if I can reproduce the issue here so that I can track down where the memory leak is. Has anyone tried img2img on the development branch or one of the optimization branches?

lstein avatar Sep 10 '22 13:09 lstein

I'll jump on the dev branch to play around to see if I experience the same issues.

joshfaust avatar Sep 10 '22 16:09 joshfaust

I've done a lot of testing on the development branch using a CUDA VT100 system and I can't reproduce a memory leak on my system (either with txt2img or img2img). To help track this down I'm committing an improvement to the CUDA VRAM memory reporting on the development branch. It now shows total VRAM currently used by torch in addition to peak VRAM.

Interestingly, nvidia-smi reports higher numbers for VRAM usage than the torch memory metrics do. This may be related to the issue discussed here: https://github.com/pytorch/pytorch/issues/37250 . However, I couldn't see leakage with nvidia-smi either.

lstein avatar Sep 11 '22 14:09 lstein

I've done a lot of testing on the development branch using a CUDA VT100 system and I can't reproduce a memory leak on my system

Can you describe the proper behavior you saw with your VRAM usage during your tests? Did the VRAM usage go up during an img2img operation, but then return down again to a baseline level afterwards (only with the model loaded)? I am just curious of the specific differences between what I am observing vs how it is supposed to work, might uncover a clue

benedlore avatar Sep 11 '22 17:09 benedlore

That's right. With the new diagnostics I added, I get the line:

>>   Max VRAM used for this generation: 3.35G. Current VRAM utilization:2.21G

The first number comes from torch.cuda.max_memory_allocated(), and the second from torch.cuda.memory_allocated(). If I interpret this correctly, the baseline VRAM usage is 2.21, which is basically the space the model occupies. It goes up to 3.35 during img2img, and then drops back to baseline. I've run img2img up to fifty times and don't see it go up from 2.21 G. Come to think of it, do you see the memory leak when you generate images with -n50, or does it have to be spread across multiple commands with different prompt?

lstein avatar Sep 11 '22 22:09 lstein

I'm sure they were different prompts with different images used as inputs, but I'll do another test later specifically testing this

benedlore avatar Sep 12 '22 16:09 benedlore

Man is this code changing fast. Glad to see the up-scaling working again for M1 macs.

Pardon my terminology, new to this group.

I am testing on the latest dev and I see @lstein's memory changes but it appears that pyTorch does not provide the same memory capabilities for mps as it does for cuda so I am unable to get those settings working and previously (before the latest code) it would print vram 0.0 on macs anyway.

To be fair this bug may have been patched but I ran into it after leaving the dream script running for a long time and having generated probably 20-50 pictures. At that point I was generating 1024X768 (the up-scaling was not originally working). I will try it out again and see if I can get a reproducible way to trigger this for M1 macs. I was originally using s=100 as well.

crs117 avatar Sep 12 '22 17:09 crs117

Not the OP, but I cannot currently cannot reproduce the issue I ran into at some point last week.

Current Dev branch seems to be pretty stable. Generation seems to use M1 GPU now and Real-ESRGAN seems to be working again (even though in this branch those options are not yet available in the web interface like another branch I was testing).

I am on a M1 Max with 64 GB but at some point I was hitting the swap file while running dream.py, but I think that was on the main branch from some point last week.

If run into this issue again, I will update.

crs117 avatar Sep 12 '22 21:09 crs117

I'm afraid that real-esrgan is still not 100% on Macs. We're seeing ~40s render times when making library calls, and ~2s render times using the precompiled real-esrgan binary. See #390 for discussion.

lstein avatar Sep 12 '22 21:09 lstein

It does not seem to be using the gpu on M1 so yeah it takes a while but at least it works. I will look at that discussion to see if I can help or at least test.

Thanks for your work, this tool is incredible.

crs117 avatar Sep 12 '22 21:09 crs117