跳到主要内容

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+ 模型

参考资料

资源