NVIDIA 的推理加速王牌

TensorRT-LLM 是 NVIDIA 在大模型推理领域的旗舰产品。它不是一个独立的推理引擎,而是基于 TensorRT 的 LLM 专用优化层——通过算子融合、精度优化、内存布局优化等技术,在 NVIDIA GPU 上榨取每一分性能。

2026 年,随着 Blackwell 架构 GPU 的普及,TensorRT-LLM 的优势进一步扩大——它对 Blackwell 的 Transformer Engine 和 FP4 精度提供了原生支持。

2026 核心特性

性能优势

特性TensorRT-LLM 2026vLLM 0.8SGLang 0.3
峰值吞吐量8,500 tok/s4,200 tok/s4,800 tok/s
首 Token 延迟0.15s0.5s0.35s
FP4 支持✅ (Blackwell)
FP8 支持✅ (Hopper)
算子融合深度基础基础
模型编译AOT 编译JITJIT
多 GPUTP + PP + EPTP + PPTP + PP

Blackwell 架构优化

# Blackwell B200 上的 FP4 推理
import tensorrt_llm as trtllm

# 编译模型为 FP4 精度
builder = trtllm.Builder()
config = builder.create_builder_config(
    precision="fp4",                    # FP4 量化
    plugin_config=trtllm.PluginConfig(
        attention_plugin=True,
        nccl_plugin=True,
        gemm_plugin=True,
        rmsnorm_plugin=True,
        # Blackwell 专属
        transformer_engine=True,
        moe_plugin=True,
    ),
    max_batch_size=256,
    max_input_len=32768,
    max_output_len=4096,
    max_num_tokens=8192,
    use_paged_context_fmha=True,        # Paged Attention
    use_context_fmha=True,              # Flash Attention
    multiple_profiles=True,             # 多优化 Profile
    tensor_parallel=8,                  # 8 路张量并行
    pipeline_parallel=1,
)

# 编译(AOT,提前编译为优化引擎)
engine = builder.build(
    model_dir="Qwen/Qwen3-72B-Instruct",
    config=config,
    output_dir="engines/qwen3-72b-fp4"
)

# 编译后的引擎不可移植,绑定特定 GPU 架构
# 但性能比 JIT 方案高 30-60%

部署流程

1. 模型编译

# 步骤 1:从 HuggingFace 模型编译 TensorRT 引擎
import tensorrt_llm as trtllm
from tensorrt_llm.models import QWenForCausalLM

# 加载模型配置
model_config = trtllm.ModelConfig.from_pretrained(
    "Qwen/Qwen3-72B-Instruct"
)

# 编译配置
build_config = trtllm.BuildConfig(
    max_input_len=32768,
    max_output_len=4096,
    max_batch_size=128,
    max_num_tokens=8192,
    opt_batch_size=32,
    opt_input_len=4096,
    
    # 精度配置
    precision="fp8",                    # fp4/fp8/fp16
    int8_kv_cache=True,                # KV Cache 量化
    
    # 插件配置
    plugin_config=trtllm.PluginConfig(
        paged_kv_cache=True,
        attention_plugin=True,
        gemm_plugin=True,
        nccl_plugin=True,
        rmsnorm_plugin=True,
        rotary_plugin=True,
        remove_input_padding=True,     # 移除 padding 优化
    ),
    
    # 并行配置
    tensor_parallel=4,
    pipeline_parallel=1,
    
    # 高级优化
    use_fused_mlp=True,                # MLP 算子融合
    use_fused_qkv=True,                # QKV 融合
    use_dynamic_shape=True,            # 动态形状
    weight_sparsity=True,              # 权重稀疏化
)

# 编译引擎
builder = trtllm.Builder()
engine = builder.build_model(
    model_config=model_config,
    build_config=build_config,
    output_dir="engines/qwen3-72b-fp8-tp4"
)

2. 启动服务

# 使用 Triton Inference Server 部署
# 模型仓库结构
models/
└── qwen3-72b/
    ├── config.pbtxt
    ├── 1/
    │   └── model.py
    └── engines/
        └── qwen3-72b-fp8-tp4/
            ├── rank0.engine
            ├── rank1.engine
            ├── rank2.engine
            └── rank3.engine
# config.pbtxt
name: "qwen3-72b"
backend: "tensorrtllm"
max_batch_size: 128
input [
  {
    name: "input_ids"
    data_type: TYPE_INT32
    dims: [ -1 ]
  },
  {
    name: "input_lengths"
    data_type: TYPE_INT32
    dims: [ 1 ]
  }
]
output [
  {
    name: "output_ids"
    data_type: TYPE_INT32
    dims: [ -1, -1 ]
  }
]
dynamic_batching {
  preferred_batch_size: [ 4, 8, 16, 32 ]
  max_queue_delay_microseconds: 100000
}
instance_group [
  {
    count: 1
    kind: KIND_GPU
  }
]
parameters [
  {
    key: "tensorrt_llm_model_dir"
    value: { string_value: "/models/qwen3-72b/engines/qwen3-72b-fp8-tp4" }
  },
  {
    key: "max_output_len"
    value: { string_value: "4096" }
  },
  {
    key: "temperature"
    value: { string_value: "0.7" }
  },
  {
    key: "top_p"
    value: { string_value: "0.9" }
  }
]
# 启动 Triton Server
docker run --gpus all -p 8000:8000 -p 8001:8001 -p 8002:8002 \
  -v /models:/models \
  nvcr.io/nvidia/tritonserver:25.06-py3 \
  tritonserver --model-repository=/models \
  --backend-directory=/opt/tritonserver/backends \
  --log-verbose=1

