AutoEncoderKL isn't releasing memory
Describe the bug
Using the encode function to get the latent_dist product returns the proper object but I can't seem to be able to get rid of it once it's introduced, del the variable isn't working, accelerator.free_memory() not working, gc.collect etc.
Perhaps I'm doing something wrong or is it a bug?
Reproduction
get a latent and try to delete it
Logs
1524 3047.7 MiB 3047.7 MiB 1 @profile(stream=fp)
1525 def collect_data():
1526 3047.7 MiB 0.0 MiB 1 train_dataset = LatentsDataset((), ())
1527 3047.7 MiB 0.0 MiB 1 counter = 1
1528 3047.7 MiB 0.0 MiB 1 index = 1
1529 3286.8 MiB 239.1 MiB 1 for batch in train_dataloader: #tqdm(, desc="Caching latents"):
1530 5959.7 MiB 0.0 MiB 2 with torch.no_grad():
1531 3286.8 MiB 0.0 MiB 1 batch["pixel_values"] = batch["pixel_values"].to(accelerator.device, non_blocking=True, dtype=weight_dtype)
1532 3286.8 MiB 0.0 MiB 1 batch["input_ids"] = batch["input_ids"].to(accelerator.device, non_blocking=True)
1533
1534 5633.7 MiB 2346.9 MiB 1 cached_latent = vae.encode(batch["pixel_values"]).latent_dist
1535 5633.7 MiB 0.0 MiB 1 if args.train_text_encoder:
1536 5633.7 MiB 0.0 MiB 1 cached_text_enc = batch["input_ids"]
1537 else:
1538 cached_text_enc = text_encoder(batch["input_ids"])[0]
1539 5633.7 MiB 0.0 MiB 1 train_dataset.add_latent(tuple([cached_latent]), tuple([cached_text_enc]))
1540 5633.7 MiB 0.0 MiB 1 print(gc.is_tracked(cached_latent))
1541
1542 5633.7 MiB 0.0 MiB 1 del batch
1543 5633.7 MiB 0.0 MiB 1 del cached_latent
1544 5633.7 MiB 0.0 MiB 1 del cached_text_enc
1545 5959.7 MiB 326.0 MiB 1 gc.collect()
1546 5959.7 MiB 0.0 MiB 1 return
System Info
pytorch 1.12 python 3.10
Try del'ing the vae as well? It feels like the 2 gigs is the VAE weights. The encoded latents are tiny by comparison (less than 100k for a 512x512 image).
Tried to delete and recreate vae every 250 batches, didn't help
RAM consumption grows to 500+gb if you want to process large amount of images
Try del'ing the vae as well? It feels like the 2 gigs is the VAE weights. The encoded latents are tiny by comparison (less than 100k for a 512x512 image).
unfortunately deleting the VAE variable doesn't release the RAM either (it's not in this log obviously), that includes del it and collecting and even going into the globals to delete any mentions of it.
Right now I'm trying to use multiprocess to keep it in a subprocess.
Ahh I see! Thanks for testing this! What's your setup for running the encode? Is this CPU or GPU? From the discussion I'm guessing that running vae.encode() on a GPU (probably Nvidia?) using accelerate is allocating around 2GB of CPU RAM on each invocation and that allocation is never released. Is that an accurate description?
Do you have a minimal testcase to reproduce this? (e.g. ... accelerate setup ... for i in range(500): vae.encode(someimage))
Ahh I see! Thanks for testing this! What's your setup for running the encode? Is this CPU or GPU? From the discussion I'm guessing that running
vae.encode()on a GPU (probably Nvidia?) using accelerate is allocating around 2GB of CPU RAM on each invocation and that allocation is never released. Is that an accurate description?Do you have a minimal testcase to reproduce this? (e.g. ... accelerate setup ... for i in range(500): vae.encode(someimage))
You are correct, setup is using accelerate with an nvidia GPU, I did try accelerate.free_memory() but that doesn't release it.
Something like what you described would work to reproduce it, but it's part of a larger code currently. I'm not sure it allocates 2GB every iteration but it does keep on growing with each iteration.
Here's the code: https://github.com/devilismyfriend/StableTuner
Unrelated to diffusers
@devilismyfriend was this issue resolved? I'm also finding vae.encode is not releasing memory. Any tips you have for fixing this would be very welcome!