使用 TorchServe 进行批量推理¶
本文档的内容¶
介绍¶
批量推理是聚合推理请求并通过 ML/DL 框架一次性发送此聚合请求以进行推理的过程。 TorchServe 旨在原生支持传入推理请求的批处理。此功能使您能够以最佳方式使用主机资源。 因为大多数 ML/DL 框架都针对批处理请求进行了优化。 这种对主机资源的最佳使用反过来又降低了使用 TorchServe 托管推理服务的运营费用。
在本文档中,我们展示了一个示例,说明在本地提供模型或使用 docker 容器时如何在 Torchserve 中使用批量推理。
先决条件¶
在进入本文档之前,请阅读以下文档:
使用 TorchServe 的默认处理程序进行批量推理¶
TorchServe 的默认处理程序支持开箱即用的批量推理,但 handler 除外。text_classifier
使用 ResNet-152 模型通过 TorchServe 进行批量推理¶
为了支持批量推理,TorchServe 需要满足以下条件:
TorchServe 模型配置:配置并使用 “POST /models” 管理 API 或 config.properties 中的设置。 TorchServe 需要知道模型可以处理的最大批量大小,以及 TorchServe 应等待填充每个批量请求的最长时间。
batch_size
max_batch_delay
模型处理程序代码:TorchServe 需要 Model 处理程序来处理批量推理请求。
有关具有批处理的自定义模型处理程序的完整工作示例,请参阅 Hugging face transformer generalized handler
TorchServe 模型配置¶
从 Torchserve 0.4.1 开始,有两种方法可以配置 TorchServe 使用批处理功能:
通过 POST /models API 提供批量配置信息。
通过配置文件 config.properties 提供批处理配置信息。
我们感兴趣的配置属性如下:
batch_size
:这是模型预期处理的最大批量大小。max_batch_delay
:这是 TorchServe 等待接收请求数的最大批处理延迟时间。如果 TorchServe 未收到 请求,它会将收到的任何请求发送到模型。ms
batch_size
batch_size
handler
让我们看一个通过管理 API 使用此配置的示例:
# The following command will register a model "resnet-152.mar" and configure TorchServe to use a batch_size of 8 and a max batch delay of 50 milliseconds.
curl -X POST "localhost:8081/models?url=resnet-152.mar&batch_size=8&max_batch_delay=50"
这是通过 config.properties 使用此配置的示例:
# The following command will register a model "resnet-152.mar" and configure TorchServe to use a batch_size of 8 and a max batch delay of 50 milli seconds, in the config.properties.
models={\
"resnet-152": {\
"1.0": {\
"defaultVersion": true,\
"marName": "resnet-152.mar",\
"minWorkers": 1,\
"maxWorkers": 1,\
"batchSize": 8,\
"maxBatchDelay": 50,\
"responseTimeout": 120\
}\
}\
}
这些配置既用于 TorchServe 中,也用于模型的自定义服务代码(也称为处理程序代码)。 TorchServe 将与批处理相关的配置与每个模型相关联。 然后,前端尝试聚合批量大小的请求并将其发送到后端。
使用批处理支持的模型配置 TorchServe ResNet-152 模型的演示¶
在本节中,让我们启动模型服务器并启动 Resnet-152 模型,该模型使用默认处理程序进行批量推理。image_classifier
设置 TorchServe 和 Torch 模型存档器¶
首先,按照主自述文件并安装所有必需的软件包,包括 .torchserve
使用管理 API 配置的 Resnet-152 的批量推理¶
启动模型服务器。在此示例中,我们将启动模型服务器以在推理端口 8080 和管理端口 8081 上运行。
$ cat config.properties
...
inference_address=http://127.0.0.1:8080
management_address=http://127.0.0.1:8081
...
$ torchserve --start --model-store model_store
验证 TorchServe 是否已启动并正在运行
$ curl localhost:8080/ping
{
"status": "Healthy"
}
现在,我们启动 resnet-152 模型,我们构建该模型用于处理批量推理。因为这是一个示例,所以我们将启动 1 个 worker,它处理的批处理大小为 3,a 为 10ms。
max_batch_delay
$ curl -X POST "localhost:8081/models?url=https://torchserve.pytorch.org/mar_files/resnet-152-batch_v2.mar&batch_size=3&max_batch_delay=10&initial_workers=1"
{
"status": "Processing worker updates..."
}
验证 worker 是否已正确启动。
curl http://localhost:8081/models/resnet-152-batch_v2
[
{
"modelName": "resnet-152-batch_v2",
"modelVersion": "2.0",
"modelUrl": "https://torchserve.pytorch.org/mar_files/resnet-152-batch_v2.mar",
"runtime": "python",
"minWorkers": 1,
"maxWorkers": 1,
"batchSize": 3,
"maxBatchDelay": 10,
"loadedAtStartup": false,
"workers": [
{
"id": "9000",
"startTime": "2021-06-14T23:18:21.793Z",
"status": "READY",
"memoryUsage": 1726554112,
"pid": 19946,
"gpu": true,
"gpuUsage": "gpuId::0 utilization.gpu [%]::0 % utilization.memory [%]::0 % memory.used [MiB]::678 MiB"
}
]
}
]
现在让我们测试一下这个服务。
获取映像以测试此服务
$ curl -LJO https://github.com/pytorch/serve/raw/master/examples/image_classifier/kitten.jpg
运行推理以测试模型。
$ curl http://localhost:8080/predictions/resnet-152-batch_v2 -T kitten.jpg { "tiger_cat": 0.5798614621162415, "tabby": 0.38344162702560425, "Egyptian_cat": 0.0342114195227623, "lynx": 0.0005819813231937587, "quilt": 0.000273319921689108 }
通过 config.properties 配置的 Resnet-152 的批量推理¶
这里,我们首先在 config.properties 中设置 and,确保 mar 文件位于模型商店中,并且 models 设置中的版本与创建的 mar 文件版本一致。要了解有关配置的更多信息,请参阅此文档。
batch_size
max_batch_delay
load_models=resnet-152-batch_v2.mar
models={\
"resnet-152-batch_v2": {\
"2.0": {\
"defaultVersion": true,\
"marName": "resnet-152-batch_v2.mar",\
"minWorkers": 1,\
"maxWorkers": 1,\
"batchSize": 3,\
"maxBatchDelay": 5000,\
"responseTimeout": 120\
}\
}\
}
然后通过使用标志传递 config.properties 来启动 Torchserve
--ts-config
torchserve --start --model-store model_store --ts-config config.properties
验证 TorchServe 是否已启动并正在运行
$ curl localhost:8080/ping
{
"status": "Healthy"
}
验证 worker 是否已正确启动。
curl http://localhost:8081/models/resnet-152-batch_v2
[
{
"modelName": "resnet-152-batch_v2",
"modelVersion": "2.0",
"modelUrl": "resnet-152-batch_v2.mar",
"runtime": "python",
"minWorkers": 1,
"maxWorkers": 1,
"batchSize": 3,
"maxBatchDelay": 5000,
"loadedAtStartup": true,
"workers": [
{
"id": "9000",
"startTime": "2021-06-14T22:44:36.742Z",
"status": "READY",
"memoryUsage": 0,
"pid": 19116,
"gpu": true,
"gpuUsage": "gpuId::0 utilization.gpu [%]::0 % utilization.memory [%]::0 % memory.used [MiB]::678 MiB"
}
]
}
]
现在让我们测试一下这个服务。
获取映像以测试此服务
$ curl -LJO https://github.com/pytorch/serve/raw/master/examples/image_classifier/kitten.jpg
运行推理以测试模型。
$ curl http://localhost:8080/predictions/resnet-152-batch_v2 -T kitten.jpg { "tiger_cat": 0.5798614621162415, "tabby": 0.38344162702560425, "Egyptian_cat": 0.0342114195227623, "lynx": 0.0005819813231937587, "quilt": 0.000273319921689108 }
使用 Docker 使用批处理支持的模型配置 TorchServe ResNet-152 模型的演示¶
在这里,我们将展示在使用 docker 容器为模型提供服务时如何注册支持批量推理的模型。我们在 config.properties 中设置 and,类似于 dockered_entrypoint.sh 使用的上一节。batch_size
max_batch_delay
使用 docker 容器批量推理 Resnet-152¶
按照 dockered_entrypoint.sh 中引用的 config.properties 和 config.properties 中设置批处理 和
batch_size
max_batch_delay
inference_address=http://127.0.0.1:8080
management_address=http://127.0.0.1:8081
metrics_address=http://127.0.0.1:8082
number_of_netty_threads=32
job_queue_size=1000
model_store=/home/model-server/model-store
load_models=resnet-152-batch_v2.mar
models={\
"resnet-152-batch_v2": {\
"1.0": {\
"defaultVersion": true,\
"marName": "resnet-152-batch_v2.mar",\
"minWorkers": 1,\
"maxWorkers": 1,\
"batchSize": 3,\
"maxBatchDelay": 100,\
"responseTimeout": 120\
}\
}\
}
从这里构建目标 Docker 镜像,这里我们使用 GPU 镜像
./build_image.sh -g -cv cu102
开始使用容器为模型提供服务,并将 config.properties 传递给容器
docker run --rm -it --gpus all -p 127.0.0.1:8080:8080 -p 127.0.0.1:8081:8081 --name mar -v /home/ubuntu/serve/model_store:/home/model-server/model-store -v $ path to config.properties:/home/model-server/config.properties pytorch/torchserve:latest-gpu
验证 worker 是否已正确启动。
curl http://localhost:8081/models/resnet-152-batch_v2
[
{
"modelName": "resnet-152-batch_v2",
"modelVersion": "2.0",
"modelUrl": "resnet-152-batch_v2.mar",
"runtime": "python",
"minWorkers": 1,
"maxWorkers": 1,
"batchSize": 3,
"maxBatchDelay": 5000,
"loadedAtStartup": true,
"workers": [
{
"id": "9000",
"startTime": "2021-06-14T22:44:36.742Z",
"status": "READY",
"memoryUsage": 0,
"pid": 19116,
"gpu": true,
"gpuUsage": "gpuId::0 utilization.gpu [%]::0 % utilization.memory [%]::0 % memory.used [MiB]::678 MiB"
}
]
}
]
现在让我们测试一下这个服务。
获取映像以测试此服务
$ curl -LJO https://github.com/pytorch/serve/raw/master/examples/image_classifier/kitten.jpg
运行推理以测试模型。
$ curl http://localhost:8080/predictions/resnet-152-batch_v2 -T kitten.jpg { "tiger_cat": 0.5798614621162415, "tabby": 0.38344162702560425, "Egyptian_cat": 0.0342114195227623, "lynx": 0.0005819813231937587, "quilt": 0.000273319921689108 }