一、引言
本文圍繞昇騰通用推理鏡像的自定義部署展開最佳實踐梳理,旨在從代碼包準備、模型準備、部署流程等關鍵維度,提供一套可復用的最佳實踐。通過標準化的操作指南,幫助開發者快速掌握昇騰推理鏡像的自定義部署方法。
二、模型準備
1.開發機完成推理代碼開發和調試
開發機界面啟動VSCode。
編寫推理服務代碼
推理服務代碼要滿足openai api接口規范,當前平臺支持的接口能力請參考【一站式服務平臺】幫助手冊-【推理服務API】//www.daliqc.cn/document/10541165/10876778實例demo代碼見附錄
IDE內啟動推理服務準備驗證
調用/驗證推理服務接口
2. 保存代碼包
在VSCode列表頁面保存開發機中的代碼到代碼包。
3.保存模型
在VSCode列表頁面保存模型到模型管理。
三、服務部署
點擊“部署我的模型”,填寫表單創建服務。
服務名稱:服務名稱應小于50字符,不能包含空格。
鏡像選擇:系統內置鏡像、從JupyterLab/VSCode中制作的自定義鏡像、容器鏡像服務共享過來的鏡像。平臺預置支持910B的通用推理服務鏡像ascend-common-infer-svc
模型選擇:開發機保存的模型,或其他方式保存的模型。
代碼包選擇:開發機保存的代碼包,或其他方式保存的代碼包。
包安裝:推理服務依賴的包 & 版本
環境變量:輸入鏡像的啟動依賴的環境變量。
運行命令:輸入鏡像的啟動運行命令。
端口號:在線服務進程監聽的端口號。
獲取服務的APP_KEY和modelId
調用OpenAPI驗證
App Key需要放置在請求的Authrization請求頭中: Authorization: Bearer APP_KEY
e.g.
modelId需要添加在請求體中的model字段:model: modelId
e.g.
Postman請求示例
curl--location '//wishub.daliqc.cn:1443/v1/chat/completions' \
--header 'Authorization: Bearer 4a5**************************6fe' \
--header 'Content-Type: application/json' \
--data '{
"model":"b41************************b61",
"max_tokens":1024,
"top_p":0.1,
"top_k":5,
"stream":false,
"temperature":0.8,
"stream_options": {
"include_usage":true
},
"messages": [
{
"role":"user",
"content":"ping"
}
]
}'四、總結
昇騰通用推理鏡像自定義部署最佳實踐,覆蓋代碼包準備、模型準備、部署流程全流程。該實踐可適配多類場景,有效提升部署效率與服務穩定性。未來將進一步優化推理部署體系,為企業 AI 業務規模化落地提供有力支撐。
附錄
實例中使用的demo代碼
download_model_and_export_model_path.sh
mkdir Qwen2-0.5B-Instruct
cd Qwen2-0.5B-Instruct
wget -q --no-check-certificate //modelers.cn/coderepo/web/v1/file/OpenSource/Qwen2-0.5B-Instruct/main/media/config.json
wget -q --no-check-certificate //modelers.cn/coderepo/web/v1/file/OpenSource/Qwen2-0.5B-Instruct/main/media/generation_config.json
wget -q --no-check-certificate //modelers.cn/coderepo/web/v1/file/OpenSource/Qwen2-0.5B-Instruct/main/media/merges.txt
wget -q --no-check-certificate //modelers.cn/coderepo/web/v1/file/OpenSource/Qwen2-0.5B-Instruct/main/media/model.safetensors
wget -q --no-check-certificate //modelers.cn/coderepo/web/v1/file/OpenSource/Qwen2-0.5B-Instruct/main/media/qwen_inference.py
wget -q --no-check-certificate //modelers.cn/coderepo/web/v1/file/OpenSource/Qwen2-0.5B-Instruct/main/media/tokenizer.json
wget -q --no-check-certificate //modelers.cn/coderepo/web/v1/file/OpenSource/Qwen2-0.5B-Instruct/main/media/tokenizer_config.json
wget -q --no-check-certificate //modelers.cn/coderepo/web/v1/file/OpenSource/Qwen2-0.5B-Instruct/main/media/vocab.json
exportMODEL_PATH=`pwd`
cd..llm.py
from fastapi import FastAPI, Request
from transformers import pipeline
import json
import torch
import torch_npu
import os
app = FastAPI()
generator = pipeline('text-generation', model=os.environ.get('MODEL_PATH'), device=torch.device('npu:0'))
@app.post("/v1/chat/completions")
async def create_completion(request: Request):
data = await request.json()
response = generator(data.get('messages'), max_new_tokens =500)
return{
"choices":[
{
"message":response[0]['generated_text'][-1],
"finish_reason":"stop",
"index":0
}
],
"id":"chatcmpl-1234567890",
"model":"Qwen2-0.5B-Instruct",
"object":"chat.completion"
}
if __name__ =="__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8899)test.sh
curl --location '//0.0.0.0:8899/v1/chat/completions' --header 'Content-Type: application/json' --data '{
"messages": [
{"role": "system","content": "你是個客服"},
{"role": "user","content": "在嗎"}
]
}'