捷徑

開始使用

安裝 TorchServe 和 torch-model-archiver

  1. 安裝依賴項目

    注意:對於 Conda,需要 Python >=3.8 才能執行 Torchserve。

適用於基於 Debian 的系統/ MacOS

  • 適用於 CPU

    python ./ts_scripts/install_dependencies.py
    
  • 適用於具有 Cuda 12.1 的 GPU。選項為 cu92cu101cu102cu111cu113cu116cu117cu118cu121

    python ./ts_scripts/install_dependencies.py --cuda=cu121
    

注意:PyTorch 1.9+ 不支援 cu92 和 cu101。因此 TorchServe 僅支援高達 PyTorch 1.8.1 的 cu92 和 cu101。

適用於 Windows

請參考此處的文件 這裡

  • 安裝 torchserve、torch-model-archiver 和 torch-workflow-archiver

    對於 Conda 注意:Windows 不支援 Conda 套件。請參考此處的文件 這裡

    conda install torchserve torch-model-archiver torch-workflow-archiver -c pytorch
    

    適用於 Pip

    pip install torchserve torch-model-archiver torch-workflow-archiver
    
  • 現在您可以準備好 使用 TorchServe 封裝和提供模型服務

    提供模型服務

    本節展示了使用 TorchServe 提供模型服務的簡單範例。要完成此範例,您必須已經 安裝了 TorchServe 和模型封存器

    要執行此範例,請複製 TorchServe 儲存庫

    git clone https://github.com/pytorch/serve.git
    

    然後從儲存庫根目錄的父目錄執行以下步驟。例如,如果您將儲存庫複製到 /home/my_path/serve,請從 /home/my_path 執行這些步驟。

    儲存模型

    要使用 TorchServe 提供模型服務,首先將模型封存為 MAR 檔案。您可以使用模型封存器來封裝模型。您也可以建立模型儲存區來儲存您封存的模型。

    1. 建立一個目錄來儲存您的模型。

      mkdir model_store
      
    2. 下載已訓練的模型。

      wget https://download.pytorch.org/models/densenet161-8d451a50.pth
      
    3. 使用模型封存器封存模型。extra-files 參數使用來自 TorchServe 儲存庫的檔案,因此請在必要時更新路徑。

      torch-model-archiver --model-name densenet161 --version 1.0 --model-file ./serve/examples/image_classifier/densenet_161/model.py --serialized-file densenet161-8d451a50.pth --export-path model_store --extra-files ./serve/examples/image_classifier/index_to_name.json --handler image_classifier
      

    有關模型封存器的更多資訊,請參閱 Torch 模型封存器,適用於 TorchServe

    啟動 TorchServe 以提供模型服務

    在您封存並儲存模型後,請使用 torchserve 命令來提供模型服務。

    torchserve --start --ncs --model-store model_store --models densenet161.mar
    

    在您執行上面的 torchserve 命令後,TorchServe 會在您的主機上執行,並監聽推論請求。

    注意: 如果您在執行 TorchServe 時指定模型,它會自動將後端 worker 擴展到等於可用 vCPU (如果您在 CPU 實例上執行) 或可用 GPU 數量 (如果您在 GPU 實例上執行)。 在具有大量運算資源 (vCPU 或 GPU) 的強大主機上,此啟動和自動擴展過程可能需要相當長的時間。 如果您想最小化 TorchServe 啟動時間,您應該避免在啟動時註冊和擴展模型,並將其移至稍後的時間點,方法是使用相應的 管理 API,該 API 允許更精細地控制為任何特定模型分配的資源。

    從模型取得預測

    要測試模型伺服器,請將請求發送到伺服器的 predictions API。 TorchServe 支援所有通過 gRPCHTTP/REST推論管理 API。

    透過 Python 客戶端使用 GRPC API

    • 安裝 grpc python 依賴項

    pip install -U grpcio protobuf grpcio-tools
    
    • 使用 proto 檔案生成推論客戶端

    python -m grpc_tools.protoc --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
    
    python ts_scripts/torchserve_grpc_client.py infer densenet161 examples/image_classifier/kitten.jpg
    

    使用 REST API

    作為一個例子,我們將下載下面的可愛小貓

    kitten

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

    然後呼叫預測端點

    curl http://127.0.0.1:8080/predictions/densenet161 -T kitten_small.jpg
    

    它將返回以下 JSON 物件

    [
      {
        "tiger_cat": 0.46933549642562866
      },
      {
        "tabby": 0.4633878469467163
      },
      {
        "Egyptian_cat": 0.06456148624420166
      },
      {
        "lynx": 0.0012828214094042778
      },
      {
        "plastic_bag": 0.00023323034110944718
      }
    ]
    

    與端點的所有交互都將記錄在 logs/ 目錄中,所以請務必查看它!

    現在您已經了解使用 TorchServe 提供深度學習模型有多麼容易! 您想了解更多嗎?

    停止 TorchServe

    要停止目前執行的 TorchServe 實例,請執行

    torchserve --stop
    

    檢查日誌

    您已看到的所有作為 stdout 輸出的日誌,與模型註冊、管理、推論相關的日誌,都記錄在 /logs 資料夾中。

    高階效能數據,如輸送量或百分位數精度,可以使用 基準測試 生成,並在報告中視覺化。

    調試處理程序程式碼

    如果您想調試您的處理程序程式碼,您可以只使用後端運行 TorchServe,因此可以使用任何 Python 調試器。 您可以參考 此處 定義的範例

    貢獻

    如果您計劃使用 TorchServe 開發並更改一些原始程式碼,請遵循 貢獻指南

    文件

    取得 PyTorch 的全面開發人員文件

    檢視文件

    教學

    取得初學者和進階開發人員的深入教學

    檢視教學

    資源

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

    檢視資源