Browse Source

Merge pull request #54 from martindevans/fix_multiple_enumeration

Fixed Multiple Enumeration
tags/v0.4.2-preview
Rinne GitHub 2 years ago
parent
commit
e5ca18fd86
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 13 deletions
  1. +15
    -5
      LLama/Common/FixedSizeQueue.cs
  2. +4
    -4
      LLama/LLamaEmbedder.cs
  3. +1
    -1
      LLama/LLamaExecutorBase.cs
  4. +3
    -2
      LLama/LLamaStatelessExecutor.cs
  5. +1
    -1
      LLama/LLamaTransforms.cs

+ 15
- 5
LLama/Common/FixedSizeQueue.cs View File

@@ -30,14 +30,24 @@ namespace LLama.Common
/// <param name="data"></param>
public FixedSizeQueue(int size, IEnumerable<T> data)
{
// Try an early check on the amount of data supplied (if possible)
if (data.TryGetNonEnumeratedCount(out var count) && count > size)
throw new ArgumentException($"The max size set for the quene is {size}, but got {count} initial values.");

// Size of "data" is unknown, copy it all into a list
_maxSize = size;
if(data.Count() > size)
{
throw new ArgumentException($"The max size set for the quene is {size}, but got {data.Count()} initial values.");
}
_storage = new(data);
_storage = new List<T>(data);
// Now check if that list is a valid size
if (_storage.Count > _maxSize)
throw new ArgumentException($"The max size set for the quene is {size}, but got {count} initial values.");
}

/// <summary>
/// Replace every item in the queue with the given value
/// </summary>
/// <param name="value">The value to replace all items with</param>
/// <returns>returns this</returns>
public FixedSizeQueue<T> FillWith(T value)
{
for(int i = 0; i < Count; i++)


+ 4
- 4
LLama/LLamaEmbedder.cs View File

@@ -54,13 +54,13 @@ namespace LLama
{
text = text.Insert(0, " ");
}
var embed_inp = Utils.Tokenize(_ctx, text, addBos, Encoding.GetEncoding(encoding));

var embed_inp_array = Utils.Tokenize(_ctx, text, addBos, Encoding.GetEncoding(encoding)).ToArray();

// TODO(Rinne): deal with log of prompt

if (embed_inp.Count() > 0)
if (embed_inp_array.Length > 0)
{
var embed_inp_array = embed_inp.ToArray();
if (NativeApi.llama_eval(_ctx, embed_inp_array, embed_inp_array.Length, n_past, threads) != 0)
{
throw new RuntimeError("Failed to eval.");
@@ -71,7 +71,7 @@ namespace LLama
var embeddings = NativeApi.llama_get_embeddings(_ctx);
if (embeddings == null)
{
return new float[0];
return Array.Empty<float>();
}
var span = new Span<float>(embeddings, n_embed);
float[] res = new float[n_embed];


+ 1
- 1
LLama/LLamaExecutorBase.cs View File

@@ -95,7 +95,7 @@ namespace LLama
_pathSession = filename;
if (string.IsNullOrEmpty(filename))
{
throw new ArgumentNullException("File name cannot be empty.");
throw new ArgumentNullException(nameof(filename), "File name cannot be empty.");
}
if (File.Exists(filename))
{


+ 3
- 2
LLama/LLamaStatelessExecutor.cs View File

@@ -29,8 +29,9 @@ namespace LLama
public StatelessExecutor(LLamaModel model)
{
_model = model;
var tokens = model.Tokenize(" ", true);
Utils.Eval(_model.NativeHandle, tokens.ToArray(), 0, tokens.Count(), 0, _model.Params.Threads);
var tokens = model.Tokenize(" ", true).ToArray();
Utils.Eval(_model.NativeHandle, tokens, 0, tokens.Length, 0, _model.Params.Threads);
_originalState = model.GetState();
}



+ 1
- 1
LLama/LLamaTransforms.cs View File

@@ -165,7 +165,7 @@ namespace LLama
public KeywordTextOutputStreamTransform(IEnumerable<string> keywords, int redundancyLength = 3, bool removeAllMatchedTokens = false)
{
_keywords = new(keywords);
_maxKeywordLength = keywords.Select(x => x.Length).Max() + redundancyLength;
_maxKeywordLength = _keywords.Select(x => x.Length).Max() + redundancyLength;
_removeAllMatchedTokens = removeAllMatchedTokens;
}
/// <inheritdoc />


Loading…
Cancel
Save