Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help


title: “Tensorrt Llm — High-throughput LLM inference on NVIDIA GPUs” sidebar_label: “Tensorrt Llm” description: “High-throughput LLM inference on NVIDIA GPUs”

{/* 本页面由 website/scripts/generate-skill-docs.py 根据技能对应的 SKILL.md 文件自动生成。请直接编辑源文件 SKILL.md,而非此页面。 */}

Tensorrt Llm

在 NVIDIA GPU 上实现高吞吐量的大语言模型推理。

技能元数据

来源可选 — 通过 hermes skills install official/mlops/tensorrt-llm 安装
路径optional-skills/mlops\tensorrt-llm
版本1.0.1
开发者Orchestra Research
许可协议MIT
依赖项tensorrt-llm, torch
支持平台linux, macos
标签推理服务, TensorRT-LLM, NVIDIA, 推理优化, 高吞吐量, 低延迟, 生产环境, FP8, INT4, 动态批处理, 多GPU

参考:完整的 SKILL.md 文件

:::info 以下是当触发该技能时 Hermes 会加载的完整技能定义。技能启用后,智能体看到的指令即为此内容。
::

TensorRT-LLM

NVIDIA 提供的开源库,可在 NVIDIA GPU 上高效优化大语言模型推理性能。

何时使用 TensorRT-LLM

以下情况建议使用 TensorRT-LLM:

  • 在 NVIDIA GPU(A100、H100、GB200)上部署模型
  • 需要极高的吞吐量(Llama 3 每秒可处理 24,000 个以上令牌)
  • 实时应用对低延迟有严格要求
  • 处理量化后的模型(FP8、INT4、FP4)
  • 在多 GPU 或多节点环境中扩展部署

以下情况建议改用 vLLM:

  • 需要更简单的设置及以 Python 为主的 API
  • 希望使用 PagedAttention 而无需进行 TensorRT 编译
  • 在 AMD GPU 或非 NVIDIA 硬件上运行

以下情况建议改用 llama.cpp:

  • 在 CPU 或 Apple Silicon 平台上部署
  • 需要在无需 NVIDIA GPU 的情况下进行边缘部署
  • 希望使用更简单的 GGUF 量化格式

快速入门

安装

# Docker (recommended) — images are on NGC (nvcr.io), not Docker Hub.
# Replace x.y.z with the desired version (e.g. 1.2.1). Browse tags on NGC:
# https://catalog.ngc.nvidia.com/orgs/nvidia/teams/tensorrt-llm/containers/release/tags
docker pull nvcr.io/nvidia/tensorrt-llm/release:x.y.z

# pip install (current stable GA)
pip install tensorrt_llm

# Requires CUDA 13.2.1, TensorRT 10.x, 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
  }'

核心特性

性能优化

  • 传输中批量处理:在生成过程中实现动态批量化
  • 分页KV缓存:高效的内存管理机制
  • Flash Attention:优化的注意力计算内核
  • 量化技术:支持FP8、INT4、FP4格式,提升推理速度2-4倍
  • CUDA图优化:降低内核启动开销

并行处理能力

  • 张量并行(TP):将模型分布在多块GPU上运行
  • 流水线并行(PP):按层分配计算任务
  • 专家并行:专为混合专家模型设计
  • 多节点部署:突破单机限制实现扩展

高级功能

  • 推测解码:借助草稿模型加快生成速度
  • LoRA服务:高效的多适配器部署方案
  • 分离式服务:将预填充与生成过程分开处理

常见应用模式

量化模型(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 个令牌/秒
  • 延迟:每个令牌约 10 毫秒
  • 相较于 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 多种模型

参考资料

资源链接

  • 文档:https://nvidia.github.io/TensorRT-LLM/
  • GitHub 仓库:https://github.com/NVIDIA/TensorRT-LLM
  • 模型列表:https://huggingface.co/models?library=tensorrt_llm