写3种方法,1.含CPU与GPU间的内存copy,异步inference,2. 内存零copy, 异步inference, 3. 内存零copy, 同步inference
- 含CPU与GPU间的内存copy,异步inference
大多代码都用的是这种方式
先声明一个input和output数组,
static float data[BATCH_SIZE * 3 * INPUT_H * INPUT_W]; //input
static float prob[BATCH_SIZE * OUTPUT_SIZE]; //output
读入engine文件
char *trtModelStream{
nullptr};
string engine_name = "net.engine";
ifstream file(engine_name, std::ios::binary);
if (file.good()) {
file.seekg(0, file.end);
size = file.tellg();
file.seekg(0, file.beg);
trtModelStream = new char[size];
assert(trtModelStream);
file.read(trtModelStream, size);
file.close();
}
准备cudaEngine
IRuntime* runtime = createInferRuntime(gLogger);
assert(runtime != nullptr);
ICudaEngine* engine = runtime->deserializeCudaEngine(trtModelStream, size);
assert(engine != nullptr);
IExecutionContext* context = engine->createExecutionContext();
assert(context != nullptr);
delete[] trtModelStream;
读入图片
cv::<

本文介绍了在TensorRT中实现CPU与GPU之间的内存复制,以及如何进行异步推理以提高效率。通过示例代码展示了如何进行内存零拷贝的异步和同步推理,并探讨了不同推理方式对性能的影响。重点讨论了内存管理、CUDA流和异步操作在深度学习推理中的应用。
最低0.47元/天 解锁文章
4564

被折叠的 条评论
为什么被折叠?



