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.

LLamaBatch.cs 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Collections.Generic;
  3. namespace LLama.Native;
  4. /// <summary>
  5. /// A batch allows submitting multiple tokens to multiple sequences simultaneously
  6. /// </summary>
  7. public class LLamaBatch
  8. {
  9. private byte[] _logits;
  10. private LLamaToken[] _tokens;
  11. private LLamaPos[] _positions;
  12. private int[] _sequenceIdCount;
  13. private LLamaSeqId[][] _sequenceIds;
  14. private IntPtr[] _sequenceIdsPtrs;
  15. /// <summary>
  16. /// The number of tokens in this batch
  17. /// </summary>
  18. public int TokenCount { get; private set; }
  19. /// <summary>
  20. /// Maximum number of tokens that can be added to this batch
  21. /// </summary>
  22. private int TokenCapacity { get; set; }
  23. /// <summary>
  24. /// Maximum number of sequences a token can be assigned to
  25. /// </summary>
  26. public int MaxSequences { get; private set; }
  27. /// <summary>
  28. /// Create a new batch for submitting inputs to llama.cpp
  29. /// </summary>
  30. /// <param name="n_seq_max">Max number of sequences a token can be assigned to</param>
  31. public LLamaBatch(int n_seq_max)
  32. {
  33. // The number of tokens can be grown later, start off with a reasonable guess.
  34. const int n_tokens = 64;
  35. MaxSequences = n_seq_max;
  36. TokenCapacity = n_tokens;
  37. _logits = new byte[n_tokens];
  38. _tokens = new LLamaToken[n_tokens];
  39. _positions = new LLamaPos[n_tokens];
  40. _sequenceIdCount = new int[n_tokens];
  41. _sequenceIdsPtrs = new IntPtr[_sequenceIdCount.Length];
  42. _sequenceIds = new LLamaSeqId[n_tokens][];
  43. for (var i = 0; i < _sequenceIds.Length; i++)
  44. _sequenceIds[i] = new LLamaSeqId[MaxSequences];
  45. }
  46. private void Grow()
  47. {
  48. var n_tokens = TokenCount * 2;
  49. TokenCapacity = n_tokens;
  50. Array.Resize(ref _logits, n_tokens);
  51. Array.Resize(ref _tokens, n_tokens);
  52. Array.Resize(ref _positions, n_tokens);
  53. Array.Resize(ref _sequenceIdCount, n_tokens);
  54. Array.Resize(ref _sequenceIdsPtrs, n_tokens);
  55. Array.Resize(ref _sequenceIds, n_tokens);
  56. for (int i = 0; i < _sequenceIds.Length; i++)
  57. {
  58. // Growing the array filled elements with null, temporarily violating the nullability contract!
  59. // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
  60. if (_sequenceIds[i] == null)
  61. _sequenceIds[i] = new LLamaSeqId[MaxSequences];
  62. }
  63. }
  64. internal GroupDisposable ToNativeBatch(out LLamaNativeBatch batch)
  65. {
  66. // This group holds all of the memory pins
  67. var group = new GroupDisposable();
  68. unsafe
  69. {
  70. batch = new LLamaNativeBatch
  71. {
  72. n_tokens = TokenCount,
  73. logits = (byte*)group.Add(_logits.AsMemory().Pin()).Pointer,
  74. n_seq_id = (int*)group.Add(_sequenceIdCount.AsMemory().Pin()).Pointer,
  75. pos = (LLamaPos*)group.Add(_positions.AsMemory().Pin()).Pointer,
  76. seq_id = (LLamaSeqId**)group.Add(_sequenceIdsPtrs.AsMemory().Pin()).Pointer,
  77. // embd is not currently supported, so this is always null!
  78. embd = null,
  79. // Note that if embd is **not null** then this will be null!
  80. tokens = (LLamaToken*)group.Add(_tokens.AsMemory().Pin()).Pointer,
  81. };
  82. // Create pointers to each of the arrays in turns
  83. for (var i = 0; i < _sequenceIdsPtrs.Length; i++)
  84. _sequenceIdsPtrs[i] = (IntPtr)group.Add(_sequenceIds[i].AsMemory().Pin()).Pointer;
  85. }
  86. return group;
  87. }
  88. /// <summary>
  89. /// Add a single token to the batch at the same position in several sequences
  90. /// </summary>
  91. /// <remarks>https://github.com/ggerganov/llama.cpp/blob/ad939626577cd25b462e8026cc543efb71528472/common/common.cpp#L829C2-L829C2</remarks>
  92. /// <param name="token">The token to add</param>
  93. /// <param name="pos">The position to add it att</param>
  94. /// <param name="sequences">The set of sequences to add this token to</param>
  95. /// <param name="logits"></param>
  96. public void Add(LLamaToken token, LLamaPos pos, ReadOnlySpan<LLamaSeqId> sequences, bool logits)
  97. {
  98. if (TokenCount == TokenCapacity)
  99. Grow();
  100. _tokens[TokenCount] = token;
  101. _positions[TokenCount] = pos;
  102. _sequenceIdCount[TokenCount] = sequences.Length;
  103. for (var i = 0; i < sequences.Length; i++)
  104. _sequenceIds[TokenCount][i] = sequences[i];
  105. _logits[TokenCount] = Convert.ToByte(logits);
  106. TokenCount++;
  107. }
  108. /// <summary>
  109. /// Add a single token to the batch at a certain position for a single sequences
  110. /// </summary>
  111. /// <remarks>https://github.com/ggerganov/llama.cpp/blob/ad939626577cd25b462e8026cc543efb71528472/common/common.cpp#L829C2-L829C2</remarks>
  112. /// <param name="token">The token to add</param>
  113. /// <param name="pos">The position to add it att</param>
  114. /// <param name="sequence">The sequence to add this token to</param>
  115. /// <param name="logits"></param>
  116. public void Add(LLamaToken token, LLamaPos pos, LLamaSeqId sequence, bool logits)
  117. {
  118. // Create a temporary span to contain 1 item without allocating
  119. Span<LLamaSeqId> sequences = stackalloc LLamaSeqId[1];
  120. sequences[0] = sequence;
  121. // Add it
  122. Add(token, pos, sequences, logits);
  123. }
  124. /// <summary>
  125. /// Set TokenCount to zero for this batch
  126. /// </summary>
  127. public void Clear()
  128. {
  129. TokenCount = 0;
  130. }
  131. }