triton入门实战

这篇文章主要讲的是基于官方镜像及, pytorch script 格式模型,构建tritonserver 服务

1、环境准备:

  • 1.1. 下载 tritonserver镜像: Triton Inference Server | NVIDIA NGC

    • a. 注意:tritonserver 镜像中的invdia驱动版本对应,否则后面会启动失败。
  • 1.2. 然后,拉取Pytorch官方镜像作为推理系统的客户端同时进行一些预处理操作(当然也可以直接拉取tritonserver客户端SDK镜像)。

    • a. docker pull pytorch/pytorch:2.0.0-cuda11.7-cudnn8-devel暂时无法提供下载链接,因为无法访问dockerhub。
    • b. tritonserver客户端SDK镜像 Triton Inference Server | NVIDIA NGC
      # nvcr.io/nvidia/tritonserver:<yy.mm>-py3-sdk
      # docker pull nvcr.io/nvidia/tritonserver:23.04-py3-sdk
  • 1.3. 接下来,基于官方Pytorch镜像创建一个容器客户端。

    • a. 本地创建共享目录, D:\chinasoft\shumei\triton\demo_first\pytorch_container\workspace
    • b. docker run -dt --name pytorch200_cu117_dev --restart=always --gpus all --network=host --shm-size 4G -v /D/chinasoft/shumei/triton/demo_first/pytorch_container/workspace:/workspace -w /workspace pytorch/pytorch:2.0.0-cuda11.7-cudnn8-devel /bin/bash
    • c. 进入容器,docker exec -it pytorch200_cu117_dev bash
    • d. pip install datasets transformers -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn
    • e. pip install tritonclient[all] -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn

2、模型准备

  • 2.1. 文将基于 PyTorch 后端使用 resnet50 模型来进行图片分类,因此,需预先下载 resnet50 模型,然后将其转换为torchscript格式。具体代码(resnet50_convert_torchscript.py )如下所示:
import torch
import torchvision.models as modelsresnet50 = models.resnet50(pretrained=True)
resnet50.eval()
image = torch.randn(1, 3, 244, 244)
resnet50_traced = torch.jit.trace(resnet50, image)
resnet50(image)
# resnet50_traced.save('/workspace/model/resnet50/model.pt')
torch.jit.save(resnet50_traced, "/workspace/model/resnet50/model.pt")
  • 2 2. 最后,拉取Triton Server 代码库。

    git clone -b r23.04 https://github.com/triton-inference-server/server.git

    一些常见后端backend的配置都在server/docs/examples目录下。

tree docs/examples -L 2
docs/examples
|-- README.md
|-- fetch_models.sh
|-- jetson
|   |-- README.md
|   `-- concurrency_and_dynamic_batching
`-- model_repository|-- densenet_onnx|-- inception_graphdef|-- simple|-- simple_dyna_sequence|-- simple_identity|-- simple_int8|-- simple_sequence`-- simple_string11 directories, 3 files
  • 2.3. 拉取Triton Tutorials库,该仓库中包含Triton的教程和样例,本文使用Quick_Deploy/PyTorch下部署一个Pytorch模型进行讲解。

    git clone https://github.com/triton-inference-server/tutorials.git

3、开发实践

  • 3.1 首先,在宿主机构建一个模型仓库,仓库的目录结构如下所示:
model_repository/
`-- resnet50|-- 1|   `-- model.pt`-- config.pbtxt

其中, config.pbtxt 是模型配置文件; 1表示模型版本号; resnet50表示模型名,需要与config.pbtxt文件中的name字段保存一致;model.pt为模型权重(即上面转换后的模型权重)。

  • 3.2. 编辑config.pbtxt文件,具体内容如下所示:
name: "resnet50"
platform: "pytorch_libtorch"
max_batch_size : 0
input [{name: "input__0"data_type: TYPE_FP32dims: [ 3, 224, 224 ]reshape { shape: [ 1, 3, 224, 224 ] }}
]
output [{name: "output__0"data_type: TYPE_FP32dims: [ 1, 1000 ,1, 1]reshape { shape: [ 1, 1000 ] }}
]

重要字段说明如下:

  • name:模型名
  • platform:用于指定模型对应的后端(backend),比如:pytorch_libtorch、onnxruntime_onnx、tensorrt_plan等
  • max_batch_size:模型推理在batch模式下支持的最大batch数
  • input:模型输入属性配置。
  • output:模型输出属性配置。

