如何减少专用 GPU 内存使用量并为 CUDA 和 Pytorch 使用共享 GPU 内存

时间:2021-03-04 13:20:52

标签: python memory pytorch gpu

当我尝试使用拥抱脸模型之一进行情感分析时出现以下错误:

RuntimeError: CUDA out of memory. Tried to allocate 72.00 MiB (GPU 0; 3.00 GiB total capacity; 1.84 GiB already allocated; 5.45 MiB free; 2.04 GiB reserved in total by PyTorch)

虽然我没有使用 CUDA 内存,但它仍然保持在同一水平。 enter image description here

我尝试使用 torch.cuda.empty_cache() 但它没有影响问题。当我关闭 jupyter notebook 时,它会减少到 0。所以我很确定它与 pytorch 和 python 相关。

这是我的代码:

import joblib
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification,pipeline
import torch.nn.functional as F
from torch.utils.data import DataLoader
import pandas as pd
import numpy as np
from tqdm import tqdm

tokenizer = AutoTokenizer.from_pretrained("savasy/bert-base-turkish-sentiment-cased")
model = AutoModelForSequenceClassification.from_pretrained("savasy/bert-base-turkish-sentiment-cased")

sa= pipeline("sentiment-analysis", tokenizer=tokenizer, model=model,device=0)
batcher = DataLoader(dataset=comments,
                      batch_size=100,
                      shuffle=True,
                      pin_memory=True)
predictions= []
for batch in tqdm(batcher):
     p = sa(batch)
     predictions.append(p)

我有一个 GTX 1060、python 3.8 和 torch==1.7.1,我的操作系统是 Windows 10。 评论数为187K。我想知道是否有解决此内存问题的方法。也许以某种方式在 CPU 上保持张量并且只在 GPU 上使用批处理。使用并收到此错误后,内存使用仍在继续。当我关闭我的 jupyter 笔记本时,它就会消失。有什么办法可以清除这段记忆吗?有什么办法可以利用共享 GPU 内存吗?

0 个答案:

没有答案
相关问题