TensorRT多线程下运行

TensorRT多线程背景

  • TensorRT 在主线程速度比 tensorflow 快了很多,30-60倍的提升。
  • pycuda多线程的demo,是一块显卡启动一个线程
  • 实际生产环境中用的thrift RPC,每个连接都是用 threading.Thread 多线程下运行的

解决方案

threading.lock加锁可以在多线程下限制单线程可以运行起来,平均一次计算耗时4.5ms左右,表现比单线程慢了2-3倍,但是比tensorflow要快了很多

coding如下,也可以用threading.Semaphore(1)来加锁

lock = threading.Lock()

def predict():
	with lock:
		self.cfx.push()
		....
		self.cfx.pop()

多线程代码参考
如果可以使用多进程实现性能会更好

备注

做过其他类型的多线程尝试都以失败告终,或者性能非常差
developer-guide中列举的方式执行的时候,显存得不到释放,并且多线程下执行速度低下

with engine.create_execution_context() as context:
		# Transfer input data to the GPU.
		cuda.memcpy_htod_async(d_input, h_input, stream)
		# Run inference.
		context.execute_async_v2(bindings=[int(d_input), int(d_output)], stream_handle=stream.handle)
		# Transfer predictions back from the GPU.
		cuda.memcpy_dtoh_async(h_output, d_output, stream)
		# Synchronize the stream
		stream.synchronize()
		# Return the host output. 
return h_output

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值