模型仓库构建好之后,接下来启动Triton推理服务端。

4、启动tritonserver推理服务

启动推理服务启动服务的方法有两种:一种是用 docker 启动并执行命令,一种是进入 docker 中然后手动调用命令。

我们在这里使用docker启动并执行命令:

docker run --gpus all --rm -p 8000:8000 -p 8001:8001 -p 8002:8002 -v /D/chinasoft/shumei/triton/demo_first/model_repository:/models nvcr.io/nvidia/tritonserver:22.12-py3 tritonserver --model-repository=/models

参数说明:

  • p:宿主机与容器内端口映射
  • v:将宿主机存储挂载进容器,这里将模型仓库挂载进容器
  • -model-repository:指定Triton服务模型仓库的地址
  • 这里注意指定的model_repository路径必须正确且模型文件已经配置无误,具体参考:模型准备章节。
(base) PS C:\Users\lenovo> docker run --gpus all --rm -p 8000:8000 -p 8001:8001 -p 8002:8002 -v /D/chinasoft/shumei/triton/demo_first/model_repository:/models nvcr.io/nvidia/tritonserver:22.12-py3 tritonserver --model-repository=/models=============================
== Triton Inference Server ==
=============================NVIDIA Release 22.12 (build 50109463)
Triton Server Version 2.29.0Copyright (c) 2018-2022, NVIDIA CORPORATION & AFFILIATES.  All rights reserved.Various files include modifications (c) NVIDIA CORPORATION & AFFILIATES.  All rights reserved.This container image and its contents are governed by the NVIDIA Deep Learning Container License.
By pulling and using the container, you accept the terms and conditions of this license:
https://developer.nvidia.com/ngc/nvidia-deep-learning-container-licenseWARNING: CUDA Minor Version Compatibility mode ENABLED.Using driver version 516.94 which has support for CUDA 11.7.  This containerwas built with CUDA 11.8 and will be run in Minor Version Compatibility mode.CUDA Forward Compatibility is preferred over Minor Version Compatibility for usewith this container but was unavailable:[[]]See https://docs.nvidia.com/deploy/cuda-compatibility/ for details.I0804 01:46:15.003883 1 pinned_memory_manager.cc:240] Pinned memory pool is created at '0x304800000' with size 268435456
I0804 01:46:15.004050 1 cuda_memory_manager.cc:105] CUDA memory pool is created on device 0 with size 67108864
I0804 01:46:15.322720 1 model_lifecycle.cc:459] loading: resnet50:1
I0804 01:46:17.472054 1 libtorch.cc:1985] TRITONBACKEND_Initialize: pytorch
I0804 01:46:17.472105 1 libtorch.cc:1995] Triton TRITONBACKEND API version: 1.10
I0804 01:46:17.472587 1 libtorch.cc:2001] 'pytorch' TRITONBACKEND API version: 1.10
I0804 01:46:17.472634 1 libtorch.cc:2034] TRITONBACKEND_ModelInitialize: resnet50 (version 1)
W0804 01:46:17.473291 1 libtorch.cc:284] skipping model configuration auto-complete for 'resnet50': not supported for pytorch backend
I0804 01:46:17.473618 1 libtorch.cc:313] Optimized execution is enabled for model instance 'resnet50'
I0804 01:46:17.473624 1 libtorch.cc:332] Cache Cleaning is disabled for model instance 'resnet50'
I0804 01:46:17.473626 1 libtorch.cc:349] Inference Mode is disabled for model instance 'resnet50'
I0804 01:46:17.473640 1 libtorch.cc:444] NvFuser is not specified for model instance 'resnet50'
I0804 01:46:17.473699 1 libtorch.cc:2078] TRITONBACKEND_ModelInstanceInitialize: resnet50 (GPU device 0)
I0804 01:46:22.750763 1 model_lifecycle.cc:694] successfully loaded 'resnet50' version 1
I0804 01:46:22.750870 1 server.cc:563]
+------------------+------+
| Repository Agent | Path |
+------------------+------+
+------------------+------+I0804 01:46:22.750917 1 server.cc:590]
+---------+---------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Backend | Path                                                    | Config                                                                                                                                                        |
+---------+---------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
| pytorch | /opt/tritonserver/backends/pytorch/libtriton_pytorch.so | {"cmdline":{"auto-complete-config":"true","min-compute-capability":"6.000000","backend-directory":"/opt/tritonserver/backends","default-max-batch-size":"4"}} |
+---------+---------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+I0804 01:46:22.750948 1 server.cc:633]
+----------+---------+--------+
| Model    | Version | Status |
+----------+---------+--------+
| resnet50 | 1       | READY  |
+----------+---------+--------+I0804 01:46:22.810861 1 metrics.cc:864] Collecting metrics for GPU 0: NVIDIA GeForce GTX 1650
I0804 01:46:22.811494 1 metrics.cc:757] Collecting CPU metrics
I0804 01:46:22.811657 1 tritonserver.cc:2264]
+----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Option                           | Value                                                                                                                                                                                                |
+----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| server_id                        | triton                                                                                                                                                                                               |
| server_version                   | 2.29.0                                                                                                                                                                                               |
| server_extensions                | classification sequence model_repository model_repository(unload_dependents) schedule_policy model_configuration system_shared_memory cuda_shared_memory binary_tensor_data statistics trace logging |
| model_repository_path[0]         | /models                                                                                                                                                                                              |
| model_control_mode               | MODE_NONE                                                                                                                                                                                            |
| strict_model_config              | 0                                                                                                                                                                                                    |
| rate_limit                       | OFF                                                                                                                                                                                                  |
| pinned_memory_pool_byte_size     | 268435456                                                                                                                                                                                            |
| cuda_memory_pool_byte_size{0}    | 67108864                                                                                                                                                                                             |
| response_cache_byte_size         | 0                                                                                                                                                                                                    |
| min_supported_compute_capability | 6.0                                                                                                                                                                                                  |
| strict_readiness                 | 1                                                                                                                                                                                                    |
| exit_timeout                     | 30                                                                                                                                                                                                   |
+----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+I0804 01:46:22.813086 1 grpc_server.cc:4819] Started GRPCInferenceService at 0.0.0.0:8001
I0804 01:46:22.813243 1 http_server.cc:3477] Started HTTPService at 0.0.0.0:8000
I0804 01:46:22.890915 1 http_server.cc:184] Started Metrics Service at 0.0.0.0:8002
W0804 01:46:23.822499 1 metrics.cc:603] Unable to get power limit for GPU 0. Status:Success, value:0.000000
W0804 01:46:24.822769 1 metrics.cc:603] Unable to get power limit for GPU 0. Status:Success, value:0.000000
W0804 01:46:25.831221 1 metrics.cc:603] Unable to get power limit for GPU 0. Status:Success, value:0.000000

