推理 API¶
推理 API 正在侦听端口 8080,默认情况下只能从 localhost 访问。要更改默认设置,请参阅 TorchServe 配置。
对于所有推理 API 请求,TorchServe 都需要包含正确的推理令牌,否则必须禁用令牌授权。有关更多详细信息,请参阅 Token 授权文档
TorchServe器支持以下 API:
API 描述 - 获取可用 API 和选项的列表
运行状况检查 API - 获取正在运行的服务器的运行状况
预测 API - 从提供的模型获取预测
解释 API - 从提供的模型中获取解释
KServe 推理 API - 从 KServe 获取所提供模型的预测
KServe 解释 API - 从 KServe 获取所服务模型的解释
接口说明¶
要查看推理 API 的完整列表,您可以使用以下命令:
curl -X OPTIONS http://localhost:8080
输出采用 OpenAPI 3.0.1 json 格式。您可以使用它来生成客户端代码,有关更多详细信息,请参阅 swagger codegen。
健康检查 API¶
此 API 遵循 InferenceAPIsService.Ping gRPC API。它返回 ModelServer 中模型的状态。
TorchServe 支持一个 API,您可以调用该 API 来检查正在运行的 TorchServe器的运行状况:ping
curl http://localhost:8080/ping
如果服务器正在运行,则响应为:
{
"status": "Healthy"
}
“maxRetryTimeoutInSec” (默认: 5MIN) 可以在模型的配置 yaml 文件(例如 model-config.yaml)中定义。这是恢复死机后端 worker 的最长时间窗口。正常运行的工作程序可以处于 maxRetryTimeoutInSec 窗口中的状态:WORKER_STARTED、WORKER_MODEL_LOADED 或 WORKER_STOPPED。“Ping” 端点”
return 200 + json message “healthy”:对于任何模型,活动工作线程的数量等于或大于配置的 minWorkers。
返回 500 + JSON 消息 “unhealthy”:对于任何模型,活动工作线程的数量都小于配置的 minWorkers。
预测 API¶
此 API 遵循 InferenceAPIsService.Predictions gRPC API。它返回 ModelServer 中模型的状态。
要从每个加载模型的默认版本获取预测,请对 REST 调用:/predictions/{model_name}
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')})
要从每个加载模型的特定版本获取预测,请对 REST 调用:/predictions/{model_name}/{version}
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/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” 个令牌可能会有很高的延迟,在这种情况下,用户可以在准备好后接收每个生成的令牌,直到完整响应完成。为了实现流式响应,后端处理程序调用 “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/{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 Inference API 来返回所服务的模型的预测。
要从加载的模型中获取预测,请对 REST 调用:/v1/models/{model_name}:predict
POST /v1/models/{model_name}:p redict
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}:解释
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,
,
,
,
]
]
]
]
}