(engine: trt.ICudaEngine, profile_idx: Optional[int] = None)
| 181 | # Allocates all buffers required for an engine, i.e. host/device inputs/outputs. |
| 182 | # If engine uses dynamic shapes, specify a profile to find the maximum input & output size. |
| 183 | def allocate_buffers(engine: trt.ICudaEngine, profile_idx: Optional[int] = None): |
| 184 | inputs = [] |
| 185 | outputs = [] |
| 186 | bindings = [] |
| 187 | stream = cuda_call(cudart.cudaStreamCreate()) |
| 188 | tensor_names = [engine.get_tensor_name(i) for i in range(engine.num_io_tensors)] |
| 189 | for binding in tensor_names: |
| 190 | # get_tensor_profile_shape returns (min_shape, optimal_shape, max_shape) |
| 191 | # Pick out the max shape to allocate enough memory for the binding. |
| 192 | shape = engine.get_tensor_shape(binding) if profile_idx is None else engine.get_tensor_profile_shape(binding, profile_idx)[-1] |
| 193 | shape_valid = np.all([s >= 0 for s in shape]) |
| 194 | if not shape_valid and profile_idx is None: |
| 195 | raise ValueError(f"Binding {binding} has dynamic shape, " +\ |
| 196 | "but no profile was specified.") |
| 197 | size = trt.volume(shape) |
| 198 | if engine.has_implicit_batch_dimension: |
| 199 | size *= engine.max_batch_size |
| 200 | dtype = np.dtype(trt.nptype(engine.get_tensor_dtype(binding))) |
| 201 | |
| 202 | # Allocate host and device buffers |
| 203 | bindingMemory = HostDeviceMem(size, dtype) |
| 204 | |
| 205 | # Append the device buffer to device bindings. |
| 206 | bindings.append(int(bindingMemory.device)) |
| 207 | |
| 208 | # Append to the appropriate list. |
| 209 | if engine.get_tensor_mode(binding) == trt.TensorIOMode.INPUT: |
| 210 | inputs.append(bindingMemory) |
| 211 | else: |
| 212 | outputs.append(bindingMemory) |
| 213 | return inputs, outputs, bindings, stream |
| 214 | |
| 215 | |
| 216 | # Frees the resources allocated in allocate_buffers |
nothing calls this directly
no test coverage detected