Triton Inference Server 部署
Triton Inference Server 是 NVIDIA 开源的高性能推理服务框架,支持多种框架模型、动态批处理和多模型并发服务,是企业级模型部署的核心基础设施。
核心架构
Triton 的核心组件:
- Model Repository:模型存储目录,包含模型文件和配置
- Backend:推理后端(TensorFlow、PyTorch、ONNX、TensorRT 等)
- Dynamic Batcher:动态批处理器,自动合并请求
- Sequence Batcher:序列批处理器,支持有状态模型
- C/Python Backend:自定义后端扩展
python
# model_config.pbtxt 示例
name: "llama2_7b"
platform: "vllm"
max_batch_size: 32
dynamic_batching {
max_queue_delay_microseconds: 100000
preferred_batch_size: [8, 16, 32]
}
instance_group [
{
count: 1
kind: KIND_GPU
gpus: [0, 1]
}
]
parameters [
{
key: "model"
value: { string_value: "meta-llama/Llama-2-7b-hf" }
},
{
key: "tensor_parallel_size"
value: { string_value: "2" }
}
]部署流程
bash
# 1. 准备模型仓库
mkdir -p model_repository/llama2_7b/1
# 2. 启动 Triton Server
docker run --gpus all \
-v ./model_repository:/models \
-p 8000:8000 -p 8001:8001 -p 8002:8002 \
nvcr.io/nvidia/tritonserver:24.01-py3 \
tritonserver --model-repository=/models
# 3. 客户端调用
pip install tritonclient[http]python
# Python 客户端
import tritonclient.http as http_client
client = http_client.InferenceServerClient(url="localhost:8000")
# 文本推理
inputs = [
http_client.InferInput("text_input", [1, 1], "BYTES"),
]
inputs[0].set_data_from_numpy(
np.array([["解释深度学习"]], dtype=object)
)
result = client.infer("llama2_7b", inputs)
output = result.as_numpy("text_output")
print(output)动态批处理
Triton 的动态批处理配置:
| 参数 | 说明 | 推荐值 |
|---|---|---|
max_queue_delay_microseconds | 最大排队等待时间 | 100000 (100ms) |
preferred_batch_size | 首选批次大小 | [8, 16, 32] |
max_batch_size | 最大批次大小 | 64 |
延迟与吞吐权衡
max_queue_delay_microseconds 控制等待时间:值越大,批处理越充分,吞吐越高,但延迟也越高。对延迟敏感的场景建议设为 50ms 以下。
健康检查与监控
Triton 提供三种健康端点:
/v2/health/live:服务是否存活/v2/health/ready:服务是否就绪/v2/models/{model}/ready:模型是否就绪
模型加载时间
大模型首次加载可能需要数分钟。建议配置 --model-control-mode=explicit 并通过 API 预加载模型,避免请求超时。