onnxruntime-genai
Copilot instructions1.1k starsChanged 3 months agoMITView on GitHub
This is ONNX Runtime GenAI, a high-performance inference library for generative AI models. The codebase implements the complete generative AI loop including preprocessing, ONNX Runtime inference, logits processing, search/sampling, and KV cache management. The State class hierarchy in src/models/model.h handles device-specific execution, while the Engine class in src/engine/ manages request batching and scheduling. WinML builds require explicit SDK version specification: WinML integration downloads Microsoft.Windows.AI.MachineLearning via NuGet and copies headers/libs to a local ort/ directory. Each hardware backend implements DeviceInterface (defined…
Trending
Quiet since September.about the whole repository
Who's talking about microsoft/onnxruntime-genai
- huggingface/transformers.js“Releasing the Florence 2 ONNX conversion script?”GitHub · Jan 23, 2025 · 2 reactions
- onnxruntime/mobius“Add special tokens for tool calling in genai_config”GitHub · Aug 20, 2026 · 1 reactions
- xadupre/mbext“propagate https://github.com/microsoft/onnxruntime-genai/pull/2150”GitHub · Jun 16, 2026 · 1 reactions
- xadupre/mbext“propagate https://github.com/microsoft/onnxruntime-genai/pull/2144”GitHub · Jun 16, 2026 · 1 reactions
- xadupre/mbext“propagate https://github.com/microsoft/onnxruntime-genai/pull/2147”GitHub · Jun 16, 2026 · 1 reactions
The biggest 5 of 29 mentions linking to the repository that our crawl found. Each line is their own headline.
See all 29
Talked about on GitHub (29 times).
On GitHub · 29
- Jan 23, 2025Releasing the Florence 2 ONNX conversion script? — huggingface/transformers.js2 reactions · 6 comments
- Aug 20, 2026Add special tokens for tool calling in genai_config — onnxruntime/mobius1 reaction
- Jun 16, 2026propagate https://github.com/microsoft/onnxruntime-genai/pull/2150 — xadupre/mbext1 reaction
- Jun 16, 2026propagate https://github.com/microsoft/onnxruntime-genai/pull/2144 — xadupre/mbext1 reaction
- Jun 16, 2026propagate https://github.com/microsoft/onnxruntime-genai/pull/2147 — xadupre/mbext1 reaction
24 more on GitHub
- Jun 16, 2026propagate https://github.com/microsoft/onnxruntime-genai/pull/2171 — xadupre/mbext1 reaction
- Jun 16, 2026propagate https://github.com/microsoft/onnxruntime-genai/pull/2139 — xadupre/mbext1 reaction
- Jun 16, 2026propagate https://github.com/microsoft/onnxruntime-genai/pull/2146 — xadupre/mbext1 reaction
- Jun 16, 2026propagate https://github.com/microsoft/onnxruntime-genai/pull/2188 — xadupre/mbext1 reaction
- Jun 16, 2026propagate https://github.com/microsoft/onnxruntime-genai/pull/2186 — xadupre/mbext1 reaction
- Jun 10, 2026[CUDA] Qwen3.6-35B-A3B Throughput Optimization — microsoft/onnxruntime1 reaction · 4 comments
- Jun 3, 2026add a page related to project mbext — xadupre/xadupre.github.io1 reaction
- May 24, 2026brings changes from https://github.com/microsoft/onnxruntime-genai/pull/2078 subfolder builders — xadupre/mbext1 reaction
- May 24, 2026import changes from https://github.com/microsoft/onnxruntime-genai/pull/2101 and add fast test — xadupre/mbext1 reaction
- May 24, 2026import changes from https://github.com/microsoft/onnxruntime-genai/pull/1979 and add fast tests — xadupre/mbext1 reaction
- May 24, 2026import changes from https://github.com/microsoft/onnxruntime-genai/pull/2127 and add fast tests — xadupre/mbext1 reaction
- May 24, 2026import changes from https://github.com/microsoft/onnxruntime-genai/pull/2137, make sure it passes the tests — xadupre/mbext1 reaction
- May 24, 2026import changes from https://github.com/microsoft/onnxruntime-genai/pull/2157 and add fast unit test — xadupre/mbext1 reaction
- May 24, 2026import changes from https://github.com/microsoft/onnxruntime-genai/pull/2144 and add unit test — xadupre/mbext1 reaction
- May 24, 2026import changes from https://github.com/microsoft/onnxruntime-genai/pull/2147/changes#diff-11d14636083c4529597f09601b8f66e5df0094978debab425bfe6dd850cfcb50, add fast unit test — xadupre/mbext1 reaction
- May 24, 2026import changes from https://github.com/microsoft/onnxruntime-genai/pull/2146, add fast unit test — xadupre/mbext1 reaction
- Apr 9, 2026missing fp8 in mistral? — xadupre/mbext1 reaction
- Apr 9, 2026use io_binding to support bfloat16 in tests — xadupre/mbext1 reaction · 1 comment
- Aug 14, 2024Python: #6761 Onnx Connector — microsoft/semantic-kernel1 reaction · 6 comments
- Sep 30, 2026deps(backend): bump onnxruntime-genai from 0.4.0 to 0.17.0 in /backend — beckkenstschaft/Hexacode0 reactions · 1 comment
- Aug 26, 2026Bump onnxruntime-genai from 0.9.0 to 0.15.2 in /python — microsoft/semantic-kernel0 reactions · 2 comments
- Aug 24, 2026cuda graph bug for recurrent state inplace update — microsoft/onnxruntime0 reactions · 2 comments
- Mar 2, 2026Bump Microsoft.ML.OnnxRuntimeGenAI from 0.11.4 to 0.12.1 — yuniko-software/qwen3-onnx0 reactions
- Aug 30, 2024Bump Microsoft.ML.OnnxRuntimeGenAI.Cuda from 0.3.0 to 0.4.0 in /dotnet — MilanJB/SemanticKernel-Playground0 reactions · 1 comment
The file
We split this copilot-instructions.md into the rules it gives an agent
3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 94
Use OgaCheckResult() wrapper for C API error propagation:
- Very specific: line 163
Always test with actual model files from test/models/ directory rather than mock data.
- Distinctive: line 175
When reviewing those files, prioritize these model-builder rules over generic guidance and flag violations explicitly.
≈ “Common” is an estimate: at least 1 in 5 of the 1,644 Copilot instructions files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
ONNX Runtime GenAI - AI Coding Agent Instructions
Architecture Overview
This is ONNX Runtime GenAI, a high-performance inference library for generative AI models. The codebase implements the complete generative AI loop including preprocessing, ONNX Runtime inference, logits processing, search/sampling, and KV cache management.
Core Components
src/models/- Model implementations with support for LLMs, VLMs (Vision), ALMs (Audio), and Pipeline modelssrc/engine/- Request batching engine for concurrent model execution with dynamic schedulingsrc/generators.h- Central generator logic coordinating the full inference pipelinesrc/ort_genai.h- Zero-cost C++ wrapper around the C API for automatic resource management- Language bindings: Python (
src/python/), C# (src/csharp/), Java (src/java/), Objective-C (src/objectivec/)
Key Abstractions
// Core inference flow: Model -> Generator -> Tokenizer
auto model = OgaModel::Create("phi-2");
auto tokenizer = OgaTokenizer::Create(*model);
auto generator = OgaGenerator::Create(*model, params);The State class hierarchy in src/models/model.h handles device-specific execution, while the Engine class in src/engine/ manages request batching and scheduling.
Build System & Development Workflow
Primary Build Commands
# Cross-platform Python build script (preferred)
python build.py --config Release --use_cuda --build_java --enable_tests
# Platform-specific scripts
build.bat # Windows batch
build.sh # Linux/Mac shellKey Build Options (cmake/options.cmake)
USE_CUDA/USE_DML- Hardware acceleration backendsUSE_WINML- Windows ML integration requiringWINML_SDK_VERSIONparameterENABLE_JAVA/ENABLE_PYTHON- Language binding compilationUSE_GUIDANCE- Constrained generation support
WinML Build Pattern
WinML builds require explicit SDK version specification:
# WinML build - WINML_SDK_VERSION is mandatory
python build.py --use_winml -DWINML_SDK_VERSION=2.1.1WinML integration downloads Microsoft.Windows.AI.MachineLearning via NuGet and copies headers/libs to a local ort/ directory.
Testing
# Python tests with test models
python -m pytest -sv test_onnxruntime_genai_api.py -k "test_name" --test_models ..\models
# C++ unit tests via CMake/CTest
ctest --build-config Release --output-on-failureCode Patterns & Conventions
Device Interface Pattern
Each hardware backend implements DeviceInterface (defined in src/smartptrs.h):
struct CudaInterface : DeviceInterface {
std::unique_ptr<DeviceBuffer> Allocate(size_t size) override;
void CopyToDevice(DeviceSpan<T> dst, std::span<const T> src) override;
};Model State Management
Models follow the State pattern where each model type extends the base State class:
struct State {
virtual DeviceSpan<float> Run(int total_length,
DeviceSpan<int32_t>& next_tokens) = 0;
virtual void RewindTo(size_t index) {} // For session continuation
};Error Handling Convention
Use OgaCheckResult() wrapper for C API error propagation:
OgaCheckResult(OgaCreateModel(model_path, &model)); // Throws std::runtime_errorMemory Management
- DeviceSpan/DeviceBuffer: Device-agnostic memory abstractions
- std::unique_ptr with custom deleters: For C API resource cleanup
- LeakChecked
<T>: Debug-mode leak detection for core types
Critical Integration Points
ONNX Runtime Dependency Management
ADO pipelines obtain ORT lib/headers via three methods:
- Explicit
ORT_HOME- Pipeline provides pre-built ORT artifacts (preferred) - Auto-download via CMake -
cmake/ortlib.cmakefetches from ORT-Nightly feed whenORT_HOMEunset - Python build driver -
tools/python/util/dependency_resolver.pydownloads NuGet packages
Model Loading Pipeline
- Config parsing (
src/config.cpp) - Readsgenai_config.jsonmodel metadata - ONNX session creation via
onnxruntime_api.hwrappers - Device interface selection based on provider availability
- KV cache initialization (
src/models/io/kv_cache.cpp) for transformer models
Multi-Modal Support
Vision models (Phi-Vision) use separate processor classes:
PhiImageProcessor- Image tokenization and preprocessingMultiModalProcessor- Coordinates text/image inputs
Execution Provider Detection
Hardware acceleration auto-detection follows this priority:
- CUDA (if
USE_CUDA=ONand CUDA runtime available) - DirectML (Windows, if
USE_DML=ON) - CPU fallback
Project-Specific Gotchas
Windows-Specific Build Requirements
- Visual Studio 2022 required for C++20 features
- WinML integration requires specific NuGet package versions (see
cmake/nuget.cmake) - Cross-compilation for ARM64/ARM64EC supported via CMake platform flags
Model Compatibility Matrix
The repo supports specific model architectures - check src/models/model_type.h for the canonical list. New models require:
- Config template in model directory
- State implementation extending base
Stateclass - Optional custom processors for multi-modal inputs
Performance Considerations
- KV caching is automatically managed but can be configured via
src/runtime_settings.cpp - Continuous decoding (session continuation) requires careful state management
- Multi-LoRA adapters use separate weight loading in
src/models/io/adapters.cpp
Testing Strategy
Tests are organized by language binding:
- C++ tests:
test/directory, focused on core API validation - Python tests:
test/python/, includes end-to-end model testing - Platform tests: Android/iOS tests run via emulator/simulator
Always test with actual model files from test/models/ directory rather than mock data.
Model Builder Review Rules
For any pull request that modifies files under src/python/py/models/**, reviewers and Copilot must apply:
.github/instructions/python-model-builder.instructions.mdsrc/python/py/models/README.mdsrc/python/py/models/DESIGN.mdsrc/python/py/models/loaders/LOADERS.mdsrc/python/py/models/quantization/QUANTIZATION.md
When reviewing those files, prioritize these model-builder rules over generic guidance and flag violations explicitly.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

