⚠️ 注意:维护有限
本项目不再积极维护。虽然现有版本仍然可用,但没有计划的更新、错误修复、新功能或安全补丁。用户应注意,可能无法解决漏洞问题。
推理 API¶
推理 API 默认监听端口 8080 且仅可从 localhost 访问。要更改默认设置,请参阅TorchServe 配置。
对于所有推理 API 请求,TorchServe 要求包含正确的推理令牌,或者必须禁用令牌授权。更多详情请参阅令牌授权文档
TorchServe 服务器支持以下 API
API 描述 - 获取可用 API 和选项列表
健康检查 API - 获取正在运行的服务器的健康状态
预测 API - 从已服务的模型获取预测结果
解释 API - 从已服务的模型获取解释结果
KServe 推理 API - 从 KServe 获取已服务模型的预测结果
KServe 解释 API - 从 KServe 获取已服务模型的解释结果
API 描述¶
要查看完整的推理 API 列表,您可以使用以下命令
curl -X OPTIONS http://localhost:8080
输出采用 OpenAPI 3.0.1 json 格式。您可以使用它生成客户端代码,更多详情请参阅swagger codegen。
健康检查 API¶
此 API 遵循 InferenceAPIsService.Ping gRPC API。它返回 ModelServer 中模型的状态。
TorchServe 支持一个 ping
API,您可以通过调用它来检查正在运行的 TorchServe 服务器的健康状态
curl http://localhost: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 http://localhost:8080/predictions/resnet-18 -T kitten_small.jpg
or:
curl http://localhost:8080/predictions/resnet-18 -F "data=@kitten_small.jpg"
从需要多个输入的加载模型获取预测结果
curl http://localhost:8080/predictions/squeezenet1_1 -F 'data=@docs/images/dogs-before.jpg' -F 'data=@docs/images/kitten_small.jpg'
or:
import requests
res = requests.post("http://localhost: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 http://localhost:8080/predictions/resnet-18/2.0 -T kitten_small.jpg
or:
curl http://localhost: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,
,
,
,
]
]
]
]
}