Skip to content
Merged
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
4 changes: 2 additions & 2 deletions examples/csharp/HelloPhi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ static string GetPrompt(bool interactive)
while (!generator.IsDone())
{
generator.GenerateNextToken();
Console.Write(tokenizerStream.Decode(generator.GetSequence(0)[^1]));
Console.Write(tokenizerStream.Decode(generator.GetNextTokens()[0]));
}
Console.WriteLine();
watch.Stop();
Expand Down Expand Up @@ -195,7 +195,7 @@ static string GetPrompt(bool interactive)
while (!generator.IsDone())
{
generator.GenerateNextToken();
Console.Write(tokenizerStream.Decode(generator.GetSequence(0)[^1]));
Console.Write(tokenizerStream.Decode(generator.GetNextTokens()[0]));
}
Console.WriteLine();
watch.Stop();
Expand Down
2 changes: 1 addition & 1 deletion examples/csharp/HelloPhi3V/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void PrintUsage()
{
break;
}
Console.Write(stream.Decode(generator.GetSequence(0)[^1]));
Console.Write(stream.Decode(generator.GetNextTokens()[0]));
}
watch.Stop();
var runTimeInSeconds = watch.Elapsed.TotalSeconds;
Expand Down
2 changes: 1 addition & 1 deletion examples/csharp/HelloPhi4MM/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ void PrintUsage()
{
break;
}
Console.Write(stream.Decode(generator.GetSequence(0)[^1]));
Console.Write(stream.Decode(generator.GetNextTokens()[0]));
}
watch.Stop();
var runTimeInSeconds = watch.Elapsed.TotalSeconds;
Expand Down
9 changes: 9 additions & 0 deletions src/csharp/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ public void RewindTo(ulong newLength)
Result.VerifySuccess(NativeMethods.OgaGenerator_RewindTo(_generatorHandle, (UIntPtr)newLength));
}

public ReadOnlySpan<int> GetNextTokens()
{
Result.VerifySuccess(NativeMethods.OgaGenerator_GetNextTokens(_generatorHandle, out IntPtr tokenIds, out UIntPtr tokenCount));
unsafe
{
return new ReadOnlySpan<int>(tokenIds.ToPointer(), (int)tokenCount.ToUInt64());
}
}

public ReadOnlySpan<int> GetSequence(ulong index)
{
ulong sequenceLength = NativeMethods.OgaGenerator_GetSequenceCount(_generatorHandle, (UIntPtr)index).ToUInt64();
Expand Down
5 changes: 5 additions & 0 deletions src/csharp/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ internal class NativeLib
[DllImport(NativeLib.DllName, CallingConvention = CallingConvention.Winapi)]
public static extern byte OgaGenerator_IsDone(IntPtr /* const OgaGenerator* */ generator);

[DllImport(NativeLib.DllName, CallingConvention = CallingConvention.Winapi)]
public static extern IntPtr /* OgaResult* */ OgaGenerator_GetNextTokens(IntPtr /* const OgaGenerator* */ generator,
out IntPtr /* const int32_t** */ outTokenIds,
out UIntPtr /* size_t* */ outTokenCount);

// This function is used to generate the next token in the sequence using the greedy search algorithm.
[DllImport(NativeLib.DllName, CallingConvention = CallingConvention.Winapi)]
public static extern IntPtr /* OgaResult* */ OgaGenerator_GenerateNextToken(IntPtr /* OgaGenerator* */ generator);
Expand Down
Loading