Skip to content

Commit

Permalink
.Net: Bump ONNX to 0.5.2 (#9644)
Browse files Browse the repository at this point in the history
### Motivation and Context

- The Latest 0.5.0 package also requires the caller to handle the
resources with the `OgaHandler` instance, when a service is instantiated
this resource needs to be also present and exposed together with the
service. Otherwise a message will be sent to the console and the
application will crash before finishing.

- Resolves #9628

---------

Co-authored-by: westey <[email protected]>
  • Loading branch information
RogerBarreto and westey-m authored Dec 2, 2024
1 parent d9fd8ff commit 4674281
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 43 deletions.
8 changes: 4 additions & 4 deletions dotnet/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" />
<PackageVersion Include="Microsoft.Bcl.TimeProvider" Version="8.0.1" />
<PackageVersion Include="Microsoft.Identity.Client" Version="4.66.2" />
<PackageVersion Include="Microsoft.ML.OnnxRuntime" Version="1.19.2" />
<PackageVersion Include="Microsoft.ML.OnnxRuntime" Version="1.20.1" />
<PackageVersion Include="FastBertTokenizer" Version="1.0.28" />
<PackageVersion Include="PdfPig" Version="0.1.9" />
<PackageVersion Include="Pinecone.NET" Version="2.1.1" />
Expand Down Expand Up @@ -161,8 +161,8 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<!-- OnnxRuntimeGenAI -->
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI" Version="0.4.0" />
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI.Cuda" Version="0.4.0" />
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI.DirectML" Version="0.4.0" />
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI" Version="0.5.2" />
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI.Cuda" Version="0.5.2" />
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI.DirectML" Version="0.5.2" />
</ItemGroup>
</Project>
18 changes: 17 additions & 1 deletion dotnet/samples/Concepts/ChatCompletion/Onnx_ChatCompletion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task ServicePromptAsync()

Console.WriteLine("======== Onnx - Chat Completion ========");

var chatService = new OnnxRuntimeGenAIChatCompletionService(
using var chatService = new OnnxRuntimeGenAIChatCompletionService(
modelId: TestConfiguration.Onnx.ModelId,
modelPath: TestConfiguration.Onnx.ModelPath);

Expand Down Expand Up @@ -105,5 +105,21 @@ public async Task ChatPromptAsync()
reply = await kernel.InvokePromptAsync(chatPrompt.ToString());

Console.WriteLine(reply);

DisposeServices(kernel);
}

/// <summary>
/// To avoid any potential memory leak all disposable services created by the kernel are disposed.
/// </summary>
/// <param name="kernel">Target kernel</param>
private static void DisposeServices(Kernel kernel)
{
foreach (var target in kernel
.GetAllServices<IChatCompletionService>()
.OfType<IDisposable>())
{
target.Dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,36 @@ public class Onnx_ChatCompletionStreaming(ITestOutputHelper output) : BaseTest(o
/// </list>
/// </remarks>
[Fact]
public Task StreamChatAsync()
public async Task StreamChatAsync()
{
Assert.NotNull(TestConfiguration.Onnx.ModelId); // dotnet user-secrets set "Onnx:ModelId" "<model-id>"
Assert.NotNull(TestConfiguration.Onnx.ModelPath); // dotnet user-secrets set "Onnx:ModelPath" "<model-folder-path>"

Console.WriteLine("======== Onnx - Chat Completion Streaming ========");

var chatService = new OnnxRuntimeGenAIChatCompletionService(
using var chatService = new OnnxRuntimeGenAIChatCompletionService(
modelId: TestConfiguration.Onnx.ModelId,
modelPath: TestConfiguration.Onnx.ModelPath);

return this.StartStreamingChatAsync(chatService);
Console.WriteLine("Chat content:");
Console.WriteLine("------------------------");

var chatHistory = new ChatHistory("You are a librarian, expert about books");
OutputLastMessage(chatHistory);

// First user message
chatHistory.AddUserMessage("Hi, I'm looking for book suggestions");
OutputLastMessage(chatHistory);

// First assistant message
await StreamMessageOutputAsync(chatService, chatHistory, AuthorRole.Assistant);

// Second user message
chatHistory.AddUserMessage("I love history and philosophy, I'd like to learn something new about Greece, any suggestion?");
OutputLastMessage(chatHistory);

// Second assistant message
await StreamMessageOutputAsync(chatService, chatHistory, AuthorRole.Assistant);
}

/// <summary>
Expand Down Expand Up @@ -86,6 +104,8 @@ public async Task StreamChatPromptAsync()
reply = await StreamMessageOutputFromKernelAsync(kernel, chatPrompt.ToString());

Console.WriteLine(reply);

DisposeServices(kernel);
}

/// <summary>
Expand Down Expand Up @@ -115,7 +135,7 @@ public async Task StreamTextFromChatAsync()
Console.WriteLine("======== Stream Text from Chat Content ========");

// Create chat completion service
var chatService = new OnnxRuntimeGenAIChatCompletionService(
using var chatService = new OnnxRuntimeGenAIChatCompletionService(
modelId: TestConfiguration.Onnx.ModelId,
modelPath: TestConfiguration.Onnx.ModelPath);

Expand All @@ -135,30 +155,7 @@ public async Task StreamTextFromChatAsync()
}
}

private async Task StartStreamingChatAsync(IChatCompletionService chatCompletionService)
{
Console.WriteLine("Chat content:");
Console.WriteLine("------------------------");

var chatHistory = new ChatHistory("You are a librarian, expert about books");
OutputLastMessage(chatHistory);

// First user message
chatHistory.AddUserMessage("Hi, I'm looking for book suggestions");
OutputLastMessage(chatHistory);

// First assistant message
await StreamMessageOutputAsync(chatCompletionService, chatHistory, AuthorRole.Assistant);

// Second user message
chatHistory.AddUserMessage("I love history and philosophy, I'd like to learn something new about Greece, any suggestion?");
OutputLastMessage(chatHistory);

// Second assistant message
await StreamMessageOutputAsync(chatCompletionService, chatHistory, AuthorRole.Assistant);
}

private async Task StreamMessageOutputAsync(IChatCompletionService chatCompletionService, ChatHistory chatHistory, AuthorRole authorRole)
private async Task StreamMessageOutputAsync(OnnxRuntimeGenAIChatCompletionService chatCompletionService, ChatHistory chatHistory, AuthorRole authorRole)
{
bool roleWritten = false;
string fullMessage = string.Empty;
Expand Down Expand Up @@ -205,4 +202,18 @@ private async Task<string> StreamMessageOutputFromKernelAsync(Kernel kernel, str
Console.WriteLine("\n------------------------");
return fullMessage;
}

/// <summary>
/// To avoid any potential memory leak all disposable services created by the kernel are disposed.
/// </summary>
/// <param name="kernel">Target kernel</param>
private static void DisposeServices(Kernel kernel)
{
foreach (var target in kernel
.GetAllServices<IChatCompletionService>()
.OfType<IDisposable>())
{
target.Dispose();
}
}
}
2 changes: 1 addition & 1 deletion dotnet/samples/Demos/OnnxSimpleRAG/OnnxSimpleRAG.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<NoWarn>$(NoWarn);CA2007;CS0612;VSTHRD111</NoWarn>
<NoWarn>$(NoWarn);CA2007;CS0612;VSTHRD111;SKEXP0070;SKEXP0050;SKEXP0001;SKEXP0020</NoWarn>
<UserSecretsId>5ee045b0-aea3-4f08-8d31-32d1a6f8fed0</UserSecretsId>
</PropertyGroup>

Expand Down
32 changes: 25 additions & 7 deletions dotnet/samples/Demos/OnnxSimpleRAG/Program.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
// Copyright (c) Microsoft. All rights reserved.

#pragma warning disable SKEXP0070
#pragma warning disable SKEXP0050
#pragma warning disable SKEXP0001
#pragma warning disable SKEXP0020

using System;
using System.IO;
using System.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.VectorData;
using Microsoft.ML.OnnxRuntimeGenAI;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.InMemory;
using Microsoft.SemanticKernel.Connectors.Onnx;
using Microsoft.SemanticKernel.Data;
using Microsoft.SemanticKernel.Embeddings;
using Microsoft.SemanticKernel.PromptTemplates.Handlebars;
Expand All @@ -29,6 +27,10 @@
// Path to the vocab file your ONNX BGE-MICRO-V2 model
var embeddingVocabPath = config["Onnx:EmbeddingVocabPath"]!;

// If using Onnx GenAI 0.5.0 or later, the OgaHandle class must be used to track
// resources used by the Onnx services, before using any of the Onnx services.
using var ogaHandle = new OgaHandle();

// Load the services
var builder = Kernel.CreateBuilder()
.AddOnnxRuntimeGenAIChatCompletion(chatModelId, chatModelPath)
Expand All @@ -38,7 +40,7 @@
var kernel = builder.Build();

// Get the instances of the services
var chatService = kernel.GetRequiredService<IChatCompletionService>();
using var chatService = kernel.GetRequiredService<IChatCompletionService>() as OnnxRuntimeGenAIChatCompletionService;
var embeddingService = kernel.GetRequiredService<ITextEmbeddingGenerationService>();

// Create a vector store and a collection to store information
Expand Down Expand Up @@ -71,9 +73,12 @@ await collection.UpsertAsync(new()
Console.Write("User > ");
var question = Console.ReadLine()!;

// Clean resources and exit the demo if the user input is null or empty
if (question is null || string.IsNullOrWhiteSpace(question))
{
// Exit the demo if the user input is null or empty
// To avoid any potential memory leak all disposable
// services created by the kernel are disposed
DisposeServices(kernel);
return;
}

Expand Down Expand Up @@ -105,6 +110,19 @@ await collection.UpsertAsync(new()
Console.WriteLine();
}

static void DisposeServices(Kernel kernel)
{
foreach (var target in kernel
.GetAllServices<IChatCompletionService>()
.OfType<IDisposable>())
{
target.Dispose();
}
}

/// <summary>
/// Information item to represent the embedding data stored in the memory
/// </summary>
internal sealed class InformationItem
{
[VectorStoreRecordKey]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<AssemblyName>Microsoft.SemanticKernel.Connectors.Onnx</AssemblyName>
<RootNamespace>$(AssemblyName)</RootNamespace>
<TargetFrameworks>net8.0;netstandard2.0</TargetFrameworks>
<VersionSuffix>alpha</VersionSuffix>
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>
<VersionSuffix>alpha</VersionSuffix>
</PropertyGroup>

<!-- IMPORT NUGET PACKAGE SHARED PROPERTIES -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public sealed class OnnxRuntimeGenAIChatCompletionService : IChatCompletionServi
private readonly JsonSerializerOptions? _jsonSerializerOptions;
private Model? _model;
private Tokenizer? _tokenizer;

private Dictionary<string, object?> AttributesInternal { get; } = new();

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Microsoft.SemanticKernel.Connectors.Onnx;
/// <summary>
/// OnnxRuntimeGenAI Execution Settings.
/// </summary>
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
public sealed class OnnxRuntimeGenAIPromptExecutionSettings : PromptExecutionSettings
{
/// <summary>
Expand Down

0 comments on commit 4674281

Please sign in to comment.