[THUDM/ChatGLM-6B]怎么释放GPU内存,使用torch.cuda.empty_cache()不起作用

2024-05-13 800 views
1

随着提问的问题的增多,GPU内存占用也会增加,内存会溢出,有没有办法,每次推理完成后,释放下内存,我使用torch.cuda.empty_cache()不起作用。

怎么修改代码,可以在推理内存满了后,释放下内存,继续推理

回答

9

怎么会不起作用,可能是你修改的位置错了。在返回answer之前的那一行修改

3

要同时清空history才行吧,要不下次推理会把历史对话带上,依然OOM

4

我也遇到了内存溢出的问题 OutOfMemoryError: CUDA out of memory. Tried to allocate 646.00 MiB (GPU 0; 14.76 GiB total capacity; 12.35 GiB already allocated; 529.75 MiB free; 13.41 GiB reserved in total by PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF

1

@sujunze @ixjx @dizhenx ,是的,我同时清空了history和torch.cuda.empty_cache(),起作用了,torch.cuda.empty_cache()这个之前没起作用是位置不对

7

清除 torch.cuda.empty_cache() 和history也不起作用

4

@controZheng , 你是不是清理的卡和位置不对,看下你用的是哪张卡,清理对应的卡,在predict函数后,return之前释放下。 def torch_gc(): if torch.cuda.is_available(): with torch.cuda.device('cuda:1'): torch.cuda.empty_cache() torch.cuda.ipc_collect()

8

@controZheng , 你是不是清理的卡和位置不对,看下你用的是哪张卡,清理对应的卡,在predict函数后,return之前释放下。 def torch_gc(): if torch.cuda.is_available(): with torch.cuda.device('cuda:1'): torch.cuda.empty_cache() torch.cuda.ipc_collect()

确实不行 我检查很多次位置和history 一旦 GPU爆掉(torch.cuda.OutOfMemoryError: CUDA out of memory.)之后 清除就不起作用了

2

你是不是一次推理的token太长,导致一次推理就把显存拉满了,根本就没有清空的机会

4

对的对的 我在找一个办法能在GPU爆了之后能够清除内存 不然每次都要重启服务

7

对的对的 我在找一个办法能在GPU爆了之后能够清除内存 不然每次都要重启服务

找到办法了吗?我也不知道该如何处理这种情况。我正在做一个能稳定运行的API服务器,偶尔爆显存我希望程序能自己恢复过来。