快捷方式

⚠️ 注意:維護受限

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

入門

安裝 TorchServe 和 torch-model-archiver

  1. 安裝依賴項

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

適用於基於 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
      

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

    啟動 TorchServe 來服務模型

    歸檔並存儲模型後,使用 torchserve 命令來服務模型。

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

    執行上述 torchserve 命令後,TorchServe 將在您的主機上執行,監聽推理請求。

    注意:如果您在執行 TorchServe 時指定了模型,它會自動將後端工作程序的數量擴充套件到可用 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
    

    檢查日誌

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

    可以使用 Benchmark 生成吞吐量或百分位精度等高階效能資料,並在報告中進行視覺化。

    除錯處理程式程式碼

    如果您想除錯處理程式程式碼,可以只執行 TorchServe 的後端,從而使用任何 Python 偵錯程式。您可以參考此處定義的一個示例。

    貢獻

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


    © 版權所有 2020, PyTorch Serve 貢獻者。

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

    文件

    訪問 PyTorch 的全面開發者文件

    檢視文件

    教程

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

    檢視教程

    資源

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

    檢視資源