Pytorch基礎(chǔ)教程之torchserve模型部署解析
note
torch-model-archiver
打包模型;利用torchserve
加載前面打包的模型,并以grpc和http等接口往外提供推理服務(wù)- 自定義
handler
類時(shí)initialize()、preprocess()、postprocess()和handle()這四個(gè)方法都是可選的
- 自定義
- 啟動(dòng)模型的api服務(wù)、
curl
命令發(fā)送http post請(qǐng)求,請(qǐng)求模型服務(wù)API;流程和TensorFlow serving流程大同小異 torchserve
是基于netty
網(wǎng)絡(luò)框架實(shí)現(xiàn)的,底層使用EpollServerSocketChannel
服務(wù)進(jìn)行網(wǎng)絡(luò)通信,通過(guò)epoll
多路復(fù)用技術(shù)實(shí)現(xiàn)高并發(fā)網(wǎng)絡(luò)連接處理。
一、torchserve和archiver模塊
- 模型部署需要用到兩個(gè)模塊
- torchserve用來(lái)模型部署
- torch-model-archiver打包模型
pip: - torch-workflow-archiver - torch-model-archiver - torchserve
二、Speech2Text Wav2Vec2模型部署
2.1 準(zhǔn)備模型和自定義handler
- Wav2Vec2語(yǔ)音轉(zhuǎn)文本的模型。這里我們?yōu)榱撕?jiǎn)化流程從huggingface下載對(duì)應(yīng)的模型,進(jìn)行本地化利用torchserve部署
hander
將原始data進(jìn)行轉(zhuǎn)為模型輸入所需的格式;nlp中很多任務(wù)可以直接用torchtext的text_classifier
。
# 1. 導(dǎo)入huggingface模型 from transformers import AutoModelForCTC, AutoProcessor import os modelname = "facebook/wav2vec2-base-960h" model = AutoModelForCTC.from_pretrained(modelname) processor = AutoProcessor.from_pretrained(modelname) modelpath = "model" os.makedirs(modelpath, exist_ok=True) model.save_pretrained(modelpath) processor.save_pretrained(modelpath) # 2. 自定義handler import torch import torchaudio from transformers import AutoProcessor, AutoModelForCTC import io class Wav2VecHandler(object): def __init__(self): self._context = None self.initialized = False self.model = None self.processor = None self.device = None # Sampling rate for Wav2Vec model must be 16k self.expected_sampling_rate = 16_000 def initialize(self, context): """Initialize properties and load model""" self._context = context self.initialized = True properties = context.system_properties # See https://pytorch.org/serve/custom_service.html#handling-model-execution-on-multiple-gpus self.device = torch.device("cuda:" + str(properties.get("gpu_id")) if torch.cuda.is_available() else "cpu") model_dir = properties.get("model_dir") self.processor = AutoProcessor.from_pretrained(model_dir) self.model = AutoModelForCTC.from_pretrained(model_dir) def handle(self, data, context): """Transform input to tensor, resample, run model and return transcribed text.""" input = data[0].get("data") if input is None: input = data[0].get("body") # torchaudio.load accepts file like object, here `input` is bytes model_input, sample_rate = torchaudio.load(io.BytesIO(input), format="WAV") # Ensure sampling rate is the same as the trained model if sample_rate != self.expected_sampling_rate: model_input = torchaudio.functional.resample(model_input, sample_rate, self.expected_sampling_rate) model_input = self.processor(model_input, sampling_rate = self.expected_sampling_rate, return_tensors="pt").input_values[0] logits = self.model(model_input)[0] pred_ids = torch.argmax(logits, axis=-1)[0] output = self.processor.decode(pred_ids) return [output]
在自定義 Handler 中,需要實(shí)現(xiàn)以下方法:
- initialize: 用于初始化模型,加載權(quán)重等操作。
- preprocess: 用于將原始輸入數(shù)據(jù)轉(zhuǎn)換為 PyTorch 張量。
- inference: 用于執(zhí)行模型推理。
- postprocess: 用于將模型輸出轉(zhuǎn)換為 API 輸出格式。
2.2 打包模型和啟動(dòng)模型api服務(wù)
- 可以直接在linux環(huán)境的terminal進(jìn)行如下相關(guān)操作(打包模型、啟動(dòng)模型的api服務(wù)、curl命令發(fā)送http post請(qǐng)求,請(qǐng)求模型服務(wù)API)
- curl命令發(fā)送http post請(qǐng)求,請(qǐng)求模型服務(wù)API,如果遇到報(bào)錯(cuò)java.lang.NoSuchMethodError: java.nio.file.Files.readString(Ljava/nio/file/Path;)Ljava/lang/String;則應(yīng)該是JRE沒(méi)有安裝或者需要升級(jí):sudo apt install default-jre即可。
- curl那坨后正常會(huì)返回I HAD THAT CURIOSITY BESIDE ME AT THIS MOMENT%,測(cè)試數(shù)據(jù)是一段簡(jiǎn)單的sample.wav語(yǔ)音文件
- Waveform Audio File Format(WAVE,又或者是因?yàn)閃AV后綴而被大眾所知的),它采用RIFF(Resource Interchange File Format)文件格式結(jié)構(gòu)。通常用來(lái)保存PCM格式的原始音頻數(shù)據(jù),所以通常被稱為無(wú)損音頻
# 打包部署模型文件, 把model部署到torchserve torch-model-archiver --model-name Wav2Vec2 --version 1.0 --serialized-file model/pytorch_model.bin --handler ./handler.py --extra-files "model/config.json,model/special_tokens_map.json,model/tokenizer_config.json,model/vocab.json,model/preprocessor_config.json" -f mv Wav2Vec2.mar model_store # 啟動(dòng)model服務(wù), 加載前面打包的model, 并以grpc和http接口向外提供推理服務(wù) torchserve --start --model-store model_store --models Wav2Vec2=Wav2Vec2.mar --ncs # Once the server is running, let's try it with: curl -X POST http://127.0.0.1:8080/predictions/Wav2Vec2 --data-binary '@./sample.wav' -H "Content-Type: audio/basic" # 暫停torchserve serving torchserve --stop
2.3 相關(guān)參數(shù)記錄
torch-model-archiver:用來(lái)打包模型
- model-name: 設(shè)定部署的模型名稱
- version: 設(shè)定部署的模型版本
- model-file: 定義模型結(jié)構(gòu)的python文件
- serialized-file: 設(shè)定訓(xùn)練模型保存的pth文件
- export-path: 設(shè)定打包好的模型保存路徑
- extra-files: 設(shè)定額外的文件,如label跟id映射的定義文件等,用于一并打包到模型壓縮包中
- handler: 為一個(gè)處理器,用來(lái)指定模型推理預(yù)測(cè)前后的數(shù)據(jù)的處理問(wèn)題;如 nlp模型中的文本分詞和轉(zhuǎn)換為id的步驟;以及分類問(wèn)題中,模型預(yù)測(cè)結(jié)果映射為具體的label等數(shù)據(jù)處理功能
torch-model-archiver:用來(lái)打包模型 usage: torch-model-archiver [-h] --model-name MODEL_NAME [--serialized-file SERIALIZED_FILE] [--model-file MODEL_FILE] --handler HANDLER [--extra-files EXTRA_FILES] [--runtime {python,python2,python3}] [--export-path EXPORT_PATH] [--archive-format {tgz,no-archive,default}] [-f] -v VERSION [-r REQUIREMENTS_FILE]
torchserve:該組件用來(lái)加載前面打包的模型,并以grpc和http等接口往外提供推理服務(wù)
- start 和 stop: 即推理服務(wù)的啟動(dòng)和停止;
- model-store: 打包模型存儲(chǔ)的路徑;
- models: 設(shè)定模型名稱和模型文件名,如:MODEL_NAME=MODEL_PATH2 格式;
- no-config-snapshots: 即 --ncs,用來(lái)設(shè)置防止服務(wù)器存儲(chǔ)配置快照文件;
Reference
[1] https://github.com/pytorch/serve
[2] Torch Model archiver for TorchServe
[3] https://github.com/pytorch/serve/tree/master/examples/speech2text_wav2vec2
[4] https://huggingface.co/docs/transformers/model_doc/wav2vec2
[5] https://github.com/pytorch/serve/tree/master/model-archiver
[7] cURL - 學(xué)習(xí)/實(shí)踐
[8] Serving PyTorch Models Using TorchServe(by using transformer model for example)
[9] 四種常見(jiàn)的 POST 提交數(shù)據(jù)方式
[10] TorchServe 詳解:5 步將模型部署到生產(chǎn)環(huán)境
[11] PyTorch最新工具torchserve用于0.部署模型
到此這篇關(guān)于Pytorch基礎(chǔ)教程之torchserve模型部署和推理的文章就介紹到這了,更多相關(guān)torchserve模型部署內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python?基本結(jié)構(gòu)語(yǔ)句(函數(shù)和模塊)
這篇文章主要介紹了python?基本結(jié)構(gòu)語(yǔ)句(函數(shù)和模塊),文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09Appium+Python自動(dòng)化環(huán)境搭建實(shí)例教程
這篇文章主要介紹了Appium+Python自動(dòng)化環(huán)境搭建實(shí)例教程,本文通過(guò)實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08Python+OpenCV繪制多instance的Mask圖像
Mask圖像中,不同值表示不同的實(shí)例(instance)。本文將詳細(xì)為大家講講如何利用OpenCV繪制多instance的Mask圖像,感興趣的可以學(xué)習(xí)一下2022-06-06在Apache服務(wù)器上同時(shí)運(yùn)行多個(gè)Django程序的方法
這篇文章主要介紹了在Apache服務(wù)器上同時(shí)運(yùn)行多個(gè)Django程序的方法,Django是Python各色高人氣web框架中最為著名的一個(gè),需要的朋友可以參考下2015-07-07Python StringIO及BytesIO包使用方法解析
這篇文章主要介紹了Python StringIO及BytesIO包使用方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06python fabric實(shí)現(xiàn)遠(yuǎn)程部署
這篇文章主要為大家詳細(xì)介紹了 python fabric實(shí)現(xiàn)遠(yuǎn)程部署,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01python3 中時(shí)間戳、時(shí)間、日期的轉(zhuǎn)換和加減操作
這篇文章主要介紹了python3 中時(shí)間戳、時(shí)間、日期的轉(zhuǎn)換和加減操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07