使用 ONNX Runtime 运行生成式 AI 模型。该 API 提供了一种简单、灵活且高性能的方式,让你能在设备端运行大型语言模型(LLM)。它实现了面向 ONNX 模型的生成式 AI 循环,包括预处理与后处理、基于 ONNX Runtime 的推理、logits 处理、搜索与采样、KV 缓存管理,以及用于工具调用的语法规范(grammar specification)。
ONNX Runtime GenAI 为 Foundry Local、Windows ML 以及 Visual Studio Code AI Toolkit 提供底层支持。
更多细节请查阅 ONNX Runtime 官网文档。
| 支持矩阵 | 当前已支持 | 开发中 | 规划中 |
|---|---|---|---|
| 模型架构 | AMD OLMo ChatGLM DeepSeek ERNIE 4.5 Fara Gemma gpt-oss Granite Granite MoE Hybrid HunYuan Dense V1 InternLM2 LFM2(语言 + 视觉 + 语音输入) LFM3(语言) Llama Mistral Nemotron Phi(语言 + 视觉) Qwen(语言 + 视觉) SmolLM3 Whisper |
Stable diffusion | 多模态模型 |
| API | Python C# C/C++ Java ^ |
Objective-C | |
| 操作系统 | Linux Windows Mac Android |
iOS | |
| CPU 架构 | x86 x64 arm64 |
||
| 硬件加速 | CPU CUDA DirectML NvTensorRtRtx (TRT-RTX) OpenVINO QNN WebGPU |
AMD GPU | |
| 功能特性 | 多 LoRA 连续解码 约束解码 |
投机解码(Speculative decoding) |
^ 需要从源码构建
shell
huggingface-cli download microsoft/Phi-3-mini-4k-instruct-onnx --include cpu_and_mobile/cpu-int4-rtn-block-32-acc-level-4/* --local-dir .
shell
pip install numpy
pip install --pre onnxruntime-genai
```python
import onnxruntime_genai as og
model = og.Model('cpu_and_mobile/cpu-int4-rtn-block-32-acc-level-4')
tokenizer = og.Tokenizer(model)
stream = tokenizer.create_stream()
# 默认设置一个合理的最大长度,
# 否则会被设置为整个上下文长度
search_options = {}
search_options['max_length'] = 2048
search_options['batch_size'] = 1
chat_template = '<|user|>\n{input} <|end|>\n<|assistant|>'
text = input("Input: ")
if not text:
print("Error, input cannot be empty")
exit()
prompt = f'{chat_template.format(input=text)}'
input_tokens = tokenizer.encode(prompt)
params = og.GeneratorParams(model)
params.set_search_options(**search_options)
generator = og.Generator(model, params)
print("Output: ", end='', flush=True)
try:
generator.append_tokens(input_tokens)
while not generator.is_done():
generator.generate_next_token()
new_token = generator.get_next_tokens()[0]
print(stream.decode(new_token), end='', flush=True)
except KeyboardInterrupt:
print(" --control+c pressed, aborting generation--")
print()
del generator
```
由于项目持续演进、功能不断增加,main 分支中的示例可能并不总是与最新的稳定版本保持一致。本节说明如何确保示例与对应版本之间的兼容性。
根据安装说明安装软件包。例如,安装 Python 包:
pip install onnxruntime-genai
查看已安装包的版本
Linux/Mac:
pip list | grep onnxruntime-genai
Windows:
pip list | findstr "onnxruntime-genai"
然后,检出与该发行版本对应的示例版本。
# 克隆仓库
git clone https://github.com/microsoft/onnxruntime-genai.git && cd onnxruntime-genai
# 检出你所用版本对应的分支
git checkout v0.11.5
cd examples
检出仓库的 main 分支
git clone https://github.com/microsoft/onnxruntime-genai.git && cd onnxruntime-genai
参照这些说明从源码构建。例如,构建 Python wheel:
python build.py
进入 main 分支中的 examples 文件夹。
cd examples
安装每日构建的 Python 包:
# 将 onnxruntime-genai 替换为你想要安装的 Python 包
pip install --index-url https://aiinfra.pkgs.visualstudio.com/PublicPackages/_packaging/ORT-Nightly/pypi/simple/ onnxruntime-genai
请前往 Discussions 提出新功能需求,并为已有需求投票。
本项目可能会收集使用数据并发送给 Microsoft,以帮助改进我们的产品和服务。详情请参阅隐私声明。
本项目欢迎各类贡献和建议。大多数贡献要求你同意一份
贡献者许可协议(CLA),声明你有权并且确实授予我们
使用你贡献的权利。详情请访问 https://cla.opensource.microsoft.com。
有关如何从源码构建、测试和检查(lint)该库的说明,请参阅 DEVELOPMENT.md。
当你提交拉取请求时,CLA 机器人会自动判断你是否需要提供
CLA,并对 PR 做出相应标记(例如状态检查、评论)。只需按照机器人
给出的说明操作即可。在跨所有使用我们 CLA 的仓库中,你只需完成一次。
本项目已采用 Microsoft 开源行为准则。
更多信息请参阅行为准则常见问题,或
通过 opencode@microsoft.com 联系我们以提出其他问题或意见。
本项目启用了 lintrunner 进行代码检查。你可以安装依赖并通过以下命令初始化
pip install -r requirements-lintrunner.txt
lintrunner init
这会在你的系统上安装 lintrunner,并下载在本地运行检查器所需的全部依赖。
格式化本地更改:
lintrunner -a
格式化所有文件:
lintrunner -a --all-files
本项目可能包含项目、产品或服务的商标或徽标。获得授权后使用 Microsoft
商标或徽标须遵守并遵循 Microsoft 商标与品牌指南。在本项目的修改版本中使用 Microsoft 商标或徽标不得造成混淆或暗示 Microsoft 的赞助。任何第三方商标或徽标的使用均适用相应第三方的政策。