How to use trtexec as a subprocess in python

Viewed 546

I have a python program and i have following code snippet inside that .py file, which converts the ONNX model to a TRT engine using trtexec :

if USE_FP16:
    subprocess.run([sys.executable, "-c", "trtexec --onnx=rmpx.onnx --saveEngine=rmpx_engine_pytorch.trt --explicitBatch --fp16"])
else:
    subprocess.run([sys.executable, "-c", "trtexec --onnx=rmpx.onnx --saveEngine=rmpx_engine_pytorch.trt --explicitBatch"])
f = open("/home/my_username/github/TensorRT/quickstart/IntroNotebooks/rmpx_engine_pytorch.trt", "rb")

But this subprocess doesnt generate trt file. i get file not found error. does someone know what i am doing wrong here?

1 Answers

In my case, it can successfully work in this way.

onnx_file_path = 'rmpx.onnx'
engine_file_path = 'rmpx_engine_pytorch.trt'
command = 'trtexec --onnx=' + onnx_file_path + ' --saveEngine=' + engine_file_path + ' --workspace=2048 --explicitBatch --fp16'
subprocess.run(command,shell=True)

You can check that whether you can find the trt engine (rmpx_engine_pytorch.trt) in that folder first! Basically, it will take some time on generating the trt engine.

Related