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

Function allocate_buffers

tools/tensorflow-quantization/examples/infer_engine.py:49–85  ·  view source on GitHub ↗

Function to allocate buffers and bindings for TensorRT inference. Args: engine (trt.ICudaEngine): batch_size (int): batch size to be used during inference. Returns: inputs (List): list of input buffers. outputs (List): list of output buffers. db

(engine: trt.ICudaEngine, batch_size: int)

Source from the content-addressed store, hash-verified

47
48
49def allocate_buffers(engine: trt.ICudaEngine, batch_size: int) -> [list, list, list]:
50 """
51 Function to allocate buffers and bindings for TensorRT inference.
52
53 Args:
54 engine (trt.ICudaEngine):
55 batch_size (int): batch size to be used during inference.
56
57 Returns:
58 inputs (List): list of input buffers.
59 outputs (List): list of output buffers.
60 dbindings (List): list of device bindings.
61 """
62 inputs = []
63 outputs = []
64 dbindings = []
65
66 for binding in engine:
67 binding_shape = engine.get_binding_shape(binding)
68 if binding_shape[0] == TRT_DYNAMIC_DIM: # dynamic shape
69 size = batch_size * abs(trt.volume(binding_shape))
70 else:
71 size = abs(trt.volume(binding_shape))
72 dtype = trt.nptype(engine.get_binding_dtype(binding))
73 # Allocate host and device buffers
74 host_mem = cuda.pagelocked_empty(size, dtype)
75 device_mem = cuda.mem_alloc(host_mem.nbytes)
76 # Append the device buffer to device bindings
77 dbindings.append(int(device_mem))
78
79 # Append to the appropriate list (input/output)
80 if engine.binding_is_input(binding):
81 inputs.append(HostDeviceMem(host_mem, device_mem))
82 else:
83 outputs.append(HostDeviceMem(host_mem, device_mem))
84
85 return inputs, outputs, dbindings
86
87
88def infer(

Callers 2

inferFunction · 0.70
activate_implMethod · 0.50

Calls 4

volumeMethod · 0.80
HostDeviceMemClass · 0.70
absFunction · 0.50
appendMethod · 0.45

Tested by

no test coverage detected