Ray Serve:可扩展且可编程的服务#
Ray Serve 是一个可扩展的模型服务库,用于构建在线推理 API。Serve 支持框架无关,因此您可以使用单一工具包来服务从 PyTorch、TensorFlow 和 Keras 等框架构建的深度学习模型,到 Scikit-Learn 模型,再到任意 Python 业务逻辑。它具有多项功能和性能优化,用于大型语言模型(LLM)的服务,例如响应流式传输、动态请求批处理、多节点/多 GPU 服务等。
Ray Serve 特别适合模型组合和多模型服务,使您能够使用 Python 代码构建一个由多个 ML 模型和业务逻辑组成的复杂推理服务。
Ray Serve 构建在 Ray 之上,因此可以轻松扩展到多台机器,并提供灵活的调度支持,例如分数 GPU,以便您可以共享资源并以低成本服务许多机器学习模型。
快速入门#
安装 Ray Serve 及其依赖项
pip install "ray[serve]"
定义一个简单的“hello world”应用程序,在本地运行它,并通过 HTTP 查询它。
import requests
from starlette.requests import Request
from typing import Dict
from ray import serve
# 1: Define a Ray Serve application.
@serve.deployment
class MyModelDeployment:
def __init__(self, msg: str):
# Initialize model state: could be very large neural net weights.
self._msg = msg
def __call__(self, request: Request) -> Dict:
return {"result": self._msg}
app = MyModelDeployment.bind(msg="Hello world!")
# 2: Deploy the application locally.
serve.run(app, route_prefix="/")
# 3: Query the application and print the result.
print(requests.get("https://:8000/").json())
# {'result': 'Hello world!'}
更多示例#
使用 Serve 的模型组合 API 将多个部署组合成一个应用程序。
import requests
import starlette
from typing import Dict
from ray import serve
from ray.serve.handle import DeploymentHandle
# 1. Define the models in our composition graph and an ingress that calls them.
@serve.deployment
class Adder:
def __init__(self, increment: int):
self.increment = increment
def add(self, inp: int):
return self.increment + inp
@serve.deployment
class Combiner:
def average(self, *inputs) -> float:
return sum(inputs) / len(inputs)
@serve.deployment
class Ingress:
def __init__(
self,
adder1: DeploymentHandle,
adder2: DeploymentHandle,
combiner: DeploymentHandle,
):
self._adder1 = adder1
self._adder2 = adder2
self._combiner = combiner
async def __call__(self, request: starlette.requests.Request) -> Dict[str, float]:
input_json = await request.json()
final_result = await self._combiner.average.remote(
self._adder1.add.remote(input_json["val"]),
self._adder2.add.remote(input_json["val"]),
)
return {"result": final_result}
# 2. Build the application consisting of the models and ingress.
app = Ingress.bind(Adder.bind(increment=1), Adder.bind(increment=2), Combiner.bind())
serve.run(app)
# 3: Query the application and print the result.
print(requests.post("https://:8000/", json={"val": 100.0}).json())
# {"result": 101.5}
使用 Serve 的FastAPI 集成,优雅地处理 HTTP 解析和验证。
import requests
from fastapi import FastAPI
from ray import serve
# 1: Define a FastAPI app and wrap it in a deployment with a route handler.
app = FastAPI()
@serve.deployment
@serve.ingress(app)
class FastAPIDeployment:
# FastAPI will automatically parse the HTTP request for us.
@app.get("/hello")
def say_hello(self, name: str) -> str:
return f"Hello {name}!"
# 2: Deploy the deployment.
serve.run(FastAPIDeployment.bind(), route_prefix="/")
# 3: Query the deployment and print the result.
print(requests.get("https://:8000/hello", params={"name": "Theodore"}).json())
# "Hello Theodore!"
要运行此示例,请安装以下内容:pip install transformers
使用 Ray Serve 来服务预训练的Hugging Face Transformers 模型。我们将使用的模型是一个情感分析模型:它将接收文本字符串作为输入,并返回文本是“正面”还是“负面”。
import requests
from starlette.requests import Request
from typing import Dict
from transformers import pipeline
from ray import serve
# 1: Wrap the pretrained sentiment analysis model in a Serve deployment.
@serve.deployment
class SentimentAnalysisDeployment:
def __init__(self):
self._model = pipeline("sentiment-analysis")
def __call__(self, request: Request) -> Dict:
return self._model(request.query_params["text"])[0]
# 2: Deploy the deployment.
serve.run(SentimentAnalysisDeployment.bind(), route_prefix="/")
# 3: Query the deployment and print the result.
print(
requests.get(
"https://:8000/", params={"text": "Ray Serve is great!"}
).json()
)
# {'label': 'POSITIVE', 'score': 0.9998476505279541}
为什么选择 Serve?#
Serve 如何为我这样的… 提供帮助?#
Serve 与… 相比如何?#
我们坚信 Serve 是独一无二的,因为它使您能够完全控制您的 ML 应用程序,同时提供可扩展性和高性能。要通过其他工具实现 Serve 的功能,您需要将 Tensorflow Serving 和 SageMaker 等多个框架结合起来,甚至需要自己编写微批处理组件来提高吞吐量。
了解更多#
请查看入门和关键概念,或者前往示例开始构建您的 Ray Serve 应用程序。
入门
从我们的快速入门教程开始,了解在本地部署单个模型以及如何将现有模型转换为 Ray Serve 部署。
关键概念
了解 Ray Serve 背后的关键概念。了解部署、如何查询它们,以及使用DeploymentHandles来组合多个模型和业务逻辑。
示例
遵循教程,学习如何将 Ray Serve 与TensorFlow和Scikit-Learn集成。
API 参考
获取有关 Ray Serve API 的更深入信息。
有关更多信息,请参阅以下有关 Ray Serve 的博客文章
在生产环境中服务 ML 模型:常见模式,作者:Simon Mo、Edward Oakes 和 Michael Galarnyk
使用纯 Python 在生产环境中服务 NLP 模型的最简单方法,作者:Edward Oakes 和 Bill Chambers
机器学习服务已失效,作者:Simon Mo
如何使用 Ray Serve 扩展您的 FastAPI 应用程序,作者:Archit Kulkarni