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.

NativeApi.Sampling.cs 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System.Runtime.InteropServices;
  2. namespace LLama.Native
  3. {
  4. public static partial class NativeApi
  5. {
  6. /// <summary>
  7. /// Repetition penalty described in CTRL academic paper https://arxiv.org/abs/1909.05858, with negative logit fix.
  8. /// Frequency and presence penalties described in OpenAI API https://platform.openai.com/docs/api-reference/parameter-details.
  9. /// </summary>
  10. /// <param name="ctx"></param>
  11. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  12. /// <param name="last_tokens"></param>
  13. /// <param name="last_tokens_size"></param>
  14. /// <param name="penalty_repeat">Repetition penalty described in CTRL academic paper https://arxiv.org/abs/1909.05858, with negative logit fix.</param>
  15. /// <param name="penalty_freq">Frequency and presence penalties described in OpenAI API https://platform.openai.com/docs/api-reference/parameter-details.</param>
  16. /// <param name="penalty_present">Frequency and presence penalties described in OpenAI API https://platform.openai.com/docs/api-reference/parameter-details.</param>
  17. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  18. public static extern unsafe void llama_sample_repetition_penalties(SafeLLamaContextHandle ctx,
  19. ref LLamaTokenDataArrayNative candidates,
  20. LLamaToken* last_tokens, ulong last_tokens_size,
  21. float penalty_repeat,
  22. float penalty_freq,
  23. float penalty_present);
  24. /// <summary>
  25. /// 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
  26. /// </summary>
  27. /// <param name="ctx"></param>
  28. /// <param name="logits">Logits extracted from the original generation context.</param>
  29. /// <param name="logits_guidance">Logits extracted from a separate context from the same model.
  30. /// Other than a negative prompt at the beginning, it should have all generated and user input tokens copied from the main context.</param>
  31. /// <param name="scale">Guidance strength. 1.0f means no guidance. Higher values mean stronger guidance.</param>
  32. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  33. public static extern unsafe void llama_sample_apply_guidance(SafeLLamaContextHandle ctx, float* logits, float* logits_guidance, float scale);
  34. /// <summary>
  35. /// Sorts candidate tokens by their logits in descending order and calculate probabilities based on logits.
  36. /// </summary>
  37. /// <param name="ctx"></param>
  38. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  39. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  40. public static extern void llama_sample_softmax(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates);
  41. /// <summary>
  42. /// Top-K sampling described in academic paper "The Curious Case of Neural Text Degeneration" https://arxiv.org/abs/1904.09751
  43. /// </summary>
  44. /// <param name="ctx"></param>
  45. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  46. /// <param name="k"></param>
  47. /// <param name="min_keep"></param>
  48. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  49. public static extern void llama_sample_top_k(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, int k, ulong min_keep);
  50. /// <summary>
  51. /// Nucleus sampling described in academic paper "The Curious Case of Neural Text Degeneration" https://arxiv.org/abs/1904.09751
  52. /// </summary>
  53. /// <param name="ctx"></param>
  54. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  55. /// <param name="p"></param>
  56. /// <param name="min_keep"></param>
  57. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  58. public static extern void llama_sample_top_p(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, float p, ulong min_keep);
  59. /// <summary>
  60. /// Minimum P sampling as described in https://github.com/ggerganov/llama.cpp/pull/3841
  61. /// </summary>
  62. /// <param name="ctx"></param>
  63. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  64. /// <param name="p"></param>
  65. /// <param name="min_keep"></param>
  66. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  67. public static extern void llama_sample_min_p(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, float p, ulong min_keep);
  68. /// <summary>
  69. /// Tail Free Sampling described in https://www.trentonbricken.com/Tail-Free-Sampling/.
  70. /// </summary>
  71. /// <param name="ctx"></param>
  72. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  73. /// <param name="z"></param>
  74. /// <param name="min_keep"></param>
  75. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  76. public static extern void llama_sample_tail_free(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, float z, ulong min_keep);
  77. /// <summary>
  78. /// Locally Typical Sampling implementation described in the paper https://arxiv.org/abs/2202.00666.
  79. /// </summary>
  80. /// <param name="ctx"></param>
  81. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  82. /// <param name="p"></param>
  83. /// <param name="min_keep"></param>
  84. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  85. public static extern void llama_sample_typical(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, float p, ulong min_keep);
  86. /// <summary>
  87. /// Dynamic temperature implementation described in the paper https://arxiv.org/abs/2309.02772.
  88. /// </summary>
  89. /// <param name="ctx"></param>
  90. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  91. /// <param name="min_temp"></param>
  92. /// <param name="max_temp"></param>
  93. /// <param name="exponent_val"></param>
  94. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  95. public static extern void llama_sample_typical(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, float min_temp, float max_temp, float exponent_val);
  96. /// <summary>
  97. /// Modify logits by temperature
  98. /// </summary>
  99. /// <param name="ctx"></param>
  100. /// <param name="candidates"></param>
  101. /// <param name="temp"></param>
  102. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  103. public static extern void llama_sample_temp(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, float temp);
  104. /// <summary>
  105. /// Mirostat 1.0 algorithm described in the paper https://arxiv.org/abs/2007.14966. Uses tokens instead of words.
  106. /// </summary>
  107. /// <param name="ctx"></param>
  108. /// <param name="candidates">A vector of `llama_token_data` containing the candidate tokens, their probabilities (p), and log-odds (logit) for the current position in the generated text.</param>
  109. /// <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>
  110. /// <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>
  111. /// <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>
  112. /// <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>
  113. /// <returns></returns>
  114. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  115. public static extern LLamaToken llama_sample_token_mirostat(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, float tau, float eta, int m, ref float mu);
  116. /// <summary>
  117. /// Mirostat 2.0 algorithm described in the paper https://arxiv.org/abs/2007.14966. Uses tokens instead of words.
  118. /// </summary>
  119. /// <param name="ctx"></param>
  120. /// <param name="candidates">A vector of `llama_token_data` containing the candidate tokens, their probabilities (p), and log-odds (logit) for the current position in the generated text.</param>
  121. /// <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>
  122. /// <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>
  123. /// <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>
  124. /// <returns></returns>
  125. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  126. public static extern LLamaToken llama_sample_token_mirostat_v2(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, float tau, float eta, ref float mu);
  127. /// <summary>
  128. /// Selects the token with the highest probability.
  129. /// </summary>
  130. /// <param name="ctx"></param>
  131. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  132. /// <returns></returns>
  133. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  134. public static extern LLamaToken llama_sample_token_greedy(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates);
  135. /// <summary>
  136. /// Randomly selects a token from the candidates based on their probabilities.
  137. /// </summary>
  138. /// <param name="ctx"></param>
  139. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  140. /// <returns></returns>
  141. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  142. public static extern LLamaToken llama_sample_token(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates);
  143. }
  144. }