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

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