当我试图微调一个怀抱面孔的预训练XML Roberta模型时,我总是遇到一个CUDA out of memory
错误。所以,我想知道的第一件事是预训练模型的大小
model = XLMRobertaForCausalLM.from_pretrained('xlm-roberta-base', config=config)
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
model.to(device)
我试着用它来确定模型的尺寸
sys.getsizeof(model)
不出所料,我得到了一个错误的结果。结果是56,这是python对象的大小
但是后来,我尝试了model. element_size()
,我得到了错误
ModuleAttributeError: 'XLMRobertaForCausalLM' object has no attribute 'element_size'
我已经在拥抱脸文档中搜索过了,但是我没有找到如何做。这里有人知道怎么做吗
如果您面临
CUDA out of memory
错误,问题主要不是模型,而是训练数据。您可以减少batch_size
(并行使用的训练示例的数量),因此您的gpu只需要在每次迭代中处理几个示例,而不需要处理大量示例但是,对于你的问题:
我推荐你。它是一个计算“真实”大小(也称为“深度”大小)的库。因此,一个简单的解决方案是:
然而,文件说:
这应该不是问题,但是如果它对于您的模型仍然太小,您可以使用Pympler,这是另一个通过递归计算“深度”大小的库
另一种方法是自己实现
get_deep_size()
函数,例如从this article:相关问题 更多 >
编程相关推荐