docstr: Attempts to load a serialized engine if available, otherwise builds a new TensorRT engine and saves it.
docstr_tok: ["Attempts","to","load","a","serialized","engine","if","available",",","otherwise","builds","a","new","TensorRT","engine","and","saves","it","."]
code: def trt5_get_engine(onnx_file_path, engine_file_path=""):
      """Attempts to load a serialized engine if available, otherwise builds a new TensorRT engine and saves it."""
      TRT_LOGGER = trt.Logger()
  
      def build_engine():
          """Takes an ONNX file and creates a TensorRT engine to run inference with"""
          with trt.Builder(
              TRT_LOGGER
          ) as builder, builder.create_network() as network, trt.OnnxParser(
              network, TRT_LOGGER
          ) as parser:
              builder.max_workspace_size = 1 << 30  # 1GB
              builder.max_batch_size = 1
              # Parse model file
              if not os.path.exists(onnx_file_path):
                  print(
                      "ONNX file {} not found.".format(onnx_file_path)
                  )
                  exit(0)
              print(
                  "Loading ONNX file from path {}...".format(
                      onnx_file_path
                  )
              )
              with open(onnx_file_path, "rb") as model:
                  print("Beginning ONNX file parsing")
                  parser.parse(model.read())
              print("Completed parsing of ONNX file")
  
              print(
                  "Building an engine from file {}; this may take a while...".format(
                      onnx_file_path
                  )
              )
              engine = builder.build_cuda_engine(network)
              print("Completed creating Engine")
  
              if engine_file_path != "":
                  with open(engine_file_path, "wb") as f:
                      f.write(engine.serialize())
                      print(
                          "Save trt model in {}".format(
                              engine_file_path
                          )
                      )
  
              return engine
  
      if os.path.exists(engine_file_path):
          # If a serialized engine exists, use it instead of building an engine.
          print("Reading engine from file {}".format(engine_file_path))
          with open(engine_file_path, "rb") as f, trt.Runtime(
              TRT_LOGGER
          ) as runtime:
              return runtime.deserialize_cuda_engine(f.read())
      else:
          return build_engine()
  
imports: import argparse
  import os
  import time
  import numpy
  import paddle
  import tensorrt
  import pycuda.driver
  import pycuda.autoinit
  import onnx
  import onnxruntime
  from paddleseg.cvlibs import Config, SegBuilder
  from paddleseg.utils import logger, utils
label_desc: Use image node_id 6 for calling the TRTPredictorV2 obj's underlying member method code with example usage: obj.trt5_get_engine(onnx_file_path, engine_file_path) and returns: build_engine, engine, runtime