Skip to content

Commit 8a306e7

Browse files
committed
Make Microsoft.ML.OnnxRuntimeGenAI.Tokenizer a Microsoft.ML.Tokenizers.Tokenizer
This enables an ONNX Runtime GenAI tokenizer instance to be used anywhere a Microsoft.ML.Tokenizers tokenizer is accepted. If we'd prefer, rather than having Tokenizer be a base class for the ONNX Runtime one, we could instead expose some sort of `public Microsoft.ML.Tokenizer.Tokenizer AsTokenizer()` conversion method that returns a wrapper object (though that's a bit confusing given the names of the type are the same, just different namespaces).
1 parent 90c8b03 commit 8a306e7

File tree

13 files changed

+274
-76
lines changed

13 files changed

+274
-76
lines changed

src/csharp/Adapters.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ public Adapters(Model model) : base(IntPtr.Zero, true)
3131
/// <param name="adapterName">adapter name</param>
3232
public void LoadAdapter(string adapterPath, string adapterName)
3333
{
34-
Result.VerifySuccess(NativeMethods.OgaLoadAdapter(handle,
35-
StringUtils.ToUtf8(adapterPath), StringUtils.ToUtf8(adapterName)));
34+
Result.VerifySuccess(NativeMethods.OgaLoadAdapter(
35+
handle,
36+
StringUtils.ToNullTerminatedUtf8(adapterPath),
37+
StringUtils.ToNullTerminatedUtf8(adapterName)));
3638
}
3739

3840
/// <summary>
@@ -42,7 +44,9 @@ public void LoadAdapter(string adapterPath, string adapterName)
4244
/// <param name="adapterName"></param>
4345
public void UnloadAdapter(string adapterName)
4446
{
45-
Result.VerifySuccess(NativeMethods.OgaUnloadAdapter(handle, StringUtils.ToUtf8(adapterName)));
47+
Result.VerifySuccess(NativeMethods.OgaUnloadAdapter(
48+
handle,
49+
StringUtils.ToNullTerminatedUtf8(adapterName)));
4650
}
4751

4852
internal IntPtr Handle { get { return handle; } }

src/csharp/Audios.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ public static Audios Load(string[] audioPaths)
2323
Result.VerifySuccess(NativeMethods.OgaCreateStringArray(out IntPtr stringArray));
2424
foreach (string audioPath in audioPaths)
2525
{
26-
Result.VerifySuccess(NativeMethods.OgaStringArrayAddString(stringArray, StringUtils.ToUtf8(audioPath)));
26+
Result.VerifySuccess(NativeMethods.OgaStringArrayAddString(
27+
stringArray,
28+
StringUtils.ToNullTerminatedUtf8(audioPath)));
2729
}
2830
Result.VerifySuccess(NativeMethods.OgaLoadAudios(stringArray, out IntPtr audiosHandle));
2931
NativeMethods.OgaDestroyStringArray(stringArray);

src/csharp/Config.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ public class Config : IDisposable
1111
private bool _disposed = false;
1212
public Config(string modelPath)
1313
{
14-
Result.VerifySuccess(NativeMethods.OgaCreateConfig(StringUtils.ToUtf8(modelPath), out _configHandle));
14+
Result.VerifySuccess(NativeMethods.OgaCreateConfig(
15+
StringUtils.ToNullTerminatedUtf8(modelPath),
16+
out _configHandle));
1517
}
1618

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

2325
public void AppendProvider(string provider)
2426
{
25-
Result.VerifySuccess(NativeMethods.OgaConfigAppendProvider(_configHandle, StringUtils.ToUtf8(provider)));
27+
Result.VerifySuccess(NativeMethods.OgaConfigAppendProvider(
28+
_configHandle,
29+
StringUtils.ToNullTerminatedUtf8(provider)));
2630
}
2731

2832
public void SetProviderOption(string provider, string option, string value)
2933
{
30-
Result.VerifySuccess(NativeMethods.OgaConfigSetProviderOption(_configHandle, StringUtils.ToUtf8(provider), StringUtils.ToUtf8(option), StringUtils.ToUtf8(value)));
34+
Result.VerifySuccess(NativeMethods.OgaConfigSetProviderOption(
35+
_configHandle,
36+
StringUtils.ToNullTerminatedUtf8(provider),
37+
StringUtils.ToNullTerminatedUtf8(option),
38+
StringUtils.ToNullTerminatedUtf8(value)));
3139
}
3240