可以看到resnet50模型已经 READY状态了,但是显卡没有用到,因为上面报警我宿主 机驱动版本和镜像驱动版本不匹配

WARNING: CUDA Minor Version Compatibility mode ENABLED.Using driver version 516.94 which has support for CUDA 11.7.  This containerwas built with CUDA 11.8 and will be run in Minor Version Compatibility mode.CUDA Forward Compatibility is preferred over Minor Version Compatibility for usewith this container but was unavailable:[[]]See https://docs.nvidia.com/deploy/cuda-compatibility/ for details.

5、发送推理请求

  • 5.1 首先,创建客户端脚本client.py,放到:
import numpy as np
from torchvision import transforms
from PIL import Image
import tritonclient.http as httpclient
from tritonclient.utils import triton_to_np_dtype# 图片预处理
# preprocessing function
def rn50_preprocess(img_path="img1.jpg"):img = Image.open(img_path)preprocess = transforms.Compose([transforms.Resize(256),transforms.CenterCrop(224),transforms.ToTensor(),transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),])return preprocess(img).numpy()transformed_img = rn50_preprocess()# 设置连接到Triton服务端
# Setting up client
client = httpclient.InferenceServerClient(url="localhost:8000")# 指定resnet50模型的输入和输出
inputs = httpclient.InferInput("input__0", transformed_img.shape, datatype="FP32")
inputs.set_data_from_numpy(transformed_img, binary_data=True)# class_count表示获取 TopK 分类预测结果。如果没有设置这个选项,默认值为0,那么将会得到一个 1000 维的向量。
outputs = httpclient.InferRequestedOutput("output__0", binary_data=True, class_count=1000)# 发送一个推理请求到Triton服务端
# Querying the server
results = client.infer(model_name="resnet50", inputs=[inputs], outputs=[outputs])
inference_output = results.as_numpy('output__0')
print(inference_output[:5])
  • 5.2. 进入客户端容器:docker exec -it pytorch200_cu117_dev bash
  • 5.3. 预先下载好,用于推理请求的图片:
    wget -O img1.jpg "https://www.hakaimagazine.com/wp-content/uploads/header-gulf-birds.jpg"
  • 5.4. 执行客户端脚本发送请求:
