一个轻量级的http客户端库,用于与Nvidia Triton Inference Server通信(支持在浏览器中使用Pyodide)
项目描述
Triton HTTP客户端用于Pyodide
一个Pyodide python http客户端库和工具,用于与Triton Inference Server通信(基于NVIDIA的tritonclient)。
这是NVIDIA triton客户端的一个简化实现,它既可以在浏览器中使用Pyodide Python,也可以在本地Python中使用。它只实现了http客户端,并且大多数API保持相似,但改为异步并增加了附加实用函数。
安装
要在原生CPython中使用它,您可以通过运行以下命令来安装此包
pip install pyotritonclient
对于基于Pyodide的Python环境,例如:JupyterLite 或 Pyodide控制台,您可以通过运行以下Python代码来安装客户端
import micropip
micropip.install("pyotritonclient")
用法
基本示例
要执行模型,我们提供了实用函数以使其变得更加容易
import numpy as np
from pyotritonclient import execute
# create fake input tensors
input0 = np.zeros([2, 349, 467], dtype='float32')
# run inference
results = await execute(inputs=[input0, {"diameter": 30}], server_url='https://ai.imjoy.io/triton', model_name='cellpose-python')
以上示例假设您正在Jupyter笔记本或支持顶级await的环境中运行代码,如果您尝试在普通Python脚本中运行示例代码,请将代码包装在异步函数中,并使用asyncio运行,如下所示:
import asyncio
import numpy as np
from pyotritonclient import execute
async def run():
results = await execute(inputs=[np.zeros([2, 349, 467], dtype='float32'), {"diameter": 30}], server_url='https://ai.imjoy.io/triton', model_name='cellpose-python')
print(results)
loop = asyncio.get_event_loop()
loop.run_until_complete(run())
您还可以访问底层API,请参阅测试示例。
您还可以找到官方客户端示例,演示如何使用该包向triton推理服务器发送请求。但是请注意,您需要将HTTP客户端代码更改为异步风格。例如,您需要将client.infer(...)
改为await client.infer(...)
。
HTTP客户端代码是从triton客户端git仓库的提交b3005f9db154247a4c792633e54f25f35ccadff0以来分叉的。
使用具有状态模型的序列执行器
为了简化对具有序列的状态模型的操作,我们还提供了SequenceExecutor
,以便更容易地按序列运行模型。
from pyotritonclient import SequenceExcutor
seq = SequenceExcutor(
server_url='https://ai.imjoy.io/triton',
model_name='cellpose-train',
sequence_id=100
)
inputs = [
image.astype('float32'),
labels.astype('float32'),
{"steps": 1, "resume": True}
]
for (image, labels, info) in train_samples:
result = await seq.step(inputs)
result = await seq.end(inputs)
请注意,上述示例通过发送最后一个输入再次调用seq.end()
来结束序列。如果您想要指定执行输入,可以运行result = await se.end(inputs)
。
对于小批量数据,您也可以这样运行
from pyotritonclient import SequenceExcutor
seq = SequenceExcutor(
server_url='https://ai.imjoy.io/triton',
model_name='cellpose-train',
sequence_id=100
)
# a list of inputs
inputs_batch = [[
image.astype('float32'),
labels.astype('float32'),
{"steps": 1, "resume": True}
] for (image, labels, _) in train_samples]
def on_step(i, result):
"""Function called on every step"""
print(i)
results = await seq(inputs_batch, on_step=on_step)
服务器设置
由于我们从浏览器环境访问服务器,而浏览器环境通常具有更多的安全限制,因此将服务器配置为启用浏览器访问非常重要。
请确保您配置以下方面
- 服务器必须提供HTTPS端点而不是HTTP
- 服务器应发送以下头信息
Access-Control-Allow-Headers: Inference-Header-Content-Length,Accept-Encoding,Content-Encoding,Access-Control-Allow-Headers
Access-Control-Expose-Headers: Inference-Header-Content-Length,Range,Origin,Content-Type
Access-Control-Allow-Methods: GET,HEAD,OPTIONS,PUT,POST
Access-Control-Allow-Origin: *
(这取决于您是否想支持CORS,是可选的)
项目详情
下载文件
下载适合您平台文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源代码分发
构建版本
pyotritonclient-0.2.6.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 3534b76f4d33a9a41da332b63b3e7d2527ce79901197baf75c00dd3434a2dace |
|
MD5 | dcbdcba113e161709c5b67ac6beb13f1 |
|
BLAKE2b-256 | f11770badc7a1b7f5c66d3712ca6c22c363978cab00aba70f9ccd272fd29ef6d |
pyotritonclient-0.2.6-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | ef638011b79b390214ca3c895d75119cf2dd551adbaad38bf9106f023169f1e1 |
|
MD5 | 59dcc5a27b8b61abbb4e6e5ced516c40 |
|
BLAKE2b-256 | 5a39b1c278ecd419a32195fd53145584fd65b067c26bfb2b8c2bb820d3fd7bb0 |