部署TensorRT时发生的问题总结
报错:“引发了异常: 读取访问权限冲突。 this 是 nullptr”
原因:
- cuda the launch timed out and was terminated
解决方案
①安装cuda(需要里面带的Nsight Monitor)
②使用管理员权限打开Nsight Monitor(在开始界面,找到Nsight Monitor,右键管理员身份运行)
③右下角找到Nsight Monitor,右键打开Options,将TDR关闭
- [runtime.cpp::deserializeCudaEngine::76] Error Code 4: Internal Error (Engine deserialization failed.)
解决方案:在自己电脑上将onnx文件重新生成一下engine,其他设备上拷贝过来的engine可能不可用。 - 报错
`[04/07/2023-11:11:35] [I] [TRT] Loaded engine size: 17 MiB
[04/07/2023-11:11:35] [E] [TRT] 1: [pluginV2Runner.cpp::nvinfer1::rt::load::300] Error Code 1: Serialization (Serialization assertion creator failed.Cannot deserialize plugin since corresponding IPluginCreator not found in Plugin Registry)
[04/07/2023-11:11:35] [E] [TRT] 4: [runtime.cpp::nvinfer1::Runtime::deserializeCudaEngine::66] Error Code 4: Internal Error (Engine deserialization failed.)
解决方案:
1)添加#include "NvInferPlugin.h"的头文件
#include "NvInferPlugin.h"
2)在加载模型的代码处,进行插件注册
nvinfer1::IRuntime* runtime = nvinfer1::createInferRuntime(sample::gLogger.getTRTLogger());
initLibNvInferPlugins(&sample::gLogger.getTRTLogger(),""); //加这一句
ICudaEngine* engine = runtime->deserializeCudaEngine(data.get(), length);
参考文档:https://blog.csdn.net/ListenAlone/article/details/125014592
https://blog.csdn.net/qq_42178122/article/details/128046706
`
-
报错2
[05/09/2023-16:50:02] [E] [TRT] 1: [convBaseRunner.cpp::nvinfer1::rt::task::CaskConvBaseRunner::execute::271] Error Code 1: Cask (Cask convolution execution)
[05/09/2023-16:50:02] [E] [TRT] 1: [checkMacros.cpp::nvinfer1::catchCudaError::202] Error Code 1: Cuda Runtime (invalid resource handle)
解决方案::英伟达提供的方案是"This error is more related to the CUDA context issue.Please make sure you’re handling the CUDA context/stream correctly." —译:“此错误与CUDA上下文问题更相关。请确保正确处理CUDA上下文/流。”
因此,查阅资料,发现CUDA runtime不提供对CUDA上下文的直接访问,它通过延迟初始化(deferred initialization)来创建上下文。每一个 CUDART库调用和内核调用都会检查上下文是否是当前使用的,如果必要的话,创建CUDA上下文(使用之前用 cudaSetDevice, cudaSetDeviceFlags(),cudaGLSetGLDevice()等函数设置状态)。
所以,尝试在推理之前加一句cudaSetDevice(0)问题就解决了。(如果是多GPU,可以更改gpuid,我这一张显卡所以就默认为0)
参考链接:https://blog.csdn.net/hjxu2016/article/details/109354226
https://blog.csdn.net/wsl_longwudi/article/details/126940635 -
报错3
exe文件调用tensorrt的dll时,一开始报了“尝试读取或写入受保护的内存”,跟进代码后发现“引发了异常: 读取访问权限冲突。 this 是 nullptr”,进一步捕获vs输出,显示如下异常:
0x00007FFFA17DCD29 处(位于 ******.exe 中)引发的异常: Microsoft C++ 异常: nvinfer1::InternalError,位于内存位置 0x000000A97E7FD080 处。
0x00007FFFA17DCD29 处(位于 *******.exe 中)引发的异常: Microsoft C++ 异常: [rethrow],位于内存位置 0x0000000000000000 处。
解决方案: 一定要将tensorrt的DLL放置在exe根目录下!