3341
~Config()

src/csharp/Generator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public ReadOnlySpan<int> GetSequence(ulong index)
7070
public Tensor GetOutput(string outputName)
7171
{
7272
Result.VerifySuccess(NativeMethods.OgaGenerator_GetOutput(_generatorHandle,
73-
StringUtils.ToUtf8(outputName),
73+
StringUtils.ToNullTerminatedUtf8(outputName),
7474
out IntPtr outputTensor));
7575
return new Tensor(outputTensor);
7676
}
@@ -85,7 +85,7 @@ public void SetActiveAdapter(Adapters adapters, string adapterName)
8585
{
8686
Result.VerifySuccess(NativeMethods.OgaSetActiveAdapter(_generatorHandle,
8787
adapters.Handle,
88-
StringUtils.ToUtf8(adapterName)));
88+
StringUtils.ToNullTerminatedUtf8(adapterName)));
8989
}
9090

9191
~Generator()

src/csharp/GeneratorParams.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
// Licensed under the MIT License.
33

44
using System;
5-
using System.Collections.Generic;
6-
using System.Linq;
7-
using System.Runtime.InteropServices;
85

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

2219
public void SetSearchOption(string searchOption, double value)
2320
{
24-
Result.VerifySuccess(NativeMethods.OgaGeneratorParamsSetSearchNumber(_generatorParamsHandle, StringUtils.ToUtf8(searchOption), value));
21+
Result.VerifySuccess(NativeMethods.OgaGeneratorParamsSetSearchNumber(
22+
_generatorParamsHandle,
23+
StringUtils.ToNullTerminatedUtf8(searchOption), value));
2524
}
2625

2726
public void SetSearchOption(string searchOption, bool value)
2827
{
29-
Result.VerifySuccess(NativeMethods.OgaGeneratorParamsSetSearchBool(_generatorParamsHandle, StringUtils.ToUtf8(searchOption), value));
28+
Result.VerifySuccess(NativeMethods.OgaGeneratorParamsSetSearchBool(
29+
_generatorParamsHandle,
30+
StringUtils.ToNullTerminatedUtf8(searchOption), value));
3031
}
3132

3233
public void TryGraphCaptureWithMaxBatchSize(int maxBatchSize)
3334
{
34-
Result.VerifySuccess(NativeMethods.OgaGeneratorParamsTryGraphCaptureWithMaxBatchSize(_generatorParamsHandle, maxBatchSize));
35+
Result.VerifySuccess(NativeMethods.OgaGeneratorParamsTryGraphCaptureWithMaxBatchSize(
36+
_generatorParamsHandle,
37+
maxBatchSize));
3538
}
3639

3740
public void SetModelInput(string name, Tensor value)
3841
{
39-
Result.VerifySuccess(NativeMethods.OgaGeneratorParamsSetModelInput(_generatorParamsHandle, StringUtils.ToUtf8(name), value.Handle));
42+
Result.VerifySuccess(NativeMethods.OgaGeneratorParamsSetModelInput(
43+
_generatorParamsHandle,
44+
StringUtils.ToNullTerminatedUtf8(name),
45+
value.Handle));
4046
}
4147

4248
public void SetInputs(NamedTensors namedTensors)
4349
{
44-
Result.VerifySuccess(NativeMethods.OgaGeneratorParamsSetInputs(_generatorParamsHandle, namedTensors.Handle));
50+
Result.VerifySuccess(NativeMethods.OgaGeneratorParamsSetInputs(
51+
_generatorParamsHandle,
52+
namedTensors.Handle));
4553
}
4654

4755
~GeneratorParams()

src/csharp/Images.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License.
33

44
using System;
5-
using System.Runtime.InteropServices;
65

