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

Improved the BatchedDecoding demo #444

Merged
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
8 changes: 4 additions & 4 deletions LLama.Examples/Examples/BatchedDecoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static async Task Run()
using var model = LLamaWeights.LoadFromFile(parameters);

// Tokenize prompt
var prompt_tokens = model.NativeHandle.Tokenize(prompt, true, false, Encoding.UTF8);
var prompt_tokens = model.Tokenize(prompt, true, false, Encoding.UTF8);
var n_kv_req = prompt_tokens.Length + (n_len - prompt_tokens.Length) * n_parallel;

// Create a context
Expand Down Expand Up @@ -86,9 +86,9 @@ public static async Task Run()
var n_cur = batch.TokenCount;
var n_decode = 0;

var streams = new List<LLamaToken>[n_parallel];
var streams = new StreamingTokenDecoder[n_parallel];
for (var i = 0; i < n_parallel; i++)
streams[i] = new();
streams[i] = new StreamingTokenDecoder(context);

var eos = model.EndOfSentenceToken;
var nl = model.NewlineToken;
Expand Down Expand Up @@ -159,7 +159,7 @@ public static async Task Run()
var index = 0;
foreach (var stream in streams)
{
var text = context.DeTokenize(stream);
var text = stream.Read();

Console.ForegroundColor = ConsoleColor.Green;
Console.Write($"{index++}. {prompt}");
Expand Down
14 changes: 14 additions & 0 deletions LLama/LLamaWeights.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;
using LLama.Abstractions;
using LLama.Extensions;
using LLama.Native;
Expand Down Expand Up @@ -109,5 +110,18 @@ public LLamaContext CreateContext(IContextParams @params, ILogger? logger = null
{
return new LLamaContext(this, @params, logger);
}

/// <summary>
/// Convert a string of text into tokens
/// </summary>
/// <param name="text"></param>
/// <param name="add_bos"></param>
/// <param name="encoding"></param>
/// <param name="special">Allow tokenizing special and/or control tokens which otherwise are not exposed and treated as plaintext.</param>
/// <returns></returns>
public LLamaToken[] Tokenize(string text, bool add_bos, bool special, Encoding encoding)
{
return NativeHandle.Tokenize(text, add_bos, special, encoding);
}
}
}
Loading