Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytorch delete model from gpu

I want to make a cross validation in my project based on Pytorch. And I didn't find any method that pytorch provided to delete the current model and empty the memory of GPU. Could you tell that how can I do it?

like image 204
Charmander Avatar asked Nov 17 '18 11:11

Charmander


People also ask

How do I remove models from GPU Pytorch?

Like said above: if you want to free the memory on the GPU you need to get rid of all references pointing on the GPU object. Then it will be freed automatically. So assuming model is on GPU: model=model. cpu() will free the GPU-memory if you don't keep any other references to of model, but model_cpu=model.


1 Answers

Freeing memory in PyTorch works as it does with the normal Python garbage collector. This means once all references to an Python-Object are gone it will be deleted.

You can delete references by using the del operator:

del model

You have to make sure though that there is no reference to the respective object left, otherwise the memory won't be freed.

So once you've deleted all references of your model, it should be deleted and the memory freed.

If you want to learn more about memory management you can take a look here: https://pytorch.org/docs/stable/notes/cuda.html#cuda-memory-management

like image 79
MBT Avatar answered Sep 19 '22 03:09

MBT