CLI tool
microsoft/onnxruntime-genai avatar
microsoft/onnxruntime-genai

ONNX Runtime GenAI: A Device-First Generative AI Loop with a Narrow Model List

Generative AI extensions for onnxruntime

1,120 stars353 forksC++MIT

At a glance

What is it?
Microsoft's ONNX Runtime GenAI wraps the generative AI loop for ONNX models, covering tokenization, sampling, KV cache, and grammar constraints. It targets on-device LLM inference but supports a limited set of architectures and hardware, so it fits some deployments and excludes others.
Who is it for?
Adopt ONNX Runtime GenAI if you need a single API to run supported ONNX LLMs on CPU, CUDA, DirectML, OpenVINO, QNN, or WebGPU, and if your model is on the support matrix. Do not adopt it if you need speculative decoding, AMD GPU support, or a model outside the listed architectures, because those are absent or still on the roadmap.
Can I use it commercially?
Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 1 day ago.
What is it written in?
Mainly C++, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on September 14, 2026, and from our analysis. They are not legal advice.

DEEP OPEN-SOURCE ANALYSIS

What It Solves and Who It Is For

ONNX Runtime GenAI solves a specific problem: running generative AI models on a device without managing the generative loop yourself. The README states that it implements the full loop for ONNX models, including pre and post processing, inference with ONNX Runtime, logits processing, search and sampling, KV cache management, and grammar specification for tool calling. That means the library handles the repetitive parts of autoregressive generation, like feeding tokens back into the model and managing the key-value cache, so you do not have to write that plumbing. The intended user is a developer who wants to ship an LLM-powered feature inside an application that runs locally, on a phone, a desktop, or an edge device. The project powers Foundry Local, Windows ML, and the Visual Studio Code AI Toolkit, which gives a hint about the target scenarios: local assistants, on-device chat, and code completion tools. It is not aimed at large-scale server inference where you would use a distributed serving framework. The scope is narrow, and the project is honest about that in its support matrix.

The Mechanism: A Wrapper Around the ONNX Runtime Execution

The core mechanism is a wrapper that turns an ONNX model into a generative model with a high-level API. The README shows a Python example where you create an og.Model object from a directory of model files, then create a Tokenizer and a stream. The search options are set as a dictionary, with max_length and batch_size as keys. The library then handles the iterative decoding loop internally. The data flow is straightforward: you pass a prompt string, the tokenizer converts it to input IDs, the model runs inference, the logits are processed, sampling or search picks the next token, and the KV cache is updated for the next step. The grammar specification feature is notable because it constrains the output tokens to follow a defined grammar, which is useful for tool calling where the model must output valid JSON or another structured format. The README lists grammar specification as a feature under the support matrix, but it does not provide an example in the snippet, so the exact API for grammar is not shown. The library also handles continuous decoding and constrained decoding, which are listed as features. The mechanism is not a new inference engine; it is an orchestration layer that sits on top of ONNX Runtime, which means it inherits ONNX Runtime's execution providers and optimization capabilities.

Getting It Running: Installation and a Minimal Phi-3 Example

