⚠️ 注意:有限維護
本專案不再積極維護。現有版本仍然可用,但沒有計劃的更新、錯誤修復、新功能或安全補丁。使用者應注意,漏洞可能不會得到解決。
模型 API 控制¶
TorchServe 現在預設停用模型 API 的使用(特別是模型註冊和刪除功能)。可以透過命令列或 config.properties 檔案啟用這些 API 的使用。
TorchServe 預設在啟動後停用透過 API 呼叫註冊和刪除模型的功能。這是一項安全功能,用於解決 TorchServe 啟動後模型被意外註冊和刪除的問題。這適用於使用者可能以模型形式上傳惡意程式碼到模型伺服器,或使用者可能刪除正在使用的模型的情況。預設行為可防止使用者在 TorchServe 執行後註冊或刪除模型。可以啟用模型 API 控制,允許使用者使用 TorchServe 模型載入和刪除 API 來註冊和刪除模型。
設定模型 API 控制的三種方法¶
環境變數:使用
TS_ENABLE_MODEL_API並將其設定為true以啟用模型 API 使用,設定為false以停用。請注意,必須在 config.properties 中設定enable_envvars_config=true才能使用環境變數配置在啟動 TorchServe 時向命令列新增
--enable-model-api,將模式從停用切換到啟用。命令列無法用於停用,只能用於啟用向 config.properties 檔案新增
enable_model_api=false或enable_model_api=trueenable_model_api=false是預設值,可防止使用者在 TorchServe 執行後註冊或刪除模型enable_model_api=true不是預設值,允許使用者使用 TorchServe 模型 API 註冊和刪除模型
優先順序遵循以下TorchServe 標準
示例 1
配置檔案:
enable_model_api=false命令列:
torchserve --start --ncs --model-store model_store --enable-model-api結果:模型 API 模式已啟用
示例 2
配置檔案:
enable_model_api=true命令列:
torchserve --start --ncs --model-store model_store結果:模式已啟用(無法透過命令列停用 API 模式)
模型 API 控制預設行為¶
在啟動時,TorchServe 僅載入透過 --models 命令列選項明確指定的模型。在此模式下,使用者在啟動後將無法註冊或刪除模型。
預設示例¶
ubuntu@ip-172-31-11-32:~/serve$ torchserve --start --ncs --model-store model_store --models resnet-18=resnet-18.mar --ts-config config.properties
...
ubuntu@ip-172-31-11-32:~/serve$ curl -X POST "https://:8081/models?url=https://torchserve.pytorch.org/mar_files/squeezenet1_1.mar"
2024-05-30T21:46:03,625 [INFO ] epollEventLoopGroup-3-2 ACCESS_LOG - /127.0.0.1:53514 "POST /models?url=https://torchserve.pytorch.org/mar_files/squeezenet1_1.mar HTTP/1.1" 405 0
2024-05-30T21:46:03,626 [INFO ] epollEventLoopGroup-3-2 TS_METRICS - Requests4XX.Count:1.0|#Level:Host|#hostname:ip-172-31-11-32,timestamp:1717105563
{
"code": 405,
"type": "MethodNotAllowedException",
"message": "Requested method is not allowed, please refer to API document."
}
模型控制 API 已啟用¶
將模型 API 設定為 enabled 允許使用者使用模型載入 API 載入和解除安裝模型。
使用命令列設定模式為已啟用的示例¶
ubuntu@ip-172-31-11-32:~/serve$ torchserve --start --ncs --model-store model_store --models resnet-18=resnet-18.mar --ts-config config.properties --enable-model-api
ubuntu@ip-172-31-11-32:~/serve$ curl -X POST "https://:8081/models?url=https://torchserve.pytorch.org/mar_files/squeezenet1_1.mar"
{
"status": "Model \"squeezenet1_1\" Version: 1.0 registered with 0 initial workers. Use scale workers API to add workers for the model."
}
ubuntu@ip-172-31-11-32:~/serve$ curl https://:8081/models
2024-05-30T21:41:47,098 [INFO ] epollEventLoopGroup-3-2 ACCESS_LOG - /127.0.0.1:36270 "GET /models HTTP/1.1" 200 2
2024-05-30T21:41:47,099 [INFO ] epollEventLoopGroup-3-2 TS_METRICS - Requests2XX.Count:1.0|#Level:Host|#hostname:ip-172-31-11-32,timestamp:1717105307
{
"models": [
{
"modelName": "resnet-18",
"modelUrl": "resnet-18.mar"
},
{
"modelName": "squeezenet1_1",
"modelUrl": "https://torchserve.pytorch.org/mar_files/squeezenet1_1.mar"
}
]
}
ubuntu@ip-172-31-11-32:~/serve$ torchserve --stop
TorchServe has stopped.