使用 ray.serve 传递多个对象以并行执行
passing multiple objects for parallel execution using ray.serve
我正在关注 ray 文档中的 this 教程
具体到这部分:
client = serve.start()
config = {"num_replicas": 3}
client.create_backend("tf:v1", TFMnistModel, TRAINED_MODEL_PATH,config=config)
client.create_endpoint("tf_classifier", backend="tf:v1", route="/mnist")
下面将单个样本发送到后端
sample_data= np.random.randn(28 * 28).tolist()
resp = requests.get(
"http://localhost:8000/mnist",
json={"array": sample_data})
如何同时发送多个样本,以便利用所有内核并行执行它们?例如使用以下
创建的 100 个 MNIST 样本
# 100 MNIST sample 28x28
sample_data = np.random.randn(100 * 28 * 28).reshape((100, 28, 28))
requests.get()
调用是阻塞的,所以你说得对,我们不应该在 for 循环中只调用它 100 次。
要通过 HTTP 并行发送多个样本,您需要有多个连接。以下使用 asyncio
和 aiohttp
的代码示例显示了一种实现此目的的方法:https://gist.github.com/architkulkarni/0bd0a92c3195c58ec460a5a0e5eb0e88#file-benchmark-py(您需要编辑 url
并将 JSON 输入添加到session.get()
以匹配您的示例)
另一种方法是跳过 HTTP 并使用 Ray Serve 的 ServeHandle API.
从 Python 执行此操作
handle = client.get_handle("tf_classifier")
futures = [handle.remote({"array": np.random.randn(28 * 28)}) for i in range(100)]
results = ray.get(futures)
我正在关注 ray 文档中的 this 教程
具体到这部分:
client = serve.start()
config = {"num_replicas": 3}
client.create_backend("tf:v1", TFMnistModel, TRAINED_MODEL_PATH,config=config)
client.create_endpoint("tf_classifier", backend="tf:v1", route="/mnist")
下面将单个样本发送到后端
sample_data= np.random.randn(28 * 28).tolist()
resp = requests.get(
"http://localhost:8000/mnist",
json={"array": sample_data})
如何同时发送多个样本,以便利用所有内核并行执行它们?例如使用以下
创建的 100 个 MNIST 样本# 100 MNIST sample 28x28
sample_data = np.random.randn(100 * 28 * 28).reshape((100, 28, 28))
requests.get()
调用是阻塞的,所以你说得对,我们不应该在 for 循环中只调用它 100 次。
要通过 HTTP 并行发送多个样本,您需要有多个连接。以下使用 asyncio
和 aiohttp
的代码示例显示了一种实现此目的的方法:https://gist.github.com/architkulkarni/0bd0a92c3195c58ec460a5a0e5eb0e88#file-benchmark-py(您需要编辑 url
并将 JSON 输入添加到session.get()
以匹配您的示例)
另一种方法是跳过 HTTP 并使用 Ray Serve 的 ServeHandle API.
从 Python 执行此操作handle = client.get_handle("tf_classifier")
futures = [handle.remote({"array": np.random.randn(28 * 28)}) for i in range(100)]
results = ray.get(futures)