解决PyTorch DDP: Finding the cause of “Expected to mark a variable ready only once“

早上做消融实验的时候需要复现俩月前的实验结果,但是莫名其妙同样的代码和环境却跑不通了,会在loss.backward()的时候报如下错误:
RuntimeError: Expected to mark a variable ready only once. This error is caused by one of the following reasons: 1) Use of a module parameter outside the ``forward`` function. Please make sure model parameters are not shared across multiple concurrent forward-backward passes. 2) Reused parameters in multiple reentrant backward passes. For example, if you use multiple ``checkpoint`` functions to wrap the same part of your model, it would result in the same set of parameters been used by different reentrant backward passes multiple times, and hence marking a variable ready multiple times. DDP does not support such use cases yet.

在网上找了一圈发现一共也没几个人问过这个报错,其中stackoverflow上有人解决了这问题,说是把find_unused_parameters设置为false就莫名其妙好了,但是我这么设置之后在固定D训练G的时候又报错:之前写代码时碰到了这样一个错误:
RuntimeError: Expected to have finished reduction in the prior iteration before starting a new one. This error indicates that your module has parameters that were not used in producing loss. You can enable unused parameter detection by (1) passing the keyword argument find_unused_parameters=True to torch.nn.parallel.DistributedDataParallel; (2) making sure all forward function outputs participate in calculating loss. If you already have done the above two steps, then the distributed data parallel module wasn’t able to locate the output tensors in the return value of your module’s forward function. Please include the loss function and the structure of the return value of forward of your module when reporting this issue (e.g. list, dict, iterable).
在这里插入图片描述
但解决“Expected to have finished reduction in the prior iteration before starting a new one”又涉及到把find_unused_parameters设置为True,这看起来直接和上面的解决方法矛盾了…

最后实在是无可奈何,觉得可能是因为单卡采用分布式训练出了啥问题,于是干脆把ddp给关了再训练,报错才得以解决,太玄学了。。。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
根据引用和引用的信息,当使用pip安装软件包时,经常会出现"ERROR: Could not find a version that satisfies the requirement xxx"和"ERROR: No matching distribution found for xxx"的错误。这种错误通常是因为所需软件包的某个版本与当前系统或Python环境不兼容导致的。 对于你提到的"ERROR: Could not find a version that satisfies the requirement pytorch"和"ERROR: No matching distribution found for pytorch"的错误,这可能是由于你尝试使用pip安装pytorch时,所使用的pip源没有包含pytorch包或者你所使用的Python版本与pytorch的兼容性不符。 解决这个问题的方法有以下几种途径: 1. 确保你使用的pip源包含pytorch包。你可以尝试更换pip源,比如使用清华大学的pip源或者阿里云的pip源,这些源通常包含了更多的软件包。 2. 检查你所使用的Python版本是否与pytorch的兼容性要求相符。pytorch官方文档会提供所需的Python版本信息,你需要确保你所使用的Python版本符合要求。如果不符合,你可以尝试升级或降级你的Python版本。 3. 如果以上两种方法都没有解决问题,你可以考虑从官方网站或其他可信的渠道手动下载并安装pytorch。确保选择与你的操作系统和Python版本相匹配的pytorch安装包。 通过以上方法,你应该能够解决"ERROR: Could not find a version that satisfies the requirement pytorch"和"ERROR: No matching distribution found for pytorch"的问题。如果问题仍然存在,你可以提供更多的详细信息,以便更好地帮助你解决问题。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Polaris_T

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值