[英]Resuming pytorch model training raises error “CUDA out of memory”
[英]Pytorch CUDA OutOfMemory Error while training
我正在尝试在 AWS Sagemaker 中训练 PyTorch FLAIR model。 这样做时出现以下错误:
RuntimeError: CUDA out of memory. Tried to allocate 84.00 MiB (GPU 0; 11.17 GiB total capacity; 9.29 GiB already allocated; 7.31 MiB free; 10.80 GiB reserved in total by PyTorch)
对于培训,我使用了 sagemaker.pytorch.estimator.PyTorch class。
我尝试了从 ml.m5、g4dn 到 p3 的不同实例类型变体(即使是 96GB memory 之一)。 在 ml.m5 中出现 CPUmemoryIssue 错误,在 g4dn 中出现 GPUMemoryIssue 以及在 P3 中出现 GPUMemoryIssue 主要是因为 Pytorch 仅使用 8*12GB 中 12GB 的 GPU 之一。
即使在本地尝试使用 CPU 机器并得到以下错误,也无法完成此培训:
RuntimeError: [enforce fail at ..\c10\core\CPUAllocator.cpp:72] data. DefaultCPUAllocator: not enough memory: you tried to allocate 67108864 bytes. Buy new RAM!
model 训练脚本:
corpus = ClassificationCorpus(data_folder, test_file='../data/exports/val.csv', train_file='../data/exports/train.csv')
print("finished loading corpus")
word_embeddings = [WordEmbeddings('glove'), FlairEmbeddings('news-forward-fast'), FlairEmbeddings('news-backward-fast')]
document_embeddings = DocumentLSTMEmbeddings(word_embeddings, hidden_size=512, reproject_words=True, reproject_words_dimension=256)
classifier = TextClassifier(document_embeddings, label_dictionary=corpus.make_label_dictionary(), multi_label=False)
trainer = ModelTrainer(classifier, corpus, optimizer=Adam)
trainer.train('../model_files', max_epochs=12,learning_rate=0.0001, train_with_dev=False, embeddings_storage_mode="none")
PS:我能够在我的本地 GPU 机器上用一个较小的数据集训练相同的架构,该机器带有 4GB GTX 1650 DDR5 memory,而且速度非常快。
这个错误是因为你的 GPU 用完了 memory。 你可以尝试几件事
减少训练数据的大小
减小 model 的大小,即隐藏层数或深度
您也可以尝试减少批量大小
好的,经过2天的不断调试,终于找到了根本原因。 我的理解是Flair对句子长度没有任何限制,从字数的意义上讲,它是以最长的句子作为最大值。 所以这引起了问题,因为在我的情况下,只有 15 万行的内容很少,这对于将嵌入加载到 memory 甚至是 16GB GPU 来说太多了。 所以它在那里破裂了。
要解决这个问题:对于包含这么长单词的内容,您可以从任何部分(左/右/中间任何地方)的此类内容中提取 n 个单词(在我的情况下为 10K),然后将 rest 中继,或者干脆忽略那些如果比较计数非常少,则用于培训记录。
在此之后,我希望你能够在训练中取得进步,就像我的情况一样。
PS:如果您正在关注此线程并遇到类似问题,请随时发表评论,以便我可以探索并帮助您解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.