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.

SamplingApi.cs 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System;
  2. namespace LLama.Native
  3. {
  4. using llama_token = Int32;
  5. public unsafe class SamplingApi
  6. {
  7. /// <summary>
  8. /// Apply grammar rules to candidate tokens
  9. /// </summary>
  10. /// <param name="ctx"></param>
  11. /// <param name="candidates"></param>
  12. /// <param name="grammar"></param>
  13. public static void llama_sample_grammar(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, SafeLLamaGrammarHandle grammar)
  14. {
  15. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  16. NativeApi.llama_sample_grammar(ctx, ref st, grammar);
  17. }
  18. /// <summary>
  19. /// Repetition penalty described in CTRL academic paper https://arxiv.org/abs/1909.05858, with negative logit fix.
  20. /// </summary>
  21. /// <param name="ctx"></param>
  22. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  23. /// <param name="last_tokens"></param>
  24. /// <param name="last_tokens_size"></param>
  25. /// <param name="penalty"></param>
  26. [Obsolete("last_tokens_size parameter is no longer needed")]
  27. public static void llama_sample_repetition_penalty(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, Memory<llama_token> last_tokens, ulong last_tokens_size, float penalty)
  28. {
  29. llama_sample_repetition_penalty(ctx, candidates, last_tokens, penalty);
  30. }
  31. /// <summary>
  32. /// Repetition penalty described in CTRL academic paper https://arxiv.org/abs/1909.05858, with negative logit fix.
  33. /// </summary>
  34. /// <param name="ctx"></param>
  35. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  36. /// <param name="last_tokens"></param>
  37. /// <param name="penalty"></param>
  38. public static void llama_sample_repetition_penalty(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, Memory<llama_token> last_tokens, float penalty)
  39. {
  40. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  41. using var last_tokens_handle = last_tokens.Pin();
  42. NativeApi.llama_sample_repetition_penalty(ctx, ref st, (int*)last_tokens_handle.Pointer, (ulong)last_tokens.Length, penalty);
  43. }
  44. /// <summary>
  45. /// Frequency and presence penalties described in OpenAI API https://platform.openai.com/docs/api-reference/parameter-details.
  46. /// </summary>
  47. /// <param name="ctx"></param>
  48. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  49. /// <param name="last_tokens"></param>
  50. /// <param name="last_tokens_size"></param>
  51. /// <param name="alpha_frequency"></param>
  52. /// <param name="alpha_presence"></param>
  53. [Obsolete("last_tokens_size parameter is no longer needed")]
  54. public static void llama_sample_frequency_and_presence_penalties(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, Memory<llama_token> last_tokens, ulong last_tokens_size, float alpha_frequency, float alpha_presence)
  55. {
  56. llama_sample_frequency_and_presence_penalties(ctx, candidates, last_tokens, alpha_frequency, alpha_presence);
  57. }
  58. /// <summary>
  59. /// Frequency and presence penalties described in OpenAI API https://platform.openai.com/docs/api-reference/parameter-details.
  60. /// </summary>
  61. /// <param name="ctx"></param>
  62. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  63. /// <param name="last_tokens"></param>
  64. /// <param name="alpha_frequency"></param>
  65. /// <param name="alpha_presence"></param>
  66. public static void llama_sample_frequency_and_presence_penalties(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, Memory<llama_token> last_tokens, float alpha_frequency, float alpha_presence)
  67. {
  68. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  69. using var last_tokens_handle = last_tokens.Pin();
  70. NativeApi.llama_sample_frequency_and_presence_penalties(ctx, ref st, (int*)last_tokens_handle.Pointer, (ulong)last_tokens.Length, alpha_frequency, alpha_presence);
  71. }
  72. /// <summary>
  73. /// Sorts candidate tokens by their logits in descending order and calculate probabilities based on logits.
  74. /// </summary>
  75. /// <param name="ctx"></param>
  76. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  77. public static void llama_sample_softmax(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates)
  78. {
  79. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  80. NativeApi.llama_sample_softmax(ctx, ref st);
  81. }
  82. /// <summary>
  83. /// Top-K sampling described in academic paper "The Curious Case of Neural Text Degeneration" https://arxiv.org/abs/1904.09751
  84. /// </summary>
  85. /// <param name="ctx"></param>
  86. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  87. /// <param name="k"></param>
  88. /// <param name="min_keep"></param>
  89. public static void llama_sample_top_k(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, int k, ulong min_keep)
  90. {
  91. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  92. NativeApi.llama_sample_top_k(ctx, ref st, k, min_keep);
  93. }
  94. /// <summary>
  95. /// Nucleus sampling described in academic paper "The Curious Case of Neural Text Degeneration" https://arxiv.org/abs/1904.09751
  96. /// </summary>
  97. /// <param name="ctx"></param>
  98. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  99. /// <param name="p"></param>
  100. /// <param name="min_keep"></param>
  101. public static void llama_sample_top_p(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, float p, ulong min_keep)
  102. {
  103. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  104. NativeApi.llama_sample_top_p(ctx, ref st, p, min_keep);
  105. }
  106. /// <summary>
  107. /// Tail Free Sampling described in https://www.trentonbricken.com/Tail-Free-Sampling/.
  108. /// </summary>
  109. /// <param name="ctx"></param>
  110. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  111. /// <param name="z"></param>
  112. /// <param name="min_keep"></param>
  113. public static void llama_sample_tail_free(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, float z, ulong min_keep)
  114. {
  115. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  116. NativeApi.llama_sample_tail_free(ctx, ref st, z, min_keep);
  117. }
  118. /// <summary>
  119. /// Locally Typical Sampling implementation described in the paper https://arxiv.org/abs/2202.00666.
  120. /// </summary>
  121. /// <param name="ctx"></param>
  122. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  123. /// <param name="p"></param>
  124. /// <param name="min_keep"></param>
  125. public static void llama_sample_typical(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, float p, ulong min_keep)
  126. {
  127. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  128. NativeApi.llama_sample_typical(ctx, ref st, p, min_keep);
  129. }
  130. public static void llama_sample_temperature(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, float temp)
  131. {
  132. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  133. NativeApi.llama_sample_temperature(ctx, ref st, temp);
  134. }
  135. /// <summary>
  136. /// Mirostat 1.0 algorithm described in the paper https://arxiv.org/abs/2007.14966. Uses tokens instead of words.
  137. /// </summary>
  138. /// <param name="ctx"></param>
  139. /// <param name="candidates">A vector of `LLamaTokenData` containing the candidate tokens, their probabilities (p), and log-odds (logit) for the current position in the generated text.</param>
  140. /// <param name="tau">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.</param>
  141. /// <param name="eta">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.</param>
  142. /// <param name="m">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.</param>
  143. /// <param name="mu">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.</param>
  144. /// <returns></returns>
  145. public static llama_token llama_sample_token_mirostat(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, float tau, float eta, int m, ref float mu)
  146. {
  147. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  148. return NativeApi.llama_sample_token_mirostat(ctx, ref st, tau, eta, m, ref mu);
  149. }
  150. /// <summary>
  151. /// Mirostat 2.0 algorithm described in the paper https://arxiv.org/abs/2007.14966. Uses tokens instead of words.
  152. /// </summary>
  153. /// <param name="ctx"></param>
  154. /// <param name="candidates">A vector of `LLamaTokenData` containing the candidate tokens, their probabilities (p), and log-odds (logit) for the current position in the generated text.</param>
  155. /// <param name="tau">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.</param>
  156. /// <param name="eta">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.</param>
  157. /// <param name="mu">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.</param>
  158. /// <returns></returns>
  159. public static llama_token llama_sample_token_mirostat_v2(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, float tau, float eta, ref float mu)
  160. {
  161. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  162. return NativeApi.llama_sample_token_mirostat_v2(ctx, ref st, tau, eta, ref mu);
  163. }
  164. /// <summary>
  165. /// Selects the token with the highest probability.
  166. /// </summary>
  167. /// <param name="ctx"></param>
  168. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  169. /// <returns></returns>
  170. public static llama_token llama_sample_token_greedy(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates)
  171. {
  172. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  173. return NativeApi.llama_sample_token_greedy(ctx, ref st);
  174. }
  175. /// <summary>
  176. /// Randomly selects a token from the candidates based on their probabilities.
  177. /// </summary>
  178. /// <param name="ctx"></param>
  179. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  180. /// <returns></returns>
  181. public static llama_token llama_sample_token(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates)
  182. {
  183. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  184. return NativeApi.llama_sample_token(ctx, ref st);
  185. }
  186. }
  187. }