假設我創建了一個張量并將其放在 GPU 上,以后不需要它并想釋放分配給它的 GPU 內存;我該怎么做?import torcha=torch.randn(3,4).cuda() # nvidia-smi shows that some mem has been allocated.# do something# a does not exist and nvidia-smi shows that mem has been freed.我努力了:del adel a; torch.cuda.empty_cache()但它們都不起作用。
1 回答

MYYA
TA貢獻1868條經驗 獲得超4個贊
運行del tensor
會從 GPU 中釋放內存,但不會將其返回到設備,這就是為什么內存仍顯示為已使用的原因nvidia-smi
。您可以創建一個新的張量,并重用該內存。
來源
https://discuss.pytorch.org/t/how-to-delete-pytorch-objects-correctly-from-memory/947
https://discuss.pytorch.org/t/about-torch-cuda-empty-cache/34232
添加回答
舉報
0/150
提交
取消