莱州网站建设多少钱,虚拟主机wordpress解压位置,银行官网登录入口,建设景区网站的目的TRT8 一个大版本#xff0c;8.4-、 8.5、 8.6#xff08;包含预览功能#xff09;却有很多变动#xff0c;一不注意就发现很混乱#xff0c;特备注此贴。建议具体case可以参考这个合集#xff0c;真心安利#xff1a;https://github.com/NVIDIA/trt-samples-for-hackath… TRT8 一个大版本8.4-、 8.5、 8.6包含预览功能却有很多变动一不注意就发现很混乱特备注此贴。建议具体case可以参考这个合集真心安利https://github.com/NVIDIA/trt-samples-for-hackathon-cn/tree/master/cookbook
版本差异概述 1当前使用了8.4 参考 https://github.com/NVIDIA/trt-samples-for-hackathon-cn/blob/master/cookbook/01-SimpleDemo/TensorRT8.0/main-cuda.py 28.5版本废弃了binding, 导致开内存代码不同 参考 https://github.com/NVIDIA/trt-samples-for-hackathon-cn/blob/master/cookbook/01-SimpleDemo/TensorRT8.5/main.py 38.6版本以后支持dynamic shape统一个profile 用于不同的context。不再支持Pascal P40但完全改变了原来TRT engine必须绑定硬件和版本的要求 现在可以解耦出来但是有如下限制 1、binding 改变
18.5前需要使用biding 接口
对应dynamic batch 多个profile 需要计算偏置。偏置或者说index 是针对profile 同一个engine的不是针对context的。下图是一个engine里有3个profile模型有4个输入1个输出那么就有41*3 15个binding。 engine.num_bindings 获取binding 总数 engine.binding_is_input 计算出input数量每个profile里 算一个 engine.binding_is_inpu // nProfile 才是模型的输入个数 context.set_binding_shape 设置输入输出的维度如上面表格需要计算出pad和对应输入输出的顺序index contex.get_binding_shape 获得对应profile 的输出的维度 context.execute_async_v2 执行推理这里传入的第一个参数为输出输出GPU显存的地址并且需要填满如上的表格不用的位置填0。 完整代码 https://github.com/NVIDIA/trt-samples-for-hackathon-cn/blob/master/cookbook/08-Advance/MultiOptimizationProfile/main-BindingAPI.py logger trt.Logger(trt.Logger.ERROR)
engine trt.Runtime(logger).deserialize_cuda_engine(engineString) # engineString 是二进制 open engine文件
nIO engine.num_bindings
nInput np.sum([engine.binding_is_input(i) for i in range(engine.num_bindings)])
nOutput nIO - nInput
nIO, nInput, nOutput nIO // nProfile, nInput // nProfile, nOutput // nProfile
context engine.create_execution_context()
context.set_optimization_profile_async(index, stream)
bindingPad nIO * index_profile # 计算binding 的偏置
context.set_binding_shape(bindingPad 0, inputShape) # 第一个输入
outputShape contex.get_binding_shape(bindingPad nInput 0) # 第一个输出
bufferList [int(0) for b in bufferD[:bindingPad]] \[int(b) for b in bufferD[bindingPad:(bindingPad nInput nOutput)]] \[int(0) for b in bufferD[(bindingPad nInput nOutput):]]
context.execute_async_v2(bufferList, stream) 28.5 之后废弃binding
使用范例如下 engine.num_io_tensors 获取输入输出总个数 engine.get_tensor_name 获得对应的输入输出名字 engine.get_tensor_mode 判断是输入还是输出trt.TensorIOMode.INPUT 、trt.TensorIOMode.OUTPUT context.set_input_shape 直接设置对应的形状不再需要计算偏置 context.get_tensor_shape 直接获得输入输出需要先设置输入自动计算形状 context.set_tensor_address 绑定TRT 输入输出和 开辟的显存 context.execute_async_v3 来执行推理 完整代码 https://github.com/NVIDIA/trt-samples-for-hackathon-cn/blob/master/cookbook/08-Advance/MultiOptimizationProfile/main.py logger trt.Logger(trt.Logger.ERROR)
engine trt.Runtime(logger).deserialize_cuda_engine(engineString) # engineString 是二进制 open engine文件
nIO engine.num_io_tensors
lTensorName [engine.get_tensor_name(i) for i in range(nIO)]
nInput [engine.get_tensor_mode(lTensorName[i]) for i in range(nIO)].count(trt.TensorIOMode.INPUT)
context engine.create_execution_context()
context.set_optimization_profile_async(index, stream)
context.set_input_shape(lTensorName[0], inputShape)
outputShape context.get_tensor_shape(lTensorName[1])
context.set_tensor_address(lTensorName[0], int(cudart.cudaMalloc(nbytes)[1]))
context.execute_async_v3(0) 2、dynamic shape 性能
如果跨度较大建议使用多个配置文件可以明显提升推理性能 参考 Developer Guide :: NVIDIA Deep Learning TensorRT Documentation https://github.com/NVIDIA/trt-samples-for-hackathon-cn/blob/master/cookbook/09-BestPractice/UsingMultiOptimizationProfile/result-A30.md https://github.com/NVIDIA/trt-samples-for-hackathon-cn/blob/master/cookbook/09-BestPractice/UsingMultiOptimizationProfile/main.py 获得真实推理形状假设真实为3150150要使用 context.get_tensor_shape(0) 返回 (3, 150, 250)而不是 engine.get_tensor_shape(foo”) 返回 (3, -1, -1) 。 3、engine、 context、stream、profile动态batch的配置文件 一个engine 可以拥有多个context共享一个权重。 可以将不同的context 放到不同的stream 中实现并行。 一个context可以拥有多个profile至少一个但是8.6 0805预览特性前一个profile 只能绑定一个contextcontext 和profile 需要错开使用。 参考 多流8.6 https://github.com/NVIDIA/trt-samples-for-hackathon-cn/blob/master/cookbook/08-Advance/MultiStream/main.py 多上下文-动态batch8.6 https://github.com/NVIDIA/trt-samples-for-hackathon-cn/blob/master/cookbook/08-Advance/MultiContext/main.py 多上下文-动态batch8.5- https://github.com/NVIDIA/trt-samples-for-hackathon-cn/blob/master/cookbook/08-Advance/MultiContext/main-MultiOptimizationProfile.py