Pidoku

TensorRT and TensorRT-LLM

Module 15 Lesson 15.c 1h Difficulty 4/5

Prerequisites 15.a, 11.i, 13.e, 13.j


Before you start: words you need#

WordPlain meaning
Ahead-of-time (AOT) compilerA tool that turns a model into an optimised program before serving starts, instead of deciding what to do while requests arrive.
Engine (TensorRT)The file TensorRT produces: chosen kernels, fused layers, a memory plan and the weights, built for one GPU architecture.
AutotuningTrying several candidate kernels for the same operation on the real GPU and keeping the fastest.
FusionMerging several small operations into one kernel so the data stays on chip between them (13.j).
PyTorch backendRunning the model as ordinary PyTorch code, calling fast custom kernels, instead of as a pre-built TensorRT engine.
In-flight batchingNVIDIA’s name for continuous batching (12.j).
NVFP4NVIDIA’s 4-bit floating-point format with a small scale per block of 16 values, native on Blackwell (13.g).
ModelOptNVIDIA’s Model Optimizer library, which quantizes checkpoints (FP8, NVFP4, INT4) for TensorRT-LLM, vLLM and SGLang.

1. What is it?#

Two related NVIDIA products, which used to be one workflow and are now two:

  • TensorRT (v11.4, October 2026) is NVIDIA’s ahead-of-time inference compiler. It takes a model graph (usually ONNX), fuses layers, autotunes kernels on the actual GPU, plans memory and writes a hardware-specific engine file. It remains the fastest way to run vision, speech and other fixed-shape models on NVIDIA GPUs.
  • TensorRT-LLM is NVIDIA’s LLM serving engine. It began as an LLM layer on top of TensorRT engines. Since version 1.0 (September 2025) its default has been a PyTorch backend, and in July 2026 (the 1.3 release-candidate series) the TensorRT engine backend was removed: trtllm-build, the per-model convert_checkpoint.py scripts and LLM(backend="tensorrt") are gone. Hugging Face checkpoints now load directly.

Releases on 12 November 2026: the latest stable TensorRT-LLM is v1.2.1 (20 April 2026), which still carries the old engine path as a legacy option; 1.3 never left release candidate; v1.4.0rc3 (6 November 2026) is the current preview. Facts checked on 12 November 2026.


2. Why does it exist?#

TensorRT exists because a framework running eagerly (one operation at a time, decided at run time) leaves performance on the table. Kernel choice is a heuristic, fusion is incomplete, memory planning is cautious. If you fix the model, precision and shape ranges ahead of time, a compiler can optimise exhaustively, including measuring candidate kernels on your GPU.

TensorRT-LLM exists because NVIDIA wants the fastest possible LLM serving on its own GPUs, with kernels that ship on day one of each new architecture (Blackwell’s NVFP4, the NVL72 rack’s NVLink all-to-all). The original engine-building approach worked badly for LLMs:

the engine approach                     why it hurt for LLMs
-------------------                     --------------------
build takes minutes to hours            every model, GPU and config change is a rebuild
shape limits baked in                   max batch, max input, max sequence fixed at build
one engine per GPU architecture         the build matrix grew with every fleet change
new architectures land slowly           each model needed a hand-written TensorRT graph

The gains from compiling the whole graph were small next to the gains from a few excellent kernels (attention, GEMM, MoE, all-reduce). So NVIDIA kept the kernels and moved the rest to PyTorch, where new models are quick to add, then dropped the engine path.


3. Simple analogy#

TensorRT is a bespoke suit: measured to your exact body, it fits better than anything off the peg, but it takes time to make and if you change shape you start again. That is still the right deal for a model that does not change, such as an image classifier at the edge.

Modern TensorRT-LLM is a good off-the-peg suit with tailored parts: you take it home today (load any supported checkpoint), and the parts that matter most (the kernels) were made by the best tailor available for that cloth (NVIDIA’s own GPUs).


4. Tiny example: serving a model with TensorRT-LLM#

Shell
# install (Python 3.12 is the main supported version; pick the wheel for your CUDA)
pip install tensorrt_llm

# serve a Hugging Face checkpoint, OpenAI-compatible, on port 8000
trtllm-serve Qwen/Qwen3-8B --tp_size 1 --max_batch_size 64 --max_num_tokens 8192

curl http://localhost:8000/v1/chat/completions -H "Content-Type: application/json" \
    -d '{"model": "Qwen/Qwen3-8B", "messages": [{"role": "user", "content": "hi"}]}'

