File size: 10,004 Bytes
7a87926 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
"""
TensorRT export utilities for optimized inference.
TensorRT provides 5-10x speedup over standard PyTorch inference for production deployment.
Requires: NVIDIA GPU, TensorRT SDK, and ONNX model.
"""
import logging
from pathlib import Path
from typing import Dict, List, Optional
import numpy as np
logger = logging.getLogger(__name__)
# Try to import TensorRT
try:
import pycuda.driver as cuda
import tensorrt as trt
TENSORRT_AVAILABLE = True
except ImportError:
TENSORRT_AVAILABLE = False
logger.warning("TensorRT not available. Install with: " "pip install nvidia-tensorrt pycuda")
def check_tensorrt_available() -> bool:
"""Check if TensorRT is available."""
return TENSORRT_AVAILABLE
def build_tensorrt_engine(
onnx_path: Path,
engine_path: Path,
precision: str = "fp16",
max_batch_size: int = 1,
max_workspace_size: int = 1 << 30, # 1GB
min_timing_iterations: int = 1,
avg_timing_iterations: int = 8,
int8_calibration_cache: Optional[Path] = None,
) -> Path:
"""
Build TensorRT engine from ONNX model.
Args:
onnx_path: Path to ONNX model
precision: Precision mode: "fp32", "fp16", or "int8"
max_batch_size: Maximum batch size
max_workspace_size: Maximum workspace size in bytes
min_timing_iterations: Minimum timing iterations for optimization
avg_timing_iterations: Average timing iterations for optimization
int8_calibration_cache: Path to INT8 calibration cache (for INT8 mode)
Returns:
Path to saved TensorRT engine
"""
if not TENSORRT_AVAILABLE:
raise RuntimeError(
"TensorRT not available. Install with: pip install nvidia-tensorrt pycuda"
)
if not onnx_path.exists():
raise FileNotFoundError(f"ONNX model not found: {onnx_path}")
logger.info(f"Building TensorRT engine from {onnx_path}")
logger.info(f"Precision: {precision}, Max batch size: {max_batch_size}")
# Create TensorRT logger
trt_logger = trt.Logger(trt.Logger.WARNING)
# Create builder and network
builder = trt.Builder(trt_logger)
network = builder.create_network(1 << int(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH))
parser = trt.OnnxParser(network, trt_logger)
# Parse ONNX model
with open(onnx_path, "rb") as model:
if not parser.parse(model.read()):
logger.error("Failed to parse ONNX model")
for error in range(parser.num_errors):
logger.error(parser.get_error(error))
raise RuntimeError("Failed to parse ONNX model")
logger.info(f"ONNX model parsed successfully. Inputs: {network.num_inputs}")
# Configure builder
config = builder.create_builder_config()
config.max_workspace_size = max_workspace_size
# Set precision
if precision == "fp16":
if builder.platform_has_fast_fp16:
config.set_flag(trt.BuilderFlag.FP16)
logger.info("FP16 precision enabled")
else:
logger.warning("FP16 not supported on this platform, using FP32")
elif precision == "int8":
if builder.platform_has_fast_int8:
config.set_flag(trt.BuilderFlag.INT8)
logger.info("INT8 precision enabled")
if int8_calibration_cache:
# Load calibration cache
with open(int8_calibration_cache, "rb") as f:
config.int8_calibration_cache = f.read()
else:
logger.warning("INT8 not supported on this platform, using FP32")
# Set optimization profile (for dynamic shapes)
profile = builder.create_optimization_profile()
for i in range(network.num_inputs):
input_tensor = network.get_input(i)
shape = input_tensor.shape
# Set min, opt, max shapes (assuming batch dimension is first)
profile.set_shape(
input_tensor.name,
(1, *shape[1:]), # min
(max_batch_size, *shape[1:]), # opt
(max_batch_size, *shape[1:]), # max
)
config.add_optimization_profile(profile)
# Build engine
logger.info("Building TensorRT engine (this may take a while)...")
engine = builder.build_engine(network, config)
if engine is None:
raise RuntimeError("Failed to build TensorRT engine")
# Save engine
engine_path.parent.mkdir(parents=True, exist_ok=True)
with open(engine_path, "wb") as f:
f.write(engine.serialize())
logger.info(f"TensorRT engine saved to {engine_path}")
logger.info(f"Engine size: {engine_path.stat().st_size / 1024 / 1024:.2f} MB")
return engine_path
def load_tensorrt_engine(engine_path: Path):
"""
Load TensorRT engine from file.
Args:
engine_path: Path to TensorRT engine file
Returns:
TensorRT engine
"""
if not TENSORRT_AVAILABLE:
raise RuntimeError("TensorRT not available")
if not engine_path.exists():
raise FileNotFoundError(f"TensorRT engine not found: {engine_path}")
logger.info(f"Loading TensorRT engine from {engine_path}")
trt_logger = trt.Logger(trt.Logger.WARNING)
runtime = trt.Runtime(trt_logger)
with open(engine_path, "rb") as f:
engine = runtime.deserialize_cuda_engine(f.read())
if engine is None:
raise RuntimeError("Failed to load TensorRT engine")
logger.info("TensorRT engine loaded successfully")
return engine
class TensorRTInference:
"""
TensorRT inference wrapper.
Provides a simple interface for running inference with TensorRT engines.
"""
def __init__(self, engine_path: Path, device: int = 0):
"""
Initialize TensorRT inference.
Args:
engine_path: Path to TensorRT engine file
device: CUDA device ID
"""
if not TENSORRT_AVAILABLE:
raise RuntimeError("TensorRT not available")
self.engine = load_tensorrt_engine(engine_path)
self.context = self.engine.create_execution_context()
self.device = device
# Allocate buffers
self.inputs = []
self.outputs = []
self.bindings = []
self.stream = cuda.Stream()
for i in range(self.engine.num_io_tensors):
name = self.engine.get_tensor_name(i)
shape = self.engine.get_tensor_shape(name)
dtype = trt.nptype(self.engine.get_tensor_dtype(name))
size = trt.volume(shape) * np.dtype(dtype).itemsize
# Allocate host and device buffers
host_mem = cuda.pagelocked_empty(size, dtype)
device_mem = cuda.mem_alloc(host_mem.nbytes)
self.bindings.append(int(device_mem))
if self.engine.get_tensor_mode(name) == trt.TensorIOMode.INPUT:
self.inputs.append({"name": name, "host": host_mem, "device": device_mem})
else:
self.outputs.append({"name": name, "host": host_mem, "device": device_mem})
logger.info(
f"TensorRT inference initialized: {len(self.inputs)} inputs, "
f"{len(self.outputs)} outputs"
)
def __call__(self, *inputs: np.ndarray) -> List[np.ndarray]:
"""
Run inference.
Args:
*inputs: Input arrays (numpy)
Returns:
List of output arrays
"""
# Copy inputs to device
for i, inp in enumerate(self.inputs):
np.copyto(inp["host"], inputs[i].ravel())
cuda.memcpy_htod_async(inp["device"], inp["host"], self.stream)
# Set input shapes
for i, inp in enumerate(self.inputs):
self.context.set_input_shape(inp["name"], inputs[i].shape)
# Run inference
self.context.execute_async_v2(bindings=self.bindings, stream_handle=self.stream.handle)
# Copy outputs from device
outputs = []
for out in self.outputs:
cuda.memcpy_dtoh_async(out["host"], out["device"], self.stream)
outputs.append(out["host"])
self.stream.synchronize()
# Reshape outputs
reshaped_outputs = []
for i, out in enumerate(self.outputs):
shape = self.context.get_tensor_shape(out["name"])
reshaped_outputs.append(outputs[i].reshape(shape))
return reshaped_outputs
def __del__(self):
"""Cleanup CUDA resources."""
if hasattr(self, "stream"):
del self.stream
def benchmark_tensorrt(
engine_path: Path,
sample_inputs: List[np.ndarray],
num_runs: int = 100,
warmup_runs: int = 10,
) -> Dict[str, float]:
"""
Benchmark TensorRT inference.
Args:
engine_path: Path to TensorRT engine
sample_inputs: Sample input arrays
num_runs: Number of benchmark runs
warmup_runs: Number of warmup runs
Returns:
Dict with benchmark results (fps, latency_ms, etc.)
"""
if not TENSORRT_AVAILABLE:
raise RuntimeError("TensorRT not available")
logger.info(f"Benchmarking TensorRT engine: {engine_path}")
inference = TensorRTInference(engine_path)
# Warmup
for _ in range(warmup_runs):
_ = inference(*sample_inputs)
# Benchmark
import time
times = []
for _ in range(num_runs):
start = time.time()
_ = inference(*sample_inputs)
times.append(time.time() - start)
avg_time = np.mean(times)
std_time = np.std(times)
fps = 1.0 / avg_time
results = {
"fps": fps,
"latency_ms": avg_time * 1000,
"latency_std_ms": std_time * 1000,
"min_latency_ms": np.min(times) * 1000,
"max_latency_ms": np.max(times) * 1000,
}
logger.info("TensorRT Benchmark Results:")
logger.info(f" FPS: {fps:.2f}")
logger.info(f" Latency: {avg_time * 1000:.2f}ms ± {std_time * 1000:.2f}ms")
logger.info(f" Min: {np.min(times) * 1000:.2f}ms, " f"Max: {np.max(times) * 1000:.2f}ms")
return results
|