MCPcopy Create free account
hub / github.com/NVIDIA/TensorRT / TensorRTInfer

Class TensorRTInfer

samples/python/detectron2/infer.py:30–156  ·  view source on GitHub ↗

Implements inference for the Model TensorRT engine.

Source from the content-addressed store, hash-verified

28import common
29
30class TensorRTInfer:
31 """
32 Implements inference for the Model TensorRT engine.
33 """
34
35 def __init__(self, engine_path):
36 """
37 :param engine_path: The path to the serialized engine to load from disk.
38 """
39
40 # Load TRT engine
41 self.logger = trt.Logger(trt.Logger.ERROR)
42 trt.init_libnvinfer_plugins(self.logger, namespace="")
43 with open(engine_path, "rb") as f, trt.Runtime(self.logger) as runtime:
44 assert runtime
45 self.engine = runtime.deserialize_cuda_engine(f.read())
46 assert self.engine
47 self.context = self.engine.create_execution_context()
48 assert self.context
49
50 # Setup I/O bindings
51 self.inputs = []
52 self.outputs = []
53 self.allocations = []
54 for i in range(self.engine.num_bindings):
55 is_input = False
56 if self.engine.binding_is_input(i):
57 is_input = True
58 name = self.engine.get_binding_name(i)
59 dtype = self.engine.get_binding_dtype(i)
60 shape = self.engine.get_binding_shape(i)
61 if is_input:
62 self.batch_size = shape[0]
63 size = np.dtype(trt.nptype(dtype)).itemsize
64 for s in shape:
65 size *= s
66 allocation = common.cuda_call(cudart.cudaMalloc(size))
67 binding = {
68 'index': i,
69 'name': name,
70 'dtype': np.dtype(trt.nptype(dtype)),
71 'shape': list(shape),
72 'allocation': allocation,
73 'size': size
74 }
75 self.allocations.append(allocation)
76 if self.engine.binding_is_input(i):
77 self.inputs.append(binding)
78 else:
79 self.outputs.append(binding)
80
81 assert self.batch_size > 0
82 assert len(self.inputs) > 0
83 assert len(self.outputs) > 0
84 assert len(self.allocations) > 0
85
86 def input_spec(self):
87 """

Callers 2

mainFunction · 0.90
mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected