AI模型部署:Triton Inference Server模型部署框架简介和快速实践

前言

Triton Inference Server是由NVIDIA提供的一个开源推理框架,旨在为AI算法模型提供高效的部署和推理能力,目前已经成为主流的模型部署方案。本文对Triton Inference Server做简要介绍,并且以一个简单的线性模型为例子来实践部署。


内容摘要
  • Triton Inference Server简介
  • Docker构建Triton Inference Server环境
  • Triton Inference Server部署一个线性模型

Triton Inference Server简介

Triton Inference Server是一款开源的推理服务框架,它的核心库基于C++编写的,旨在在生产环境中提供快速且可扩展的AI推理能力,具有以下优势

  • 支持多种深度学习框架:包括PyTorch,Tensorflow,TensorRT,ONNX,OpenVINO等产出的模型文件
  • 至此多种机器学习框架:支持对树模型的部署,包括XGBoost,LightGBM等
  • 支持多种推理协议:支持HTTP,GRPC推理协议
  • 服务端支持模型前后处理:提供后端API,支持将数据的前处理和模型推理的后处理在服务端实现
  • 支持模型并发推理:支持多个模型或者同一模型的多个实例在同一系统上并行执行
  • 支持动态批处理(Dynamic batching):支持将一个或多个推理请求合并成一个批次,以最大化吞吐量
  • 支持多模型的集成流水线:支持将多个模型进行连接组合,将其视作一个整体进行调度管理

Triton Inference Server架构如下图所示,从客户端请求开始,到模型调度处理,模型仓库管理和推理,响应返回,服务状态监控等。


Docker构建Triton Inference Server环境

Triton Inference Server官方推荐使用Docker来构建环境,在Docker镜像仓库下载已经构建好tritonserver镜像
,根据cuda版本下载对应版本的镜像,版本对应关系参照。笔者cuda版本为11.2,对应下载nvcr.io/nvidia/tritonserver:21.02-py3镜像

docker pull nvcr.io/nvidia/tritonserver:21.02-py3

启动容器并进入,它以Python3作为服务端语言,tritonserver是推理服务的启动命令

root@de4977a12c14:/opt/tritonserver# which tritonserver
/opt/tritonserver/bin/tritonserver
root@de4977a12c14:/opt/tritonserver# which python3
/usr/bin/python3

模型部署还需要一些常用的Python包,为了后续测试,我们以该镜像作为源,安装PyTorch,transformers依赖到环境中形成一个新的镜像

# vim Dockerfile 
From nvcr.io/nvidia/tritonserver:21.02-py3
Maintainer xxx
COPY ./torch-1.12.1+cu113-cp38-cp38-linux_x86_64.whl /home
RUN pip install /home/torch-1.12.1+cu113-cp38-cp38-linux_x86_64.whl -i https://pypi.tuna.tsinghua.edu.cn/simple
RUN pip install transformers==4.28.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

构建新镜像triton_server:v1

docker build -t triton_server:v1 .


使用Triton Inference Server部署一个线性模型

本节实践使用Triton Inference Server部署一个线性模型成为一个API服务,包含PyTorch线性模型训练,Triton模型仓库构建,模型推理配置构建,服务端代码构建,服务端启动,客户端服务调用这六个步骤。

(1)PyTorch线性模型训练

我们使用PyTorch的nn.Linear直接构建一个线性层,舍去训练部分,直接使用初始化的权重保存为一个模型文件

import torch
import torch.nn as nn

model = nn.Linear(3, 1)
nn.init.xavier_normal_(model.weight.data)

torch.save(model, "./pytorch_model.bin")


(2)Triton模型仓库构建

Triton Inference Server将模型统一放置在一个目录下进行管理,该目录下每个记录了模型的名称,配置,后端代码,模型文件等,我们创建该目录为model_repository,并且在其下创建一个线性模型目录,命名为linear,该命名是自定义的,但是在后续的配置文件和请求中要保持命名统一。

mkdir model_repository
cd model_repository
mkdir linear


(3)模型推理配置构建

进入linear目录,创建一个文件夹命名为1,代表是linear的第一个版本,将步骤1中训练得到PyTorch模型文件放置在1目录的linear目录下

mkdir 1
cd 1
mkdir linear
cd linear
mv /home/model/pytorch_model.bin ./

回到1目录下创建模型的推理配置文件命名为config.pbtxt,所有版本共用该配置,配置如下

# vim config.pbtxt
name: "linear"
backend: "python"

max_batch_size: 4
input [
  {
    name: "x"
    data_type: TYPE_FP32
    dims: [ 3 ]
  }
]
output [
  {
    name: "y"
    data_type: TYPE_FP32
    dims: [ 1 ]
  }
]

该配置定义了模型的输入和输出,在此做简要的说明

  • name:模型名称,必须和步骤二中的文件名一致
  • max_batch_size:一个批次下的最大大小,4代表一次请求最大推理4条样本
  • input:模型的输入信息,array格式,其中每个输入是一个json格式
  • input-name:一个输入的名称,该名称自定义,但是在服务端代码必须和其保持一致
  • input-data_type:一个输入的数据类型,本例中采用32位浮点
  • input-data_dims:一个输入的维度,代表一条样本的维度,若max_batch_size不为0,它和max_batch_size一起构成了最终的输入大小,本例中最终输入最大是[4, 3]的矩阵,若max_batch_size为0,则dims就是最终的维度

此处输入的x维度为3,和我们训练PyTorch模型时的nn.Linear(3, 1)保持对应。


(4)服务端代码构建

如果使用Python作为后端,Triton以Python脚本的形式来允许模型,这个Python脚本默认命名为model.py,此时服务端代码负责读取模型,并且在其中实现数据的前处理,模型推理,后处理的逻辑。Triton Inference Server提供了服务端代码模板TritonPythonModel,只需要略微修改即可,本例的服务端代码如下

import os

os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'max_split_size_mb:32'

os.environ['TRANSFORMERS_CACHE'] = os.path.dirname(os.path.abspath(__file__)) + "/work/"
os.environ['HF_MODULES_CACHE'] = os.path.dirname(os.path.abspath(__file__)) + "/work/"

import json

# triton_python_backend_utils is available in every Triton Python model. You
# need to use this module to create inference requests and responses. It also
# contains some utility functions for extracting information from model_config
# and converting Triton input/output types to numpy types.
import triton_python_backend_utils as pb_utils
import sys
import gc
import time
import logging
import torch
import numpy as np

gc.collect()
torch.cuda.empty_cache()

logging.basicConfig(format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s',
                    level=logging.INFO)


class TritonPythonModel:
    """Your Python model must use the same class name. Every Python model
    that is created must have "TritonPythonModel" as the class name.
    """

    def initialize(self, args):
        """`initialize` is called only once when the model is being loaded.
        Implementing `initialize` function is optional. This function allows
        the model to intialize any state associated with this model.

        Parameters
        ----------
        args : dict
          Both keys and values are strings. The dictionary keys and values are:
          * model_config: A JSON string containing the model configuration
          * model_instance_kind: A string containing model instance kind
          * model_instance_device_id: A string containing model instance device ID
          * model_repository: Model repository path
          * model_version: Model version
          * model_name: Model name
        """
        # You must parse model_config. JSON string is not parsed here
        self.model_config = json.loads(args['model_config'])
        output_y_config = pb_utils.get_output_config_by_name(self.model_config, "y")

        # Convert Triton types to numpy types
        self.output_response_dtype = pb_utils.triton_string_to_numpy(output_y_config['data_type'])
        # TODO 读取模型
        model_path = os.path.dirname(os.path.abspath(__file__)) + "/linear/pytorch_model.bin"
        self.model = torch.load(model_path)
        self.model = self.model.eval()
        logging.info("model init success")

    def execute(self, requests):
        """`execute` MUST be implemented in every Python model. `execute`
        function receives a list of pb_utils.InferenceRequest as the only
        argument. This function is called when an inference request is made
        for this model. Depending on the batching configuration (e.g. Dynamic
        Batching) used, `requests` may contain multiple requests. Every
        Python model, must create one pb_utils.InferenceResponse for every
        pb_utils.InferenceRequest in `requests`. If there is an error, you can
        set the error argument when creating a pb_utils.InferenceResponse

        Parameters
        ----------
        requests : list
          A list of pb_utils.InferenceRequest

        Returns
        -------
        list
          A list of pb_utils.InferenceResponse. The length of this list must
          be the same as `requests`

        """
        # output_response_dtype = self.output_response_dtype
        # output_history_dtype = self.output_history_dtype

        # output_dtype = self.output_dtype
        responses = []
        # Every Python backend must iterate over everyone of the requests
        # and create a pb_utils.InferenceResponse for each of them.
        for request in requests:
            # TODO 拿到输入
            x = pb_utils.get_input_tensor_by_name(request, "x").as_numpy()

            in_log_info = {
                "x": x,
            }
            logging.info(in_log_info)
            # TODO 推理结果
            y = self.model(torch.tensor(x).float())

            out_log_info = {
                "y": y
            }
            logging.info(out_log_info)
            y = y.detach().cpu().numpy()

            y_tensor = pb_utils.Tensor("y", y.astype(self.output_response_dtype))

            final_inference_response = pb_utils.InferenceResponse(
                output_tensors=[y_tensor])
            responses.append(final_inference_response)
            # Create InferenceResponse. You can set an error here in case
            # there was a problem with handling this inference request.
            # Below is an example of how you can set errors in inference
            # response:
            #
            # pb_utils.InferenceResponse(
            #    output_tensors=..., TritonError("An error occured"))

        # You should return a list of pb_utils.InferenceResponse. Length
        # of this list must match the length of `requests` list.
        return responses

    def finalize(self):
        """`finalize` is called only once when the model is being unloaded.
        Implementing `finalize` function is OPTIONAL. This function allows
        the model to perform any necessary clean ups before exit.
        """
        print('Cleaning up...')

其中initialize方法实现了模型的初始化,execute方法实现了模型的推理逻辑,并且包转了返回。我们将该Python脚本保存命名为model.py,放置在文件1下。
服务端工作准备完成,此时linear目录结构如下

~/model_repository/linear$ tree
.
├── 1
│   ├── linear
│   │   └── pytorch_model.bin
│   └── model.py
└── config.pbtxt



(5)服务端启动

接下来通过Docker来启动Triton Inference Server镜像,将模型仓库目录model_repository整个挂载到容器内部

docker run --rm \
-p18999:8000 -p18998:8001 -p18997:8002 \
-v /home/model_repository/:/models \
triton_server:v1 \
tritonserver --model-repository=/models

服务分别占用三个端口,将宿主机的端口和其进行映射,其中8000端口用于客户端请求推理结果。tritonserver是容器中的服务启动命令,–model-repository是参数,指定了模型仓库位置。
容器启动日志如下


=============================
== Triton Inference Server ==
=============================

NVIDIA Release 21.02 (build 20174689)

Copyright (c) 2018-2021, NVIDIA CORPORATION.  All rights reserved.

...

I0322 09:41:22.286694 1 server.cc:538] 
+--------------+---------+--------+
| Model        | Version | Status |
+--------------+---------+--------+
| linear       | 1       | READY  |
+--------------+---------+--------+

