TensorRT的构建阶段和执行阶段

本文介绍了TensorRT的两个主要阶段:构建阶段和执行阶段,并详细解释了如何通过样例代码生成PLAN文件,以及如何在执行阶段加载并运行该文件进行推理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考:https://blog.youkuaiyun.com/huibai1984/article/details/78012949

TensorRT有两种阶段: 第一个阶段是构建阶段,第二种阶段是执行阶段.

  • 下图是构建阶段的流程图

TensorRT里的sample的基本都是这个workflow. 先生成PLAN数据流,然后Validate它。

 

下图是execution的流程图

以TensorRT里的samplePlugin为例,这个PLAN文件可以在如下代码段后保存生成:

    PluginFactory pluginFactory;
    IHostMemory *gieModelStream{ nullptr };
    caffeToGIEModel("mnist.prototxt", "mnist.caffemodel", std::vector < std::string > { OUTPUT_BLOB_NAME }, 1, &pluginFa\
ctory, gieModelStream);
    pluginFactory.destroyPlugin();

这个阶段把prototxt,modelfile和plugin加载进来生成TensorRT自定定义的文件 (包含prototxt和model)。 在这段代码后,可以把gieModelStream的数据流存成PLAN文件(即 Serialize to dsik),其数据的指针为gieModelStream->data(),size是gieModelStream->size()。

 

  • 执行阶段,直接Serialized (load)上面生成的PLAN文件,然后执行如下code做inference。这样可以避免在每次应用启动时都执行 caffeToGIEModel() ,因为这个阶段执行时间比较长(因为不仅解析prototxt和caffemodel,还会做一些优化的工作)。
    // parse the mean file and  subtract it from the image
    const float *meanData = reinterpret_cast<const float*>(meanBlob->getData());
 
    float data[INPUT_H*INPUT_W];
    for (int i = 0; i < INPUT_H*INPUT_W; i++)
        data[i] = float(fileData[i])-meanData[i];
 
    meanBlob->destroy();
 
    // deserialize the engine
    IRuntime* runtime = createInferRuntime(gLogger);
    ICudaEngine* engine = runtime->deserializeCudaEngine(gieModelStream->data(), gieModelStream->size(), &pluginFactory)\
;
 
    IExecutionContext *context = engine->createExecutionContext();
 
    // run inference
    float prob[OUTPUT_SIZE];
    doInference(*context, data, prob, 1);
 
    // destroy the engine
    context->destroy();
    engine->destroy();
    runtime->destroy();
    pluginFactory.destroyPlugin();

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值