76
namespace Microsoft.ML.OnnxRuntimeGenAI
87
{
@@ -23,7 +22,9 @@ public static Images Load(string[] imagePaths)
2322
Result.VerifySuccess(NativeMethods.OgaCreateStringArray(out IntPtr stringArray));
2423
foreach (string imagePath in imagePaths)
2524
{
26-
Result.VerifySuccess(NativeMethods.OgaStringArrayAddString(stringArray, StringUtils.ToUtf8(imagePath)));
25+
Result.VerifySuccess(NativeMethods.OgaStringArrayAddString(
26+
stringArray,
27+
StringUtils.ToNullTerminatedUtf8(imagePath)));
2728
}
2829
Result.VerifySuccess(NativeMethods.OgaLoadImages(stringArray, out IntPtr imagesHandle));
2930
NativeMethods.OgaDestroyStringArray(stringArray);

src/csharp/Microsoft.ML.OnnxRuntimeGenAI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123

124124
<ItemGroup>
125125
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.3.0-preview.1.25114.11" />
126+
<PackageReference Include="Microsoft.ML.Tokenizers" Version="1.0.2" />
126127
</ItemGroup>
127128

128129
</Project>

src/csharp/Model.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License.
33

44
using System;
5-
using System.Runtime.InteropServices;
65

76
namespace Microsoft.ML.OnnxRuntimeGenAI
87
{
@@ -13,7 +12,9 @@ public class Model : IDisposable
1312

1413
public Model(string modelPath)
1514
{
16-
Result.VerifySuccess(NativeMethods.OgaCreateModel(StringUtils.ToUtf8(modelPath), out _modelHandle));
15+
Result.VerifySuccess(NativeMethods.OgaCreateModel(
16+
StringUtils.ToNullTerminatedUtf8(modelPath),
17+
out _modelHandle));
1718
}
1819

1920
public Model(Config config)

src/csharp/MultiModalProcessor.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ public MultiModalProcessor(Model model)
2020
public NamedTensors ProcessImages(string prompt, Images images)
2121
{
2222
IntPtr imagesHandle = images == null ? IntPtr.Zero : images.Handle;
23-
Result.VerifySuccess(NativeMethods.OgaProcessorProcessImages(_processorHandle, StringUtils.ToUtf8(prompt),
24-
imagesHandle, out IntPtr namedTensorsHandle));
23+
Result.VerifySuccess(NativeMethods.OgaProcessorProcessImages(
24+
_processorHandle,
25+
StringUtils.ToNullTerminatedUtf8(prompt),
26+
imagesHandle,
27+
out IntPtr namedTensorsHandle));
2528
return new NamedTensors(namedTensorsHandle);
2629
}
2730

@@ -41,8 +44,12 @@ public NamedTensors ProcessImagesAndAudios(string prompt, Images images, Audios
4144
{
4245
IntPtr imagesHandle = images == null ? IntPtr.Zero : images.Handle;
4346
IntPtr audiosHandle = audios == null ? IntPtr.Zero : audios.Handle;
44-
Result.VerifySuccess(NativeMethods.OgaProcessorProcessImagesAndAudios(_processorHandle, StringUtils.ToUtf8(prompt),
45-
imagesHandle, audiosHandle, out IntPtr namedTensorsHandle));
47+
Result.VerifySuccess(NativeMethods.OgaProcessorProcessImagesAndAudios(
48+
_processorHandle,
49+
StringUtils.ToNullTerminatedUtf8(prompt),
50+
imagesHandle,
51+
audiosHandle,
52+
out IntPtr namedTensorsHandle));
4653
return new NamedTensors(namedTensorsHandle);
4754
}
4855

@@ -68,7 +75,7 @@ public string Decode(ReadOnlySpan<int> sequence)
6875
}
6976
try
7077
{
71-
return StringUtils.FromUtf8(outStr);
78+
return StringUtils.FromNullTerminatedUtf8(outStr);
7279
}
7380
finally
7481
{

src/csharp/Result.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,22 @@
66

77
namespace Microsoft.ML.OnnxRuntimeGenAI
88
{
9-
class Result
9+
internal static class Result
1010
{
11-
private static string GetErrorMessage(IntPtr nativeResult)
11+
internal static string GetErrorMessage(IntPtr nativeResult)
1212
{
13-
14-
return StringUtils.FromUtf8(NativeMethods.OgaResultGetError(nativeResult));
13+
return StringUtils.FromNullTerminatedUtf8(NativeMethods.OgaResultGetError(nativeResult));
1514
}
1615

1716
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1817
public static void VerifySuccess(IntPtr nativeResult)
1918
{
2019
if (nativeResult != IntPtr.Zero)
20+
{
21+
Throw(nativeResult);
22+
}
23+
24+
static void Throw(IntPtr nativeResult)
2125
{
2226
try
2327
{

0 commit comments

Comments
 (0)