【问题标题】:Huggingface transformers unusual memory useHuggingface 变形金刚不寻常的内存使用
【发布时间】:2021-01-18 15:50:45
【问题描述】:

我有以下代码尝试使用 XL 转换器对文本进行矢量化:

  text = "Some string about 5000 characters long"

  tokenizer = TransfoXLTokenizerFast.from_pretrained('transfo-xl-wt103', cache_dir=my_local_dir, local_files_only=True)
  model = TransfoXLModel.from_pretrained("transfo-xl-wt103", cache_dir=my_local_dir, local_files_only=True)

  encoded_input = tokenizer(text, return_tensors='pt') 
  output = model(**encoded_input)

这会产生:

    output = model(**encoded_input)
  File "/home/user/w/default/lib/python3.7/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/user/w/default/lib/python3.7/site-packages/transformers/modeling_transfo_xl.py", line 863, in forward
    output_attentions=output_attentions,
  File "/home/user/w/default/lib/python3.7/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/user/w/default/lib/python3.7/site-packages/transformers/modeling_transfo_xl.py", line 385, in forward
    dec_inp, r, attn_mask=dec_attn_mask, mems=mems, head_mask=head_mask, output_attentions=output_attentions,
  File "/home/user/w/default/lib/python3.7/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/user/w//default/lib/python3.7/site-packages/transformers/modeling_transfo_xl.py", line 338, in forward
    attn_score = attn_score.float().masked_fill(attn_mask[:, :, :, None], -1e30).type_as(attn_score)
RuntimeError: [enforce fail at CPUAllocator.cpp:64] . DefaultCPUAllocator: can't allocate memory: you tried to allocate 2007869696 bytes. Error code 12 (Cannot allocate memory)

我对此感到有些困惑,因为它要求的是 2007869696,它只有 2GB,而这台机器有 64G 的 RAM。所以我都不明白它为什么要这个,更不明白为什么它没有得到它。

在哪里可以更改控制此设置并允许此进程更多 RAM 的设置?这是对示例代码的如此小的调用,我只看到很少有地方会接受这个参数。

【问题讨论】:

标签: python pytorch huggingface-transformers


【解决方案1】:

您确定您使用的是 gpu 而不是 cpu?

尝试使用CUDA_LAUNCH_BLOCKING=1 python script.py 运行python 脚本。这将产生正确的 python 堆栈跟踪(因为 CUDA 调用是异步的)

您也可以使用export CUDA_VISIBLE_DEVICES=device_number 设置CUDA_VISIBLE_DEVICES

还有一个issue还在pytorch github上打开,试试看。

【讨论】:

  • 即使使用 CPU,这也令人惊讶,对吧?从 64Gb RAM 分配 2Gb 并不多。
  • 是的,事实上它可能是一个 pytorch 错误。顺便说一句,你有没有尝试减少字符串text的长度?
猜你喜欢
  • 2020-11-18
  • 2021-12-31
  • 2020-08-24
  • 2020-04-15
  • 2020-05-25
  • 2022-11-03
  • 1970-01-01
  • 2020-05-20
  • 1970-01-01
相关资源
最近更新 更多