跳转到主要内容

一个轻量级的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环境,例如:JupyterLitePyodide控制台,您可以通过运行以下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 (27.0 kB 查看哈希值)

上传于 源代码

构建版本

pyotritonclient-0.2.6-py3-none-any.whl (23.2 kB 查看哈希值)

上传于 Python 3