TensorRT的workflow

TensorRT提供了两种工作流:Development和Production。在Development阶段,主要涉及生成PLAN文件并进行验证;Execution阶段则侧重于加载PLAN文件进行推理。通过示例samplePlugin,说明了如何在Development阶段生成并保存PLAN文件,以及在Production阶段如何加载和执行该文件,以实现高效的推理过程。

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

TensorRT有两种workflow: 第一种是development阶段的workflow,第二种production阶段的workflow.


下图是 development阶段的流程路:



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()。


在production阶段,直接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();


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值