RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootGet startedLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Eruo Fredoline
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
  • Suggest a feature
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. How we make money →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /Courses
  5. /Model Optimization for Local Inference
  6. /Ch. 12
Model Optimization for Local Inference

12. TensorRT-LLM

Chapter 12 of 18 · 20 min
KEY INSIGHT

TensorRT-LLM's compilation step enables optimizations impossible in runtime—kernel fusion, operator fusion, and precision calibration combine to exceed runtime-only solutions.

TensorRT-LLM provides the highest performance inference for NVIDIA GPUs, achieving 2-5× speedup over naive CUDA implementations. It compiles models into optimized CUDA kernels with automatic graph optimization, layer fusion, and precision calibration.

Installation requires matching your CUDA version:

# Check CUDA version
nvidia-smi | grep "CUDA Version"
# Expected: CUDA Version: 12.1 or 12.2

# Clone and build TensorRT-LLM
git clone https://github.com/NVIDIA/TensorRT-LLM.git
cd TensorRT-LLM
git submodule update --init --recursive

# Build for your CUDA version
pip install tensorrtllm_backend --extra-index-url https://pypi.nvidia.com

Model compilation converts HuggingFace checkpoints to TensorRT-LLM format:

# compile_model.py
import torch
from tensorrt_llm.models import LLaMAForCausalLM
from tensorrt_llm.hlapi import BuilderConfig, QuantConfig, TRTModel

model = LLaMAForCausalLM.from_hugging_face(
    "meta-llama/Llama-2-70b-hf",
    dtype=torch.float16,
)

# Quantization configuration
quant_config = QuantConfig(
    quant_algo='FP8',      # 8-bit floating point
    kv_cache_quant_algo='FP8',
)

# Compilation settings
builder_config = BuilderConfig(
    quantization=quant_config,
    hardware_compatibility='AMPERE_PLUS',  # RTX 30/40, A100, H100
    enable_fp8=True,
    builder_opt=3,
)

# Convert and optimize
model.compile(builder_config)
model.save("llama-70b-trtllm")

Multi-GPU tensor parallelism:

# Tensor parallelism across 4 GPUs
python -m tensorrt_llm.commands.build \
    --model_dir meta-llama/Llama-2-70b-hf \
    --output_dir ./llama-70b-trtllm-4gpu \
    --quantization fp8 \
    --tensor_parallel 4 \
    --hf_model_convert \
    --max_batch_size 128 \
    --max_input_len 4096 \
    --max_new_tokens 1024

TensorRT-LLM uses custom inference runtime:

# Inference with TensorRT-LLM runtime
from tensorrt_llm.runtime import LLMEngine

engine = LLMEngine.from_dir(
    "llama-70b-trtllm-4gpu",
    temperature=0.8,
    max_output_len=512,
)

# Streaming inference
for output in engine.generate_stream("Explain attention mechanism"):
    print(output.content, end="", flush=True)

Comparison with vLLM:

Metric vLLM TensorRT-LLM
Max throughput High Highest
Latency (p50) ~50ms ~20ms
Multi-GPU scaling Good Excellent
Model support Broad Optimized for Llama, GPT, Mistral
Configuration complexity Medium High
Update frequency Weekly Monthly
EXERCISE

Compile the same model with both FP16 and FP8 quantization in TensorRT-LLM. Measure throughput and latency difference. Calculate the quality impact using perplexity evaluation.

← Chapter 11
vLLM Optimization
Chapter 13 →
Pruning: Structured vs Unstructured