This is similar to How to clear Cuda memory in PyTorch.
I keep getting the CUDA out of memory error, even though I have used torch.cuda.empty_cache()
as the first line of my code, after all the import
commands. Also, this error is quite random, and I see a lot of people facing this error on other forums. Isn't there a permanent solution to this?
I also do with torch.no_grad()
during evaluation. But this is irrelevant, since my code should begin training at least. Which it doesn't. I have obviously restarted the kernel multiple times and cleared out the cache as mentioned before. Any stored tensors in GPU memory should ideally be cleared by this process.
The error:
RuntimeError: CUDA out of memory. Tried to allocate 144.00 MiB (GPU 0; 11.00 GiB total capacity; 3.99 GiB already allocated; 93.40 MiB free; 4.04 GiB reserved in total by PyTorch)
After multiple attempts of restarting the kernel and clearing out cache, I inserted print(torch.cuda.is_available())
before training begins. It magically worked! Although I do not see any logic behind it.