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.

BatchedExecutorFork.cs 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using LLama.Batched;
  2. using LLama.Common;
  3. using LLama.Native;
  4. using LLama.Sampling;
  5. using Spectre.Console;
  6. namespace LLama.Examples.Examples;
  7. /// <summary>
  8. /// This demonstrates generating multiple replies to the same prompt, with a shared cache
  9. /// </summary>
  10. public class BatchedExecutorFork
  11. {
  12. private const int n_split = 16;
  13. private const int n_len = 64;
  14. public static async Task Run()
  15. {
  16. string modelPath = UserSettings.GetModelPath();
  17. var parameters = new ModelParams(modelPath);
  18. using var model = LLamaWeights.LoadFromFile(parameters);
  19. var prompt = AnsiConsole.Ask("Prompt (or ENTER for default):", "Not many people know that");
  20. // Create an executor that can evaluate a batch of conversations together
  21. var executor = new BatchedExecutor(model, parameters);
  22. // Print some info
  23. var name = executor.Model.Metadata.GetValueOrDefault("general.name", "unknown model name");
  24. Console.WriteLine($"Created executor with model: {name}");
  25. // Evaluate the initial prompt to create one conversation
  26. var start = executor.Prompt(prompt);
  27. await executor.Infer();
  28. // Create the root node of the tree
  29. var root = new Node(start);
  30. // Run inference loop
  31. for (var i = 0; i < n_len; i++)
  32. {
  33. if (i != 0)
  34. await executor.Infer();
  35. // Occasionally fork all the active conversations
  36. if (i != 0 && i % n_split == 0)
  37. root.Split();
  38. // Sample all active conversations
  39. root.Sample();
  40. }
  41. Console.WriteLine($"{prompt}...");
  42. root.Print(1);
  43. Console.WriteLine("Press any key to exit demo");
  44. Console.ReadKey(true);
  45. }
  46. class Node
  47. {
  48. private readonly StreamingTokenDecoder _decoder;
  49. private readonly DefaultSamplingPipeline _sampler;
  50. private Conversation? _conversation;
  51. private Node? _left;
  52. private Node? _right;
  53. public int ActiveConversationCount => _conversation != null ? 1 : _left!.ActiveConversationCount + _right!.ActiveConversationCount;
  54. public Node(Conversation conversation)
  55. {
  56. _sampler = new DefaultSamplingPipeline();
  57. _conversation = conversation;
  58. _decoder = new StreamingTokenDecoder(conversation.Executor.Context);
  59. }
  60. public void Sample()
  61. {
  62. if (_conversation == null)
  63. {
  64. _left?.Sample();
  65. _right?.Sample();
  66. return;
  67. }
  68. if (_conversation.RequiresInference)
  69. return;
  70. // Sample one token
  71. var ctx = _conversation.Executor.Context.NativeHandle;
  72. var token = _sampler.Sample(ctx, _conversation.Sample(), Array.Empty<LLamaToken>());
  73. _sampler.Accept(ctx, token);
  74. _decoder.Add(token);
  75. // Prompt the conversation with this token, to continue generating from there
  76. _conversation.Prompt(token);
  77. }
  78. public void Split()
  79. {
  80. if (_conversation != null)
  81. {
  82. _left = new Node(_conversation.Fork());
  83. _right = new Node(_conversation.Fork());
  84. _conversation.Dispose();
  85. _conversation = null;
  86. }
  87. else
  88. {
  89. _left?.Split();
  90. _right?.Split();
  91. }
  92. }
  93. public void Print(int indendation)
  94. {
  95. var colors = new[] { ConsoleColor.Red, ConsoleColor.Green, ConsoleColor.Blue, ConsoleColor.Yellow, ConsoleColor.White };
  96. Console.ForegroundColor = colors[indendation % colors.Length];
  97. var message = _decoder.Read().ReplaceLineEndings("");
  98. var prefix = new string(' ', indendation * 3);
  99. var suffix = _conversation == null ? "..." : "";
  100. Console.WriteLine($"{prefix}...{message}{suffix}");
  101. _left?.Print(indendation + 2);
  102. _right?.Print(indendation + 2);
  103. }
  104. }
  105. }