问题描述
我正在关注 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 输入添加到 {{1} } 以匹配您的示例)
另一种方法是跳过 HTTP 并使用 Ray Serve 的 ServeHandle API 从 Python 中执行此操作。
session.get()