RuntimeError:CUDA内存不足。使用过多GPU内存的节lemmatazation的问题

时间:2020-08-23 17:09:18

标签: python pytorch stanford-nlp spacy

早上好, 我有11GB的GPU内存,并且遇到了经过预先训练的lemmatazation的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内存不足。尝试分配978.00 MiB(GPU 0; 11.00 GiB的总容量; 6.40 GiB已经分配; 439.75 MiB免费; PyTorch总共保留了6.53 GiB。

我尝试过

 [(tokenize(t) for t in test]

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

  1. 这种行为对我来说似乎很奇怪(可能是因为我不了解库的工作原理),因为模型已经过预训练,所以在转换新文本时它应该不会变大,对吗?为什么它需要这么多的GPU内存?
  2. 每次运行lemma_后,是否有任何方法可以清除内存中的每个文本? (#torch.cuda.empty_cache()-不起作用),batch_size也不起作用。

它在CPU上工作,但是分配所有可用内存(32G RAM)。这在CPU上要慢得多。我需要它才能使其在CUDA上运行。

1 个答案:

答案 0 :(得分:0)

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

...
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)

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