早上好, 我有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内存?
它在CPU上工作,但是分配所有可用内存(32G RAM)。这在CPU上要慢得多。我需要它才能使其在CUDA上运行。
答案 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_size
和 lemma_batch_size
,以及 pos_batch_size
和 ner_batch_size
等。这些应该确实有助于解决此问题。