【yolo系列:运行报错RuntimeError: adaptive_avg_pool2d_backward_cuda does not have a deterministic impleme】

项目场景:

使用yolo系列算法训练数据集时候,运行到一半报错。


问题描述

RuntimeError: adaptive_avg_pool2d_backward_cuda does not have a deterministic implementation, but you set 'torch.use_deterministic_algorithms(True)'. You can turn off determinism just for this operation, or you can use the 'warn_only=True' option, if that's acceptable for your application. You can also file an issue at` https://github.com/pytorch/pytorch/issues to help us prioritize adding deterministic support for this operation.

原因分析:

提示:这里填写问题的分析:
这个错误消息是由PyTorch引发的,它表明在使用torch.use_deterministic_algorithms(True)启用了确定性算法模式时,某个操作(adaptive_avg_pool2d_backward_cuda)没有一个确定性的实现。这意味着在确定性模式下,该操作的行为不是确定的,可能会导致不一致的结果。


解决方案:

提示:这里填写该问题的具体解决方案:

禁用确定性算法模式:

如果你的应用程序可以容忍一些不确定性,可以尝试禁用确定性算法模式。比如说我是yolov7就在373行加一段代码

torch.use_deterministic_algorithms(False)

添加后如下图
在这里插入图片描述
如果不是yolov7,在train.py里面ctrl+f
搜索

scaler.scale(loss)

在这个之前添加torch.use_deterministic_algorithms(False)

如果已经有了,就将

torch.use_deterministic_algorithms(True)

改为

torch.use_deterministic_algorithms(False)

这个也是最常见的解决问题的办法,但是可能导致的结果就是你添加的注意力机制的最终结果可能比初始模型要低。

为此操作禁用确定性:

如果你仅希望为某个特定操作禁用确定性,你可以在执行该操作之前临时禁用确定性,然后在操作完成后恢复确定性设置。
也就是说你加入了其他的注意力机制导致效果变差的时候就需要这样操作。

例如:

with torch.deterministic(True):
    # 执行其他操作
    output = adaptive_avg_pool2d_backward_cuda(input)
# 恢复确定性设置
torch.deterministic(False)

使用warn_only=True选项:
如果你认为警告而不是错误对你的应用程序来说是可接受的,你可以在启用确定性算法模式时为操作添加warn_only=True选项,以便在不确定性操作时产生警告而不是错误。
例如:

with torch.deterministic(True, warn_only=True):
    # 执行可能不确定的操作
    output = adaptive_avg_pool2d_backward_cuda(input)

这样就可以添加注意力机制,但是保证结果的稳定性能。

猜你喜欢

转载自blog.csdn.net/weixin_47869094/article/details/132697887