推理API¶
推理API在8080端口上监听,默认情况下仅可从本地主机访问。要更改默认设置,请参阅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分钟)可以在模型的配置yaml文件中定义(例如model-config.yaml)。这是恢复一个死后端工作者的最大时间窗口。健康的工作者在maxRetryTimeoutInSec窗口内可以处于状态:WORKER_STARTED、WORKER_MODEL_LOADED或WORKER_STOPPED。“Ping”端点
返回200状态码,并包含JSON消息“healthy”:对于任何模型,活跃工作者的数量等于或大于配置的minWorkers。
返回500错误,消息为“unhealthy”:对于任何模型,活跃工作者的数量少于配置的minWorkers。
预测API¶
此 API 遵循 InferenceAPIsService.Predictions gRPC API。它返回 ModelServer 中模型的状态。
要从加载的每个模型的默认版本获取预测,请通过REST调用 /predictions/{model_name}:
POST /预测/{模型名称}
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')})
要从每个加载的模型中获取特定版本的预测,请通过REST调用 /predictions/{model_name}/{version}:
POST /预测/{模型名称}/{版本}
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分块编码。这个新功能仅推荐在推理全响应的延迟较高且中间结果发送给客户端时使用。例如,对于生成应用程序中的LLMs,生成“n”个令牌可能具有高延迟,在这种情况下,用户可以接收每个生成的令牌直到完成整个响应。要实现流式响应,后端处理器调用“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 的功能来返回被服务的模型的解释。
为了从每个加载的模型的默认版本获取解释,请通过REST调用 /explanations/{model_name}:
POST /explanations/{模型名称}
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 返回服务的模型预测。
为了从加载的模型中获取预测,请通过REST调用 /v1/models/{model_name}:predict:
POST /v1/models/{模型名称}: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 规范返回它服务的模型的解释。
为了从加载的模型获取解释,请通过REST调用/v1/models/{model_name}:explain:
/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,
,
,
,
]
]
]
]
}