Microsoft.ML.OnnxRuntimeGenAI.Cuda 0.2.0-rc3

Prefix Reserved
This is a prerelease version of Microsoft.ML.OnnxRuntimeGenAI.Cuda.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Microsoft.ML.OnnxRuntimeGenAI.Cuda --version 0.2.0-rc3                
NuGet\Install-Package Microsoft.ML.OnnxRuntimeGenAI.Cuda -Version 0.2.0-rc3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Microsoft.ML.OnnxRuntimeGenAI.Cuda" Version="0.2.0-rc3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Microsoft.ML.OnnxRuntimeGenAI.Cuda --version 0.2.0-rc3                
#r "nuget: Microsoft.ML.OnnxRuntimeGenAI.Cuda, 0.2.0-rc3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Microsoft.ML.OnnxRuntimeGenAI.Cuda as a Cake Addin
#addin nuget:?package=Microsoft.ML.OnnxRuntimeGenAI.Cuda&version=0.2.0-rc3&prerelease

// Install Microsoft.ML.OnnxRuntimeGenAI.Cuda as a Cake Tool
#tool nuget:?package=Microsoft.ML.OnnxRuntimeGenAI.Cuda&version=0.2.0-rc3&prerelease                

ONNX Runtime Generative AI

Run generative AI models with ONNX Runtime.

This library provides the generative AI loop for ONNX models, including inference with ONNX Runtime, logits processing, search and sampling, and KV cache management.

Users can call a high level generate() method, or run each iteration of the model in a loop.

  • Support greedy/beam search and TopP, TopK sampling to generate token sequences
  • Built in logits processing like repetition penalties
  • Easy custom scoring

See full documentation at [https://onnxruntime.ai/docs/genai].

Features

  • Supported model architectures:
    • Gemma
    • LLaMA
    • Mistral
    • Phi-2
  • Supported targets:
    • CPU
    • GPU (CUDA)
    • GPU (DirectML)
  • Supported sampling features
    • Beam search
    • Greedy search
    • Top P/Top K
  • APIs
    • Python
    • C#
    • C/C++

Coming very soon

  • Support for the encoder decoder model architectures, such as whisper, T5 and BART.

Coming soon

  • Support for mobile devices (Android and iOS) with Java and Objective-C bindings

Roadmap

  • Stable diffusion pipeline
  • Automatic model download and cache
  • More model architectures

Sample code for phi-2 in Python

Install the onnxruntime-genai Python package.

  1. Build the model
python -m onnxruntime_genai.models.builder -m microsoft/phi-2 -e cpu -p int4 -o ./models/phi2
# You can append --extra_options enable_cuda_graph=1 to build an onnx model that supports using cuda graph in ORT.
  1. Run inference
import os
import onnxruntime_genai as og

model_path = os.path.abspath("./models/phi2")

model = og.Model(model_path)

tokenizer = og.Tokenizer(model)

prompt = '''def print_prime(n):
    """
    Print all primes between 1 and n
    """'''

tokens = tokenizer.encode(prompt)

params = og.GeneratorParams(model)
params.set_search_options({"max_length":200})
# Add the following line to enable cuda graph by passing the maximum batch size.
# params.try_use_cuda_graph_with_max_batch_size(16)
params.input_ids = tokens

output_tokens = model.generate(params)

text = tokenizer.decode(output_tokens)

print("Output:")
print(text)

Model download and export

ONNX models are run from a local folder, via a string supplied to the Model() method.

You can bring your own ONNX model or use the model builder utility, included in this package.

Install model builder dependencies.

pip install numpy
pip install transformers
pip install torch
pip install onnx
pip install onnxruntime

Export int4 CPU version

huggingface-cli login --token <your HuggingFace token>
python -m onnxruntime_genai.models.builder -m microsoft/phi-2 -p int4 -e cpu -o <model folder>

Getting the latest nightly Onnxruntime build

By default, onnxruntime-genai uses the latest stable release of onnxruntime. If you want to use the latest nightly build of onnxruntime, you can download the nightly build of onnxruntime from our Azure DevOps Artifacts. nuget package can be uncompressed by renaming the extension to .zip and extracting the contents. The onnxruntime dynamlic libraries and header files are available in the nightly build. You can extract the nuget package and copy the dynamic libraries and header files to the ort/ folder under onnxruntime-genai project root on the same level as this README.md file.

The library files are located in the runtime/$OS-$Arch/native folder and the header files are located in the build/native/include folder in the nuget package.

The final folder structure should look like this:

onnxruntime-genai
│   README.md
│   ... 
│   ort/
│   │   include/
│   │   │   coreml_provider_factory.h
│   │   │   ...
│   │   │   provider_options.h
│   │   lib/
│   │   │   (lib)onnxruntime.(so|dylib|dll)
│   │   │   (lib)onnxruntime_providers_shared.(so|dylib|dll)

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Trademarks

This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.

Product 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 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
native native is compatible. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Microsoft.ML.OnnxRuntimeGenAI.Cuda:

Package Downloads
feiyun0112.SemanticKernel.Connectors.OnnxRuntimeGenAI.CUDA

Semantic Kernel connector for Microsoft.ML.OnnxRuntimeGenAI.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Microsoft.ML.OnnxRuntimeGenAI.Cuda:

Repository Stars
microsoft/semantic-kernel
Integrate cutting-edge LLM technology quickly and easily into your apps
Version Downloads Last updated
0.5.1 71 11/15/2024
0.5.0 367 11/7/2024
0.4.0 2,950 8/21/2024
0.4.0-rc1 254 8/14/2024
0.3.0 5,165 6/21/2024
0.3.0-rc2 6,533 5/29/2024
0.3.0-rc1 755 5/22/2024
0.2.0 1,666 5/20/2024
0.2.0-rc7 1,269 5/14/2024
0.2.0-rc6 1,798 5/4/2024
0.2.0-rc4 2,216 4/25/2024
0.2.0-rc3 232 4/24/2024
0.1.0 365 4/8/2024
0.1.0-rc4 276 3/27/2024

Introducing the ONNX Runtime GenAI Library.