Microsoft.ML.OnnxRuntimeGenAI 0.4.0-rc1

Prefix Reserved
This is a prerelease version of Microsoft.ML.OnnxRuntimeGenAI.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Microsoft.ML.OnnxRuntimeGenAI --version 0.4.0-rc1                
NuGet\Install-Package Microsoft.ML.OnnxRuntimeGenAI -Version 0.4.0-rc1                
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" Version="0.4.0-rc1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Microsoft.ML.OnnxRuntimeGenAI --version 0.4.0-rc1                
#r "nuget: Microsoft.ML.OnnxRuntimeGenAI, 0.4.0-rc1"                
#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 as a Cake Addin
#addin nuget:?package=Microsoft.ML.OnnxRuntimeGenAI&version=0.4.0-rc1&prerelease

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

ONNX Runtime generate() API

Latest version

Run Llama, Phi, Gemma, Mistral with ONNX Runtime.

This API gives you an easy, flexible and performant way of running LLMs on device.

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.

You can call a high level generate() method to generate all of the output at once, or stream the output one token at a time.

See documentation at https://onnxruntime.ai/docs/genai.

Support matrix Supported now Under development On the roadmap
Model architectures Gemma <br/> Llama * <br/> Mistral + <br/>Phi (language + vision)<br/>Qwen <br/> Whisper Stable diffusion
API Python <br/>C# <br/>C/C++ <br/> Java ^ Objective-C
Platform Linux <br/> Windows <br/>Mac ^ <br/>Android ^ iOS
Architecture x86 <br/> x64 <br/> Arm64 ^
Hardware Acceleration CUDA<br/>DirectML<br/> QNN <br/> ROCm OpenVINO
Features Interactive decoding <br/> Customization (fine-tuning) Speculative decoding

* The Llama model architecture supports similar model families such as CodeLlama, Vicuna, Yi, and more.

+ The Mistral model architecture supports similar model families such as Zephyr.

^ Requires build from source

Installation

See https://onnxruntime.ai/docs/genai/howto/install

Sample code for Phi-3 in Python

  1. Download the model

    huggingface-cli download microsoft/Phi-3-mini-4k-instruct-onnx --include cpu_and_mobile/cpu-int4-rtn-block-32-acc-level-4/* --local-dir .
    
  2. Install the API

    pip install numpy
    pip install --pre onnxruntime-genai
    
  3. Run the model

    import onnxruntime_genai as og
    
    model = og.Model('cpu_and_mobile/cpu-int4-rtn-block-32-acc-level-4')
    tokenizer = og.Tokenizer(model)
    tokenizer_stream = tokenizer.create_stream()
    
    # Set the max length to something sensible by default,
    # since otherwise it will be set to the entire context length
    search_options = {}
    search_options['max_length'] = 2048
    
    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)
    params.input_ids = input_tokens
    generator = og.Generator(model, params)
    
    print("Output: ", end='', flush=True)
    
    try:
       while not generator.is_done():
         generator.compute_logits()
         generator.generate_next_token()
    
         new_token = generator.get_next_tokens()[0]
         print(tokenizer_stream.decode(new_token), end='', flush=True)
    except KeyboardInterrupt:
        print("  --control+c pressed, aborting generation--")
    
    print()
    del generator
    

Roadmap

See the Discussions to request new features and up-vote existing requests.

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 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. 
.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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 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

feiyun0112.SemanticKernel.Connectors.OnnxRuntimeGenAI.CPU

Semantic Kernel connector for Microsoft.ML.OnnxRuntimeGenAI.

GitHub repositories (1)

Showing the top 1 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
Version Downloads Last updated
0.5.1 185 11/13/2024
0.5.0 537 11/7/2024
0.4.0 28,336 8/21/2024
0.4.0-rc1 194 8/14/2024
0.3.0 21,726 6/21/2024
0.3.0-rc2 1,491 5/29/2024
0.3.0-rc1 186 5/22/2024
0.2.0 544 5/20/2024
0.2.0-rc7 242 5/14/2024
0.2.0-rc6 189 5/4/2024
0.2.0-rc4 469 4/25/2024
0.2.0-rc3 156 4/24/2024
0.1.0 370 4/8/2024
0.1.0-rc4 210 3/27/2024

Release Def:
Branch: refs/heads/rel-0.4.0
Commit: a61454c8c114ac3adc9e7bb482fd86d6b0263d03