The same engine from Python, for offline batches:

Python
from tensorrt_llm import LLM, SamplingParams

llm = LLM(model="Qwen/Qwen3-8B")          # PyTorch backend; no build step
outs = llm.generate(["The capital of France is"], SamplingParams(max_tokens=16))
print(outs[0].outputs[0].text)

Compare the pre-2026 workflow, which many blog posts and older internal runbooks still show:

convert_checkpoint.py  → trtllm-build --max_batch_size 64 --max_input_len 4096 ...
                        → engine/ directory (per GPU architecture) → run.py or Triton

If you meet those commands, you are reading about TensorRT-LLM v1.2 or earlier.


5. Technical explanation#

TensorRT: what a build does#

TensorRT is still how you compile non-LLM models (vision encoders, ASR, embedding and reranking models) for NVIDIA GPUs, usually from ONNX with trtexec or the Python API:

  1. Graph optimisation. Constant folding, dead-code removal, layer fusion (convolution with bias and activation; attention blocks).
  2. Kernel selection by measurement. For each layer and shape, benchmark candidate kernels on the actual GPU and keep the fastest. This is why the build is slow and why it beats heuristic choice. cuBLAS’s own defaults are heuristics; TensorRT measures.
  3. Precision assignment. Apply the requested precision (FP16, BF16, FP8, INT8, FP4), with calibration where needed, inserting scale and dequantize steps.
  4. Memory planning. Compute each tensor’s lifetime and lay out one workspace with buffer reuse.
  5. Serialisation. Write the engine: kernels, weights and plan, for one GPU architecture and one TensorRT version.

Shape ranges are declared at build time as optimisation profiles (minimum, preferred and maximum shape per input). Inputs outside the range fail.

TensorRT-LLM today: a PyTorch engine with NVIDIA kernels#

The modern TensorRT-LLM has the same parts as any engine (15.a):

PartIn TensorRT-LLM
APItrtllm-serve (OpenAI-compatible: chat completions, completions, Responses, Anthropic Messages), gRPC, and the Python LLM API
SchedulerIn-flight batching, chunked prefill (“chunked context”), an overlap scheduler that prepares the next step while the GPU runs
KV cachePaged, with block reuse (prefix caching), host-memory offload, FP8 and NVFP4 KV cache. KV cache manager V2 is the default in 1.4
Model codePyTorch modules under tensorrt_llm/_torch/models/
KernelsNVIDIA’s trtllm-gen kernels, CUTLASS, FlashInfer and CuTe DSL kernels for attention, GEMM and MoE
ParallelismTensor, pipeline, expert and context parallelism; attention data parallelism for MoE models; wide expert parallelism across an NVL72 rack
Speculative decodingEAGLE-3, MTP, n-gram and draft models, all in “one-model” form (the drafter runs inside the same engine)
Disaggregationtrtllm-serve disaggregated, KV transfer over NIXL; usually driven by Dynamo (21.j)

Quantized checkpoints come from ModelOpt (FP8, NVFP4, INT4 AWQ, W4A8). The same checkpoints load in vLLM and SGLang, which removes much of the old lock-in.


6. Under the hood: what survived from the engine era#

What made the old TensorRT-LLM fast was never the graph compilation; it was a handful of kernels. Those survived the move to PyTorch:

attention     fused attention for prefill and decode, MLA kernels for DeepSeek-style
              models, sparse-attention kernels for long context
GEMM          FP8, NVFP4 and MXFP4 matrix multiplies generated per architecture
              (Hopper SM90, Blackwell SM100/SM103, consumer Blackwell SM120)
MoE           fused expert kernels and NVLink one-sided all-to-all for expert
              parallelism (14.e)
all-reduce    custom all-reduce for small messages, faster than NCCL at decode sizes
CUDA graphs   captured per batch size, like vLLM (13.k)

What PyTorch lost compared with a whole-graph compile, torch.compile and CUDA graphs win most of back (13.m). The remaining difference to vLLM and SGLang is mostly which kernels each engine has for your model on your GPU this month, not the framework.

Some cost of compilation remains: certain new kernels are just-in-time compiled on first start and cached on disk. TensorRT-LLM 1.4 notes that the first launch of DeepSeek-V4 on a B200 can spend 5–10 minutes compiling sparse-attention kernels, which matters for cold starts (20.g).


