高哲技术博客 高哲技术博客
首页
编程
爬虫
运维
硬件
收藏
归档
关于

嘉美伯爵

前途光明,无需畏惧
首页
编程
爬虫
运维
硬件
收藏
归档
关于
  • 架构

  • 思想

  • 语言

  • 设计模式

  • 微服务

  • 数据分析

  • 人工智能

    • 基础

    • 部署

      • pytorch部署教程及相关理论理解
      • torchserve使用指南
      • pytorch的生产部署方式
        • 整体步骤
        • torch-model-archiver
        • 接口
        • 参考
      • 如何在阿里云serverless中使用modelscope
    • 大模型

    • 传统模型

    • 调研

    • 落地

    • 评估

  • 区块链

  • 数据结构

  • 技术调研

  • 性能优化

  • 生产问题

  • 编程
  • 人工智能
  • 部署
fovegage
2023-08-11
目录

pytorch的生产部署方式

# 整体步骤

# 关在在于 initialize 初始化模型,支持部署任意模型,基于netty
# 注意java版本大于17
# /Library/Java/JavaVirtualMachines/jdk-20.jdk/Contents/Home/bin
# 切换java版本:https://juejin.cn/post/6871959224314757134
# /usr/libexec/java_home -V 查看所有版本
export JAVA_HOME=`/usr/libexec/java_home -v 20.0.1`

# 下载仓库
git clone https://github.com/pytorch/serve

# 必须使用该初始化环境 cpu
python ./ts_scripts/install_dependencies.py
python ./ts_scripts/install_dependencies.py --cuda=cu110  # gpu

# 生成protobuf
python -m grpc_tools.protoc --proto_path=frontend/server/src/main/resources/proto/ --python_out=ts_scripts --grpc_python_out=ts_scripts frontend/server/src/main/resources/proto/inference.proto frontend/server/src/main/resources/proto/management.proto

# 将pth打包为mar
torch-model-archiver --model-name densenet161 --version 1.0 --model-file ./examples/image_classifier/densenet_161/model.py --serialized-file ./model_store/densenet161-8d451a50.pth --export-path model_store --extra-files ./examples/image_classifier/index_to_name.json --handler image_classifier

# 启动serve (--models 配置使用的模型)
torchserve --start --ncs --model-store ./model_store --models ./model_store/densenet161.mar

# http 测试
curl http://127.0.0.1:8080/predictions/densenet161 -T kitten_small.jpg

# grpc 测试
python ts_scripts/torchserve_grpc_client.py infer densenet161 examples/image_classifier/kitten.jpg
python ts_scripts/torchserve_grpc_client.py register densenet161
python ts_scripts/torchserve_grpc_client.py unregister densenet161
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

# torch-model-archiver

# 必须的文件 handler.py、model.py、mar
--model-name: 模型名称,导出后的模型文件是“模型名称.mar”
--serialized-file:mar、tgz都可以,模型序列化文件,这里有两种文件数据,一种是eager mode模式下,包含状态字典的.pt或者.pth文件;另一种是TorchScript条件下的可执行模块。
--model-file:模型结构框架,通常只包含一个类,类是torch.nn.modules的子类, 包含 向前转播等
--handler: torchserver 的入口程序,它负责模型的加载,预测等
--extra-files:额外文件,模型运行等其它额外文件都可以,可以包含多个,用逗号将不同文件拼接成一个字符串。
--run-time:选择运行的python版本。
--archive-format:选择压缩文件的格式, {tgz,no-archive,default} 。可以是tgz压缩文件,也可以是mar文件。
--export-path:mar存档文件的保存地址,未设置则保存在当前目录。
-f: 强制覆盖。
-v --version: 模型的版本
-r, --requirement-f: requirements.txt依赖包,模型环境相关的依赖包
1
2
3
4
5
6
7
8
9
10
11
12

# 接口

initialize 接口: 这个接口用于初始化模型。您需要在这个接口中加载模型的权重和参数,并设置模型的状态(如评估模式)。
preprocess 接口: 这个接口用于数据预处理,将原始输入数据转换为模型可以接受的格式。您可以在这里进行数据处理、转换和编码。
inference 接口: 这个接口用于执行推理操作,将预处理后的数据传递给加载的模型,并获取模型的输出。您需要在这里调用模型的前向传播方法。
postprocess 接口: 这个接口用于后处理,将模型输出转换为最终的结果。这可能涉及到解码、映射到标签、格式转换等。
cleanup 接口: 这个接口用于清理资源,当 TorchServe 服务停止时会被调用。您可以在这里释放模型占用的资源。
1
2
3
4
5

# 参考

  • torchserve调用官方库模型 (opens new window)
  • torchserve使用-注册模型设置参数(二) (opens new window)
  • 官方github demo (opens new window)
  • 使用TorchServe部署PyTorch模型以进行大规模推理 (opens new window)
  • 使用 TorchServe 部署 Model (opens new window)
上次更新: 2023-09-07 23:06:40
torchserve使用指南
如何在阿里云serverless中使用modelscope

← torchserve使用指南 如何在阿里云serverless中使用modelscope→

最近更新
01
token embed和postion embed
06-10
02
k8s pod日志排查问题
10-24
03
golang内部私服建设方案
10-21
更多文章>
Theme by Vdoing | Copyright © 2018-2025 嘉美伯爵 | 鲁ICP备20001560号-4
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式