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

Make Microsoft.ML.OnnxRuntimeGenAI.Tokenizer a Microsoft.ML.Tokenizers.Tokenizer #970

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/csharp/Adapters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ public Adapters(Model model) : base(IntPtr.Zero, true)
/// <param name="adapterName">adapter name</param>
public void LoadAdapter(string adapterPath, string adapterName)
{
Result.VerifySuccess(NativeMethods.OgaLoadAdapter(handle,
StringUtils.ToUtf8(adapterPath), StringUtils.ToUtf8(adapterName)));
Result.VerifySuccess(NativeMethods.OgaLoadAdapter(
handle,
StringUtils.ToNullTerminatedUtf8(adapterPath),
StringUtils.ToNullTerminatedUtf8(adapterName)));
}

/// <summary>
Expand All @@ -42,7 +44,9 @@ public void LoadAdapter(string adapterPath, string adapterName)
/// <param name="adapterName"></param>
public void UnloadAdapter(string adapterName)
{
Result.VerifySuccess(NativeMethods.OgaUnloadAdapter(handle, StringUtils.ToUtf8(adapterName)));
Result.VerifySuccess(NativeMethods.OgaUnloadAdapter(
handle,
StringUtils.ToNullTerminatedUtf8(adapterName)));
}

internal IntPtr Handle { get { return handle; } }
Expand Down
4 changes: 3 additions & 1 deletion src/csharp/Audios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public static Audios Load(string[] audioPaths)
Result.VerifySuccess(NativeMethods.OgaCreateStringArray(out IntPtr stringArray));
foreach (string audioPath in audioPaths)
{
Result.VerifySuccess(NativeMethods.OgaStringArrayAddString(stringArray, StringUtils.ToUtf8(audioPath)));
Result.VerifySuccess(NativeMethods.OgaStringArrayAddString(
stringArray,
StringUtils.ToNullTerminatedUtf8(audioPath)));
}
Result.VerifySuccess(NativeMethods.OgaLoadAudios(stringArray, out IntPtr audiosHandle));
NativeMethods.OgaDestroyStringArray(stringArray);
Expand Down
14 changes: 11 additions & 3 deletions src/csharp/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ public class Config : IDisposable
private bool _disposed = false;
public Config(string modelPath)
{
Result.VerifySuccess(NativeMethods.OgaCreateConfig(StringUtils.ToUtf8(modelPath), out _configHandle));
Result.VerifySuccess(NativeMethods.OgaCreateConfig(
StringUtils.ToNullTerminatedUtf8(modelPath),
out _configHandle));
}

internal IntPtr Handle { get { return _configHandle; } }
Expand All @@ -22,12 +24,18 @@ public void ClearProviders()

public void AppendProvider(string provider)
{
Result.VerifySuccess(NativeMethods.OgaConfigAppendProvider(_configHandle, StringUtils.ToUtf8(provider)));
Result.VerifySuccess(NativeMethods.OgaConfigAppendProvider(
_configHandle,
StringUtils.ToNullTerminatedUtf8(provider)));
}

public void SetProviderOption(string provider, string option, string value)
{
Result.VerifySuccess(NativeMethods.OgaConfigSetProviderOption(_configHandle, StringUtils.ToUtf8(provider), StringUtils.ToUtf8(option), StringUtils.ToUtf8(value)));
Result.VerifySuccess(NativeMethods.OgaConfigSetProviderOption(
_configHandle,
StringUtils.ToNullTerminatedUtf8(provider),
StringUtils.ToNullTerminatedUtf8(option),
StringUtils.ToNullTerminatedUtf8(value)));
}

~Config()
Expand Down
4 changes: 2 additions & 2 deletions src/csharp/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public ReadOnlySpan<int> GetSequence(ulong index)
public Tensor GetOutput(string outputName)
{
Result.VerifySuccess(NativeMethods.OgaGenerator_GetOutput(_generatorHandle,
StringUtils.ToUtf8(outputName),
StringUtils.ToNullTerminatedUtf8(outputName),
out IntPtr outputTensor));
return new Tensor(outputTensor);
}
Expand All @@ -85,7 +85,7 @@ public void SetActiveAdapter(Adapters adapters, string adapterName)
{
Result.VerifySuccess(NativeMethods.OgaSetActiveAdapter(_generatorHandle,
adapters.Handle,
StringUtils.ToUtf8(adapterName)));
StringUtils.ToNullTerminatedUtf8(adapterName)));
}

~Generator()
Expand Down
24 changes: 16 additions & 8 deletions src/csharp/GeneratorParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;

