快捷方式

⚠️ 注意:有限維護

本專案不再積極維護。現有版本仍然可用,但沒有計劃的更新、錯誤修復、新功能或安全補丁。使用者應注意漏洞可能不會得到解決。

TorchServe gRPC API

注意:當前的 TorchServe gRPC 不支援工作流。

TorchServe 也支援用於推理呼叫和管理呼叫的 gRPC API

TorchServe 提供以下 gRPC API

  • 推理 API

    • Ping :獲取正在執行的伺服器的健康狀態

    • Predictions :從已服務的模型獲取預測結果

    • StreamPredictions :從已儲存的模型獲取伺服器端流式預測結果

對於所有推理 API 請求,TorchServe 要求包含正確的推理令牌,或者必須停用令牌授權。有關更多詳細資訊,請參閱令牌授權文件

  • 管理 API

    • RegisterModel :在 TorchServe 上服務一個模型/模型版本

    • UnregisterModel :透過從 TorchServe 登出特定版本的模型來釋放系統資源

    • ScaleWorker :動態調整任何模型版本的工作程序數量,以更好地服務不同的推理請求負載。

    • ListModels :查詢當前已註冊模型的預設版本

    • DescribeModel :獲取模型預設版本的詳細執行時狀態

    • SetDefault :將模型的任何已註冊版本設定為預設版本

對於所有管理 API 請求,TorchServe 要求包含正確的管理令牌,或者必須停用令牌授權。有關更多詳細資訊,請參閱令牌授權文件

預設情況下,TorchServe 在本地主機上監聽埠 7070 用於 gRPC 推理 API,埠 7071 用於 gRPC 管理 API。要在不同地址和埠上配置 gRPC API,請參考配置文件

gRPC API 的 Python 客戶端示例

執行以下命令,使用 gRPC python 客戶端,從 TorchServe 模型庫註冊、執行推理和登出 densenet161 模型。

git clone --recurse-submodules https://github.com/pytorch/serve
cd serve
  • 安裝 gRPC python 依賴項

pip install -U grpcio protobuf grpcio-tools googleapis-common-protos
  • 啟動 TorchServe

mkdir models
torchserve --start --disable-token-auth --enable-model-api --model-store models/
  • 使用 proto 檔案生成 python gRPC 客戶端 stub

python -m grpc_tools.protoc -I third_party/google/rpc --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
  • 註冊 densenet161 模型

注意:要在 TorchServe 啟動後使用此 API,必須啟用模型 API 控制。啟動 TorchServe 時,在命令列新增 --enable-model-api 以啟用此 API 的使用。有關更多詳細資訊,請參閱模型 API 控制

如果令牌授權已停用,請使用

python ts_scripts/torchserve_grpc_client.py register densenet161

如果令牌授權已啟用,請使用

python ts_scripts/torchserve_grpc_client.py register densenet161 --auth-token <management-token>
  • 使用以下命令執行推理

如果令牌授權已停用,請使用

python ts_scripts/torchserve_grpc_client.py infer densenet161 examples/image_classifier/kitten.jpg

如果令牌授權已啟用,請使用

python ts_scripts/torchserve_grpc_client.py infer densenet161 examples/image_classifier/kitten.jpg --auth-token <inference-token>
  • 登出 densenet161 模型

注意:要在 TorchServe 啟動後使用此 API,必須啟用模型 API 控制。啟動 TorchServe 時,在命令列新增 --enable-model-api 以啟用此 API 的使用。有關更多詳細資訊,請參閱模型 API 控制

如果令牌授權已停用,請使用

python ts_scripts/torchserve_grpc_client.py unregister densenet161

如果令牌授權已啟用,請使用

python ts_scripts/torchserve_grpc_client.py unregister densenet161 --auth-token <management-token>

GRPC 伺服器端流式處理

TorchServe GRPC API 添加了推理 API “StreamPredictions” 的伺服器端流式處理,以允許透過相同的 GRPC 流傳送一系列推理響應。此新 API 僅推薦用於完整響應的推理延遲較高且推理中間結果傳送到客戶端的用例。一個例子可能是用於生成式應用程式的 LLM,生成“n”個 token 可能具有高延遲,在這種情況下,使用者可以在每個生成的 token 就緒後立即接收,直到完整響應完成。此新 API 會自動強制 batchSize 為一。

service InferenceAPIsService {
    // Check health status of the TorchServe server.
    rpc Ping(google.protobuf.Empty) returns (TorchServeHealthResponse) {}

    // Predictions entry point to get inference using default model version.
    rpc Predictions(PredictionsRequest) returns (PredictionResponse) {}

    // Streaming response for an inference request.
    rpc StreamPredictions(PredictionsRequest) returns (stream PredictionResponse) {}
}

後端處理器呼叫 “send_intermediate_predict_response” 將一箇中間結果傳送到前端,並以現有方式返回最後一個結果。例如

from ts.handler_utils.utils import send_intermediate_predict_response
''' Note: TorchServe v1.0.0 will deprecate
"from ts.protocol.otf_message_handler import send_intermediate_predict_response".
Please replace it with "from ts.handler_utils.utils import send_intermediate_predict_response".
'''

def handle(data, context):
    if type(data) is list:
        for i in range (3):
            send_intermediate_predict_response(["intermediate_response"], context.request_ids, "Intermediate Prediction success", 200, context)
        return ["hello world "]

文件

訪問 PyTorch 的全面開發者文件

檢視文件

教程

獲取面向初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源