当我尝试使用huggingface模型进行情感分析时,出现了以下错误:
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](https://istack.dev59.com/vTJJ1.webp)
我尝试使用torch.cuda.empty_cache()
,但它没有影响到问题。 当我关闭jupyter笔记本时,内存使用量下降到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内存?