I0322 09:41:22.286937 1 tritonserver.cc:1642] 
+----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------+
| Option                           | Value                                                                                                                                              |
+----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------+
| server_id                        | triton                                                                                                                                             |
| server_version                   | 2.7.0                                                                                                                                              |
| server_extensions                | classification sequence model_repository schedule_policy model_configuration system_shared_memory cuda_shared_memory binary_tensor_data statistics |
| model_repository_path[0]         | /models                                                                                                                                            |
| model_control_mode               | MODE_NONE                                                                                                                                          |
| strict_model_config              | 1                                                                                                                                                  |
| pinned_memory_pool_byte_size     | 268435456                                                                                                                                          |
| min_supported_compute_capability | 6.0                                                                                                                                                |
| strict_readiness                 | 1                                                                                                                                                  |
| exit_timeout                     | 30                                                                                                                                                 |
+----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------+

I0322 09:41:22.289210 1 grpc_server.cc:3979] Started GRPCInferenceService at 0.0.0.0:8001
I0322 09:41:22.289923 1 http_server.cc:2717] Started HTTPService at 0.0.0.0:8000
I0322 09:41:22.331775 1 http_server.cc:2736] Started Metrics Service at 0.0.0.0:8002

提示模型linear版本1已经READY,则启动成功。


(6)客户端服务调用

本例中客户端采用HTTP请求的方式进行调用,使用Python的requests即可进行请求,代码如下

import numpy as np
import requests
import json


if __name__ == "__main__":
    model_name = "linear"
    model_version = "1"

    raw_data = {
        "inputs": [
            {
                "name": "x",
                "datatype": "FP32",
                "shape": [3, 3],
                "data": [[2.0, 3.0, 4.0], [1.1, 2.3, 10.3], [2.0, 3.0, 4.0]]
            }
        ],
        "outputs": [
            {
                "name": "y"
            }
        ]
    }

    url = "http://0.0.0.0:18999/v2/models/linear/infer"
    import requests
    import json

    response = requests.post(url=url,
                             data=json.dumps(raw_data, ensure_ascii=True),
                             headers={"Content_Type": "application/json"},
                             timeout=2000)
    res = json.loads(response.text)
    print(res)

在客户端我们创建了三条样本,形成[3, 3]的矩阵输入给Triton服务,请求url指定了要请求的模型,url中的模型名称linear必须和配置和文件夹命名一致。请求结果打印如下

{'model_name': 'linear', 'model_version': '1', 'outputs': [{'name': 'y', 'datatype': 'FP32', 'shape': [3, 1], 'data': [-0.21258652210235596, -1.3153640031814575, -0.21258652210235596]}]}

其中data就是模型推理的输出,维度和训练过程中的nn.Linear(3, 1)一致。同步的在服务端日志会打印出请求的数据和输出的数据

I0322 09:54:17.211954 1 grpc_server.cc:3979] Started GRPCInferenceService at 0.0.0.0:8001
I0322 09:54:17.212302 1 http_server.cc:2717] Started HTTPService at 0.0.0.0:8000
I0322 09:54:17.253904 1 http_server.cc:2736] Started Metrics Service at 0.0.0.0:8002
2024-03-22 09:54:24,068 - model.py[line:98] - INFO: {'x': array([[ 2. ,  3. ,  4. ],
       [ 1.1,  2.3, 10.3],
       [ 2. ,  3. ,  4. ]], dtype=float32)}
2024-03-22 09:54:24,073 - model.py[line:105] - INFO: {'y': tensor([[-0.2126],
        [-1.3154],
        [-0.2126]], grad_fn=<AddmmBackward0>)}

至此,基于Triton Inference Server搭建的一个简单的线性模型的预测推理服务完成。

最后的最后

感谢你们的阅读和喜欢,我收藏了很多技术干货,可以共享给喜欢我文章的朋友们,如果你肯花时间沉下心去学习,它们一定能帮到你。

因为这个行业不同于其他行业,知识体系实在是过于庞大,知识更新也非常快。作为一个普通人,无法全部学完,所以我们在提升技术的时候,首先需要明确一个目标,然后制定好完整的计划,同时找到好的学习方法,这样才能更快的提升自己。

这份完整版的大模型 AI 学习资料已经上传CSDN,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费
在这里插入图片描述

一、全套AGI大模型学习路线

AI大模型时代的学习之旅:从基础到前沿,掌握人工智能的核心技能!

img

二、640套AI大模型报告合集

这套包含640份报告的合集,涵盖了AI大模型的理论研究、技术实现、行业应用等多个方面。无论您是科研人员、工程师,还是对AI大模型感兴趣的爱好者,这套报告合集都将为您提供宝贵的信息和启示。

img

三、AI大模型经典PDF籍

随着人工智能技术的飞速发展,AI大模型已经成为了当今科技领域的一大热点。这些大型预训练模型,如GPT-3、BERT、XLNet等,以其强大的语言理解和生成能力,正在改变我们对人工智能的认识。 那以下这些PDF籍就是非常不错的学习资源。

img

四、AI大模型商业化落地方案

img
这份完整版的大模型 AI 学习资料已经上传CSDN,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费
在这里插入图片描述

<think>好的,用户想了解如何部署AI模型的方法及工具。首先,我需要明确AI模型部署的基本概念流程。根据引用[1],模型部署是将训练好的模型应用到实际场景中的过程。接下来,要分步骤介绍部署方法,可能包括环境准备、模型转换、接口开发、部署平台选择等。 然后要考虑不同部署方式,比如云端、边缘端本地部署,用户可能对这些场景的区别应用感兴趣。需要引用[1][2]提到的工具,比如TensorFlow Serving、ONNX Runtime、Docker等,说明它们的作用使用场景。 还要注意用户可能的背景,他们可能对技术细节不太熟悉,所以需要语言简洁明了,避免过于专业的术语。同时,按照用户要求,生成相关问题,比如工具比较、本地部署优势、性能优化等,这有助于用户进一步探索。 最后,确保回答结构清晰,分点列出方法工具,每个部分有例子或工具推荐,并在末尾添加引用标识。检查是否所有数学公式正确使用$...$或$$...$$,但可能这个问题在部署内容中涉及不多,不过如果有性能指标或数学表达式需要注意格式。同时,使用中文回答,保持自然流畅。</think>### AI模型部署方法及工具详解 AI模型部署是将训练好的模型集成到实际应用环境的核心技术流程,主要包括以下步骤: #### 一、部署方法 1. **环境配置** - 安装CUDA/cuDNN等GPU加速库(适用于深度学习) - 配置Python虚拟环境:$conda create -n deploy_env python=3.8$ - 安装框架依赖:PyTorch/TensorFlow运行时库 2. **模型转换** - ONNX标准化转换:$torch.onnx.export(model, dummy_input, "model.onnx")$ - 格式转换工具:TensorFlow SavedModel、PyTorch TorchScript - 量化压缩:FP32转INT8提升推理速度[^1] 3. **接口开发** - REST API开发(Flask/FastAPI): ```python from fastapi import FastAPI app = FastAPI() @app.post("/predict") def predict(data: InputSchema): return model.inference(data) ``` 4. **部署架构选择** - 云端部署:AWS SageMaker、Azure ML - 边缘计算:NVIDIA Jetson设备 - 混合部署:云端训练+边缘推理 #### 二、主流工具对比 | 工具名称 | 适用场景 | 核心优势 | |---------|--------|---------| | TensorFlow Serving | 生产级推理服务 | 支持模型版本管理、自动批量处理 | | ONNX Runtime | 多框架模型部署 | 跨平台统一推理接口 | | Docker | 环境容器化 | 解决依赖冲突问题[^2] | | Triton Inference Server | 多模型并行服务 | 支持GPU/CPU混合调度 | #### 三、典型部署流程示例 $$部署效率 = \frac{推理吞吐量}{资源消耗} \times 100\%$$ 通过监控该指标可优化资源配置,例如使用Kubernetes进行自动扩缩容[^1]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值