升级到 TensorRT 7 后加载或构建 cuda 引擎偶尔会崩溃
Posted
技术标签:
【中文标题】升级到 TensorRT 7 后加载或构建 cuda 引擎偶尔会崩溃【英文标题】:Loading or building cuda engine crashes occassionaly after upgrading to TensorRT 7 【发布时间】:2021-06-12 12:15:37 【问题描述】:我正在尝试在 C++ 中运行 TensorRT 推理。有时代码会在尝试构建新引擎或从文件加载引擎时崩溃。它偶尔会发生(有时它运行没有任何问题)。我按照以下步骤准备网络:
initLibNvInferPlugins(&gLogger.getTRTLogger(), "");
if (mParams.loadEngine.size() > 0)
std::vector<char> trtModelStream;
size_t size0;
std::ifstream file(mParams.loadEngine, std::ios::binary);
if (file.good())
file.seekg(0, file.end);
size = file.tellg();
file.seekg(0, file.beg);
trtModelStream.resize(size);
file.read(trtModelStream.data(), size);
file.close();
IRuntime* infer_Runtime = nvinfer1::createInferRuntime(gLogger);
if (mParams.dlaCore >= 0)
infer_Runtime->setDLACore(mParams.dlaCore);
mEngine = std::shared_ptr<nvinfer1::ICudaEngine>(
infer_Runtime->deserializeCudaEngine(trtModelStream.data(), size, nullptr), samplesCommon::InferDeleter());
gLogInfo << "TRT Engine loaded from: " << mParams.loadEngine << endl;
infer_Runtime->destroy();
if (!mEngine)
return false;
else
return true;
auto builder = SampleUniquePtr<nvinfer1::IBuilder>(nvinfer1::createInferBuilder(gLogger.getTRTLogger()));
const auto explicitBatch = 1U << static_cast<uint32_t>(NetworkDefinitionCreationFlag::kEXPLICIT_BATCH);
auto network = SampleUniquePtr<nvinfer1::INetworkDefinition>(builder->createNetworkV2(explicitBatch));
auto config = SampleUniquePtr<nvinfer1::IBuilderConfig>(builder->createBuilderConfig());
auto parser = SampleUniquePtr<nvonnxparser::IParser>(nvonnxparser::createParser(*network, gLogger.getTRTLogger()));
mEngine = nullptr;
parser->parseFromFile(
locateFile(mParams.onnxFileName, mParams.dataDirs).c_str(), static_cast<int>(gLogger.getReportableSeverity()));
// Calibrator life time needs to last until after the engine is built.
std::unique_ptr<IInt8Calibrator> calibrator;
config->setAvgTimingIterations(1);
config->setMinTimingIterations(1);
config->setMaxWorkspaceSize(4_GiB);
builder->setMaxBatchSize(mParams.batchSize);
mEngine = std::shared_ptr<nvinfer1::ICudaEngine>(
builder->buildEngineWithConfig(*network, *config), samplesCommon::InferDeleter());
错误发生在这里:
[05/12/2021-16:46:42] [I] [TRT] Detected 1 inputs and 1 output network tensors.
16:46:42: The program has unexpectedly finished.
加载现有引擎时此行崩溃:
mEngine = std::shared_ptr<nvinfer1::ICudaEngine(
infer_Runtime->deserializeCudaEngine(trtModelStream.data(), size, nullptr), samplesCommon::InferDeleter());
或者在构建引擎时:
mEngine = std::shared_ptr<nvinfer1::ICudaEngine>(
builder->buildEngineWithConfig(*network, *config), samplesCommon::InferDeleter());
更多信息:
张量RT 7.2.3 Ubuntu 18.04 cuDNN 8.1.1 CUDA 11.1 更新1 ONNX 1.6.0 Pytorch 1.5.0
【问题讨论】:
它在 TensorRT 8.0.3 中持续存在。 【参考方案1】:终于明白了! 我重写了 CMake.txt 并添加了所有必需的库和路径并删除了重复的库和路径。这可能是 cuBLAS 中的 lib 冲突。
【讨论】:
以上是关于升级到 TensorRT 7 后加载或构建 cuda 引擎偶尔会崩溃的主要内容,如果未能解决你的问题,请参考以下文章
尝试从 Babel 6 升级到 Babel 7 时,webpack 模块构建失败
一个简单的tensorRT mnist推理案例,模型采用代码构建