TensorRT 常用函数功能

模型推理流程

本节主要看每个函数功能解释,不一定是最优

1、加载engine模型参数
"step0.通过文件流读取engine文件"
std::vector<char> trtModelStream_;   //engine文件参数读取后保存到该数组中    
size_t size{0};
 
std::ifstream file(engineFile, std::ios::binary);
if (file.good()) {
    file.seekg(0, file.end);    //设置读取位置为文件的末尾
    size = file.tellg();        //获取到文件的大小, tellg()函数不需要带参数,它返回当前定位指针的位置,也代表着输入流的大小。
    file.seekg(0, file.beg);    //设置读取位置为文件的开头
    trtModelStream_.resize(size);
    file.read(trtModelStream_.data(), size);    //读取文件流的内容保存到数组中
    file.close();
}
 
"step1.通过RunTime对象反序列化engine引擎"
IRuntime *runtime = createInferRuntime(gLogger);
assert(runtime != nullptr);
m_engine = runtime->deserializeCudaEngine(trtModelStream_.data(), size, nullptr);  //最后一个参数不再使用,必须是一个空指针.
assert(m_engine != nullptr);
 
"step2.创建context来开辟空间存储中间值,一个engine可以有多个context来并行处理"
//由于引擎保存网络定义和训练参数,因此需要额外的空间。这些保存在context中
m_context = m_engine->createExecutionContext();
assert(m_context != nullptr);
2、创建流并预分配GPU缓冲区内存
"步骤一:创建流(可以在推理之前提前创建好)"
    // Pointers to input and output device buffers to pass to engine.
    // Engine requires exactly IEngine::getNbBindings() number of buffers.
    assert(engine.getNbBindings() == 2);    //getNbBindings()获取网络输入输出数量
    void* buffers[2];
 
    // In order to bind the buffers, we need to know the names of the input and output tensors.
    // Note that indices are guaranteed to be less than IEngine::getNbBindings()
    // 为了绑定缓冲区,我们需要知道输入和输出张量的名称。 请注意,索引必须小于IEngine::getNbBindings()
    "step1.指定输入和输出节点名来获取输入输出索引"
    const int inputIndex = engine.getBindingIndex(m_input_tensor_name.data());//blob名称是转换模型时设置好的,这里需要保持一致
    const int outputIndex = engine.getBindingIndex(m_output_tensor_name.data());
 
    "step2.在设备上开辟网络输入输出需要的GPU缓冲区(内存)"
    //开辟输入输出需要的GPU内存,由网络输入输出决定
    TENSORRTCHECK(cudaMalloc(&buffers[inputIndex], batchSize * 3 * m_height * m_width * sizeof(float)));
    TENSORRTCHECK(cudaMalloc(&buffers[outputIndex], batchSize * m_output_size * sizeof(float)));
 
    "step3.创建流"
    cudaStream_t stream;
    TENSORRTCHECK(cudaStreamCreate(&stream));
3、执行模型推理
"调用推理函数(自己创建的)"
float *outdata = new float[m_batch_size * m_output_size];
//注意输入数据传入的方式.ptr的首地址
doInference(*m_context, (float *)pre_img.ptr<float>(0), outdata, m_batch_size); 
bool TensorRTWrapper::doInference(nvinfer1::IExecutionContext& context, float* input, float* output, int batchSize) {
    const nvinfer1::ICudaEngine& engine = context.getEngine();
    
    "步骤二:执行推理"
    "step1.拷贝数据 从主机(CPU)--->设备(GPU)"
    TENSORRTCHECK(cudaMemcpyAsync(buffers[inputIndex], input, batchSize * 3 * m_height * m_width * sizeof(float), cudaMemcpyHostToDevice, stream));
 
    "step2.执行推理"
    context.enqueue(batchSize, buffers, stream, nullptr);
 
    "step3.拷贝数据 从设备(GPU)--->主机(CPU)"
    TENSORRTCHECK(cudaMemcpyAsync(output, buffers[outputIndex], batchSize * m_output_size * sizeof(float), cudaMemcpyDeviceToHost, stream));
 
    "step4.同步流"
    cudaStreamSynchronize(stream);//因为上面的cudaMemcpyAsync()函数是异步方式执行的,所有这里需要进行同步
}
4、释放前面创建的对象和内存
    "步骤三:释放内存(可以放在类析构时)"
    cudaStreamDestroy(stream);
    TENSORRTCHECK(cudaFree(buffers[inputIndex]));
    TENSORRTCHECK(cudaFree(buffers[outputIndex]));

相关函数功能

bindings

bindings是tensorRT对输入输出张量的描述,bindings可以认为是个数组,bindings=input-tensor+output-tensor,比如input有a,output有b,c,d。即输入一个tensor,输出3个tensor,那么bindings=[a,b,c,d],bindings[0]=a,bindings[1] =a,bindings[2] =c。此时看到engine->getBindingDimensions(0),0表示索引,你就知道获取的维度是多少了,这里获取的就是a。

GetBindingDimensions

GetBindingDimensions(int index)/GetBindingDimensions(string nodeName) 

  • 获取节点维度接口: 通过端口编号或者端口名称,获取绑定的端口的形状信息.
  • int index: 绑定端口的编号
  • string nodeName: 绑定端口的名称
  • return Dims: 接口返回一个Dims结构体,该结构体包含了节点的维度大小以及每个维度的具体大小。

getNbBindings()

获取网络 输入+输出 数量,看 bindings 解释

cudaHostAlloc 和 cudaMalloc

CUDA:cudaHostAlloc()-CSDN博客

cudaMalloc / cudaFree 和 cudaMallocAsync / cudaFreeAsync

使用 NVIDIA CUDA 流顺序内存分配器,第 1 部分 - NVIDIA 技术博客

极市开发者平台-计算机视觉算法开发落地平台-极市科技

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值