【问题标题】:RuntimeError: CUDA out of memory. Problem with stanza lemmatazation using too much GPU memory运行时错误:CUDA 内存不足。使用过多 GPU 内存的节词形还原问题
【发布时间】:2020-12-12 10:22:00
【问题描述】:

美好的一天, 我有 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)

运行时错误: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 上运行。

【问题讨论】:

  • StanzaLanguage 在做什么?
  • 这不是 Stanza 的一部分。另外,我不确定这会有多大帮助,但您应该将管道减少到仅运行“tokenize,pos,lemma”。如果您不指定,我认为您也在运行许多其他处理器。
  • 对不起,您实际上可以简化为“tokenize, lemma”
  • 糟糕,错了,你需要“tokenize,pos,lemma”
  • 另外,你想在什么语言上运行它?

标签: python pytorch stanford-nlp spacy


【解决方案1】:

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

...
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 等。这些应该确实有助于解决此问题。

【讨论】:

    猜你喜欢
    • 2020-12-31
    • 2020-04-09
    • 2019-10-09
    • 2021-08-08
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    相关资源
    最近更新 更多