Loading...

文章背景图

TensorRT-LLM:NVIDIA 官方 GPU 优化大模型推理引擎安装与使用指南

2026-10-25
0
- 字
- 分钟
|

TensorRT-LLM:NVIDIA 官方 GPU 优化大模型推理引擎安装与使用指南

一、官方信息

项目名称TensorRT-LLM
仓库地址https://github.com/NVIDIA/TensorRT-LLM
官方文档https://nvidia.github.io/TensorRT-LLM
最新更新持续高频迭代(2026-10-25)
Star 数11k+
项目定位NVIDIA 官方出品,基于 TensorRT 深度编译优化,面向 A100/H100/B200 数据中心级 GPU 的生产级推理引擎

二、项目背景

TensorRT-LLM 是 NVIDIA 在 2023 年底推出的大模型推理编译器,把 PyTorch 模型图编译成高度优化的 TensorRT 引擎,配合自定义 CUDA Kernel、In-Flight Batching、Paged KV Cache、FP8/INT4 量化、张量并行与流水线并行,在 H100/B200 上达到业界最高吞吐。它是 NVIDIA NIM、DGX Cloud 等商业产品的底层引擎。

与 vLLM、LMDeploy 等偏应用层框架不同,TensorRT-LLM 更接近"编译器",需要先 build engine 再 deploy,部署链路稍长但性能上限最高。适合对延迟和吞吐有极致要求的企业级生产场景,例如互联网公司在线客服、金融问答等高并发业务。

三、核心特性

  • NVIDIA 官方深度优化:针对 Hopper (H100/H200)、Ada (L40S)、Ampere (A100) 架构量身定制 Kernel。
  • FP8 量化:H100 上原生支持 FP8 推理,吞吐翻倍且精度损失极小。
  • In-Flight Batching:连续批处理,新请求无需等待批次完成即可插入。
  • Paged KV Cache:KV Cache 分页管理,显存利用率 > 95%。
  • 多卡并行:原生支持张量并行 TP + 流水线并行 PP,可扩展到 70B/405B 模型。
  • OpenAI 兼容 Server:内置 trtllm-serve,一条命令起 API。
  • MoE 支持:DeepSeek-V3/R1、Mixtral 等 MoE 模型通过 Expert Parallel 高效部署。

四、环境要求

  • 操作系统:Linux(Ubuntu 20.04/22.04)
  • GPU:NVIDIA 数据中心级或游戏卡,算力 ≥ 8.0(A10/A100/3090/4090/L40S/H100)
  • 驱动:≥ 550
  • CUDA:12.4 / 12.5
  • TensorRT:10.x
  • Docker:推荐使用官方 NGC 镜像,避免环境地狱

五、详细部署步骤

方式一:Docker NGC 镜像(官方推荐)

# 1. 拉取官方镜像(已内置所有依赖)
docker pull nvidia/trt_llm:latest

# 2. 启动容器
docker run -it --rm --gpus all --ipc=host \
  -v /path/to/models:/models \
  -v /path/to/engines:/engines \
  nvidia/trt_llm:latest bash

# 3. 容器内验证
python3 -c "import tensorrt_llm; print(tensorrt_llm.__version__)"

方式二:pip 安装(仅支持特定 Python/CUDA 组合)

# 需要 Python 3.10 + CUDA 12.4
pip install tensorrt_llm==0.13.0
pip install tensorrt_llm_cuda12 --extra-index-url https://pypi.nvidia.com

# 验证
python3 -c "import tensorrt_llm; print(tensorrt_llm.__version__)"

构建 LLaMA 模型 Engine(核心步骤)

# 进入容器后

# 1. 克隆仓库(拿示例脚本)
git clone https://github.com/NVIDIA/TensorRT-LLM.git
cd TensorRT-LLM
git submodule update --init --recursive

# 2. 安装依赖
pip install -r requirements-dev.txt

# 3. 下载 HuggingFace 模型(以 Llama-3.1-8B 为例)
huggingface-cli download meta-llama/Meta-Llama-3.1-8B-Instruct \
  --local-dir /models/Llama-3.1-8B-Instruct

# 4. 把 HF 模型转换成 TensorRT-LLM checkpoint
python3 examples/llama/convert_checkpoint.py \
  --model_dir /models/Llama-3.1-8B-Instruct \
  --output_dir /engines/llama-8b-ckpt \
  --dtype float16 \
  --tp_size 1

# 5. 构建 TensorRT engine
trtllm-build \
  --checkpoint_dir /engines/llama-8b-ckpt \
  --output_dir /engines/llama-8b-engine \
  --gpt_attention_plugin float16 \
  --use_paged_context_fmha enable \
  --max_batch_size 32 \
  --max_input_len 4096 \
  --max_output_len 4096

启动推理

# 命令行对话
python3 examples/run.py \
  --engine_dir /engines/llama-8b-engine \
  --tokenizer_dir /models/Llama-3.1-8B-Instruct \
  --input_text "你好,请介绍自己"

启动 OpenAI 兼容服务(trtllm-serve)

# 新版直接支持一条命令起服务
trtllm-serve /engines/llama-8b-engine \
  --host 0.0.0.0 \
  --port 8000 \
  --backend ppp \
  --max_batch_size 32

# 调用
curl http://localhost:8000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "llama-8b",
    "messages": [{"role":"user","content":"写一首诗"}]
  }'

FP8 量化(H100 专属,性能翻倍)

# 1. 量化 HF 模型
python3 examples/llama/quantize.py \
  --model_dir /models/Llama-3.1-70B-Instruct \
  --dtype float16 \
  --qformat fp8 \
  --output_dir /engines/llama-70b-fp8

# 2. 构建 engine 时加 --use_fp8_context_fmha enable
trtllm-build --checkpoint_dir /engines/llama-70b-fp8 ...

六、常见问题(FAQ)

Q1:build engine 时报 out of memory?
A:build 过程需要加载完整权重,70B FP16 需 140GB 显存。可减小 --max_batch_size,或用多卡 build(--tp_size 4)。

Q2:支持消费级显卡 4090 吗?
A:可以,但 FP8 在 4090 上不支持(Ada 消费卡 cut FP8),只能用 FP16/BF16 或 INT8/INT4。

Q3:engine 能跨 GPU 型号使用吗?
A:不能。TensorRT engine 与具体 GPU 架构、CUDA 版本绑定,换卡必须重新 build。

Q4:和 vLLM 比怎么选?
A:追求极致吞吐、有 NVIDIA 专职团队运维选 TRT-LLM;追求灵活、模型迭代快选 vLLM。

Q5:支持 DeepSeek-R1 吗?
A:已官方支持,参考 examples/DeepSeek,需多卡(≥ 8×H100)部署。

七、跳转链接

评论交流

文章目录