Tensorrt Llm
利用 NVIDIA TensorRT 優化大語言模型(LLM)推理,以實現最大吞吐量和最低延遲。適用於在 NVIDIA GPU(A100/H100)上進行生產部署,當您需要比 PyTorch 快 10-100 倍的推理速度,或需要服務於支持量化(FP8/INT4)、飛行中批處理(in-flight batching)和多 GPU 擴展的模型時。
技能元數據
| 來源 | 可選 — 使用 hermes skills install official/mlops/tensorrt-llm 安裝 |
| 路徑 | optional-skills/mlops/tensorrt-llm |
| 版本 | 1.0.0 |
| 作者 | Orchestra Research |
| 許可證 | MIT |
| 依賴項 | tensorrt-llm, torch |
| 標籤 | Inference Serving, TensorRT-LLM, NVIDIA, Inference Optimization, High Throughput, Low Latency, Production, FP8, INT4, In-Flight Batching, Multi-GPU |
參考:完整 SKILL.md
信息
以下是 Hermes 在觸發此技能時加載的完整技能定義。這是技能激活時代理所看到的指令。
TensorRT-LLM
NVIDIA 的開源庫,用於在 NVIDIA GPU 上以尖端性能優化 LLM 推理。
何時使用 TensorRT-LLM
在以下情況使用 TensorRT-LLM:
- 在 NVIDIA GPU(A100、H100、GB200)上部署
- 需要最大吞吐量(在 Llama 3 上超過 24,000 tokens/sec)
- 實時應用需要低延遲
- 使用量化模型(FP8、INT4、FP4)
- 跨多個 GPU 或節點進行擴展
在以下情況改用 vLLM:
- 需要更簡單的設置和優先使用 Python 的 API
- 希望使用 PagedAttention 而無需 TensorRT 編譯
- 使用 AMD GPU 或非 NVIDIA 硬件
在以下情況改用 llama.cpp:
- 在 CPU 或 Apple Silicon 上部署
- 需要在沒有 NVIDIA GPU 的情況下進行邊緣部署
- 希望使用更簡單的 GGUF 量化格式
快速開始
安裝
# Docker (recommended)
docker pull nvidia/tensorrt_llm:latest
# pip install
pip install tensorrt_llm==1.2.0rc3
# Requires CUDA 13.0.0, TensorRT 10.13.2, Python 3.10-3.12
基本推理
from tensorrt_llm import LLM, SamplingParams
# Initialize model
llm = LLM(model="meta-llama/Meta-Llama-3-8B")
# Configure sampling
sampling_params = SamplingParams(
max_tokens=100,
temperature=0.7,
top_p=0.9
)
# Generate
prompts = ["Explain quantum computing"]
outputs = llm.generate(prompts, sampling_params)
for output in outputs:
print(output.text)
使用 trtllm-serve 提供服務
# Start server (automatic model download and compilation)
trtllm-serve meta-llama/Meta-Llama-3-8B \
--tp_size 4 \ # Tensor parallelism (4 GPUs)
--max_batch_size 256 \
--max_num_tokens 4096
# Client request
curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "meta-llama/Meta-Llama-3-8B",
"messages": [{"role": "user", "content": "Hello!"}],
"temperature": 0.7,
"max_tokens": 100
}'
主要特性
性能優化
- 飛行中批處理(In-flight batching):生成過程中的動態批處理
- 分頁 KV 緩存(Paged KV cache):高效的內存管理
- Flash Attention:優化的注意力機制內核
- 量化:FP8、INT4、FP4,推理速度提升 2-4 倍
- CUDA 圖(CUDA graphs):減少內核啟動開銷
並行性
- 張量並行(TP):跨 GPU 拆分模型
- 流水線並行(PP):按層分佈
- 專家並行:適用於混合專家(Mixture-of-Experts)模型
- 多節點:超越單機擴展
高級功能
- 投機解碼(Speculative decoding):使用草稿模型加速生成
- LoRA 服務:高效的多適配器部署
- 解耦服務(Disaggregated serving):分離預填充(prefill)和生成階段
常見模式
量化模型(FP8)
from tensorrt_llm import LLM
# Load FP8 quantized model (2× faster, 50% memory)
llm = LLM(
model="meta-llama/Meta-Llama-3-70B",
dtype="fp8",
max_num_tokens=8192
)
# Inference same as before
outputs = llm.generate(["Summarize this article..."])
多 GPU 部署
# Tensor parallelism across 8 GPUs
llm = LLM(
model="meta-llama/Meta-Llama-3-405B",
tensor_parallel_size=8,
dtype="fp8"
)
批量推理
# Process 100 prompts efficiently
prompts = [f"Question {i}: ..." for i in range(100)]
outputs = llm.generate(
prompts,
sampling_params=SamplingParams(max_tokens=200)
)
# Automatic in-flight batching for maximum throughput
性能基準測試
Meta Llama 3-8B(H100 GPU):
- 吞吐量:24,000 tokens/sec
- 延遲:每 token 約 10ms
- 對比 PyTorch:快 100 倍
Llama 3-70B(8× A100 80GB):
- FP8 量化:比 FP16 快 2 倍
- 內存:使用 FP8 減少 50%
支持的模型
- LLaMA 系列:Llama 2、Llama 3、CodeLlama
- GPT 系列:GPT-2、GPT-J、GPT-NeoX
- Qwen:Qwen、Qwen2、QwQ
- DeepSeek:DeepSeek-V2、DeepSeek-V3
- Mixtral:Mixtral-8x7B、Mixtral-8x22B
- 視覺:LLaVA、Phi-3-vision
- HuggingFace 上的 100+ 模型