使用 TorchServe 进行批量推理¶
本文档内容¶
简介¶
批量推理是一个聚合推理请求并通过 ML/DL 框架一次性发送这些聚合请求以进行推理的过程。TorchServe 被设计为原生支持传入推理请求的批处理。此功能使您能够最佳地利用主机资源,因为大多数 ML/DL 框架都针对批量请求进行了优化。这种主机资源的最佳利用反过来降低了使用 TorchServe 托管推理服务的运营费用。
在本文档中,我们将展示一个示例,说明如何在本地或使用 docker 容器服务模型时在 Torchserve 中使用批量推理。
先决条件¶
在开始阅读本文档之前,请阅读以下文档
使用 TorchServe 默认处理器进行批量推理¶
TorchServe 的默认处理器开箱即用地支持批量推理,除了 text_classifier
处理器。
使用 ResNet-152 模型通过 TorchServe 进行批量推理¶
为了支持批量推理,TorchServe 需要以下内容
TorchServe 模型配置:通过使用“POST /models”管理 API 或 config.properties 中的设置来配置
batch_size
和max_batch_delay
。TorchServe 需要知道模型可以处理的最大批处理大小,以及 TorchServe 应该等待填充每个批处理请求的最长时间。模型处理器代码:TorchServe 要求模型处理器处理批量推理请求。
有关具有批量处理的自定义模型处理器的完整工作示例,请参阅 Hugging face transformer generalized handler
TorchServe 模型配置¶
从 Torchserve 0.4.1 开始,有两种方法可以配置 TorchServe 以使用批处理功能
通过 POST /models API 提供批处理配置信息。
通过配置文件 config.properties 提供批处理配置信息。
我们感兴趣的配置属性如下
batch_size
:这是模型预期处理的最大批处理大小。max_batch_delay
:这是 TorchServe 等待接收batch_size
个请求的最大批处理延迟时间(以ms
为单位)。如果 TorchServe 在此计时器超时之前未收到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 将批处理相关配置与每个模型关联。前端然后尝试聚合 batch-size 数量的请求并将其发送到后端。
演示如何配置具有批量支持模型的 TorchServe ResNet-152 模型¶
在本节中,让我们启动模型服务器并启动 Resnet-152 模型,该模型使用默认的 image_classifier
处理器进行批量推理。
设置 TorchServe 和 Torch Model Archiver¶
首先,请按照主要的 Readme 并安装所有必需的软件包,包括 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 个工作进程,该工作进程处理批处理大小为 3,
max_batch_delay
为 10 毫秒。
$ 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..."
}
验证工作进程是否已正确启动。
curl https://127.0.0.1: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 https://127.0.0.1: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 中设置
batch_size
和max_batch_delay
,确保 mar 文件位于 model-store 中,并且 models 设置中的版本与创建的 mar 文件的版本一致。要了解有关配置的更多信息,请参阅此文档。
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\
}\
}\
}
然后将通过使用
--ts-config
标志传递 config.properties 来启动 Torchserve
torchserve --start --model-store model_store --ts-config config.properties
验证 TorchServe 是否已启动并正在运行
$ curl localhost:8080/ping
{
"status": "Healthy"
}
验证工作进程是否已正确启动。
curl https://127.0.0.1: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 https://127.0.0.1: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 中设置 batch_size
和 max_batch_delay
,dockerd_entrypoint.sh 正在使用它。
使用 docker 容器的 Resnet-152 批量推理¶
在 config.properties 中设置批处理
batch_size
和max_batch_delay
,如 dockerd_entrypoint.sh 中引用的那样
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
验证工作进程是否已正确启动。
curl https://127.0.0.1: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 https://127.0.0.1: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 }