The README gives a concrete path to running a model. First, you download a model with huggingface-cli, pointing to a specific ONNX export of Phi-3-mini. The command is: huggingface-cli download microsoft/Phi-3-mini-4k-instruct-onnx --include cpu_and_mobile/cpu-int4-rtn-block-32-acc-level-4/* --local-dir . The include flag selects a subfolder with a CPU int4 quantized version, which is a sign that the model files are organized by hardware and quantization. Then you install the API with pip install numpy and pip install --pre onnxruntime-genai. The pre flag is important because the project recommends a nightly or pre-release package for the latest features. The Python code then creates a Model, Tokenizer, and stream, sets max_length to 2048, and builds a prompt with a chat template. The example uses a simple input() call to get text from the user. The README also warns about a version mismatch: examples in the main branch may not align with the latest stable release. It tells you to check your installed package version with pip list, then checkout the corresponding tag, like git checkout v0.11.5, before looking at the examples. That is a real friction point. For a nightly build, you would build from source with python build.py or install from a custom index URL.

Supported Platforms and the Version Alignment Problem

The support matrix is the most informative part of the README. It lists model architectures that are supported now, including Llama, Mistral, Gemma, Phi, Qwen, DeepSeek, and Whisper for speech. It also lists architectures under development, like stable diffusion, and on the roadmap, like multi-modal models. The API support includes Python, C#, C/C++, and Java, but Java requires a build from source, which adds friction. The operating systems cover Linux, Windows, Mac, and Android, but iOS is absent. Hardware acceleration includes CPU, CUDA, DirectML, NvTensorRtRtx, OpenVINO, QNN, and WebGPU, but AMD GPU is only on the roadmap. That matrix tells you exactly where the project is mature and where it is not. The version alignment problem is a separate issue. The README explicitly says that examples in the main branch may not align with the latest stable release. That means if you clone the repo and run the examples, you might get errors because the API changed. The README tells you to checkout a tag matching your installed package version. That is a maintenance cost that you do not see in many projects, and it suggests the API is still evolving quickly. The release cadence is high, with three releases in a week (v0.15.0, v0.15.1, v0.15.2), which supports that observation.

Limitations and Failure Modes

The most obvious limitation is the narrow model support. If your model is not in the list, you cannot use this library without waiting for it to be added. The README lists architectures like AMD OLMo and gpt-oss, but many popular models, such as GPT-2 or BERT, are absent. That is not a bug; it is a design choice to focus on instruction-tuned LLMs, but it means the library is the wrong tool for any project that needs a different architecture. A second limitation is the lack of speculative decoding, which is listed as on the roadmap. Speculative decoding can speed up generation by using a draft model, and its absence means you are stuck with standard autoregressive decoding. The README also lists multi-modal models as on the roadmap, so if you need vision-language models beyond Phi, you are out of luck. A third failure mode is the version mismatch between the main branch and the stable release. The README warns about it, but it is still a trap for new users who clone the repo and try the examples. The package name on PyPI is onnxruntime-genai, and the nightly build uses a different index URL, which can confuse dependency management. Finally, the telemetry note says the project may collect usage data and send it to Microsoft. That is a privacy consideration that some users will find unacceptable, especially for on-device applications that handle sensitive data.

Alternatives and How They Differ

The most direct alternative is llama.cpp, but it takes a different approach. llama.cpp is a C++ inference engine that runs GGUF models, not ONNX models. It has its own quantization formats and its own set of supported architectures, and it does not require a separate runtime like ONNX Runtime. The key difference is the model format and the execution stack. ONNX Runtime GenAI depends on ONNX Runtime and its execution providers, so you get access to DirectML, OpenVINO, QNN, and WebGPU. llama.cpp has its own backends, including Metal for Apple devices and CUDA for NVIDIA, but it does not support DirectML or OpenVINO in the same way. If you are already invested in ONNX models or need to run on Windows with DirectML, ONNX Runtime GenAI is a better fit. If you want a self-contained engine with a huge community and support for many model architectures, llama.cpp is more flexible. Another alternative is Hugging Face Transformers with the generate() method, but that is a Python library that runs on PyTorch, not a lightweight on-device solution. It gives you more control over the generation loop, but it is heavier and not designed for edge deployment. The choice depends on whether you are tied to the ONNX ecosystem or not.

Maintenance, Licensing, and Telemetry

The project is MIT licensed, which is permissive and suitable for commercial use, but the README includes a trademark notice that restricts how you use Microsoft trademarks and logos in modified versions. That is a legal boundary to keep in mind, though it is not a license restriction on the code itself. The maintenance cost is visible in the version alignment requirement. You cannot just use the latest examples; you must match them to your installed package version. That means you need to track the release tags and be careful when upgrading. The build-from-source path is documented, but it requires a development environment and is more involved than a simple pip install. The nightly builds are available from a custom index URL, which is a separate package source that you need to configure. The README also mentions lintrunner for code contributions, which is a development detail, not a user concern. The telemetry statement is a genuine cost for privacy-sensitive projects. It says the project may collect usage data and send it to Microsoft, and it points to a privacy document. You can review that document, but the fact that telemetry is present at all is a decision point. If you are building an application that must not send any data outside the device, you need to check whether the telemetry can be disabled and how.

Editorial conclusion

Adopt ONNX Runtime GenAI if you need a single API to run supported ONNX LLMs on CPU, CUDA, DirectML, OpenVINO, QNN, or WebGPU, and if your model is on the support matrix. Do not adopt it if you need speculative decoding, AMD GPU support, or a model outside the listed architectures, because those are absent or still on the roadmap. Before committing, verify that your exact model version has an ONNX export with the required quantization format, and check the version alignment between the installed package and the examples, since the main branch may not match the latest stable release. The project is MIT licensed, but it may send usage data to Microsoft, so review docs/Privacy.md if telemetry is a concern.

Official sources

  1. Official README
  2. Project repository
  3. Release notes
Community notes

Community notes