单卡trainer.train(resume_from_checkpoint=True)报错AttributeError: ‘list‘ object has no attribute ‘clone‘

deepspeed单卡训练后trainer.train(resume_from_checkpoint=True)报错AttributeError: 'list' object has no attribute 'clone'

具体情况

我的配置是使用的deepspeed zero_stage2进行的单卡训练,在使用hugging face的Trainer类进行trainer.train(resume_from_checkpoint=True)的过程中出现了如下错误:
Traceback (most recent call last):
File “src/train.py”, line 393, in
main()
File “src/train.py”, line 385, in main
trainer.train(resume_from_checkpoint=True)
File “/opt/conda/lib/python3.8/site-packages/transformers/trainer.py”, line 1661, in train
return inner_training_loop(
File “/opt/conda/lib/python3.8/site-packages/transformers/trainer.py”, line 1901, in _inner_training_loop
self._load_rng_state(resume_from_checkpoint)
File “/opt/conda/lib/python3.8/site-packages/transformers/trainer.py”, line 2322, in _load_rng_state
torch.cuda.random.set_rng_state(checkpoint_rng_state[“cuda”])
File “/opt/conda/lib/python3.8/site-packages/torch/cuda/random.py”, line 51, in set_rng_state
new_state_copy = new_state.clone(memory_format=torch.contiguous_format)
AttributeError: ‘list’ object has no attribute ‘clone’

处理过程

原始报错
看报错是因为new_state是一个list,所以不能clone。
简单来想直接将new_state进行torch.tensor(new_state)一下就解决了,实际不能解决问题,出现typeError报错:
首次处理后
那么只能进行调试看看这个神秘的new_state了,print()一下发现了它真是一个列表,不过列表里面有一个张量,也就是torch.cuda.random.set_rng_state实际需要的输入了。
new_state状态

向上一层看,问题出现在/transformers/trainer.py包中的self._load_rng_state(resume_from_checkpoint)的torch.cuda.random.set_rng_state(checkpoint_rng_state[“cuda”]),可以看到源码中加载的就是一个列表,没有对其进行处理(可能是因为deepspeed的原因导致其在外面套了个列表),那么为了符合我单卡deepspeed的情况,将其修改为torch.cuda.random.set_rng_state(checkpoint_rng_state[“cuda”][0])便可以符合函数的torch.cuda.random.set_rng_state()的输入需求了

修改后

成果

运行成功,效果如下:
成功运行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值