Docker torchserve 部署模型流程

news/2024/12/21 21:12:46/

1.拉取官方镜像

地址: https://hub.docker.com/r/pytorch/torchserve/tags

docker pull pytorch/torchserve:0.7.1-gpu

docker_6">2. docker启动指令

CPU

docker run --rm -it -d -p 8380:8080 -p 8381:8081 --name torch-server -v /path/model-server/extra-files:/home/model-server/extra-files -v /path/model-server/model-store:/home/model-server/model-store pytorch/torchserve:0.7.1-gpu

GPU

docker run --rm -it -d --gpus all -p 8380:8080 -p 8381:8081 --name torch-server -v /path/model-server/extra-files:/home/model-server/extra-files -v /path/model-server/model-store:/home/model-server/model-store pytorch/torchserve:0.7.1-gpu

/home/model-server/model-store 是docker映射地址,不能更改

进入容器,可以发现各个端口的意义,8080是通信访问接口,8081是管理服务配置接口,8082是服务监控接口
在这里插入图片描述

3. 打包模型文件

3.1 使用框架中脚本或者自己写脚本将模型转为torchscript(.pt)

3.2 torchscript转.mar文件

(1) run_hander.py
from xx_model_handler import KnowHandler_service = KnowHandler()def handle(data, context):try:if not _service.initialized:print('ENTERING INITIALIZATION')_service.initialize(context)if data is None:return Nonedata = _service.preprocess(data)data = _service.inference(data)data = _service.postprocess(data)return dataexcept Exception as e:raise Exception("Unable to process input data. " + str(e))
(2) xx_model_handler.py
"""
ModelHandler defines a custom model handler.
"""
import torch
import os
import json
import logging
from transformers import BertTokenizerclass KnowHandler(object):"""A custom model handler implementation."""def __init__(self):super(KnowHandler, self).__init__()self.initialized = Falsedef initialize(self, ctx):"""Initialize model. This will be called during model loading time:param context: Initial context contains model server system properties.:return:"""self.manifest = ctx.manifestproperties = ctx.system_propertiesmodel_dir = properties.get("model_dir")serialized_file = self.manifest["model"]["serializedFile"]model_pt_path = os.path.join(model_dir, serialized_file)self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")config_path = os.path.join(model_dir, "config.json")with open(config_path,"r") as fr:setup_config = json.load(fr)self.model = torch.jit.load(model_pt_path, map_location=self.device)self.tokenizer = BertTokenizer(setup_config["vocab_path"])self.max_length = setup_config["max_length"]self.initialized = True#  load the model, refer 'custom handler class' above for detailsdef preprocess(self, data):"""Transform raw input into model input data.:param batch: list of raw requests, should match batch size:return: list of preprocessed model input data"""# Take the input data and make it inference readypreprocessed_data = data[0].get("data")if preprocessed_data is None:preprocessed_data = data[0].get("body")inputs = preprocessed_data.decode('utf-8')inputs = json.loads(inputs) # {"text": []}return inputsdef inference(self, model_input):"""Internal inference methods:param model_input: transformed model input data:return: list of inference output in NDArray"""# Do some inference call to engine here and return outputtext = model_input["text"]inputs = self.tokenizer(text,max_length=self.max_length,truncation=True,padding='max_length',return_tensors='pt')#inputs = {k: torch.as_tensor(v, dtype=torch.int64) for k, v in inputs.items()}for key, value in inputs.items():if isinstance(value, torch.Tensor):inputs[key] = value.to(self.device)input_ids = inputs['input_ids']token_type_ids = inputs['token_type_ids']attention_mask = inputs['attention_mask']logits = self.model(input_ids,attention_mask,token_type_ids)return logitsdef postprocess(self, inference_output):"""Return inference result.:param inference_output: list of inference output:return: list of predict results"""# Take output from network and post-process to desired formatpostprocess_output = [inference_output.tolist()]return postprocess_output
(3) config.json
{"threshold": 0.8,"max_length": 40
}

torch-model-archiver --model-name {name of model} --version {模型版本} --serialized-file {torchscript文件地址} --export-path {.mar文件存放地址} --handler run_handler.py --extra-files {其它文件如配置文件等} --runtime python3 -f

torch-model-archiver --model-name my_model --version 1.0 --serialized-file /path/mymodel.pt --export-path /home/model-server/model-store --handler run_handler.py --extra-files "xx_model_handler,utils.py,config.json,vocab.txt"  --runtime python -f

–model-name: 模型的名称,后来的接口名称和管理的模型名称都是这个
–serialized-file: 模型环境及代码及参数的打包文件
–export-path: 本次打包文件存放位置
–extra-files: handle.py中需要使用到的其他文件
–handler: 指定handler函数。(模型名:函数名)
-f 覆盖之前导出的同名打包文件

4. torchserver配置接口

(1)查询已注册的模型
curl "http://localhost:8381/models"
(2)注册模型并为模型分配资源

将.mar模型文件注册,注意:.mar文件必须放在model-store文件夹下,即/path/model-server/model-store

curl -X POST "{ip:port}/models?url={.mar文件名}&model_name={model_name}&batch_size=8&max_batch_delay=10&initial_workers=1"curl -X POST "localhost:8381/models?url=my_model.mar&model_name=my_model&batch_size=8&max_batch_delay=10&initial_workers=1"
(3)查看模型状态
curl http://localhost:8381/models/{model_name}
(4)删除注册模型
curl -X DELETE http://localhost:8381/models/{model_name}/{version}

5. 模型推理

response = requests.post('http://localhost:8380/predictions/{model_name}/{version}',data = data)
# -*- coding: utf-8 -*-
import requests
import json
text = ['xxxxx']
data = {'data':json.dumps({'text':text})}
print(data)
response = requests.post('http://localhost:8380/predictions/my_model',data = data)
print(response)
if response.status_code==200:vectors = response.json()print(vectors)

参考:
https://blog.51cto.com/u_16213661/8750698
https://blog.csdn.net/wangzitaotao/article/details/131101852
https://pytorch.org/serve/index.html
https://docs.aws.amazon.com/zh_cn/sagemaker/latest/dg/deploy-models-frameworks-torchserve.html


http://www.ppmy.cn/news/1527446.html

相关文章

计算机四级操作系统考试大纲.md

基本要求 1.掌握操作系统的基本概念、基本结构及运行机制。 操作系统(Operating System,简称OS)是计算机系统中负责管理和控制计算机硬件与软件资源的核心软件,它提供了用户与计算机硬件之间的接口&#…

HC-SR04超声波传感器详解(STM32)

目录 一、介绍 二、传感器原理 1.原理图 2.引脚描述 3.工作原理介绍 三、程序设计 main.c文件 ultrasonic.h文件 ultrasonic.c文件 四、实验效果 五、资料获取 项目分享 一、介绍 HC-SR04超声波传感器是通过发送和接收超声波,利用时间差和声音传播速度…

linux 双网卡服务器突然断电后网卡单通故障解决

某台linux 双网卡服务器突然断电后网卡单通故障解决 故障现象:断电后重启服务器,主用网卡IP只能同网段访问,其他网段无法访问,备用网卡则正常; 解决方案:route -n查询路由信息,发现主网卡路由…

JavaWeb开发02 - js+vue

什么是JavaScript? JavaScript(简称:JS) 是一门跨平台、面向对象的脚本语言。是用来控制网页行为的&#xff0c;它能使网页可交互。 JS引入方式 内部脚本:将JS代码定义在HTML页面中 JavaScript代码必须位于<script></script>标签之间在HTML文档中&#xff0c;可…

财富之眼用经济思维看清世界PDF高清下载

财富之眼用经济思维看清世界.pdf: 下载地址&#xff1a; http://share.vpssw.com/f/28426853-1358046308-e8b70f

大数据-130 - Flink CEP 详解 - CEP开发流程 与 案例实践:恶意登录检测实现

点一下关注吧&#xff01;&#xff01;&#xff01;非常感谢&#xff01;&#xff01;持续更新&#xff01;&#xff01;&#xff01; 目前已经更新到了&#xff1a; Hadoop&#xff08;已更完&#xff09;HDFS&#xff08;已更完&#xff09;MapReduce&#xff08;已更完&am…

C++:内部类,匿名对象,操作符new与delete

一.内部类 1.如果一个类定义在另一个类的内部&#xff0c;这个内部类就叫做内部类。内部类是一个独立的类&#xff0c;跟定义在全局相比&#xff0c;他只是受外部类类域限制和访问限定符限制&#xff0c;所以外部类定义的对象中不包含内部类。 2.内部类默认是外部类的友元类。…

合宙Air201资产定位模组LuatOS入门课程:FOTA远程升级,点点鼠标就搞定

你是否也经常吐槽&#xff1a;开发是个苦差事&#xff01;做项目一时爽&#xff0c;遇到升级火葬场。 如果你也有这种困惑&#xff0c;可以多了解了解合宙的开发工具&#xff0c;简单实用又高效&#xff0c;甚至只需点点鼠标&#xff01; 本期&#xff0c;我们来学习合宙Air2…