I am training PyTorch deep learning models on a Jupyter-Lab notebook, using CUDA on a Tesla K80 GPU to train. While doing training iterations, the 12 GB of GPU memory are used. I finish training by saving the model checkpoint, but want to continue using the notebook for further analysis (analyze intermediate results, etc.).
However, these 12 GB continue being occupied (as seen from nvtop
) after finishing training. I would like to free up this memory so that I can use it for other notebooks.
My solution so far is to restart this notebook's kernel, but that is not solving my issue because I can't continue using the same notebook and its respective output computed so far.
The answers so far are correct for the Cuda side of things, but there's also an issue on the ipython side of things.
When you have an error in a notebook environment, the ipython shell stores the traceback of the exception so you can access the error state with %debug
. The issue is that this requires holding all variables that caused the error to be held in memory, and they aren't reclaimed by methods like gc.collect()
. Basically all your variables get stuck and the memory is leaked.
Usually, causing a new exception will free up the state of the old exception. So trying something like 1/0
may help. However things can get weird with Cuda variables and sometimes there's no way to clear your GPU memory without restarting the kernel.
For more detail see these references:
https://github.com/ipython/ipython/pull/11572
How to save traceback / sys.exc_info() values in a variable?