Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ONNX Runtime GenAI C# API #72

Merged
merged 19 commits into from
Feb 16, 2024
Merged

ONNX Runtime GenAI C# API #72

merged 19 commits into from
Feb 16, 2024

Conversation

baijumeswani
Copy link
Collaborator

@baijumeswani baijumeswani commented Feb 12, 2024

This pull-request introduces the C# API for ONNX Runtime GenAI. The C# API follows the C API closely and leverages the C API as the interface with the ONNX Runtime GenAI core.

The API 📚

The C# API primarily revolves around three classes:

  • Model - Entry point for running GenAI with the library. Used for loading the model and the config.
  • GeneratorParams - Used to set the configurable parameters for search.
  • Generator - Main computation engine. Used for computing the logits and retrieving the generated tokens.

Sample usage 👨‍💻

using Model model = new Model(modelPath, DeviceType.CPU)
using GeneratorParams generatorParams = new GeneratorParams(model)

generatorParams.SetMaxLength(maxLength);
generatorParams.SetInputIDs(inputIDs, sequenceLength, batchSize);

using Generator generator = new Generator(model, generatorParams)

while (!generator.IsDone())
{
    generator.ComputeLogits();
    generator.GenerateNextTokenTop();
}

for (ulong i = 0; i < batchSize; i++)
{
    var sequence = generator.GetSequence((int)i);
    ... // use the output tokens
}

What does this Pull-Request Include? 🤔

  • The C# API for Windows. The build generates a managed dll called Microsoft.ML.OnnxRuntimeGenAI.dll for x64 using net8.0.
  • Test that leverages the managed dll built.
  • This pull-request does not include packaging.
  • Module definition for exporting the native functions in the C library.

What's next? 🔄

  • Run build and tests in CI pipeline.
  • C# packaging.
  • Linux C# support
  • Build the C# API as part of the cmake build.
  • Tokenizer functions to be added to C# API to complete the API

@baijumeswani baijumeswani force-pushed the baijumeswani/csharp-api branch from 07430ca to 0acf678 Compare February 13, 2024 03:23
@baijumeswani baijumeswani marked this pull request as ready for review February 13, 2024 03:24
src/onnxruntime-genai.def Outdated Show resolved Hide resolved
@yuslepukhin
Copy link
Member

yuslepukhin commented Feb 14, 2024

The summary example features nested using blocks. It would be better to use using statements that are scope based, so blocks are not required, although they may be useful when you want to dispose of something before the current scope ends. #Resolved

@baijumeswani
Copy link
Collaborator Author

The summary example features nested using blocks. It would be better to use using statements that are scope based, so blocks are not required, although they may be useful when you want to dispose of something before the current scope ends.

Updated the sample example to avoid using the nested using statements.

src/onnxruntime-genai.def Outdated Show resolved Hide resolved
src/csharp/Microsoft.ML.OnnxRuntimeGenAI.csproj Outdated Show resolved Hide resolved
src/csharp/Microsoft.ML.OnnxRuntimeGenAI.csproj Outdated Show resolved Hide resolved
src/csharp/NativeMethods.cs Outdated Show resolved Hide resolved
src/csharp/NativeMethods.cs Show resolved Hide resolved
src/csharp/NativeMethods.cs Outdated Show resolved Hide resolved
src/csharp/NativeMethods.cs Show resolved Hide resolved
src/csharp/NativeMethods.cs Outdated Show resolved Hide resolved
src/csharp/NativeMethods.cs Outdated Show resolved Hide resolved
src/csharp/NativeMethods.cs Outdated Show resolved Hide resolved
src/ort_genai_c.h Outdated Show resolved Hide resolved
src/ort_genai_c.h Outdated Show resolved Hide resolved
src/ort_genai_c.h Outdated Show resolved Hide resolved
src/ort_genai_c.h Outdated Show resolved Hide resolved
src/ort_genai_c.h Outdated Show resolved Hide resolved
src/ort_genai_c.h Show resolved Hide resolved
src/ort_genai_c.h Show resolved Hide resolved
src/ort_genai_c.h Show resolved Hide resolved
src/csharp/Microsoft.ML.OnnxRuntimeGenAI.csproj Outdated Show resolved Hide resolved
src/csharp/GeneratorParams.cs Show resolved Hide resolved
src/csharp/Model.cs Show resolved Hide resolved
src/csharp/Model.cs Outdated Show resolved Hide resolved
src/csharp/Generator.cs Outdated Show resolved Hide resolved
Copy link
Member

@yuslepukhin yuslepukhin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@baijumeswani baijumeswani merged commit 0955c85 into main Feb 16, 2024
11 checks passed
@baijumeswani baijumeswani deleted the baijumeswani/csharp-api branch February 16, 2024 03:50
@baijumeswani
Copy link
Collaborator Author

baijumeswani commented Feb 16, 2024

Thank you for the review @yuslepukhin @RyanUnderhill @snnn :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants