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 8.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. #pragma warning disable IDE1006 // Naming Styles
  3. namespace LLama.Native
  4. {
  5. /// <summary>
  6. /// Direct translation of the llama.cpp sampling API
  7. /// </summary>
  8. public class SamplingApi
  9. {
  10. /// <summary>
  11. /// Apply grammar rules to candidate tokens
  12. /// </summary>
  13. /// <param name="ctx"></param>
  14. /// <param name="candidates"></param>
  15. /// <param name="grammar"></param>
  16. [Obsolete("use LLamaTokenDataArray ApplyGrammar method")]
  17. public static void llama_sample_grammar(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, SafeLLamaGrammarHandle grammar)
  18. {
  19. candidates.ApplyGrammar(ctx, grammar);
  20. }
  21. /// <summary>
  22. /// Sorts candidate tokens by their logits in descending order and calculate probabilities based on logits.
  23. /// </summary>
  24. /// <param name="ctx"></param>
  25. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  26. [Obsolete("use LLamaTokenDataArray Softmax method")]
  27. public static void llama_sample_softmax(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates)
  28. {
  29. candidates.Softmax(ctx);
  30. }
  31. /// <summary>
  32. /// Top-K sampling described in academic paper "The Curious Case of Neural Text Degeneration" https://arxiv.org/abs/1904.09751
  33. /// </summary>
  34. /// <param name="ctx"></param>
  35. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  36. /// <param name="k"></param>
  37. /// <param name="min_keep"></param>
  38. [Obsolete("use LLamaTokenDataArray TopK method")]
  39. public static void llama_sample_top_k(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, int k, ulong min_keep)
  40. {
  41. candidates.TopK(ctx, k, min_keep);
  42. }
  43. /// <summary>
  44. /// Nucleus sampling described in academic paper "The Curious Case of Neural Text Degeneration" https://arxiv.org/abs/1904.09751
  45. /// </summary>
  46. /// <param name="ctx"></param>
  47. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  48. /// <param name="p"></param>
  49. /// <param name="min_keep"></param>
  50. [Obsolete("use LLamaTokenDataArray TopP method")]
  51. public static void llama_sample_top_p(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, float p, ulong min_keep)
  52. {
  53. candidates.TopP(ctx, p, min_keep);
  54. }
  55. /// <summary>
  56. /// Tail Free Sampling described in https://www.trentonbricken.com/Tail-Free-Sampling/.
  57. /// </summary>
  58. /// <param name="ctx"></param>
  59. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  60. /// <param name="z"></param>
  61. /// <param name="min_keep"></param>
  62. [Obsolete("use LLamaTokenDataArray TailFree method")]
  63. public static void llama_sample_tail_free(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, float z, ulong min_keep)
  64. {
  65. candidates.TailFree(ctx, z, min_keep);
  66. }
  67. /// <summary>
  68. /// Locally Typical Sampling implementation described in the paper https://arxiv.org/abs/2202.00666.
  69. /// </summary>
  70. /// <param name="ctx"></param>
  71. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  72. /// <param name="p"></param>
  73. /// <param name="min_keep"></param>
  74. [Obsolete("use LLamaTokenDataArray LocallyTypical method")]
  75. public static void llama_sample_typical(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, float p, ulong min_keep)
  76. {
  77. candidates.LocallyTypical(ctx, p, min_keep);
  78. }
  79. /// <summary>
  80. /// Sample with temperature.
  81. /// As temperature increases, the prediction becomes diverse but also vulnerable to hallucinations -- generating tokens that are sensible but not factual
  82. /// </summary>
  83. /// <param name="ctx"></param>
  84. /// <param name="candidates"></param>
  85. /// <param name="temp"></param>
  86. [Obsolete("use LLamaTokenDataArray Temperature() method")]
  87. public static void llama_sample_temperature(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, float temp)
  88. {
  89. candidates.Temperature(ctx, temp);
  90. }
  91. /// <summary>
  92. /// Mirostat 1.0 algorithm described in the paper https://arxiv.org/abs/2007.14966. Uses tokens instead of words.
  93. /// </summary>
  94. /// <param name="ctx"></param>
  95. /// <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>
  96. /// <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>
  97. /// <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>
  98. /// <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>
  99. /// <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>
  100. /// <returns></returns>
  101. [Obsolete("use LLamaTokenDataArray SampleTokenMirostat() method")]
  102. public static LLamaToken llama_sample_token_mirostat(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, float tau, float eta, int m, ref float mu)
  103. {
  104. return candidates.SampleTokenMirostat(ctx, tau, eta, m, ref mu);
  105. }
  106. /// <summary>
  107. /// Mirostat 2.0 algorithm described in the paper https://arxiv.org/abs/2007.14966. Uses tokens instead of words.
  108. /// </summary>
  109. /// <param name="ctx"></param>
  110. /// <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>
  111. /// <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>
  112. /// <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>
  113. /// <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>
  114. /// <returns></returns>
  115. [Obsolete("use LLamaTokenDataArray SampleTokenMirostat2() method")]
  116. public static LLamaToken llama_sample_token_mirostat_v2(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, float tau, float eta, ref float mu)
  117. {
  118. return candidates.SampleTokenMirostat2(ctx, tau, eta, ref mu);
  119. }
  120. /// <summary>
  121. /// Selects the token with the highest probability.
  122. /// </summary>
  123. /// <param name="ctx"></param>
  124. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  125. /// <returns></returns>
  126. [Obsolete("Use LLamaTokenDataArray SampleTokenGreedy() method")]
  127. public static LLamaToken llama_sample_token_greedy(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates)
  128. {
  129. return candidates.SampleTokenGreedy(ctx);
  130. }
  131. /// <summary>
  132. /// Randomly selects a token from the candidates based on their probabilities.
  133. /// </summary>
  134. /// <param name="ctx"></param>
  135. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  136. /// <returns></returns>
  137. [Obsolete("use LLamaTokenDataArray SampleToken() method")]
  138. public static LLamaToken llama_sample_token(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates)
  139. {
  140. return candidates.SampleToken(ctx);
  141. }
  142. }
  143. }