前言
使用一个古老的开源仓库时触发的。
错误信息
[W LegacyTypeDispatch.h:74] Warning: AutoNonVariableTypeMode is deprecated and will be removed in 1.10 release. For kernel implementations please use AutoDispatchBelowADInplaceOrView instead, If you are looking for a user facing API to enable running your inference-only workload, please use c10::InferenceMode. Using AutoDispatchBelowADInplaceOrView in user code is under risk of producing silent wrong result in some edge cases. See Note [AutoDispatchBelowAutograd] for more details. (function operator())
原因
因为torch的版本更新后,autograd机制做了点调整。
引入了inference mode
https://github.com/ailzhang/rfcs/blob/rfc0011/RFC-0011-InferenceMode.md
解决方案
看torch官方Note
https://github.com/pytorch/pytorch/blob/main/docs/cpp/source/notes/inference_mode.rst#migration-guide-from-autononvariabletypemode
在原来的cpp代码中有
at::AutoNonVariableTypeMode g;
表示禁用自动求导。
但这个方法不安全。
根据官方指引
1. 只想在inference mode下使用
替换成
c10::InferenceMode guard;
2. 想自定义backward过程
替换成
at::AutoDispatchBelowADInplaceOrView guard;
Note
AutoDispatchBelowADInplaceOrView
is just a new name ofAutoNonVariableTypeMode
since it explains the guard’s functionality better.