Browse Source

Improved the BatchedDecoding demo:

- using less `NativeHandle`
 - Using `StreamingTokenDecoder` instead of obsolete detokenize method
tags/v0.10.0
Martin Evans 1 year ago
parent
commit
5b6e82a594
2 changed files with 18 additions and 4 deletions
  1. +4
    -4
      LLama.Examples/Examples/BatchedDecoding.cs
  2. +14
    -0
      LLama/LLamaWeights.cs

+ 4
- 4
LLama.Examples/Examples/BatchedDecoding.cs View File

@@ -34,7 +34,7 @@ public class BatchedDecoding
using var model = LLamaWeights.LoadFromFile(parameters); using var model = LLamaWeights.LoadFromFile(parameters);


// Tokenize prompt // 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; var n_kv_req = prompt_tokens.Length + (n_len - prompt_tokens.Length) * n_parallel;


// Create a context // Create a context
@@ -86,9 +86,9 @@ public class BatchedDecoding
var n_cur = batch.TokenCount; var n_cur = batch.TokenCount;
var n_decode = 0; 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++) for (var i = 0; i < n_parallel; i++)
streams[i] = new();
streams[i] = new StreamingTokenDecoder(context);


var eos = model.EndOfSentenceToken; var eos = model.EndOfSentenceToken;
var nl = model.NewlineToken; var nl = model.NewlineToken;
@@ -159,7 +159,7 @@ public class BatchedDecoding
var index = 0; var index = 0;
foreach (var stream in streams) foreach (var stream in streams)
{ {
var text = context.DeTokenize(stream);
var text = stream.Read();


Console.ForegroundColor = ConsoleColor.Green; Console.ForegroundColor = ConsoleColor.Green;
Console.Write($"{index++}. {prompt}"); Console.Write($"{index++}. {prompt}");


+ 14
- 0
LLama/LLamaWeights.cs View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text;
using LLama.Abstractions; using LLama.Abstractions;
using LLama.Extensions; using LLama.Extensions;
using LLama.Native; using LLama.Native;
@@ -109,5 +110,18 @@ namespace LLama
{ {
return new LLamaContext(this, @params, logger); 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…
Cancel
Save