运行时错误:CUDA内存不足。节引理使用过多GPU内存的问题

2024-10-01 22:44:37 发布

您现在位置:Python中文网/ 问答频道 /正文

你好, 我有11GB的GPU内存,我遇到了CUDA内存问题,并进行了预训练

我使用了以下代码:

snlp = stanza.Pipeline(lang="en", use_gpu=True) # tried different batch_size/ lemma_batch_size - did not help
nlp = StanzaLanguage(snlp)

def tokenize(text):
     tokens = nlp(text)
     doc_l = [token.lemma_ for token in doc]
     lower_tokens = [t.lower() for t in doc_l]
     alpha_only = [t for t in lower_tokens if t.isalpha()]
     no_stops = [t for t in alpha_only if t not in stopwords]
     #torch.cuda.empty_cache() # Tried this - did not work
     return no_stops

tfidf = TfidfVectorizer(tokenizer=tokenize, min_df=0.1, max_df=0.9)
# Construct the TF-IDF matrix
tfidf_matrix = tfidf.fit_transform(texts)

RuntimeError: CUDA out of memory. Tried to allocate 978.00 MiB (GPU 0;11.00 GiB total capacity; 6.40 GiB already allocated; 439.75 MiB free; 6.53 GiB reserved in total by PyTorch).

我试过了

 [(tokenize(t) for t in test]

它只持续了12个文本。平均每人200字。根据错误消息-“尝试分配978.00 MiB”和此数据-SNLP每一步使用1GB的GPU内存

  1. 这种行为对我来说似乎很奇怪(可能是因为我不理解库是如何工作的),因为模型已经过预训练,所以在转换新文本时它不应该变大,对吗?为什么它需要这么多GPU内存
  2. 有没有办法在每次运行每个文本的引理后清除内存?(#torch.cuda.empty_cache()-不工作)和批处理大小也不工作

它在CPU上工作,但分配所有可用内存(32G RAM)。它的CPU速度要慢得多。我需要它来让CUDA工作


Tags: 内存in文本fordocgpunotlower
1条回答
网友
1楼 · 发布于 2024-10-01 22:44:37

如果检查完整堆栈跟踪,可能会提示哪个处理器遇到内存问题。例如,我最近在堆栈跟踪方面遇到了类似的问题:

...
File "stanza/pipeline/depparse_processor.py", line 42, in process     
preds += self.trainer.predict(b)   
File "stanza/models/depparse/trainer.py", line 74, in predict     
_, preds = self.model(word, word_mask, wordchars,
wordchars_mask, upos, xpos, ufeats, pretrained, lemma, head, deprel,
word_orig_idx, sentlens, wordlens)   
... 
RuntimeError: CUDA out of memory.
Tried to allocate 14.87 GiB (GPU 0; 14.76 GiB total capacity; 460.31 MiB already
allocated; 13.39 GiB free; 490.00 MiB reserved in total by PyTorch)

这让我意识到,在调用stanza.Pipeline(...)时,我需要设置depparse_batch_size。还有其他设置,如您提到的batch_sizelemma_batch_size,以及pos_batch_sizener_batch_size等。这些设置确实有助于解决此问题

相关问题 更多 >

    热门问题