-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Open
Labels
Module:ONNXIssues relating to ONNX usage and importIssues relating to ONNX usage and importModule:RuntimeOther generic runtime issues that does not fall into other modulesOther generic runtime issues that does not fall into other modules
Description
Description:
I encountered a problem when trying to visualize inference results of ONNX and TensorRT (TRT) face detection models on the same image. The program hangs and terminates when initializing the TensorRT engine.
Steps to reproduce:
- Load a TRT engine (
face_detect_fp32.trt) using a shared CUDA context (shared_ctx). - Import PyTorch (
import torch) before loading the engine. - In the TRTInference class, during initialization, the line:
shape = tuple(self.engine.get_binding_shape(i))causes the program to hang.
- If I do not import PyTorch, everything works normally.
Code snippet (simplified):
import torch # <- importing this causes the hang
import pycuda.driver as cuda
import tensorrt as trt
cuda.init()
ctx = cuda.Device(0).retain_primary_context()
ctx.push()
class TRTInference:
def __init__(self, engine_path: str, ctx=None):
with open(engine_path, "rb") as f, trt.Runtime(trt.Logger(trt.Logger.ERROR)) as runtime:
self.engine = runtime.deserialize_cuda_engine(f.read())
for i in range(self.engine.num_bindings):
shape = tuple(self.engine.get_binding_shape(i)) # <--- hangs hereObservation:
- The hang occurs only when importing PyTorch.
- Using a shared CUDA context for TensorRT (
ctx = cuda.Device(0).retain_primary_context()) does not prevent the issue. - I suspect there is a CUDA context conflict between PyTorch and PyCUDA/TensorRT.
Question:
- Why does importing PyTorch cause
get_binding_shape()to hang? - How can I modify the code to allow PyTorch and TensorRT to work in the same process safely?
Environment:
- Python 3.8
- PyTorch, TensorRT, PyCUDA
- Shared CUDA context
Metadata
Metadata
Assignees
Labels
Module:ONNXIssues relating to ONNX usage and importIssues relating to ONNX usage and importModule:RuntimeOther generic runtime issues that does not fall into other modulesOther generic runtime issues that does not fall into other modules