快速鍵

TorchServe gRPC API

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

TorchServe 也支援 gRPC API,可用於推論和管理呼叫。

TorchServe 提供以下 gRPC API

  • 推論 API

    • Ping:取得執行中伺服器的健康狀態

    • Predictions:從服務的模型取得預測

    • StreamPredictions:從儲存的模型取得伺服器端串流預測

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

  • 管理 API

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

    • UnregisterModel:透過從 TorchServe 取消註冊模型的特定版本來釋放系統資源

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

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

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

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

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

預設情況下,TorchServe 在 localhost 上偵聽 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 控制

如果停用 Token 授權,請使用

python ts_scripts/torchserve_grpc_client.py register densenet161

如果啟用 Token 授權,請使用

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

如果停用 Token 授權,請使用

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

如果啟用 Token 授權,請使用

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 控制

如果停用 Token 授權,請使用

python ts_scripts/torchserve_grpc_client.py unregister densenet161

如果啟用 Token 授權,請使用

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

GRPC 伺服器端串流

TorchServe GRPC APIs 新增了伺服器端的推論 API 串流功能 "StreamPredictions",允許透過同一個 GRPC 串流傳送一系列的推論回應。這個新的 API 僅建議用於完整回應的推論延遲較高,且推論的中間結果需要傳送給用戶端的情況。例如,用於生成式應用的大型語言模型 (LLM),產生 "n" 個 tokens 可能會有很高的延遲,在這種情況下,使用者可以在每個生成的 token 準備好後立即接收,直到完整回應完成。這個新的 API 會自動強制將 batchSize 設為 1。

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 完整的開發者文件

查看文件

教學

取得針對初學者和進階開發者的深入教學

查看教學

資源

尋找開發資源並獲得您的問題解答

查看資源