npx skills add ...
npx skills add nvidia/tensorrt --skill trt-cpp-runtime-quickstart
Load and run a TensorRT engine (.plan / .engine) from C++ using the TensorRT 11 / 10.x **modern Runtime API**, avoiding the deprecated TRT 8.x binding-index APIs that older guidance still promotes. Use whenever the user asks about loading or running a TensorRT .plan/.engine from C++, even on "minimal example" requests — without this skill the default reply uses deprecated enqueueV2-style code. Also use when the user hits "Engine plan file is generated on an incompatible device", deserializeCudaEngine returns nullptr, gets an enqueueV2 / IStreamReader deprecation warning, or wants to stream a .plan via IStreamReaderV2. Triggers: TensorRT C++ inference, load TensorRT plan C++, run .plan from C++, IRuntime example, deserializeCudaEngine, enqueueV3, enqueueV2 deprecated, setTensorAddress, getBindingIndex, IStreamReaderV2, libnvinfer C++. NOT for building engines (`trt-onnx-quickstart`), Python deploy, plugins, multi-GPU.
npx skills add nvidia/tensorrt --skill trt-cpp-runtime-quickstart
Load a serialized TensorRT engine from disk and run inference from C++ using only the modern Runtime API. Produces a minimal, copy-pasteable deploy harness that drops next to any .plan / .engine file and extends to production.
Reference samples to open before writing new code:
quickstart/SemanticSegmentation/tutorial-runtime.cpp — cleanest minimal load-and-run example. Mirrors Steps 1–7 below.samples/sampleOnnxMNIST/sampleOnnxMNIST.cpp — end-to-end sample that also builds the engine; the runtime portion shows realistic I/O wiring.include/NvInferRuntime.h — read IRuntime, ICudaEngine, IExecutionContext, IStreamReaderV2.| Situation | Use this skill? |
|---|---|
You have a .plan/.engine and need to run it from a C++ binary | Yes |
You need a minimal harness that uses enqueueV3 + setTensorAddress | Yes |
You want to load an engine from a std::istream or large file via IStreamReaderV2 | Yes |
You need to wire dynamic shapes (setInputShape) before inference | Yes |
| You are building / optimizing the engine (calibration, INT8, sparsity, builder configs) | No - use trtexec or IBuilder directly |
| You are deploying in Python | No - use tensorrt Python bindings |
You are writing a plugin (IPluginV3) or custom layer | No - separate plugin skill |
| You need multi-GPU, MPS, MIG, or process-level orchestration | No - out of scope |
NvInferRuntime.h is on the include path
and libnvinfer.so is on the link path. On a TRT dev container these are
in /usr/include/x86_64-linux-gnu/ and /usr/lib/x86_64-linux-gnu/ (or
/opt/tensorrt/... for tarball installs).cuda_runtime_api.h and libcudart.so must
be reachable; nvcc --version should match the CUDA version the engine
was built against..plan/.engine file built on the same
major TRT version and the same GPU architecture (compute capability) you
will deploy on. Engines are not portable across major TRT versions or
across SMs unless the builder was given --hardwareCompatibilityLevel.g++ >= 9 or clang++ >= 10).The runtime owns engine deserialization and must outlive every
ICudaEngine it creates. Construct one per process for typical deployments.
A custom logger is mandatory - TensorRT does not log internally. Keep it process-global so deserialization warnings (version skew, calibrator mismatch) are not lost.
For small/medium engines (< ~1 GiB) read the whole file into a
std::vector<char> and hand the pointer to
IRuntime::deserializeCudaEngine(blob, size). This is what the
SemanticSegmentation tutorial does and the simplest correct path:
For very large engines, or when the bytes live behind a stream (HTTP,
mmap'd archive, encrypted store), implement an IStreamReaderV2 - see
Step 3.
IStreamReader (v1) is deprecated in TensorRT 11.0. Always use
IStreamReaderV2: it reads into both host and device memory and is the
only stream-reader form guaranteed for new code. Subclass and implement
read(...) and seek(...):
ICudaEngine is thread-safe for read-only queries; IExecutionContext
is not - allocate one per inference thread.
Enumerate I/O tensors via getNbIOTensors() + getIOTensorName(i). Use
getTensorIOMode, getTensorDataType, and getTensorShape to size and
allocate buffers. Set every tensor address before enqueueV3 - the
modern API has no implicit binding-index map.
Always call setInputShape for dynamic inputs before querying output
shapes - the latter depends on the former.
enqueueV3(stream) is the only non-deprecated enqueue API;
enqueueV2/execute* are gone in modern flows.
If you reuse buffers across iterations, skip the per-call
setTensorAddress - addresses persist on the context until overwritten.
Destroy in reverse construction order: contexts -> engines -> runtime,
then free CUDA memory and destroy the stream. With std::unique_ptr this
is automatic as long as the context is declared after the engine, and
the engine after the runtime. Free cudaMalloc allocations explicitly
(RAII wrapper recommended).
Wire the steps above into your application's build system. For a standalone smoke test, a minimal build is:
| Symptom | Likely cause |
|---|---|
deserializeCudaEngine returns nullptr, log says "version tag" | Engine built on a different TRT major version. Rebuild on the deploy version |
nullptr with "engine plan file is generated on an incompatible device" | SM mismatch. Rebuild on the target SM or use --hardwareCompatibilityLevel |
enqueueV3 returns false, log mentions "Tensor X has no address" | Forgot setTensorAddress for one of the I/O tensors |
enqueueV3 false, "shape" in message | Forgot setInputShape for a dynamic input, or supplied an out-of-profile shape |
cudaErrorIllegalAddress on H->D / D->H copy | Mismatched element count / dtype between host buffer and engine tensor |
| Process crashes inside TRT during destruction | Wrong destruction order - context outlived engine, or engine outlived runtime |
cudaErrorMemoryAllocation during context creation | Workspace too big for the device; rebuild with smaller workspace |
IStreamReader v1. Deprecated in TRT 11.0. Use
IStreamReaderV2 (note cudaStream_t parameter on read).enqueueV2 / execute / binding indices. These are
legacy paths; the only stable modern path is name-based
setTensorAddress + enqueueV3.IExecutionContext per thread. Sharing contexts across threads
is undefined behavior; sharing the engine is fine.enqueueV3 must outlive
the inference. Destroying it while work is in flight crashes or corrupts
output.cudaMemcpy with
enqueueV3 on a stream serializes the GPU; always pair enqueueV3
with cudaMemcpyAsync on the same stream..plan is tied to (TRT major version, GPU SM,
CUDA major version). Never check engines into a repo without recording
these three facts.createInferRuntime must
outlive the runtime; a stack-local logger in main is fine, a function-
scope local is a use-after-free.setWeightStreamingBudgetV2,
IRefitter); out of scope here.*