πŸ€– AI/ML

TensorRT-LLM

TensorRT for Large Language Models

An LLM (large language model) inference optimization library developed by NVIDIA. Delivers top inference speed on GPUs and supports advanced techniques such as In-flight Batching, KV cache optimization, and FP8/INT8 quantization.

πŸ“– Detailed Explanation

TensorRT-LLM is a library that specializes NVIDIA's TensorRT deep learning inference engine for large language models. In addition to the graph optimization, kernel auto-tuning, and precision calibration features of the existing TensorRT, it provides features optimized for the autoregressive generation pattern specific to LLMs. As of 2025, it is one of the solutions that provide the fastest LLM inference performance.

The core feature, In-flight Batching (continuous batching), dynamically batches requests of different lengths. Conventional static batching had to wait for the longest sequence to complete, but In-flight Batching immediately returns completed requests and inserts new ones. This improves throughput by up to 4-8x. Paged KV Cache manages GPU memory in page units, enabling more concurrent requests to be handled.

TensorRT-LLM supports various precisions such as FP16, BF16, INT8, INT4, and FP8. In particular, using the FP8 Tensor Core of the H100 GPU can deliver nearly 2x the throughput compared to FP16. It is compatible with various quantization techniques such as GPTQ, AWQ, and SmoothQuant, so inference speed can be maximized while minimizing quality loss.

Supported models include major open-source LLMs such as LLaMA, Mistral, GPT-J, Falcon, Gemma, and Qwen. By integrating with Triton Inference Server, model ensembles, A/B testing, and dynamic batching can be easily implemented in production environments. It also supports Multi-Instance GPU (MIG) and multi-node Tensor Parallelism, enabling distributed inference of large models.

πŸ’» Code Example

This is an example of LLaMA model inference using TensorRT-LLM.

# TensorRT-LLM μ„€μΉ˜ 및 λͺ¨λΈ λ³€ν™˜
# pip install tensorrt-llm

from tensorrt_llm import LLM, SamplingParams
from tensorrt_llm.hlapi import BuildConfig
import torch

# 1. κ°„λ‹¨ν•œ μ‚¬μš©λ²• (High-Level API)
# Hugging Face λͺ¨λΈ 직접 λ‘œλ“œ 및 μ΅œμ ν™”
llm = LLM(
    model="meta-llama/Llama-2-7b-chat-hf",
    tensor_parallel_size=1,  # GPU 수
    dtype="float16"
)

# μƒ˜ν”Œλ§ νŒŒλΌλ―Έν„° μ„€μ •
sampling_params = SamplingParams(
    temperature=0.7,
    top_p=0.9,
    max_tokens=256
)

# μΆ”λ‘  μ‹€ν–‰
prompts = [
    "인곡지λŠ₯의 λ―Έλž˜μ— λŒ€ν•΄ μ„€λͺ…ν•΄μ£Όμ„Έμš”.",
    "Python으둜 ν€΅μ†ŒνŠΈ μ•Œκ³ λ¦¬μ¦˜μ„ μž‘μ„±ν•΄μ£Όμ„Έμš”."
]

outputs = llm.generate(prompts, sampling_params)
for output in outputs:
    print(f"μž…λ ₯: {output.prompt}")
    print(f"좜λ ₯: {output.outputs[0].text}")
    print("-" * 50)


# 2. κ³ κΈ‰ μ‚¬μš©λ²• - μ—”μ§„ λΉŒλ“œ 및 μ΅œμ ν™”
from tensorrt_llm.builder import Builder
from tensorrt_llm.models import LLaMAForCausalLM

# λΉŒλ“œ μ„€μ • (INT8 μ–‘μžν™” 포함)
build_config = BuildConfig(
    max_batch_size=64,
    max_input_len=2048,
    max_output_len=512,
    max_beam_width=1,
    strongly_typed=True,
    builder_opt_level=5,  # μ΅œλŒ€ μ΅œμ ν™”
)

# INT8 μ–‘μžν™” μ„€μ •
build_config.plugin_config.update({
    "use_paged_context_fmha": True,
    "use_fp8_context_fmha": False,  # H100μ—μ„œ True
    "gemm_plugin": "auto",
    "gpt_attention_plugin": "auto"
})

# μ—”μ§„ λΉŒλ“œ
builder = Builder()
engine = builder.build_engine(
    model_dir="./llama-7b-hf",
    build_config=build_config,
    output_dir="./llama-7b-engine"
)

print("TensorRT-LLM μ—”μ§„ λΉŒλ“œ μ™„λ£Œ!")


# 3. Triton Inference Server 배포 μ„€μ •
triton_config = """
name: "llama_tensorrt_llm"
backend: "tensorrtllm"
max_batch_size: 64

model_transaction_policy {
  decoupled: True
}

input [
  {
    name: "text_input"
    data_type: TYPE_STRING
    dims: [ 1 ]
  },
  {
    name: "max_tokens"
    data_type: TYPE_INT32
    dims: [ 1 ]
  }
]

output [
  {
    name: "text_output"
    data_type: TYPE_STRING
    dims: [ -1 ]
  }
]

instance_group [
  {
    count: 1
    kind: KIND_GPU
  }
]
"""
print(triton_config)

πŸ“Š Performance & Cost

TensorRT-LLM performance benchmark (as of January 2025) - based on the LLaMA 2 70B model.

Inference Performance by GPU

GPU Throughput (tokens/s) Latency (ms/token) Concurrent Requests
H100 80GB (FP8) ~10,000+ ~5-8 256+
H100 80GB (FP16) ~6,000 ~8-12 128+
A100 80GB (FP16) ~2,200 ~15-20 64+
A100 40GB (INT8) ~1,800 ~18-25 32+
RTX 4090 (INT4) ~800 ~30-40 8+

Performance Improvement Figures

  • H100 FP8 vs A100 FP16: up to 4.6x higher throughput
  • With In-flight Batching: up to 8x throughput improvement
  • Compared to existing PyTorch/HuggingFace: up to 8x faster inference

Comparison with Other Inference Engines

Characteristics TensorRT-LLM vLLM llama.cpp
Target Hardware NVIDIA GPU NVIDIA GPU CPU/Various GPUs
Maximum Performance Highest High Medium
Configuration complexity High Low Low
Paged Attention Supported Supported Not supported
Production Deployment Triton Integration Self-Hosted Server Manual Setup

TensorRT-LLM is open source and free to use. NVIDIA GPU required

πŸ—£οΈ Say It Like This in Practice

"After building the engine with TensorRT-LLM, it got about 30% faster than vLLM"

β†’ An expression of feeling the performance improvement after TensorRT-LLM's graph optimization is applied

"After applying in-flight batching, our concurrent throughput jumped nearly 5x"

β†’ Throughput improvement due to the In-flight Batching (continuous batching) feature

"Running FP8 on the H100 gives over 4x the token processing speed compared to the A100"

β†’ Explanation of the performance improvement using the H100's FP8 Tensor Core

⚠️ Common Mistakes & Cautions

Underestimating engine build time

Building a TensorRT-LLM engine takes from tens of minutes to several hours depending on model size. Be sure to include build time in your production deployment schedule.

Setting max_batch_size too high

Setting max_batch_size too large when building the engine causes GPU out-of-memory errors. Set it to fit your actual workload.

GPU architecture compatibility

A built engine is tied to a specific GPU architecture. An engine built for the A100 does not work on the H100. A separate build is required for each GPU type.

Missing quantization quality validation

INT8/INT4 quantization increases speed but may degrade quality. Be sure to validate quality with a benchmark dataset.

πŸ”— Related Terms

πŸ“š Learn More