【问题描述:RuntimeError: one of the variables needed for gradient computation has been modified by an i 】

问题描述:分布式训练中的inplace问题

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [1, 64, 64]], which is output 0 of MaskedFillBackward0, is at version 1; expected version 0 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!

text_to_img_exp,img_to_text_exp=map(lambda t: t.masked_fill_(pos_mask,0.)(text_to_img_exp,img_to_text_exp))
text_to_img_exp,img_to_text_exp=map(lambda t: t.masked_fill(pos_mask, 0.),(text_to_img_exp, img_to_text_exp))

上面是我的错误代码,下面是正确的,只是多了一个下划线,但是在pytorch的语法中加了下划线的一般都是inplace操作。
除此之外在其它几个博客里面看到的解决方案如下:

  1. 在python中, inplace operation 可以是一些 += 或 *= 导致的。比如 x += y,需要改成 x = x +y。https://blog.csdn.net/m0_66237895/article/details/134646105
  2. 还有的会在很多变量之后加.clone()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值