开发和部署 ML 应用#
在本地开发 Ray Serve 应用并将其部署到生产环境的流程涵盖以下步骤
将机器学习模型转换为 Ray Serve 应用
在本地测试应用
为生产部署构建 Serve 配置文件
使用配置文件部署应用
将模型转换为 Ray Serve 应用#
本示例使用一个文本翻译模型
# File name: model.py
from transformers import pipeline
class Translator:
def __init__(self):
# Load model
self.model = pipeline("translation_en_to_fr", model="t5-small")
def translate(self, text: str) -> str:
# Run inference
model_output = self.model(text)
# Post-process output to return only the translation text
translation = model_output[0]["translation_text"]
return translation
translator = Translator()
translation = translator.translate("Hello world!")
print(translation)
名为 model.py 的 Python 文件使用 Translator 类将英语文本翻译成法语。
Translator的__init__方法中的self.model变量存储了一个使用 t5-small 模型翻译文本的函数。当
self.model在英文文本上调用时,它会在格式为[{"translation_text": "..."}]的字典中返回翻译后的法文文本。Translator的translate方法通过索引字典来提取翻译后的文本。
复制并粘贴该脚本并在本地运行。它会将 "Hello world!" 翻译成 "Bonjour Monde!"。
$ python model.py
Bonjour Monde!
将此模型转换为具有 FastAPI 的 Ray Serve 应用需要三个更改
导入 Ray Serve 和 FastAPI 依赖项
为使用 FastAPI 的 Serve 部署添加装饰器:
@serve.deployment和@serve.ingress(app)将
Translator部署绑定到传递给其构造函数的参数
有关其他 HTTP 选项,请参阅 设置 FastAPI 和 HTTP。
import ray
from ray import serve
from fastapi import FastAPI
from transformers import pipeline
app = FastAPI()
@serve.deployment(num_replicas=2, ray_actor_options={"num_cpus": 0.2, "num_gpus": 0})
@serve.ingress(app)
class Translator:
def __init__(self):
# Load model
self.model = pipeline("translation_en_to_fr", model="t5-small")
@app.post("/")
def translate(self, text: str) -> str:
# Run inference
model_output = self.model(text)
# Post-process output to return only the translation text
translation = model_output[0]["translation_text"]
return translation
translator_app = Translator.bind()
请注意,代码配置了部署的参数,例如 num_replicas 和 ray_actor_options。这些参数有助于配置部署副本的数量以及每个副本的资源需求。在此示例中,我们设置了 2 个模型副本,每个副本占用 0.2 个 CPU 和 0 个 GPU。有关部署的可配置参数的完整指南,请参阅 配置 Serve 部署。
在本地测试 Ray Serve 应用#
要在本地进行测试,请使用 serve run CLI 命令运行脚本。此命令接受格式为 module:application 的导入路径。请在包含脚本本地副本的目录中运行该命令,并将脚本保存为 model.py,以便它能够导入应用程序
$ serve run model:translator_app
此命令运行 translator_app 应用,然后阻塞,将日志流式传输到控制台。您可以使用 Ctrl-C 停止它,这将销毁该应用。
现在通过 HTTP 测试模型。可以通过以下默认 URL 访问它
http://127.0.0.1:8000/
发送一个包含英文文本的 JSON 数据的 POST 请求。此客户端脚本请求翻译“Hello world!”
# File name: model_client.py
import requests
response = requests.post("http://127.0.0.1:8000/", params={"text": "Hello world!"})
french_text = response.json()
print(french_text)
在 Ray Serve 应用部署期间,请使用 serve status CLI 命令检查应用和部署的状态。有关 serve status 输出格式的更多详细信息,请参阅 在生产环境中检查 Serve。
$ serve status
proxies:
a85af35da5fcea04e13375bdc7d2c83c7d3915e290f1b25643c55f3a: HEALTHY
applications:
default:
status: RUNNING
message: ''
last_deployed_time_s: 1693428451.894696
deployments:
Translator:
status: HEALTHY
replica_states:
RUNNING: 2
message: ''
为生产部署构建 Serve 配置文件#
要在生产环境中部署 Serve 应用,您需要生成一个 Serve 配置文件 YAML 文件。Serve 配置文件是集群的单一事实来源,允许您在一个地方指定系统级配置和您的应用。它还允许您声明式地更新您的应用。serve build CLI 命令接受导入路径作为输入,并使用 -o 标志将其保存到输出文件中。您可以在 Serve 配置文件中指定所有部署参数。
$ serve build model:translator_app -o config.yaml
serve build 命令添加了一个默认的应用名称,可以修改。生成的 Serve 配置文件是
proxy_location: EveryNode
http_options:
host: 0.0.0.0
port: 8000
grpc_options:
port: 9000
grpc_servicer_functions: []
applications:
- name: app1
route_prefix: /
import_path: model:translator_app
runtime_env: {}
deployments:
- name: Translator
num_replicas: 2
ray_actor_options:
num_cpus: 0.2
num_gpus: 0.0
您还可以将 Serve 配置文件与 serve run 一起用于本地测试。例如
$ serve run config.yaml
$ serve status
proxies:
1894261b372d34854163ac5ec88405328302eb4e46ac3a2bdcaf8d18: HEALTHY
applications:
app1:
status: RUNNING
message: ''
last_deployed_time_s: 1693430474.873806
deployments:
Translator:
status: HEALTHY
replica_states:
RUNNING: 2
message: ''
有关更多详细信息,请参阅 Serve 配置文件。
在生产环境中部署 Ray Serve#
使用 KubeRay 操作符在 Kubernetes 上生产部署 Ray Serve 应用。将上一步生成的 YAML 文件直接复制到 Kubernetes 配置中。KubeRay 支持您的生产应用的零停机升级、状态报告和容错。有关更多信息,请参阅 在 Kubernetes 上部署。对于生产环境使用,请考虑实施推荐的实践,即设置 主节点容错。
监控 Ray Serve#
使用 Ray Dashboard 获取您的 Ray Cluster 和 Ray Serve 应用状态的高级别概览。Ray Dashboard 在本地测试和生产环境中的远程集群中都可用。Ray Serve 提供了一些内置的指标和日志记录,以及用于在您的应用中添加自定义指标和日志的实用程序。对于生产部署,建议将日志和指标导出到您的可观测性平台。有关更多详细信息,请参阅 监控。