Microsoft.ML.OnnxRuntimeGenAI
0.19.0
Prefix Reserved
dotnet add package Microsoft.ML.OnnxRuntimeGenAI --version 0.19.0
NuGet\Install-Package Microsoft.ML.OnnxRuntimeGenAI -Version 0.19.0
<PackageReference Include="Microsoft.ML.OnnxRuntimeGenAI" Version="0.19.0" />
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI" Version="0.19.0" />
<PackageReference Include="Microsoft.ML.OnnxRuntimeGenAI" />
paket add Microsoft.ML.OnnxRuntimeGenAI --version 0.19.0
#r "nuget: Microsoft.ML.OnnxRuntimeGenAI, 0.19.0"
#:package Microsoft.ML.OnnxRuntimeGenAI@0.19.0
#addin nuget:?package=Microsoft.ML.OnnxRuntimeGenAI&version=0.19.0
#tool nuget:?package=Microsoft.ML.OnnxRuntimeGenAI&version=0.19.0
About
Run Llama, Phi (language and multi modal!), DeepSeek, Gemma, Mistral with ONNX Runtime.
This API gives you an easy, flexible and performant way of running LLMs on device using .NET/C#.
It implements the generative AI loop for ONNX models, including pre and post processing, inference with ONNX Runtime, logits processing, search and sampling, and KV cache management.
Key Features
- Language, vision, and audio pre and post processing
- Inference using ONNX Runtime
- Generation tuning with greedy, beam search and random sampling
- KV cache management to optimize performance
- Multi target execution (CPU, GPU, with NPU coming!)
Sample
// See https://aka.ms/new-console-template for more information
using Microsoft.ML.OnnxRuntimeGenAI;
using OgaHandle ogaHandle = new OgaHandle();
// Specify the location of your downloaded model.
// Many models are published on HuggingFace e.g.
// https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-onnx
string modelPath = "..."
Console.WriteLine("Model path: " + modelPath);
using Model model = new Model(modelPath);
using Tokenizer tokenizer = new Tokenizer(model);
using var tokenizerStream = tokenizer.CreateStream();
// Set your prompt here
string prompt = "public static bool IsPrime(int number)";
var sequences = tokenizer.Encode($"<|user|>{prompt}<|end|><|assistant|>");
using GeneratorParams generatorParams = new GeneratorParams(model);
generatorParams.SetSearchOption("max_length", 512);
using var generator = new Generator(model, generatorParams);
generator.AppendTokenSequences(sequences);
.
while (!generator.IsDone())
{
generator.GenerateNextToken();
Console.Write(tokenizerStream.Decode(generator.GetSequence(0)[^1]));
}
Generates the following output:
Here's a complete implementation of the `IsPrime` function in C# that checks if a given number is prime. The function includes basic input validation and comments for clarity.
using System;
namespace PrimeChecker
{
public class PrimeChecker
{
/// <summary>
/// Checks if the given number is prime.
/// </summary>
/// <param name="number">The number to check.</param>
/// <returns>true if the number is prime; otherwise, false.</returns>
public static bool IsPrime(int number)
{
// Input validation
if (number < 2)
{
return false;
}
// 2 is the only even prime number
if (number == 2)
{
return true;
}
// Exclude even numbers greater than 2
if (number % 2 == 0)
{
return false;
}
// Check for factors up to the square root of the number
int limit = (int)Math.Floor(Math.Sqrt(number));
for (int i = 3; i <= limit; i += 2)
{
if (number % i == 0)
{
return false;
}
}
return true;
}
static void Main(string[] args)
{
int number = 29;
bool isPrime = PrimeChecker.IsPrime(number);
Console.WriteLine($"Is {number} prime? {isPrime}");
}
}
}
This implementation checks if a number is prime by iterating only up to the square root of the number, which is an optimization over checking all numbers up to the number itself. It also excludes even numbers greater than 2, as they cannot be prime.
Source code repository
ONNX Runtime is an open source project. See:
Documentation
See ONNX Runtime GenAI Documentation
ONNX Runtime dependency
GenAI NuGet packages do not bundle ONNX Runtime binaries. ONNX Runtime is
supplied by separate runtime packages. The WinML NuGet depends on
Microsoft.Windows.AI.MachineLearning for its runtime.
Linux ARM64 CUDA
The CUDA NuGet contains the Linux ARM64 GenAI core and CUDA add-on under
runtimes/linux-arm64/native, but does not bundle ONNX Runtime libraries.
Linux ARM64 consumers must provide a compatible ONNX Runtime GPU 1.30.0 native
installation separately, along with a CUDA 13.1 runtime and a compatible NVIDIA
driver. Other RIDs continue to obtain ONNX Runtime from the NuGet dependency.
The .Foundry package carries the Linux ARM64 CUDA GenAI core, but deliberately
excludes the CUDA shim and ONNX Runtime libraries, as it does for Linux x64.
Its Linux ARM64 consumer must provide a compatible ONNX Runtime GPU 1.30.0
native installation separately.
0.19.0 migration note
The Linux ARM64 CUDA NuGet no longer includes ONNX Runtime binaries.
Applications must obtain a compatible ONNX Runtime GPU 1.30.0 native
installation separately. The GenAI core and CUDA add-on remain in the package.
The WinML Python wheel now depends on the standard onnxruntime Python package
instead of bundling WinML SDK runtime DLLs.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-android31.0 is compatible. net9.0-browser was computed. net9.0-ios was computed. net9.0-ios15.4 is compatible. net9.0-maccatalyst was computed. net9.0-maccatalyst14.0 is compatible. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| native | native is compatible. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 0.0
- Microsoft.ML.OnnxRuntime (>= 1.31.0)
- Microsoft.ML.OnnxRuntimeGenAI.Managed (>= 0.19.0)
-
.NETFramework 0.0
- Microsoft.ML.OnnxRuntime (>= 1.31.0)
- Microsoft.ML.OnnxRuntimeGenAI.Managed (>= 0.19.0)
-
.NETStandard 0.0
- Microsoft.ML.OnnxRuntime (>= 1.31.0)
- Microsoft.ML.OnnxRuntimeGenAI.Managed (>= 0.19.0)
-
net9.0-android31.0
- Microsoft.ML.OnnxRuntime (>= 1.31.0)
- Microsoft.ML.OnnxRuntimeGenAI.Managed (>= 0.19.0)
-
net9.0-ios15.4
- Microsoft.ML.OnnxRuntime (>= 1.31.0)
- Microsoft.ML.OnnxRuntimeGenAI.Managed (>= 0.19.0)
-
net9.0-maccatalyst14.0
- Microsoft.ML.OnnxRuntime (>= 1.31.0)
- Microsoft.ML.OnnxRuntimeGenAI.Managed (>= 0.19.0)
NuGet packages (22)
Showing the top 5 NuGet packages that depend on Microsoft.ML.OnnxRuntimeGenAI:
| Package | Downloads |
|---|---|
|
Microsoft.SemanticKernel.Connectors.Onnx
Semantic Kernel connectors for the ONNX runtime. Contains clients for text embedding generation. |
|
|
Microsoft.KernelMemory.AI.Onnx
Provide access to ONNX LLM models in Kernel Memory to generate text |
|
|
LMSupply.Generator.Onnx
ONNX Runtime GenAI backend for LMSupply.Generator. Adds local text generation on ONNX models (Phi, etc.) with CUDA GPU acceleration. Optional — LMSupply.Generator works with GGUF/llama-server alone without this package. |
|
|
feiyun0112.SemanticKernel.Connectors.OnnxRuntimeGenAI.CPU
Semantic Kernel connector for Microsoft.ML.OnnxRuntimeGenAI. |
|
|
ElBruno.LocalLLMs
Local LLM chat completions using Microsoft.Extensions.AI and ONNX Runtime GenAI. IChatClient implementation for running LLMs locally. |
GitHub repositories (6)
Showing the top 6 popular GitHub repositories that depend on Microsoft.ML.OnnxRuntimeGenAI:
| Repository | Stars |
|---|---|
|
microsoft/semantic-kernel
Integrate cutting-edge LLM technology quickly and easily into your apps
|
|
|
dluc/kernel-memory
Research project. A Memory solution for users, teams, and applications.
|
|
|
axzxs2001/Asp.NetCoreExperiment
原来所有项目都移动到**OleVersion**目录下进行保留。新的案例装以.net 5.0为主,一部分对以前案例进行升级,一部分将以前的工作经验总结出来,以供大家参考!
|
|
|
rwjdk/agent-framework-samples
Samples demonstrating the Microsoft Agent Framework in C#
|
|
|
Build5Nines/SharpVector
Lightweight, In-memory, Semantic Search, Text Vector Database to embed in any .NET Application
|
|
|
kelnishi/WACS
Pure C# WebAssembly toolchain for .NET - interpreter, AOT transpiler, NativeAOT builder, and component-model bindgen
|
| Version | Downloads | Last Updated |
|---|---|---|
| 0.19.0 | 1,019 | 11/4/2026 |
| 0.18.0 | 9,409 | 10/14/2026 |
| 0.17.1 | 12,642 | 9/29/2026 |
| 0.17.0 | 10,406 | 9/25/2026 |
| 0.16.0 | 6,986 | 9/13/2026 |
| 0.15.2 | 27,933 | 8/6/2026 |
| 0.15.1 | 2,793 | 8/2/2026 |
| 0.15.0 | 4,391 | 7/29/2026 |
| 0.14.1 | 32,913 | 6/2/2026 |
| 0.14.0 | 3,250 | 5/26/2026 |
| 0.13.2 | 12,223 | 5/1/2026 |
| 0.13.1 | 14,786 | 4/7/2026 |
| 0.13.0 | 867 | 4/5/2026 |
| 0.12.2 | 14,304 | 3/4/2026 |
| 0.12.1 | 2,330 | 2/24/2026 |
| 0.12.0 | 2,180 | 2/13/2026 |
| 0.11.4 | 218,404 | 12/12/2025 |
| 0.11.3 | 1,325 | 12/8/2025 |
| 0.11.2 | 23,596 | 11/18/2025 |
| 0.11.1 | 941 | 11/16/2025 |
Release Def:
Branch: refs/heads/rel-0.19.0
Commit: d01b2441a72d4e3223415b9b08474c6e65fbc028