question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CUDA runs out of memory

See original GitHub issue

Has anyone run into a running out of GPU memory issue when running the imagine command? Below is the error I get.

RuntimeError: CUDA out of memory. Tried to allocate 2.00 MiB (GPU 0; 6.00 GiB total capacity; 4.47 GiB already allocated; 716.80 KiB free; 4.48 GiB reserved in total by PyTorch)

I tried to use both gc.collect() and torch.cuda.empty_cache() but neither worked.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

5reactions
afiaka87commented, Jan 23, 2021

Has anyone run into a running out of GPU memory issue when running the imagine command? Below is the error I get.

RuntimeError: CUDA out of memory. Tried to allocate 2.00 MiB (GPU 0; **6.00 GiB** total capacity; 4.47 GiB already allocated; 716.80 KiB free; 4.48 GiB reserved in total by PyTorch)

I tried to use both gc.collect() and torch.cuda.empty_cache() but neither worked.

Edit: Just realized you’re working with 6 GiB of VRAM. Have you considered using the google colab notebook instead? If it’s your first time they tend to give you a decent GPU with 16 GiB of memory. Not to mention it’s free (unless you’re using it alot).

You can check your GPU’s memory usage with nvidia’s CLI tool nvidia-smi which is provided with the cuda toolkit.

This unfortunately comes with the territory. The code runs best on a graphics card with 16 GiB. If you’ve got less than that, here are some parameters you can change to lower your VRAM usage.

decrease --image_width

This one lowers memory usage a lot. I’ve even done this on a 16 GiB colab instance just so I could run 64 hidden layers on a 256px image. Just keep in mind that 512 is a decent default. You’ll probably want to decrease in multiples of 8 I assume, but I dunno, maybe it doesn’t matter. At any rate the obvious tradeoff here is that you’ll get a less detailed output.

lower --batch_size (default is 4).

increase --gradient_accumulate_every (default is 4).

This will generate more images before calculating the loss and running backpropagation (the memory intensive bit). Each images loss is divided by the e.g. the default, 4, because we don’t want to punish the network without giving it a chance to change.

decrease --num_layers (default is 32)

This one is basically a requirement on a GPU with less than 16 GiB of memory. The default of 32 is meant for Colab users and is honestly a bit high considering the consumer GPU space doesn’t tend to have cards more than 8 GiB of vram. Lowering to 16 will get you below 8 GiB of vram but the results will be more abstract and silly. If you do decrease this value make sure you only do it as much as you need and no more, because more hidden layers seems to help quite a bit.

0reactions
alien-einsteincommented, Jun 28, 2021

Hello i get this strange error the hole time ,but there is still spaces on my gpu ram left . I tried using --numlayers 16 but the error is still the same. And i used --batch_size 2 but they both didn`t helped me

RuntimeError: CUDA out of memory. Tried to allocate 128.00 MiB (GPU 0; 4.00 GiB total capacity; 2.47 GiB already allocated; 0 bytes free; 2.49 GiB reserved in total by PyTorch)

Read more comments on GitHub >

github_iconTop Results From Across the Web

"RuntimeError: CUDA error: out of memory" - Stack Overflow
The error occurs because you ran out of memory on your GPU. One way to solve it is to reduce the batch size...
Read more >
Solving the “RuntimeError: CUDA Out of memory” error
Solving the “RuntimeError: CUDA Out of memory” error · Reduce the `batch_size` · Lower the Precision · Do what the error says ·...
Read more >
Solving "CUDA out of memory" Error - Kaggle
Solving "CUDA out of memory" Error · 1) Use this code to see memory usage (it requires internet to install package): · 2)...
Read more >
Resolving CUDA Being Out of Memory With Gradient ...
Implementing gradient accumulation and automatic mixed precision to solve CUDA out of memory issue when training big deep learning models ...
Read more >
CUDA out of memory despite available memory #485 - GitHub
At the End I just want to help you and the error says that you have not enough GPU Ram. You can isntall...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found