模型推理流程
本节主要看每个函数功能解释,不肯定是最优
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 技能博客
极市开发者平台-计算机视觉算法开发落地平台-极市科技
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |