1. 程式人生 > 其它 >Flask部署TensorRT問題解決(pycuda._driver.LogicError: explicit_context_dependent failed)

Flask部署TensorRT問題解決(pycuda._driver.LogicError: explicit_context_dependent failed)

技術標籤:AI模型部署tensorRT模型推理flask使用tensorRT

問題描述:

今天使用Flask搭建服務端進行模型推理,在使用TensorRT進行推理模型時出現如下錯誤:

line 39, in allocate_buffers
stream = cuda.Stream() # pycuda 操作緩衝區
pycuda._driver.LogicError: explicit_context_dependent failed: invalid device context - no currently active context?

網上查了,說是pycuda.driver沒有初始化,導致無法得到context,然後採取以下解決辦法:

import pycuda.driver as cuda
import pycuda.autoinit

發現自己程式碼有加這兩句的,還是有同樣的錯誤。經過驗證發現放在flask裡面tensorRT推理就出現錯誤,要是模型推理不放在flask裡來啟動就沒有錯誤。

經過大量的找資料和驗證。。。發現是啟動flask時的問題,要是啟動flask使用debug=True就會出現錯誤,不使用debug就不會有錯誤。

解決辦法:

直接去掉 debug=True,或者改為debug=False即可。

socketio.run(app, host='127.0.0.1', port=12340, debug=True)

改為:

socketio.run(app, host='127.0.0.1', port=12340)

參考:

https://blog.csdn.net/weixin_42279044/article/details/102819670