如何删除GPU内存中的变压器模型

问题描述 投票:0回答:2
from transformers import CTRLTokenizer, TFCTRLLMHeadModel
tokenizer_ctrl = CTRLTokenizer.from_pretrained('ctrl', cache_dir='./cache', local_files_only=True)
model_ctrl = TFCTRLLMHeadModel.from_pretrained('ctrl', cache_dir='./cache', local_files_only=True)
print(tokenizer_ctrl)
gen_nlp  = pipeline("text-generation", model=model_ctrl, tokenizer=tokenizer_ctrl, device=1, return_full_text=False)

你好,我的代码可以将变压器模型(例如这里的 CTRL)加载到 GPU 内存中。 使用后如何将其从 GPU 中删除,以释放更多 GPU 内存?

显示我使用

torch.cuda.empty_cache()

谢谢。

pytorch huggingface-transformers
2个回答
5
投票

您可以简单地

del tokenizer_ctrl
然后使用
torch.cuda.empty_cache()

请参阅来自 pytorch 论坛的 thread 讨论它。


0
投票

@pierlj 的解决方案似乎不适用于变压器模型,但是他们链接的线程中的这个解决方案对我有用:

import gc

del model
gc.collect()
torch.cuda.empty_cache()
© www.soinside.com 2019 - 2024. All rights reserved.