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.

llama.native.llamatokendataarray.md 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. # LLamaTokenDataArray
  2. Namespace: LLama.Native
  3. Contains an array of LLamaTokenData, potentially sorted.
  4. ```csharp
  5. public struct LLamaTokenDataArray
  6. ```
  7. Inheritance [Object](https://docs.microsoft.com/en-us/dotnet/api/system.object) → [ValueType](https://docs.microsoft.com/en-us/dotnet/api/system.valuetype) → [LLamaTokenDataArray](./llama.native.llamatokendataarray.md)
  8. ## Fields
  9. ### **data**
  10. The LLamaTokenData
  11. ```csharp
  12. public Memory<LLamaTokenData> data;
  13. ```
  14. ### **sorted**
  15. Indicates if `data` is sorted by logits in descending order. If this is false the token data is in _no particular order_.
  16. ```csharp
  17. public bool sorted;
  18. ```
  19. ## Constructors
  20. ### **LLamaTokenDataArray(Memory&lt;LLamaTokenData&gt;, Boolean)**
  21. Create a new LLamaTokenDataArray
  22. ```csharp
  23. LLamaTokenDataArray(Memory<LLamaTokenData> tokens, bool isSorted)
  24. ```
  25. #### Parameters
  26. `tokens` [Memory&lt;LLamaTokenData&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.memory-1)<br>
  27. `isSorted` [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)<br>
  28. ## Methods
  29. ### **Create(ReadOnlySpan&lt;Single&gt;)**
  30. Create a new LLamaTokenDataArray, copying the data from the given logits
  31. ```csharp
  32. LLamaTokenDataArray Create(ReadOnlySpan<float> logits)
  33. ```
  34. #### Parameters
  35. `logits` [ReadOnlySpan&lt;Single&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.readonlyspan-1)<br>
  36. #### Returns
  37. [LLamaTokenDataArray](./llama.native.llamatokendataarray.md)<br>
  38. ### **OverwriteLogits(ReadOnlySpan&lt;ValueTuple&lt;LLamaToken, Single&gt;&gt;)**
  39. Overwrite the logit values for all given tokens
  40. ```csharp
  41. void OverwriteLogits(ReadOnlySpan<ValueTuple<LLamaToken, float>> values)
  42. ```
  43. #### Parameters
  44. `values` [ReadOnlySpan&lt;ValueTuple&lt;LLamaToken, Single&gt;&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.readonlyspan-1)<br>
  45. tuples of token and logit value to overwrite
  46. ### **ApplyGrammar(SafeLLamaContextHandle, SafeLLamaGrammarHandle)**
  47. Apply grammar rules to candidate tokens
  48. ```csharp
  49. void ApplyGrammar(SafeLLamaContextHandle ctx, SafeLLamaGrammarHandle grammar)
  50. ```
  51. #### Parameters
  52. `ctx` [SafeLLamaContextHandle](./llama.native.safellamacontexthandle.md)<br>
  53. `grammar` [SafeLLamaGrammarHandle](./llama.native.safellamagrammarhandle.md)<br>
  54. ### **TopK(SafeLLamaContextHandle, Int32, UInt64)**
  55. Top-K sampling described in academic paper "The Curious Case of Neural Text Degeneration" https://arxiv.org/abs/1904.09751
  56. ```csharp
  57. void TopK(SafeLLamaContextHandle context, int k, ulong minKeep)
  58. ```
  59. #### Parameters
  60. `context` [SafeLLamaContextHandle](./llama.native.safellamacontexthandle.md)<br>
  61. `k` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  62. Number of tokens to keep
  63. `minKeep` [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)<br>
  64. Minimum number to keep
  65. ### **TopP(SafeLLamaContextHandle, Single, UInt64)**
  66. Nucleus sampling described in academic paper "The Curious Case of Neural Text Degeneration" https://arxiv.org/abs/1904.09751
  67. ```csharp
  68. void TopP(SafeLLamaContextHandle context, float p, ulong minKeep)
  69. ```
  70. #### Parameters
  71. `context` [SafeLLamaContextHandle](./llama.native.safellamacontexthandle.md)<br>
  72. `p` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  73. `minKeep` [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)<br>
  74. ### **MinP(SafeLLamaContextHandle, Single, UInt64)**
  75. Minimum P sampling as described in https://github.com/ggerganov/llama.cpp/pull/3841
  76. ```csharp
  77. void MinP(SafeLLamaContextHandle context, float p, ulong minKeep)
  78. ```
  79. #### Parameters
  80. `context` [SafeLLamaContextHandle](./llama.native.safellamacontexthandle.md)<br>
  81. `p` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  82. All tokens with probability greater than this will be kept
  83. `minKeep` [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)<br>
  84. ### **TailFree(SafeLLamaContextHandle, Single, UInt64)**
  85. Tail Free Sampling described in https://www.trentonbricken.com/Tail-Free-Sampling/.
  86. ```csharp
  87. void TailFree(SafeLLamaContextHandle context, float z, ulong min_keep)
  88. ```
  89. #### Parameters
  90. `context` [SafeLLamaContextHandle](./llama.native.safellamacontexthandle.md)<br>
  91. `z` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  92. `min_keep` [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)<br>
  93. ### **LocallyTypical(SafeLLamaContextHandle, Single, UInt64)**
  94. Locally Typical Sampling implementation described in the paper https://arxiv.org/abs/2202.00666.
  95. ```csharp
  96. void LocallyTypical(SafeLLamaContextHandle context, float p, ulong min_keep)
  97. ```
  98. #### Parameters
  99. `context` [SafeLLamaContextHandle](./llama.native.safellamacontexthandle.md)<br>
  100. `p` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  101. `min_keep` [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)<br>
  102. ### **RepetitionPenalty(SafeLLamaContextHandle, ReadOnlySpan&lt;LLamaToken&gt;, Single, Single, Single)**
  103. Repetition penalty described in CTRL academic paper https://arxiv.org/abs/1909.05858, with negative logit fix.
  104. Frequency and presence penalties described in OpenAI API https://platform.openai.com/docs/api-reference/parameter-details.
  105. ```csharp
  106. void RepetitionPenalty(SafeLLamaContextHandle context, ReadOnlySpan<LLamaToken> last_tokens, float penalty_repeat, float penalty_freq, float penalty_present)
  107. ```
  108. #### Parameters
  109. `context` [SafeLLamaContextHandle](./llama.native.safellamacontexthandle.md)<br>
  110. `last_tokens` [ReadOnlySpan&lt;LLamaToken&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.readonlyspan-1)<br>
  111. `penalty_repeat` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  112. `penalty_freq` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  113. `penalty_present` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  114. ### **Guidance(SafeLLamaContextHandle, ReadOnlySpan&lt;Single&gt;, Single)**
  115. Apply classifier-free guidance to the logits as described in academic paper "Stay on topic with Classifier-Free Guidance" https://arxiv.org/abs/2306.17806
  116. ```csharp
  117. void Guidance(SafeLLamaContextHandle context, ReadOnlySpan<float> guidanceLogits, float guidance)
  118. ```
  119. #### Parameters
  120. `context` [SafeLLamaContextHandle](./llama.native.safellamacontexthandle.md)<br>
  121. `guidanceLogits` [ReadOnlySpan&lt;Single&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.readonlyspan-1)<br>
  122. Logits extracted from a separate context from the same model.
  123. Other than a negative prompt at the beginning, it should have all generated and user input tokens copied from the main context.
  124. `guidance` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  125. Guidance strength. 0 means no guidance, higher values applies stronger guidance
  126. ### **Temperature(SafeLLamaContextHandle, Single)**
  127. Sample with temperature.
  128. As temperature increases, the prediction becomes more diverse but also vulnerable to hallucinations -- generating tokens that are sensible but not factual
  129. ```csharp
  130. void Temperature(SafeLLamaContextHandle context, float temp)
  131. ```
  132. #### Parameters
  133. `context` [SafeLLamaContextHandle](./llama.native.safellamacontexthandle.md)<br>
  134. `temp` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  135. ### **Softmax(SafeLLamaContextHandle)**
  136. Sorts candidate tokens by their logits in descending order and calculate probabilities based on logits.
  137. ```csharp
  138. void Softmax(SafeLLamaContextHandle context)
  139. ```
  140. #### Parameters
  141. `context` [SafeLLamaContextHandle](./llama.native.safellamacontexthandle.md)<br>
  142. ### **SampleToken(SafeLLamaContextHandle)**
  143. Randomly selects a token from the candidates based on their probabilities.
  144. ```csharp
  145. LLamaToken SampleToken(SafeLLamaContextHandle context)
  146. ```
  147. #### Parameters
  148. `context` [SafeLLamaContextHandle](./llama.native.safellamacontexthandle.md)<br>
  149. #### Returns
  150. [LLamaToken](./llama.native.llamatoken.md)<br>
  151. ### **SampleTokenGreedy(SafeLLamaContextHandle)**
  152. Selects the token with the highest probability.
  153. ```csharp
  154. LLamaToken SampleTokenGreedy(SafeLLamaContextHandle context)
  155. ```
  156. #### Parameters
  157. `context` [SafeLLamaContextHandle](./llama.native.safellamacontexthandle.md)<br>
  158. #### Returns
  159. [LLamaToken](./llama.native.llamatoken.md)<br>
  160. ### **SampleTokenMirostat(SafeLLamaContextHandle, Single, Single, Int32, Single&)**
  161. Mirostat 1.0 algorithm described in the paper https://arxiv.org/abs/2007.14966. Uses tokens instead of words.
  162. ```csharp
  163. LLamaToken SampleTokenMirostat(SafeLLamaContextHandle context, float tau, float eta, int m, Single& mu)
  164. ```
  165. #### Parameters
  166. `context` [SafeLLamaContextHandle](./llama.native.safellamacontexthandle.md)<br>
  167. `tau` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  168. The target cross-entropy (or surprise) value you want to achieve for the generated text. A higher value corresponds to more surprising or less predictable text, while a lower value corresponds to less surprising or more predictable text.
  169. `eta` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  170. The learning rate used to update `mu` based on the error between the target and observed surprisal of the sampled word. A larger learning rate will cause `mu` to be updated more quickly, while a smaller learning rate will result in slower updates.
  171. `m` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  172. The number of tokens considered in the estimation of `s_hat`. This is an arbitrary value that is used to calculate `s_hat`, which in turn helps to calculate the value of `k`. In the paper, they use `m = 100`, but you can experiment with different values to see how it affects the performance of the algorithm.
  173. `mu` [Single&](https://docs.microsoft.com/en-us/dotnet/api/system.single&)<br>
  174. Maximum cross-entropy. This value is initialized to be twice the target cross-entropy (`2 * tau`) and is updated in the algorithm based on the error between the target and observed surprisal.
  175. #### Returns
  176. [LLamaToken](./llama.native.llamatoken.md)<br>
  177. ### **SampleTokenMirostat2(SafeLLamaContextHandle, Single, Single, Single&)**
  178. Mirostat 2.0 algorithm described in the paper https://arxiv.org/abs/2007.14966. Uses tokens instead of words.
  179. ```csharp
  180. LLamaToken SampleTokenMirostat2(SafeLLamaContextHandle context, float tau, float eta, Single& mu)
  181. ```
  182. #### Parameters
  183. `context` [SafeLLamaContextHandle](./llama.native.safellamacontexthandle.md)<br>
  184. `tau` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  185. The target cross-entropy (or surprise) value you want to achieve for the generated text. A higher value corresponds to more surprising or less predictable text, while a lower value corresponds to less surprising or more predictable text.
  186. `eta` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  187. The learning rate used to update `mu` based on the error between the target and observed surprisal of the sampled word. A larger learning rate will cause `mu` to be updated more quickly, while a smaller learning rate will result in slower updates.
  188. `mu` [Single&](https://docs.microsoft.com/en-us/dotnet/api/system.single&)<br>
  189. Maximum cross-entropy. This value is initialized to be twice the target cross-entropy (`2 * tau`) and is updated in the algorithm based on the error between the target and observed surprisal.
  190. #### Returns
  191. [LLamaToken](./llama.native.llamatoken.md)<br>