You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

LLamaInteractExecutor.cs 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using LLama.Common;
  2. using LLama.Native;
  3. using LLama.Abstractions;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text.Json;
  9. using System.Text.Json.Serialization;
  10. namespace LLama
  11. {
  12. using llama_token = Int32;
  13. /// <summary>
  14. /// The LLama executor for interactive mode.
  15. /// </summary>
  16. public class InteractiveExecutor : StatefulExecutorBase
  17. {
  18. bool _is_prompt_run = true;
  19. llama_token[] _llama_token_newline;
  20. /// <summary>
  21. ///
  22. /// </summary>
  23. /// <param name="context"></param>
  24. public InteractiveExecutor(LLamaContext context) : base(context)
  25. {
  26. _llama_token_newline = Context.NativeHandle.Tokenize("\n", false, Context.Encoding);
  27. }
  28. /// <inheritdoc />
  29. public override ExecutorBaseState GetStateData()
  30. {
  31. InteractiveExecutorState state = new()
  32. {
  33. ConsumedSessionCount = _n_session_consumed,
  34. EmbedInps = _embed_inps,
  35. IsPromptRun = _is_prompt_run,
  36. ConsumedTokensCount = _consumedTokensCount,
  37. Embeds = _embeds,
  38. LastTokens = _last_n_tokens.ToArray(),
  39. LLamaNewlineTokens = _llama_token_newline,
  40. MatchingSessionTokensCount = _n_matching_session_tokens,
  41. PastTokensCount = _pastTokensCount,
  42. SessionFilePath = _pathSession,
  43. SessionTokens = _session_tokens,
  44. LastTokensCapacity = _last_n_tokens.Capacity,
  45. MirostatMu = MirostatMu
  46. };
  47. return state;
  48. }
  49. /// <inheritdoc />
  50. public override void LoadState(ExecutorBaseState data)
  51. {
  52. if (data is InteractiveExecutorState state)
  53. {
  54. _n_session_consumed = state.ConsumedSessionCount;
  55. _embed_inps = state.EmbedInps;
  56. _is_prompt_run = state.IsPromptRun;
  57. _consumedTokensCount = state.ConsumedTokensCount;
  58. _embeds = state.Embeds;
  59. _last_n_tokens = new FixedSizeQueue<llama_token>(state.LastTokensCapacity, state.LastTokens);
  60. _llama_token_newline = state.LLamaNewlineTokens;
  61. _n_matching_session_tokens = state.MatchingSessionTokensCount;
  62. _pastTokensCount = state.PastTokensCount;
  63. _pathSession = state.SessionFilePath;
  64. _session_tokens = state.SessionTokens;
  65. }
  66. else
  67. throw new ArgumentException("Invalid state data type.");
  68. }
  69. /// <inheritdoc />
  70. public override void SaveState(string filename)
  71. {
  72. InteractiveExecutorState state = (InteractiveExecutorState)GetStateData();
  73. using(FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write))
  74. {
  75. JsonSerializer.Serialize(fs, state);
  76. }
  77. }
  78. /// <inheritdoc />
  79. public override void LoadState(string filename)
  80. {
  81. using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
  82. {
  83. var state = JsonSerializer.Deserialize<InteractiveExecutorState>(fs);
  84. LoadState(state);
  85. }
  86. }
  87. /// <summary>
  88. /// Define whether to continue the loop to generate responses.
  89. /// </summary>
  90. /// <returns></returns>
  91. protected override bool GetLoopCondition(InferStateArgs args)
  92. {
  93. return args.RemainedTokens != 0 && !args.WaitForInput || _is_prompt_run;
  94. }
  95. /// <inheritdoc />
  96. protected override void PreprocessInputs(string text, InferStateArgs args)
  97. {
  98. if (_is_prompt_run)
  99. {
  100. // When running the first input (prompt) in inteactive mode, we should specially process it.
  101. text = " " + text;
  102. _embed_inps = Context.Tokenize(text, true).ToList();
  103. }
  104. else
  105. {
  106. if (!text.EndsWith("\n"))
  107. {
  108. text += "\n";
  109. }
  110. var line_inp = Context.Tokenize(text, false);
  111. _embed_inps.AddRange(line_inp);
  112. args.RemainedTokens -= line_inp.Length;
  113. }
  114. }
  115. /// <summary>
  116. /// Return whether to break the generation.
  117. /// </summary>
  118. /// <param name="inferenceParams"></param>
  119. /// <param name="args"></param>
  120. /// <param name="extraOutputs"></param>
  121. /// <returns></returns>
  122. protected override bool PostProcess(IInferenceParams inferenceParams, InferStateArgs args, out IEnumerable<string>? extraOutputs)
  123. {
  124. extraOutputs = null;
  125. if (_embed_inps.Count <= _consumedTokensCount)
  126. {
  127. if (args.Antiprompts is not null && args.Antiprompts.Count > 0)
  128. {
  129. string last_output = "";
  130. foreach (var id in _last_n_tokens)
  131. {
  132. last_output += Context.NativeHandle.TokenToString(id, Context.Encoding);
  133. }
  134. foreach (var antiprompt in args.Antiprompts)
  135. {
  136. if (last_output.EndsWith(antiprompt))
  137. {
  138. args.WaitForInput = true;
  139. break;
  140. }
  141. }
  142. }
  143. if (_pastTokensCount > 0 && args.WaitForInput)
  144. {
  145. return true;
  146. }
  147. }
  148. if (_embeds.Count > 0 && _embeds.Last() == NativeApi.llama_token_eos())
  149. {
  150. extraOutputs = new[] { " [end of text]\n" };
  151. return true;
  152. }
  153. if (args.RemainedTokens <= 0 && inferenceParams.MaxTokens != -1)
  154. {
  155. args.RemainedTokens = inferenceParams.MaxTokens;
  156. args.WaitForInput = true;
  157. }
  158. return false;
  159. }
  160. /// <inheritdoc />
  161. protected override void InferInternal(IInferenceParams inferenceParams, InferStateArgs args)
  162. {
  163. if (_embeds.Count > 0)
  164. {
  165. _is_prompt_run = false;
  166. if (_pastTokensCount + _embeds.Count > Context.ContextSize)
  167. {
  168. HandleRunOutOfContext(inferenceParams.TokensKeep);
  169. }
  170. TryReuseMathingPrefix();
  171. _pastTokensCount = Context.Eval(_embeds, _pastTokensCount);
  172. if (_embeds.Count > 0 && !string.IsNullOrEmpty(_pathSession))
  173. {
  174. _session_tokens.AddRange(_embeds);
  175. _n_session_consumed = _session_tokens.Count;
  176. }
  177. }
  178. _embeds.Clear();
  179. if (_embed_inps.Count <= _consumedTokensCount && !args.WaitForInput)
  180. {
  181. var repeat_last_n = inferenceParams.RepeatLastTokensCount < 0 ? Context.ContextSize : inferenceParams.RepeatLastTokensCount;
  182. // optionally save the session on first sample (for faster prompt loading next time)
  183. if (!string.IsNullOrEmpty(_pathSession) && args.NeedToSaveSession)
  184. {
  185. args.NeedToSaveSession = false;
  186. SaveSessionFile(_pathSession);
  187. }
  188. var tokenDataArray = Context.ApplyPenalty(_last_n_tokens, inferenceParams.LogitBias, repeat_last_n,
  189. inferenceParams.RepeatPenalty, inferenceParams.FrequencyPenalty, inferenceParams.PresencePenalty, inferenceParams.PenalizeNL);
  190. var mu = MirostatMu;
  191. var id = Context.Sample(
  192. tokenDataArray, ref mu, inferenceParams.Temperature, inferenceParams.Mirostat, inferenceParams.MirostatTau,
  193. inferenceParams.MirostatEta, inferenceParams.TopK, inferenceParams.TopP, inferenceParams.TfsZ, inferenceParams.TypicalP,
  194. inferenceParams.Grammar
  195. );
  196. MirostatMu = mu;
  197. _last_n_tokens.Enqueue(id);
  198. if (id == NativeApi.llama_token_eos())
  199. {
  200. id = _llama_token_newline.First();
  201. if (args.Antiprompts is not null && args.Antiprompts.Count > 0)
  202. {
  203. var first_antiprompt = Context.Tokenize(args.Antiprompts[0], false);
  204. _embed_inps.AddRange(first_antiprompt);
  205. }
  206. }
  207. _embeds.Add(id);
  208. args.RemainedTokens--;
  209. args.ReturnValue = true;
  210. }
  211. else
  212. {
  213. while (_embed_inps.Count > _consumedTokensCount)
  214. {
  215. _embeds.Add(_embed_inps[_consumedTokensCount]);
  216. _last_n_tokens.Enqueue(_embed_inps[_consumedTokensCount]);
  217. _consumedTokensCount++;
  218. if (_embeds.Count >= Context.Params.BatchSize)
  219. {
  220. break;
  221. }
  222. }
  223. }
  224. }
  225. /// <summary>
  226. /// The descriptor of the state of the interactive executor.
  227. /// </summary>
  228. public class InteractiveExecutorState : ExecutorBaseState
  229. {
  230. /// <summary>
  231. /// Whether the executor is running for the first time (running the prompt).
  232. /// </summary>
  233. [JsonPropertyName("is_prompt_run")]
  234. public bool IsPromptRun { get; set; }
  235. /// <summary>
  236. /// Tokens that represent a new line in with the current model.
  237. /// </summary>
  238. [JsonPropertyName("llama_token_newline")]
  239. public llama_token[] LLamaNewlineTokens { get; set; }
  240. }
  241. }
  242. }