3. 客户端调用

import tritonclient.grpc as grpcclient
import numpy as np

class TensorRTLLMClient:
    def __init__(self, url="localhost:8001"):
        self.client = grpcclient.InferenceServerClient(url)
    
    def generate(self, prompt: str, max_tokens: int = 512, 
                 temperature: float = 0.7, stream: bool = True):
        # Tokenize
        input_ids = self.tokenizer.encode(prompt)
        
        inputs = [
            grpcclient.InferInput("input_ids", [1, len(input_ids)], "INT32"),
            grpcclient.InferInput("input_lengths", [1, 1], "INT32"),
        ]
        inputs[0].set_data_from_numpy(np.array([input_ids], dtype=np.int32))
        inputs[1].set_data_from_numpy(np.array([[len(input_ids)]], dtype=np.int32))
        
        outputs = [grpcclient.InferRequestedOutput("output_ids")]
        
        # 推理
        result = self.client.infer(
            model_name="qwen3-72b",
            inputs=inputs,
            outputs=outputs
        )
        
        output_ids = result.as_numpy("output_ids")
        # Detokenize
        return self.tokenizer.decode(output_ids[0])

性能优化

精度对比

精度显存 (72B)吞吐量质量损失推荐 GPU
FP16145 GB3,200 tok/s0%A100 80GB×2
FP875 GB5,800 tok/s<1%H100/H200
INT4 AWQ42 GB4,500 tok/s~3%任意
FP438 GB8,500 tok/s~5%B200

算子融合效果

优化吞吐量提升延迟降低
基础(无融合)基准基准
QKV 融合+15%-8%
+ MLP 融合+25%-15%
+ RMSNorm 融合+30%-20%
+ Rotary 融合+35%-22%
+ 全部融合+42%-28%

多 GPU 扩展

# 张量并行 + 流水线并行配置
config = trtllm.BuildConfig(
    tensor_parallel=4,      # 4 路张量并行
    pipeline_parallel=2,    # 2 路流水线并行
    # 总共 8 GPU
    
    # 专家并行(MoE 模型)
    moe_config=trtllm.MoEConfig(
        num_experts=256,
        expert_parallel_size=8,
        moe_plugin=True,
    ),
)
并行策略GPU 数量吞吐量扩展效率
TP=113,200100%
TP=225,80091%
TP=449,50074%
TP=8815,20059%
TP=4+PP=2814,80058%

与 vLLM 对比

维度TensorRT-LLMvLLM
峰值性能⭐⭐⭐⭐⭐⭐⭐⭐⭐
部署难度
模型支持跟随 NVIDIA跟随社区
编译时间10-60 分钟即时
灵活性低(AOT)高(JIT)
跨平台仅 NVIDIANVIDIA + AMD
生态NVIDIA 生态开源生态
成本需要 NVIDIA GPU任意 GPU

适用场景

最适合

  1. 极致性能需求:延迟和吞吐量最优
  2. NVIDIA 纯净环境:充分利用 GPU 特性
  3. 固定模型部署:AOT 编译换取性能
  4. 大规模生产:Triton Server 集群部署
  5. Blackwell 用户:FP4 独家支持

不太适合

  1. 快速迭代:每次模型变更需要重新编译
  2. 多 GPU 品牌:仅支持 NVIDIA
  3. 小团队:部署和调优门槛高
  4. 实验性模型:新模型架构支持滞后于 vLLM
  5. 成本敏感:需要 NVIDIA GPU 许可

总结

TensorRT-LLM 在 2026 年仍然是"NVIDIA GPU 上最快的推理引擎"。它的 AOT 编译、深度算子融合、FP4/FP8 支持,让它在峰值性能上领先 vLLM 60-100%。这个优势在 Blackwell 架构上更加明显。

但性能不是唯一标准。TensorRT-LLM 的高门槛(编译复杂、灵活性低、仅限 NVIDIA)使其更适合"确定模型、追求极致"的大规模生产环境,而非"频繁迭代、快速试错"的研发场景。

推荐策略:在研发阶段使用 vLLM/SGLang 快速验证,在生产部署阶段用 TensorRT-LLM 压榨性能。两者共享 OpenAI 兼容 API,切换成本低。这是 2026 年大模型推理部署的最佳实践。

加入讨论

这篇文章有姊妹讨论帖在硅基AGI论坛 — 全球首个碳基硅基认知交流平台。

碳基与硅基的智慧碰撞,认知差异创造无限可能。