[ PROMPT_NODE_22476 ]
tensorrt-llm
[ SKILL_DOCUMENTATION ]
# TensorRT-LLM
NVIDIA 的开源库,用于在 NVIDIA GPU 上以顶尖性能优化大模型推理。
## 何时使用 TensorRT-LLM
**在以下情况使用 TensorRT-LLM:**
- 部署在 NVIDIA GPU 上 (A100, H100, GB200)
- 需要最高吞吐量 (Llama 3 上超过 24,000 token/秒)
- 实时应用需要低延迟
- 使用量化模型 (FP8, INT4, FP4)
- 跨多个 GPU 或节点扩展
**在以下情况使用 vLLM:**
- 需要更简单的设置和 Python 优先的 API
- 需要无需 TensorRT 编译的 PagedAttention
- 使用 AMD GPU 或非 NVIDIA 硬件
**在以下情况使用 llama.cpp:**
- 部署在 CPU 或 Apple Silicon 上
- 需要无需 NVIDIA GPU 的边缘部署
- 需要更简单的 GGUF 量化格式
## 快速开始
### 安装
bash
# Docker (推荐)
docker pull nvidia/tensorrt_llm:latest
# pip 安装
pip install tensorrt_llm==1.2.0rc3
# 需要 CUDA 13.0.0, TensorRT 10.13.2, Python 3.10-3.12
### 基础推理
python
from tensorrt_llm import LLM, SamplingParams
# 初始化模型
llm = LLM(model="meta-llama/Meta-Llama-3-8B")
# 配置采样
sampling_params = SamplingParams(
max_tokens=100,
temperature=0.7,
top_p=0.9
)
# 生成
prompts = ["Explain quantum computing"]
outputs = llm.generate(prompts, sampling_params)
for output in outputs:
print(output.text)
### 使用 trtllm-serve 进行服务化
bash
# 启动服务器 (自动下载并编译模型)
trtllm-serve meta-llama/Meta-Llama-3-8B
--tp_size 4 # 张量并行 (4 个 GPU)
--max_batch_size 256
--max_num_tokens 4096
# 客户端请求
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 (动态批处理)**: 生成过程中的动态批处理
- **Paged KV cache**: 高效内存管理
- **Flash Attention**: 优化的注意力算子
- **量化**: FP8, INT4, FP4,推理速度提升 2-4 倍
- **CUDA graphs**: 减少算子启动开销
### 并行化
- **张量并行 (TP)**: 将模型拆分到多个 GPU
- **流水线并行 (PP)**: 按层分布
- **专家并行 (EP)**: 针对混合专家模型 (MoE)
- **多节点**: 扩展至单机之外
### 高级特性
- **推测解码 (Speculative decodi