【发布时间】: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 内存??
- 这种行为对我来说似乎很奇怪(可能是因为我不了解库的工作原理),因为模型已经过预训练,所以在转换新文本时它不应该变大,对吧?为什么它需要这么多 GPU 内存?
- 是否有任何方法可以在每次运行 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