Python加载torchtrt_runtime.dll遇WinError1114问题求助
环境与现象
尝试在C++和Python中加载编译后的torch_tensorRT模块:
- C++加载代码:
const torch::Device device = torch::Device(torch::kCUDA, 0); model = torch::jit::load(model_path, device);
- Python加载代码:
torch.jit.load(model_path)
C++ Debug模式运行正常,但Release模式和Python均报错:
RuntimeError: Unknown type name 'torch.torch.classes.tensorrt.Engine':
File "code/torch/movinets/models.py", line 4 parameters = [] buffers =
[]
_torch___movinets_models_MoViNet_trt_engine : torch.torch.classes.tensorrt.Engine < -- HERE def forward(self_1:
torch.movinets.models.MoViNet_trt, input_0: Tensor) -> Tensor:
已验证的有效操作与Python加载失败情况
在C++ Release模式中,通过预加载torchtrt_runtime.dll可正常加载模型,代码如下:
HMODULE hLib = LoadLibrary(TEXT("torchtrt_runtime")); if (hLib == NULL) { std::cerr << "Library torchtrt_runtime.dll not found" << std::endl; exit(1); }
但在Python中尝试以下两种方式加载torchtrt_runtime.dll均失败:
- 使用PyTorch内置方法:
torch.ops.load_library("torchtrt_runtime.dll")
- 使用ctypes:
import ctypes hllDll = ctypes.WinDLL("torchtrt_runtime.dll") or ctypes.CDLL("torchtrt_runtime.dll")
报错详情
- 初始报错:
hllDll = ctypes.WinDLL ("torchtrt_runtime.dll") File
"C:\Users\NomanAnjum\AppData\Local\Programs\Python\Python310\lib\ctypes__init__.py",
line 374, in init self._handle = _dlopen(self._name, mode)
FileNotFoundError: Could not find module 'torchtrt_runtime.dll' (or
one of its dependencies). Try using the full path with constructor
syntax.
- 补全所有依赖DLL到同一目录后,报错变为:
File "video_play.py", line 198, in get_torch_tensorrt_converted_model
hllDll = ctypes.WinDLL(r"torchtrt_runtime.dll") File
"C:\Users\NomanAnjum\AppData\Local\Programs\Python\Python310\lib\ctypes__init__.py",
line 374, in init self._handle = _dlopen(self._name, mode)
OSError: [WinError 1114] A dynamic link library (DLL) initialization
routine failed
需求
需解决Python中成功加载torchtrt_runtime.dll的问题,以正常加载torch_tensorRT模型。
内容的提问来源于stack exchange,提问作者Noman_Anjum_Retro