namespace Microsoft.ML.OnnxRuntimeGenAI
{
Expand All @@ -21,27 +18,38 @@ public GeneratorParams(Model model)

public void SetSearchOption(string searchOption, double value)
{
Result.VerifySuccess(NativeMethods.OgaGeneratorParamsSetSearchNumber(_generatorParamsHandle, StringUtils.ToUtf8(searchOption), value));
Result.VerifySuccess(NativeMethods.OgaGeneratorParamsSetSearchNumber(
_generatorParamsHandle,
StringUtils.ToNullTerminatedUtf8(searchOption), value));
}

public void SetSearchOption(string searchOption, bool value)
{
Result.VerifySuccess(NativeMethods.OgaGeneratorParamsSetSearchBool(_generatorParamsHandle, StringUtils.ToUtf8(searchOption), value));
Result.VerifySuccess(NativeMethods.OgaGeneratorParamsSetSearchBool(
_generatorParamsHandle,
StringUtils.ToNullTerminatedUtf8(searchOption), value));
}

public void TryGraphCaptureWithMaxBatchSize(int maxBatchSize)
{
Result.VerifySuccess(NativeMethods.OgaGeneratorParamsTryGraphCaptureWithMaxBatchSize(_generatorParamsHandle, maxBatchSize));
Result.VerifySuccess(NativeMethods.OgaGeneratorParamsTryGraphCaptureWithMaxBatchSize(
_generatorParamsHandle,
maxBatchSize));
}

public void SetModelInput(string name, Tensor value)
{
Result.VerifySuccess(NativeMethods.OgaGeneratorParamsSetModelInput(_generatorParamsHandle, StringUtils.ToUtf8(name), value.Handle));
Result.VerifySuccess(NativeMethods.OgaGeneratorParamsSetModelInput(
_generatorParamsHandle,
StringUtils.ToNullTerminatedUtf8(name),
value.Handle));
}

public void SetInputs(NamedTensors namedTensors)
{
Result.VerifySuccess(NativeMethods.OgaGeneratorParamsSetInputs(_generatorParamsHandle, namedTensors.Handle));
Result.VerifySuccess(NativeMethods.OgaGeneratorParamsSetInputs(
_generatorParamsHandle,
namedTensors.Handle));
}

~GeneratorParams()
Expand Down
5 changes: 3 additions & 2 deletions src/csharp/Images.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using System;
using System.Runtime.InteropServices;

namespace Microsoft.ML.OnnxRuntimeGenAI
{
Expand All @@ -23,7 +22,9 @@ public static Images Load(string[] imagePaths)
Result.VerifySuccess(NativeMethods.OgaCreateStringArray(out IntPtr stringArray));
foreach (string imagePath in imagePaths)
{
Result.VerifySuccess(NativeMethods.OgaStringArrayAddString(stringArray, StringUtils.ToUtf8(imagePath)));
Result.VerifySuccess(NativeMethods.OgaStringArrayAddString(
stringArray,
StringUtils.ToNullTerminatedUtf8(imagePath)));
}
Result.VerifySuccess(NativeMethods.OgaLoadImages(stringArray, out IntPtr imagesHandle));
NativeMethods.OgaDestroyStringArray(stringArray);
Expand Down
1 change: 1 addition & 0 deletions src/csharp/Microsoft.ML.OnnxRuntimeGenAI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.3.0-preview.1.25114.11" />
<PackageReference Include="Microsoft.ML.Tokenizers" Version="1.0.2" />
</ItemGroup>

</Project>
5 changes: 3 additions & 2 deletions src/csharp/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using System;
using System.Runtime.InteropServices;

namespace Microsoft.ML.OnnxRuntimeGenAI
{
Expand All @@ -13,7 +12,9 @@ public class Model : IDisposable

public Model(string modelPath)
{
Result.VerifySuccess(NativeMethods.OgaCreateModel(StringUtils.ToUtf8(modelPath), out _modelHandle));
Result.VerifySuccess(NativeMethods.OgaCreateModel(
StringUtils.ToNullTerminatedUtf8(modelPath),
out _modelHandle));
}

public Model(Config config)
Expand Down
17 changes: 12 additions & 5 deletions src/csharp/MultiModalProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ public MultiModalProcessor(Model model)
public NamedTensors ProcessImages(string prompt, Images images)
{
IntPtr imagesHandle = images == null ? IntPtr.Zero : images.Handle;
Result.VerifySuccess(NativeMethods.OgaProcessorProcessImages(_processorHandle, StringUtils.ToUtf8(prompt),
imagesHandle, out IntPtr namedTensorsHandle));
Result.VerifySuccess(NativeMethods.OgaProcessorProcessImages(
_processorHandle,
StringUtils.ToNullTerminatedUtf8(prompt),
imagesHandle,
out IntPtr namedTensorsHandle));
return new NamedTensors(namedTensorsHandle);
}

Expand All @@ -41,8 +44,12 @@ public NamedTensors ProcessImagesAndAudios(string prompt, Images images, Audios
{
IntPtr imagesHandle = images == null ? IntPtr.Zero : images.Handle;
IntPtr audiosHandle = audios == null ? IntPtr.Zero : audios.Handle;
Result.VerifySuccess(NativeMethods.OgaProcessorProcessImagesAndAudios(_processorHandle, StringUtils.ToUtf8(prompt),
imagesHandle, audiosHandle, out IntPtr namedTensorsHandle));
Result.VerifySuccess(NativeMethods.OgaProcessorProcessImagesAndAudios(
_processorHandle,
StringUtils.ToNullTerminatedUtf8(prompt),
imagesHandle,
audiosHandle,
out IntPtr namedTensorsHandle));
return new NamedTensors(namedTensorsHandle);
}

Expand All @@ -68,7 +75,7 @@ public string Decode(ReadOnlySpan<int> sequence)
}
try
{
return StringUtils.FromUtf8(outStr);
return StringUtils.FromNullTerminatedUtf8(outStr);
}
finally
{
Expand Down
12 changes: 8 additions & 4 deletions src/csharp/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@

namespace Microsoft.ML.OnnxRuntimeGenAI
{
class Result
internal static class Result
{
private static string GetErrorMessage(IntPtr nativeResult)
internal static string GetErrorMessage(IntPtr nativeResult)
{

return StringUtils.FromUtf8(NativeMethods.OgaResultGetError(nativeResult));
return StringUtils.FromNullTerminatedUtf8(NativeMethods.OgaResultGetError(nativeResult));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void VerifySuccess(IntPtr nativeResult)
{
if (nativeResult != IntPtr.Zero)
{
Throw(nativeResult);
}

static void Throw(IntPtr nativeResult)
{
try
{
Expand Down
Loading
Loading