python client.py
[b'12.474869:90' b'11.527128:92' b'9.659309:14' b'8.408504:136'b'8.216769:11']

输出的格式为<confidence_score>:<classification_index>。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/819472.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

JWFD全面升级为WIN7系统

由于JWFD主控制器的核心模块已经完全支持WIN7系统&#xff0c;JWFD系统已经全面升级为WIN7平台 请各个用户升级自己的核心系统到WIN7&#xff0c;WIN7版本的JWFD和XP版本的JWFD保持完全的兼容 原来在XP及其以下的操作系统中开发的JWFD的各个模块和代码包均可以在WIN7版本中正…

Java基础知识总结(56)

/** Map集合常用Api */ public class MapDemo1 { public static void main(String[] args) { //创建Map集合对象 Map<Integer,String> map new HashMap<>(); //向Map集合中添加键值对 map.put(1,"田福军"); //在这里进行了自动装箱 map.put(2,"孙…

Java面试必问题39:SpringBoot自动配置原理(必问) SpringBoot(优点)

SpringBoot自动配置原理 Spring Boot的自动配置原理基于条件化配置和约定优于配置的机制。它通过扫描类路径下的依赖、配置文件和注解等信息&#xff0c;结合Spring Boot提供的自动配置类和条件注解&#xff0c;根据条件判断自动配置哪些组件&#xff0c;然后将它们注入到Spri…

mojo人工智能语言终于开源了,比Python 快了9万倍

让你久等的人 最后也不会选择你 没有什么人是你必须要交往的 原则上不可以 只是说明打破选择的利益 还不够大 女人喜欢追随 而不是领导 追求趋势,权利 而不是忠诚 前不久,Modular 公司宣布开源 Mojo 的核心组件。 Mojo 是一种专为编写人工智能软件设计的编程语言,去年…

百货商场用户画像描绘and价值分析(下)

目录 内容概述数据说明技术点主要内容4 会员用户画像和特征字段创造4.1 构建会员用户基本特征标签4.2 会员用户词云分析 5 会员用户细分和营销方案制定5.1 会员用户的聚类分析及可视化5.2 对会员用户进行精细划分并分析不同群体带来的价值差异 内容概述 本项目内容主要是基于P…

uniapp开发路由跳转

目录 底部导航栏 内置api跳转 uni.navigateTo uni.redirectTo uni.reLaunch uni.switchTab 使用页面链接 底部导航栏 pages.json "tabBar": {"color": "#f5deb3","selectedColor": "#f0e68c","borderStyle&qu…

C语言概述详解

1.什么是C语言&#xff1f; C语言是计算机编程语言的一种&#xff0c;主要用于人与机器交流&#xff0c;对于嵌入式工程师来说&#xff0c;C语言是必不可少的一门语言。 2.C语言的特点&#xff1f; C语言具有简洁、高效、可移植、模块化、标准化的特点。但对于C语言的简洁和高效…

数据结构-栈超详解

栈 一种先进后出的数据结构。 复杂度单次通常为 O ( 1 ) O(1) O(1) 用途&#xff1a;括号匹配&#xff0c;倒序问题等 例子&#xff1a; 序列A:{1,2,3,4,5}依次入栈再同一出栈&#xff0c;顺序为A’:{5,4,3,2,1} 代码&#xff1a; 插入&#xff1a; void insert(int x…

手写call,apply,bind,new

三种情况都是改变this的指向&#xff0c;不同的是bind返回的是一个函数 //call let foo {value: 1 };Function.prototype.call2 function (context) {const context2 context || window//this指的是要改变this函数context2.fn thisconst args [...arguments].slice(1)//调…

Springboot+Vue项目-基于Java+MySQL的免税商品优选购物商城系统(附源码+演示视频+LW)

大家好&#xff01;我是程序猿老A&#xff0c;感谢您阅读本文&#xff0c;欢迎一键三连哦。 &#x1f49e;当前专栏&#xff1a;Java毕业设计 精彩专栏推荐&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb; &#x1f380; Python毕业设计 &…

【LeetCode热题100】【贪心算法】跳跃游戏

题目链接&#xff1a;55. 跳跃游戏 - 力扣&#xff08;LeetCode&#xff09; 数组的元素表示可以跳的最大长度&#xff0c;要判断能不能跳到最后 不断更新可以跳到的最远距离&#xff0c;如果当前的位置大于可跳最远距离&#xff0c;说明不行 class Solution { public:bool …

【Qt】常用控件(LCD Number/进度条/日历)

需要云服务器等云产品来学习Linux可以移步/-->腾讯云<--/官网&#xff0c;轻量型云服务器低至112元/年&#xff0c;新用户首次下单享超低折扣。 目录 一、LCD Number(LCD显示器) 一个倒计时程序 二、ProgressBar(进度条) 1、创建一个进度条&#xff0c;100ms进度增加…

Android Binder——C++层注册服务实例(十二)

前面几篇内容都介绍了 C++ 中新增服务的调用流程,这里我们看一个 Android 源码中的实例进一步熟悉 C++ 服务的添加流程,这里以 MediaPlayerService 为例。 一、添加服务调用 1、main_mediaserver.cpp 源码位置:/frameworks/av/media/mediaserver/main_mediaserver.cpp i…

每天学习一个Linux命令之git

每天学习一个Linux命令之git Git是一个分布式版本控制系统&#xff0c;被广泛用于开发软件项目。它提供了许多强大的命令和选项&#xff0c;使开发人员可以更好地管理和跟踪代码的变化。在本篇博客中&#xff0c;我们将详细介绍一些常用的git命令及其选项。 git init git in…

✌粤嵌—2024/3/14—判断子序列

代码实现&#xff1a; 方法一&#xff1a;一次遍历 bool isSubsequence(char *s, char *t) {if (strlen(s) 0) {return true;}int i 0;for (int j 0; j < strlen(t); j) {if (s[i] t[j]) {i;}if (i strlen(s)) {return true;}}return false; } 方法二&#xff1a;动态规…

5.2 mybatis之autoMappingBehavior作用

文章目录 1. NONE关闭自动映射2. PARTIAL非嵌套结果映射3. FULL全自动映射 众所周知mybatis中标签< resultMap >是用来处理数据库库字段与java对象属性映射的。通常java对象属性&#xff08;驼峰格式&#xff09;与数据库表字段&#xff08;下划线形式&#xff09;是一 一…

基于SSM项目个人健康信息管理系统

采用技术 基于SSM项目个人健康信息管理系统的设计与实现~ 开发语言&#xff1a;Java 数据库&#xff1a;MySQL 技术&#xff1a;SpringMVCMyBatis 工具&#xff1a;IDEA/Ecilpse、Navicat、Maven 页面展示效果 用户端 用户首页 健康知识 用户注册 医院推荐信息 系统概要…

【LeetCode热题100】【矩阵】螺旋矩阵

题目链接&#xff1a;54. 螺旋矩阵 - 力扣&#xff08;LeetCode&#xff09; 先走外面的圈再走里面的圈&#xff0c;可以用递归来解决&#xff0c;对于要走的一个圈&#xff0c;由四个角决定&#xff0c;其实是三个数&#xff0c;&#xff08;0&#xff0c;0&#xff09;&…

VUE相关知识锦集

一、生命周期的使用场景 created ----- 单组件请求 mounted----- 同步可以获取dom,如果先子组件请求后父组件请求 activated-----判断id是否相等&#xff0c;如果不相同发起请求 destory-----关闭页面记录视频播放时间&#xff0c;初始化的时候从上一次的历史开始播放 补充…

Vue.js前端开发零基础教学(五)

目录 4.1 动态组件 4.1.1 定义动态组件 4.1.2 利用KeepAlive组件实现组件缓存 4.1.3 组件缓存相关的生命周期函数 4.1.4 KeepAlive组件的常用属性 4.2 插槽 4.2.1 什么是插槽 ​编辑 4.2.2 具名插槽 4.2.3 作用域插槽 4.3 自定义指令 4.3.1 什么是自定义指令…