Triton 推理服务
本文介绍如何用 NVIDIA Triton Inference Server 部署模型。在推理服务列表中新建服务时,service_type 选择 triton-server,平台会根据模型框架自动生成 config.pbtxt 与启动命令。Triton 支持 ONNX、TensorRT(plan)、TorchScript(PyTorch libtorch)、TensorFlow SavedModel 等多种后端。
事实核对依据:镜像、目录、配置、端口、命令来自
myapp/views/view_inferenceserving.py与myapp/config.py(INFERENCE_IMAGES段)。
镜像
平台内置可选的 Triton 镜像(myapp/config.py:1328 triton-server):
ccr.ccs.tencentyun.com/cube-studio/tritonserver:24.01-py3
ccr.ccs.tencentyun.com/cube-studio/tritonserver:23.12-py3
ccr.ccs.tencentyun.com/cube-studio/tritonserver:22.12-py3
ccr.ccs.tencentyun.com/cube-studio/tritonserver:21.12-py3
ccr.ccs.tencentyun.com/cube-studio/tritonserver:20.12-py3
注意:原文档使用的
nvcr.io/nvidia/tensorrtserver:19.05-py3、nvcr.io/nvidia/tritonserver:21.09-py3已不在当前平台内置镜像列表中,上方列表按myapp/config.py:1328更新。镜像版本与 ONNX/TensorRT/CUDA 算子兼容性相关,转换模型时注意对齐版本。
模型地址与目录结构
在「模型地址」(model_path)中按 框架:地址 填写(view_inferenceserving.py:229):
onnx:模型文件地址,如model.onnxpytorch:TorchScript 模型文件地址,如model.pttf:SavedModel 目录地址,如saved_modeltensorrt:plan 模型文件地址,如model.plan
平台会自动识别后缀(.onnx / .plan / .pt / .pth,否则按 tf 处理,view_inferenceserving.py:719-724),并按 Triton 要求自动整理为如下仓库结构(view_inferenceserving.py:728-731):
/models/
<model-name>/
config.pbtxt
<model-version>/
model.onnx | model.plan | model.pt | model.savedmodel/
说明:模型文件名必须符合 Triton 约定(如
model.onnx、model.plan),平台在拷贝时会自动重命名为model.<后缀>(TF 模型则放入model.savedmodel/目录),用户无需手动改名。
配置 config.pbtxt(系统自动生成)
平台根据模型框架自动生成 config.pbtxt(view_inferenceserving.py:562 triton_config()),框架到 platform 的映射为:
| 框架(model_type) | platform |
|---|---|
| onnx | onnxruntime_onnx |
| tensorrt | tensorrt_plan |
| pytorch / torch | pytorch_libtorch |
| tf | tensorflow_savedmodel |
自动生成的基本结构(max_batch_size 默认 0,input/output 为示例占位,需按实际模型修改):
name: "resnet50"
platform: "onnxruntime_onnx"
max_batch_size: 0
input [
{
name: "input_name"
data_type: TYPE_FP32
format: FORMAT_NCHW
dims: [ 3, 224, 224 ]
reshape: { shape: [ 1, 3, 224, 224 ] }
}
]
output [
{
name: "output_name"
data_type: TYPE_FP32
dims: [ 1000 ]
reshape: { shape: [ 1, 1000 ] }
}
]
... 框架相关优化参数
不同框架平台会追加不同的优化参数(triton_config()):
- tf:
optimization { execution_accelerators { gpu_execution_accelerator: [{ name: "tensorrt" precision_mode=FP16 }] } } - onnx:
intra_op_thread_count/execution_mode/inter_op_thread_count等线程参数 - pytorch:
DISABLE_OPTIMIZED_EXECUTION/INFERENCE_MODE
修改 config.pbtxt 时主要关注 input / output 的 name、data_type、dims,使其与实际模型一致。在线查看模型结构可用 https://netron.app/ 。
提示:原文档建议「不确定就先不填,系统会自动识别」。当前平台启动参数为
--strict-model-config=true(见下文),即由平台自动生成config.pbtxt,而非依赖 Triton 运行时自动推断。部署后可用curl localhost:8000/v2/models/<模型名称>/config查看 Triton 实际加载的配置。
TorchScript 模型注意:TorchScript 不携带输入输出端口命名,配置文件中 input/output 的 name 必须按约定写为 "INPUT__0", "INPUT__1" 与 "OUTPUT__0", "OUTPUT__1"。
启动命令(系统完成)
平台自动编排的启动命令(view_inferenceserving.py:728-731,以非 TF 模型为例):
mkdir -p /models/<model_name>/<model_version>/ \
&& cp /config/* /models/<model_name>/ \
&& cp -r <model_path> /models/<model_name>/<model_version>/model.<ext> \
&& tritonserver --model-repository=/models --strict-model-config=true --log-verbose=1
注意:原文档启动命令曾为
--strict-model-config=false(依赖运行时自动识别),当前代码统一使用--strict-model-config=true --log-verbose=1(INFERNENCE_COMMAND['triton-server'],myapp/views/view_inferenceserving.py:89,以及use_expand中实际拼接的启动命令)。
API 访问
端口(view_inferenceserving.py:110-127):
- HTTP 推理:
8000 - 指标:
8002(8002:/metrics),暴露端口为8000,8002 - 健康检查:
8000:/v2/health/ready
Triton 采用 KServe v2 协议,平台的访问域名 host 默认指向 /v2/models/$model_name(view_inferenceserving.py:70)。常用接口:
GET /v2/models/{model_name} 模型元数据
GET /v2/models/{model_name}/versions/{version} 指定版本元数据
POST /v2/models/{model_name}/infer 推理
GET /v2/health/ready 健康检查
GET /v2/models/{model_name}/config 查看已加载配置
注意:原文档「标准化模型接口」一节列出的是
/v1/model/$model_name、/v1/model/$model_name/version/$version_name等形式,与 Triton 实际使用的 KServe v2 协议不一致。Triton 的访问域名 host 默认指向/v2/models/$model_name(INFERNENCE_HOST['triton-server'],view_inferenceserving.py:70),健康检查为/v2/health/ready,故以上述 v2 接口为准;其余框架(tfserving 等)的接口请见对应文档。
建议使用 Triton 官方客户端包调用:
pip3 install tritonclient[all]
pip3 install attrdict
平台在服务列表的「推理测试」会打开 Notebook 示例 pipeline/example/inference/triton-client.ipynb(view_inferenceserving.py:2204)。
ONNX 模型推理加速
TF / PyTorch 转 ONNX
PyTorch 模型转 ONNX:
model.eval() # 设置模型为推理模式
dummy_input = torch.randn(1, 3, 224, 224) # 输入样本
export_onnx_file = "resnet50.onnx" # 目的 ONNX 文件名
torch.onnx.export(
model, dummy_input, export_onnx_file,
opset_version=13, # 转为 onnx 的版本
do_constant_folding=True, # 是否执行常量折叠优化
input_names=["input_name"], # 输入名
output_names=["output_name"], # 输出名
# dynamic_axes={
# "input": {0: "batch_size"}, # 批处理变量
# "output": {0: "batch_size"}
# },
dynamic_axes={'input_name': [2, 3], 'output_name': [2, 3]} # 动态 size 的输入输出维度
)
TF 模型转 ONNX:
# pip install tensorflow-onnx tf2onnx
python -m tf2onnx.convert --saved-model ./saved_model --output ./model.onnx --opset 13
按模型结构填写 config.pbtxt,主要是 input / output 信息(可用 https://netron.app/ 查看)。
PyTorch 模型加速(TorchScript)
PyTorch 模型在 Triton 上加速,需要导出 TorchScript(下例方法 4):
# 方法1:保存完整模型
# torch.save(model, 'mnist.pt')
#
# 方法2:保存网络参数
# torch.save(model.state_dict(), './cifar_net.pth')
#
# 方法3:导出到 ONNX
# dummy_input = torch.randn(1, 1, 28, 28).to(device)
# torch.onnx.export(model, dummy_input, "torch.onnx")
# 方法4:保存 TORCHSCRIPT(用于 Triton pytorch_libtorch 后端)
# dummy_input = torch.randn(1, 1, 28, 28).to(device)
# traced_cell = torch.jit.trace(model, dummy_input)
# traced_cell.save("torchscript.pt")
TorchScript 模型的 input/output 端口名按前文约定写为 INPUT__0 / OUTPUT__0 等。