快捷方式

⚠️ 注意:維護有限

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

推理 API

推理 API 預設監聽埠 8080 且僅可從 localhost 訪問。要更改預設設定,請參閱TorchServe 配置

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

TorchServe 伺服器支援以下 API

API 描述

要檢視完整的推理 API 列表,您可以使用以下命令

curl -X OPTIONS https://:8080

輸出採用 OpenAPI 3.0.1 json 格式。您可以使用它生成客戶端程式碼,更多詳情請參閱swagger codegen

健康檢查 API

此 API 遵循 InferenceAPIsService.Ping gRPC API。它返回 ModelServer 中模型的狀態。

TorchServe 支援一個 ping API,您可以透過呼叫它來檢查正在執行的 TorchServe 伺服器的健康狀態

curl https://:8080/ping

如果伺服器正在執行,響應如下

{
  "status": "Healthy"
}

“maxRetryTimeoutInSec”(預設值:5 分鐘)可以在模型的 config yaml 檔案(例如 model-config.yaml)中定義。它是恢復死亡的後端 worker 的最大時間視窗。一個健康的 worker 可以在 maxRetryTimeoutInSec 視窗內處於 WORKER_STARTED、WORKER_MODEL_LOADED 或 WORKER_STOPPED 狀態。“Ping”端點”

  • 返回 200 + json 訊息“healthy”:對於任何模型,活動 worker 的數量等於或大於配置的 minWorkers。

  • 返回 500 + json 訊息“unhealthy”:對於任何模型,活動 worker 的數量小於配置的 minWorkers。

預測 API

此 API 遵循 InferenceAPIsService.Predictions gRPC API。它返回 ModelServer 中模型的狀態。

要從每個載入模型的預設版本獲取預測結果,請向 /predictions/{model_name} 發起 REST 呼叫

  • POST /predictions/{model_name}

curl 示例

curl -O https://raw.githubusercontent.com/pytorch/serve/master/docs/images/kitten_small.jpg

curl https://:8080/predictions/resnet-18 -T kitten_small.jpg

or:

curl https://:8080/predictions/resnet-18 -F "data=@kitten_small.jpg"

從需要多個輸入的載入模型獲取預測結果

curl https://:8080/predictions/squeezenet1_1 -F 'data=@docs/images/dogs-before.jpg' -F 'data=@docs/images/kitten_small.jpg'

or:

import requests

res = requests.post("https://:8080/predictions/squeezenet1_1", files={'data': open('docs/images/dogs-before.jpg', 'rb'), 'data': open('docs/images/kitten_small.jpg', 'rb')})

要從每個載入模型的特定版本獲取預測結果,請向 /predictions/{model_name}/{version} 發起 REST 呼叫

  • POST /predictions/{model_name}/{version}

curl 示例

curl -O https://raw.githubusercontent.com/pytorch/serve/master/docs/images/kitten_small.jpg

curl https://:8080/predictions/resnet-18/2.0 -T kitten_small.jpg

or:

curl https://:8080/predictions/resnet-18/2.0 -F "data=@kitten_small.jpg"

結果是 JSON 格式,顯示該影像最有可能是一隻虎斑貓。最高預測結果為

{
    "class": "n02123045 tabby, tabby cat",
    "probability": 0.42514491081237793
}
  • 透過 HTTP 1.1 分塊編碼流式傳輸響應 TorchServe 推理 API 支援流式傳輸響應,允許透過 HTTP 1.1 分塊編碼傳送一系列推理響應。此新功能僅建議在完整響應的推理延遲較高且將推理中間結果傳送到客戶端的情況下使用。例如,對於生成式應用中的大型語言模型 (LLM),生成“n”個 token 可能會有較高的延遲,在這種情況下,使用者可以在每個生成的 token 準備好後立即接收,直到完整響應完成。為了實現流式傳輸響應,後端 handler 呼叫 “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 "]

客戶端接收分塊資料。

def test_echo_stream_inference():
    test_utils.start_torchserve(no_config_snapshots=True, gen_mar=False)
    test_utils.register_model('echo_stream',
                              'https://torchserve.pytorch.org/mar_files/echo_stream.mar')

    response = requests.post(TF_INFERENCE_API + '/predictions/echo_stream', data="foo", stream=True)
    assert response.headers['Transfer-Encoding'] == 'chunked'

    prediction = []
    for chunk in (response.iter_content(chunk_size=None)):
        if chunk:
            prediction.append(chunk.decode("utf-8"))

    assert str(" ".join(prediction)) == "hello hello hello hello world "
    test_utils.unregister_model('echo_stream')

解釋 API

Torchserve 利用 Captum 的功能返回已服務的模型的解釋結果。

要從每個載入模型的預設版本獲取解釋結果,請向 /explanations/{model_name} 發起 REST 呼叫

  • POST /explanations/{model_name}

curl 示例

curl http://127.0.0.1:8080/explanations/mnist -T examples/image_classifier/mnist/test_data/0.png

結果是一個 json 格式,提供輸入影像的解釋結果

  [
    [
      [
        [
          0.004570948731989492,
          0.006216969640322402,
          0.008197565423679522,
          0.009563574612830427,
          0.008999274832810742,
          0.009673474804303854,
          0.007599905146155397,
          ,
	        ,

        ]
      ]
    ]
  ]

KServe 推理 API

Torchserve 利用 KServe 推理 API 返回已服務的模型的預測結果。

要從載入的模型獲取預測結果,請向 /v1/models/{model_name}:predict 發起 REST 呼叫

  • POST /v1/models/{model_name}:predict

curl 示例

 curl -H "Content-Type: application/json" --data @kubernetes/kserve/kf_request_json/v1/mnist.json http://127.0.0.1:8080/v1/models/mnist:predict

結果是一個 json 格式,提供輸入 json 的預測結果

{
  "predictions": [
    2
  ]
}

KServe 解釋 API

Torchserve 利用 KServe API 規範返回已服務模型的解釋結果。

要從載入的模型獲取解釋結果,請向 /v1/models/{model_name}:explain 發起 REST 呼叫

  • /v1/models/{model_name}:explain

curl 示例

 curl -H "Content-Type: application/json" --data @kubernetes/kserve/kf_request_json/v1/mnist.json http://127.0.0.1:8080/v1/models/mnist:explain

結果是一個 json 格式,提供輸入 json 的解釋結果

{
  "explanations": [
    [
      [
        [
          0.004570948731989492,
          0.006216969640322402,
          0.008197565423679522,
          0.009563574612830427,
          0.008999274832810742,
          0.009673474804303854,
          0.007599905146155397,
          ,
          ,
	        ,
        ]
      ]
    ]
  ]
}

© Copyright 2020, PyTorch Serve 貢獻者。

使用 Sphinx 構建,主題由 Read the Docs 提供。

文件

訪問全面的 PyTorch 開發者文件

檢視文件

教程

獲取針對初學者和高階開發者的深入教程

檢視教程

資源

查詢開發資源並解答您的疑問

檢視資源