7. Performance implications#

  • Where TensorRT-LLM tends to lead: new NVIDIA hardware in its first months (NVFP4 on Blackwell, GB300, Vera Rubin), very large MoE models at rack scale, and low-latency decode with aggressive speculative decoding. NVIDIA’s own submissions dominate MLPerf Inference on NVIDIA hardware.
  • Where the gap is small: mainstream dense models on Hopper. vLLM and SGLang use many of the same kernels (FlashInfer, CUTLASS, and often NVIDIA-contributed code), so differences of more than 10–20% on the same precision and configuration are unusual and change with every release.
  • Benchmarks to distrust: a comparison that uses different precisions (FP8 against BF16), different speculative settings, or a tuned configuration against defaults. The tuning gap is often larger than the engine gap (15.f).
  • For TensorRT (non-LLM models): compiling a vision or speech model often gives a large speed-up over eager PyTorch at small batch, because those models have fixed shapes and many small layers to fuse. The size of the gain depends heavily on the model; measure your own.

8. Production implications#

  • NVIDIA only. TensorRT-LLM runs on NVIDIA GPUs only. A fleet that may add AMD or TPU capacity needs a second engine anyway.
  • Stable or preview? The last stable release is from April 2026 and still documents the removed engine path; most new work happens in release candidates. Pick one deliberately, pin it (the NGC container tag is the easiest pin), and read the release notes: 1.4 alone deprecates the V1 KV cache manager and the C++ cache transceiver, each with a three-month removal window.
  • Migrating old deployments. If you run engines built with trtllm-build, plan to move to the PyTorch backend: the TensorRT backend migration guide maps each removed piece to its replacement. The build matrix (GPU × version × model × config) and its CI disappear.
  • Shape limits still exist, but softer. --max_batch_size, --max_num_tokens and --max_seq_len are runtime settings now, not compiled-in limits. Still enforce the maximum prompt length at your gateway with a clear error.
  • Triton or Dynamo in front. TensorRT-LLM runs under Triton’s tensorrtllm backend (which now uses the PyTorch path) or as Dynamo workers. trtllm-serve alone is enough for one replica.
  • Validate quality on the deployed configuration, not on the source checkpoint: quantization and kernel choice change numerics (13.b).

9. Common mistakes#

  1. Following a pre-2026 tutorial. convert_checkpoint.py and trtllm-build no longer exist in current TensorRT-LLM.
  2. Assuming TensorRT-LLM means a slow build. That was true of the engine path; today start-up is weight loading, CUDA graph capture and some JIT kernel compilation, like other engines.
  3. Assuming the benchmark gap transfers. Benchmark your own model, precision and traffic.
  4. Using TensorRT-LLM for a fleet that is not all NVIDIA.
  5. Mixing up TensorRT and TensorRT-LLM. TensorRT is a general compiler you still use for non-LLM models; TensorRT-LLM is an LLM engine that no longer uses it for LLMs.
  6. Not re-validating quality after an upgrade. Kernel changes can move outputs.

10. Hands-on exercise#

  1. Serve and compare. Serve one 7–8B model with trtllm-serve and with vLLM on the same GPU, at the same precision. Measure throughput, p50/p95 time to first token and p50/p95 inter-token latency with a realistic length distribution. Report every number, including the ones that do not favour the engine you prefer.
  2. Quantize once, serve twice. Quantize the model to FP8 with ModelOpt. Serve the same checkpoint on TensorRT-LLM and on vLLM. Does it load unchanged in both?
  3. TensorRT for a non-LLM model. Export a small vision or embedding model to ONNX and build a TensorRT engine with trtexec. Compare latency at batch 1 and batch 32 with eager PyTorch. Then send an input outside the optimisation profile and observe the failure.
  4. Read a migration. Read the TensorRT backend migration guide and list what an old deployment must change: commands, configuration, artefact storage and CI.

11. Interview questions#

  1. What does a TensorRT build do that eager PyTorch cannot?
  2. Why did TensorRT-LLM move from TensorRT engines to a PyTorch backend, and what did it keep?
  3. Why must a TensorRT engine be rebuilt per GPU architecture?
  4. When would you choose TensorRT-LLM over vLLM or SGLang, and when not?
  5. Why is the benchmark gap between engines often smaller in practice than in vendor charts?
  6. Where would you still use plain TensorRT in an LLM platform?

12. Further reading#

Checked on 12 November 2026.

↑↓ navigate↵ openesc close

100% drag to pan · scroll or pinch to zoom · double-click to zoom in