|
- using LLama.Abstractions;
- using LLama.Common;
- using LLama.Exceptions;
- using LLama.Native;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Text.Json.Serialization;
- using System.Threading;
-
- namespace LLama
- {
- using llama_token = Int32;
- /// <summary>
- /// The base class for stateful LLama executors.
- /// </summary>
- public abstract class StatefulExecutorBase : ILLamaExecutor
- {
- /// <summary>
- /// The loaded model for this executor.
- /// </summary>
- protected readonly LLamaModel _model;
- /// <summary>
- /// The logger used by this executor.
- /// </summary>
- protected ILLamaLogger? _logger;
- /// <summary>
- /// The tokens that were already processed by the model.
- /// </summary>
- protected int _pastTokensCount; // n_past
- /// <summary>
- /// The tokens that were consumed by the model during the current inference.
- /// </summary>
- protected int _consumedTokensCount; // n_consume
- /// <summary>
- ///
- /// </summary>
- protected int _n_session_consumed;
- /// <summary>
- ///
- /// </summary>
- protected int _n_matching_session_tokens;
- /// <summary>
- /// The path of the session file.
- /// </summary>
- protected string? _pathSession;
- /// <summary>
- /// A container of the tokens to be processed and after processed.
- /// </summary>
- protected List<llama_token> _embeds = new(); // embd
- /// <summary>
- /// A container for the tokens of input.
- /// </summary>
- protected List<llama_token> _embed_inps = new();
- /// <summary>
- ///
- /// </summary>
- protected List<llama_token> _session_tokens = new();
- /// <summary>
- /// The last tokens generated by the model.
- /// </summary>
- protected FixedSizeQueue<llama_token> _last_n_tokens;
- /// <summary>
- /// The mode used by the executor.
- /// </summary>
- public LLamaModel Model => _model;
-
- /// <summary>
- /// Current "mu" value for mirostate sampling
- /// </summary>
- protected float MirostateMu { get; set; } = float.NaN;
-
- /// <summary>
- ///
- /// </summary>
- /// <param name="model"></param>
- /// <param name="logger"></param>
- protected StatefulExecutorBase(LLamaModel model, ILLamaLogger? logger = null)
- {
- _model = model;
- _logger = logger;
- _pastTokensCount = 0;
- _consumedTokensCount = 0;
- _n_session_consumed = 0;
- _last_n_tokens = new FixedSizeQueue<llama_token>(_model.ContextSize).FillWith(0);
- }
-
- /// <summary>
- /// This API is currently not verified.
- /// </summary>
- /// <param name="filename"></param>
- /// <returns></returns>
- /// <exception cref="ArgumentNullException"></exception>
- /// <exception cref="RuntimeError"></exception>
- public unsafe StatefulExecutorBase WithSessionFile(string filename)
- {
- _pathSession = filename;
- if (string.IsNullOrEmpty(filename))
- {
- throw new ArgumentNullException(nameof(filename), "File name cannot be empty.");
- }
- if (File.Exists(filename))
- {
- _logger?.Log("LLamaExecutor", $"Attempting to load saved session from {filename}", ILLamaLogger.LogLevel.Info);
- llama_token[] session_tokens = new llama_token[_model.ContextSize];
- ulong n_token_count_out = 0;
- if (!NativeApi.llama_load_session_file(_model.NativeHandle, _pathSession, session_tokens, (ulong)_model.ContextSize, &n_token_count_out))
- {
- _logger?.Log("LLamaExecutor", $"Failed to load session file {filename}", ILLamaLogger.LogLevel.Error);
- throw new RuntimeError($"Failed to load session file {_pathSession}");
- }
- _session_tokens = session_tokens.Take((int)n_token_count_out).ToList();
- _logger?.Log("LLamaExecutor", $"Loaded a session with prompt size of {session_tokens.Length} tokens", ILLamaLogger.LogLevel.Info);
- }
- else
- {
- _logger?.Log("LLamaExecutor", $"Session file does not exist, will create", ILLamaLogger.LogLevel.Warning);
- }
-
- _n_matching_session_tokens = 0;
- if (_session_tokens.Count > 0)
- {
- foreach (var id in _session_tokens)
- {
- if (_n_matching_session_tokens >= _embed_inps.Count || id != _embed_inps[_n_matching_session_tokens])
- {
- break;
- }
- _n_matching_session_tokens++;
- }
- if (_n_matching_session_tokens >= _embed_inps.Count)
- {
- _logger?.Log("LLamaExecutor", $"Session file has exact match for prompt!", ILLamaLogger.LogLevel.Info);
- }
- else if (_n_matching_session_tokens < _embed_inps.Count / 2)
- {
- _logger?.Log("LLamaExecutor", $"session file has low similarity to prompt ({_n_matching_session_tokens}" +
- $" / {_embed_inps.Count} tokens); will mostly be reevaluated", ILLamaLogger.LogLevel.Warning);
- }
- else
- {
- _logger?.Log("LLamaExecutor", $"Session file matches {_n_matching_session_tokens} / " +
- $"{_embed_inps.Count} tokens of prompt", ILLamaLogger.LogLevel.Info);
- }
- }
-
- return this;
- }
-
- /// <summary>
- /// This API has not been verified currently.
- /// </summary>
- /// <param name="filename"></param>
- public void SaveSessionFile(string filename)
- {
- var session_token_array = _session_tokens.ToArray();
- NativeApi.llama_save_session_file(_model.NativeHandle, filename, session_token_array, (ulong)session_token_array.Length);
- }
-
- /// <summary>
- /// After running out of the context, take some tokens from the original prompt and recompute the logits in batches.
- /// </summary>
- /// <param name="tokensToKeep"></param>
- protected virtual void HandleRunOutOfContext(int tokensToKeep)
- {
- // if we run out of context:
- // - take the tokensToKeep first tokens from the original prompt (via n_past)
- // - take half of the last (n_ctx - tokensToKeep) tokens and recompute the logits in batches
- int n_left = _pastTokensCount - tokensToKeep;
-
- _pastTokensCount = Math.Max(1, tokensToKeep);
-
- // insert n_left/2 tokens at the start of embed from last_n_tokens
- _embeds.InsertRange(0, _last_n_tokens.Take(_last_n_tokens.Count - _embeds.Count).Skip(_model.ContextSize - n_left / 2 - _embeds.Count));
-
- // stop saving session if we run out of context
- _pathSession = string.Empty;
- }
-
- /// <summary>
- /// Try to reuse the matching prefix from the session file.
- /// </summary>
- protected virtual void TryReuseMathingPrefix()
- {
- if (_n_session_consumed < _session_tokens.Count)
- {
- int i = 0;
- for (; i < _embeds.Count; i++)
- {
- if (_embeds[i] != _session_tokens[_n_session_consumed])
- {
- _session_tokens = _session_tokens.Take(_n_session_consumed).ToList();
- break;
- }
-
- _pastTokensCount++;
- _n_session_consumed++;
-
- if (_n_session_consumed >= _session_tokens.Count)
- {
- i++;
- break;
- }
- }
-
- if (i > 0)
- {
- _embeds.RemoveRange(0, i);
- }
- }
- }
-
- /// <summary>
- /// Decide whether to continue the loop.
- /// </summary>
- /// <param name="args"></param>
- /// <returns></returns>
- protected abstract bool GetLoopCondition(InferStateArgs args);
- /// <summary>
- /// Preprocess the inputs before the inference.
- /// </summary>
- /// <param name="text"></param>
- /// <param name="args"></param>
- protected abstract void PreprocessInputs(string text, InferStateArgs args);
- /// <summary>
- /// Do some post processing after the inference.
- /// </summary>
- /// <param name="inferenceParams"></param>
- /// <param name="args"></param>
- /// <param name="extraOutputs"></param>
- /// <returns></returns>
- protected abstract bool PostProcess(IInferenceParams inferenceParams, InferStateArgs args, out IEnumerable<string>? extraOutputs);
- /// <summary>
- /// The core inference logic.
- /// </summary>
- /// <param name="inferenceParams"></param>
- /// <param name="args"></param>
- protected abstract void InferInternal(IInferenceParams inferenceParams, InferStateArgs args);
- /// <summary>
- /// Save the current state to a file.
- /// </summary>
- /// <param name="filename"></param>
- public abstract void SaveState(string filename);
- /// <summary>
- /// Get the current state data.
- /// </summary>
- /// <returns></returns>
- public abstract ExecutorBaseState GetStateData();
- /// <summary>
- /// Load the state from data.
- /// </summary>
- /// <param name="data"></param>
- public abstract void LoadState(ExecutorBaseState data);
- /// <summary>
- /// Load the state from a file.
- /// </summary>
- /// <param name="filename"></param>
- public abstract void LoadState(string filename);
-
-
- /// <summary>
- /// Execute the inference.
- /// </summary>
- /// <param name="text"></param>
- /// <param name="inferenceParams"></param>
- /// <param name="cancellationToken"></param>
- /// <returns></returns>
- public virtual IEnumerable<string> Infer(string text, IInferenceParams? inferenceParams = null, CancellationToken cancellationToken = default)
- {
- cancellationToken.ThrowIfCancellationRequested();
- if (inferenceParams is null)
- {
- inferenceParams = new InferenceParams();
- }
-
- InferStateArgs args = new InferStateArgs()
- {
- Antiprompts = inferenceParams.AntiPrompts.ToList(),
- RemainedTokens = inferenceParams.MaxTokens,
- ReturnValue = false,
- WaitForInput = false,
- NeedToSaveSession = !string.IsNullOrEmpty(_pathSession) && _n_matching_session_tokens < _embed_inps.Count
- };
-
- PreprocessInputs(text, args);
-
- while (GetLoopCondition(args))
- {
- if (cancellationToken.IsCancellationRequested)
- {
- break;
- }
- InferInternal(inferenceParams, args);
-
- if (args.ReturnValue)
- {
- foreach (var item in _model.GenerateResult(_embeds))
- {
- yield return item;
- }
- }
-
- var breakGeneration = PostProcess(inferenceParams, args, out var extraOutputs);
- if (extraOutputs is not null)
- {
- foreach (var item in extraOutputs)
- {
- yield return item;
- }
- }
- if (breakGeneration)
- {
- break;
- }
- }
- }
-
- /// <summary>
- /// Execute the inference asynchronously.
- /// </summary>
- /// <param name="text"></param>
- /// <param name="inferenceParams"></param>
- /// <param name="cancellationToken"></param>
- /// <returns></returns>
- public virtual async IAsyncEnumerable<string> InferAsync(string text, IInferenceParams? inferenceParams = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
- {
- foreach (var result in Infer(text, inferenceParams, cancellationToken))
- {
- yield return result;
- }
- }
-
- /// <summary>
- /// State arguments that are used in single inference
- /// </summary>
- protected class InferStateArgs
- {
- /// <summary>
- ///
- /// </summary>
- public IList<string>? Antiprompts { get; set; }
- /// <summary>
- /// Tokens count remained to be used. (n_remain)
- /// </summary>
- public int RemainedTokens { get; set; }
- /// <summary>
- ///
- /// </summary>
- public bool ReturnValue { get; set; }
- /// <summary>
- ///
- /// </summary>
- public bool WaitForInput { get; set; }
- /// <summary>
- ///
- /// </summary>
- public bool NeedToSaveSession { get; set; }
- }
-
- public class ExecutorBaseState
- {
- [JsonPropertyName("n_past")]
- public int PastTokensCount { get; set; }
-
- [JsonPropertyName("n_consumed")]
- public int ConsumedTokensCount { get; set; }
-
- [JsonPropertyName("n_session_consumed")]
- public int ConsumedSessionCount { get; set; }
-
- [JsonPropertyName("n_matching_session_tokens")]
- public int MatchingSessionTokensCount { get; set; }
-
- [JsonPropertyName("path_session")]
- public string SessionFilePath { get; set; }
-
- [JsonPropertyName("embd")]
- public List<llama_token> Embeds { get; set; }
-
- [JsonPropertyName("embd_inps")]
- public List<llama_token> EmbedInps { get; set; }
-
- [JsonPropertyName("session_tokens")]
- public List<llama_token> SessionTokens { get; set; }
-
- [JsonPropertyName("last_n_tokens")]
- public llama_token[] LastTokens { get; set; }
-
- [JsonPropertyName("last_tokens_maximum_count")]
- public int LastTokensCapacity { get; set; }
-
- [JsonPropertyName("mirostate_mu")]
- public float MirostateMu { get; set; }
- }
- }
- }
|