How to make a copy of a gpu model on the cpu?

Hi All,

I am training a model on the gpu and after each epoch I would like to store the best models on the cpu. Does model.to(‘cpu’) create a copy on the cpu and does it keep the original model on the gpu?

At the moment, I have this code:
best_model = copy.deepcopy(model)
best_model = best_model.to(‘cpu’)
The problem is that this code makes a copy first on the gpu, and then it transfers the copy to the cpu. I would like to avoid making a copy on the gpu, since I don’t have enough gpu memory.

Simone

I think the best way is to save the model using torch.save(‘modelname.pt’). You can now load the saved model for inference. To run inference on CPU, specify map_location=‘cpu’ on torch.load()

Thank you for the reply. The solution that you propose consists of saving the best model to the disk. Do you know an alternative solution that doesn’t save the model to the disk. I just need to make a copy on the cpu of a model on the gpu, without touching the model on the gpu.