trt多流、多batch、多context

(1)一个engine可以创建多个context,一个engine可以有多个执行上下文,允许一组权值用于多个重叠推理任务。例如,可以使用一个引擎和一个上下文在并行CUDA流中处理图像。每个上下文将在与引擎相同的GPU上创建。

(2)engine:引擎。不能跨平台和TensorRT版本移植。若要存储,需要将引擎转化为一种格式,即序列化,若要推理,需要反序列化引擎。引擎用于保存网络定义和模型参数。在特定 config 与硬件上编译出来的计算引擎,且只能应用于特定的 config 与硬件上,支持持久化到本地以便进行发布或者节约下次使用的编译时间。engine 集成了模型结构、模型参数与最优计算 kernel 配置。同时 engine 与硬件和 TensorRT 版本强绑定,所以要求 engine 的编译与执行的硬件与 TensorRT 版本要保持一致。

(3)context:上下文。创建一些空间来存储中间值。一个engine可以创建多个context,分别执行多个推理任务。进行 inference 的实际对象,由 engine 创建,与 engine 是一对多的关系。

(4)看到官网上说
In general TensorRT objects are not thread-safe. The expected runtime concurrency model is that different threads will operate on different execution contexts. The context contains the state of the network (activation values etc) during execution, so using a context concurrently

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
文件的步骤如下: 1. 导入必要的头文件: ```c #include "NvInfer.h" #include "NvOnnxParser.h" #include "NvOnnxParserRuntime.h" #include "NvInferRuntimeCommon.h" ``` 2. 创建 `IRuntime` 对象: ```c nvinfer1::IRuntime* runtime = nvinfer1::createInferRuntime(gLogger); ``` 其中,`gLogger` 是用来记录日志的对象,需要先定义。 3. 从文件中创建 `ICudaEngine` 对象: ```c std::ifstream trt_file("model.trt", std::ios::binary); if (!trt_file.good()) { std::cerr << "Failed to load TRT file: model.trt" << std::endl; return -1; } trt_file.seekg(0, trt_file.end); const int model_size = trt_file.tellg(); trt_file.seekg(0, trt_file.beg); char* model_data = new char[model_size]; trt_file.read(model_data, model_size); nvinfer1::ICudaEngine* engine = runtime->deserializeCudaEngine(model_data, model_size, nullptr); ``` 其中,`model.trt` 是保存 TensorRT 模型的文件。 4. 创建 `IExecutionContext` 对象: ```c nvinfer1::IExecutionContext* context = engine->createExecutionContext(); ``` 5. 设置输入和输出的内存: ```c const int input_index = engine->getBindingIndex("input"); const int output_index = engine->getBindingIndex("output"); void* input_memory; cudaMalloc(&input_memory, input_size); void* output_memory; cudaMalloc(&output_memory, output_size); ``` 其中,`input_size` 和 `output_size` 分别是输入和输出的数据大小。 6. 执行推理: ```c void* bindings[] = {input_memory, output_memory}; context->execute(1, bindings); ``` 其中,`1` 是 batch size。 7. 获取输出数据: ```c float* output_data = new float[output_size / sizeof(float)]; cudaMemcpy(output_data, output_memory, output_size, cudaMemcpyDeviceToHost); ``` 8. 释放资源: ```c cudaFree(input_memory); cudaFree(output_memory); delete[] model_data; delete[] output_data; context->destroy(); engine->destroy(); runtime->destroy(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

曙光_deeplove

你的鼓励是我努力的最大源泉

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值