-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
101,473 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
|
||
namespace Microsoft.ML.OnnxRuntimeGenAI | ||
{ | ||
public class Tokenizer : IDisposable | ||
{ | ||
private IntPtr _tokenizerHandle; | ||
private bool _disposed = false; | ||
|
||
public Tokenizer(Model model) | ||
{ | ||
Result.VerifySuccess(NativeMethods.OgaCreateTokenizer(model.Handle, out _tokenizerHandle)); | ||
} | ||
|
||
public Sequences EncodeBatch(string[] strings) | ||
{ | ||
Result.VerifySuccess(NativeMethods.OgaCreateSequences(out IntPtr nativeSequences)); | ||
try | ||
{ | ||
foreach (string str in strings) | ||
{ | ||
Result.VerifySuccess(NativeMethods.OgaTokenizerEncode(_tokenizerHandle, Utils.ToUtf8(str), nativeSequences)); | ||
} | ||
|
||
return new Sequences(nativeSequences); | ||
} | ||
catch | ||
{ | ||
NativeMethods.OgaDestroySequences(nativeSequences); | ||
throw; | ||
} | ||
} | ||
|
||
public string[] DecodeBatch(Sequences sequences) | ||
{ | ||
string[] result = new string[sequences.NumSequences]; | ||
for (ulong i = 0; i < sequences.NumSequences; i++) | ||
{ | ||
result[i] = Decode(sequences[i]); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public Sequences Encode(string str) | ||
{ | ||
Result.VerifySuccess(NativeMethods.OgaCreateSequences(out IntPtr nativeSequences)); | ||
try | ||
{ | ||
Result.VerifySuccess(NativeMethods.OgaTokenizerEncode(_tokenizerHandle, Utils.ToUtf8(str), nativeSequences)); | ||
return new Sequences(nativeSequences); | ||
} | ||
catch | ||
{ | ||
NativeMethods.OgaDestroySequences(nativeSequences); | ||
throw; | ||
} | ||
} | ||
|
||
public string Decode(ReadOnlySpan<int> sequence) | ||
{ | ||
IntPtr outStr = IntPtr.Zero; | ||
unsafe | ||
{ | ||
fixed (int* sequencePtr = sequence) | ||
{ | ||
Result.VerifySuccess(NativeMethods.OgaTokenizerDecode(_tokenizerHandle, sequencePtr, (UIntPtr)sequence.Length, out outStr)); | ||
} | ||
} | ||
try | ||
{ | ||
return Utils.FromUtf8(outStr); | ||
} | ||
finally | ||
{ | ||
NativeMethods.OgaDestroyString(outStr); | ||
} | ||
} | ||
|
||
public TokenizerStream CreateStream() | ||
{ | ||
IntPtr tokenizerStreamHandle = IntPtr.Zero; | ||
Result.VerifySuccess(NativeMethods.OgaCreateTokenizerStream(_tokenizerHandle, out tokenizerStreamHandle)); | ||
return new TokenizerStream(tokenizerStreamHandle); | ||
} | ||
|
||
|
||
~Tokenizer() | ||
{ | ||
Dispose(false); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (_disposed) | ||
{ | ||
return; | ||
} | ||
NativeMethods.OgaDestroyTokenizer(_tokenizerHandle); | ||
_tokenizerHandle = IntPtr.Zero; | ||
_disposed = true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Microsoft.ML.OnnxRuntimeGenAI | ||
{ | ||
public class TokenizerStream : IDisposable | ||
{ | ||
private IntPtr _tokenizerStreamHandle; | ||
private bool _disposed = false; | ||
|
||
internal TokenizerStream(IntPtr tokenizerStreamHandle) | ||
{ | ||
_tokenizerStreamHandle = tokenizerStreamHandle; | ||
} | ||
|
||
internal IntPtr Handle { get { return _tokenizerStreamHandle; } } | ||
|
||
public string Decode(int token) | ||
{ | ||
IntPtr decodedStr = IntPtr.Zero; | ||
Result.VerifySuccess(NativeMethods.OgaTokenizerStreamDecode(_tokenizerStreamHandle, token, out decodedStr)); | ||
return Utils.FromUtf8(decodedStr); | ||
} | ||
|
||
~TokenizerStream() | ||
{ | ||
Dispose(false); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (_disposed) | ||
{ | ||
return; | ||
} | ||
NativeMethods.OgaDestroyTokenizerStream(_tokenizerStreamHandle); | ||
_tokenizerStreamHandle = IntPtr.Zero; | ||
_disposed = true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.