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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. /// Frequency and presence penalties described in OpenAI API https://platform.openai.com/docs/api-reference/parameter-details.
  11. /// </summary>
  12. /// <param name="ctx"></param>
  13. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  14. /// <param name="last_tokens"></param>
  15. /// <param name="last_tokens_size"></param>
  16. /// <param name="penalty_repeat">Repetition penalty described in CTRL academic paper https://arxiv.org/abs/1909.05858, with negative logit fix.</param>
  17. /// <param name="penalty_freq">Frequency and presence penalties described in OpenAI API https://platform.openai.com/docs/api-reference/parameter-details.</param>
  18. /// <param name="penalty_present">Frequency and presence penalties described in OpenAI API https://platform.openai.com/docs/api-reference/parameter-details.</param>
  19. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  20. public static extern void llama_sample_repetition_penalties(SafeLLamaContextHandle ctx,
  21. ref LLamaTokenDataArrayNative candidates,
  22. llama_token* last_tokens, ulong last_tokens_size,
  23. float penalty_repeat,
  24. float penalty_freq,
  25. float penalty_present);
  26. /// <summary>
  27. /// 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
  28. /// </summary>
  29. /// <param name="ctx"></param>
  30. /// <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>
  31. /// <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>
  32. /// <param name="scale">Guidance strength. 1.0f means no guidance. Higher values mean stronger guidance.</param>
  33. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  34. public static extern void llama_sample_classifier_free_guidance(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, SafeLLamaContextHandle guidance_ctx, float scale);
  35. /// <summary>
  36. /// Sorts candidate tokens by their logits in descending order and calculate probabilities based on logits.
  37. /// </summary>
  38. /// <param name="ctx"></param>
  39. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  40. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  41. public static extern void llama_sample_softmax(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates);
  42. /// <summary>
  43. /// Top-K sampling described in academic paper "The Curious Case of Neural Text Degeneration" https://arxiv.org/abs/1904.09751
  44. /// </summary>
  45. /// <param name="ctx"></param>
  46. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  47. /// <param name="k"></param>
  48. /// <param name="min_keep"></param>
  49. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  50. public static extern void llama_sample_top_k(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, int k, ulong min_keep);
  51. /// <summary>
  52. /// Nucleus sampling described in academic paper "The Curious Case of Neural Text Degeneration" https://arxiv.org/abs/1904.09751
  53. /// </summary>
  54. /// <param name="ctx"></param>
  55. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  56. /// <param name="p"></param>
  57. /// <param name="min_keep"></param>
  58. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  59. public static extern void llama_sample_top_p(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, float p, ulong min_keep);
  60. /// <summary>
  61. /// Tail Free Sampling described in https://www.trentonbricken.com/Tail-Free-Sampling/.
  62. /// </summary>
  63. /// <param name="ctx"></param>
  64. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  65. /// <param name="z"></param>
  66. /// <param name="min_keep"></param>
  67. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  68. public static extern void llama_sample_tail_free(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, float z, ulong min_keep);
  69. /// <summary>
  70. /// Locally Typical Sampling implementation described in the paper https://arxiv.org/abs/2202.00666.
  71. /// </summary>
  72. /// <param name="ctx"></param>
  73. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  74. /// <param name="p"></param>
  75. /// <param name="min_keep"></param>
  76. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  77. public static extern void llama_sample_typical(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, float p, ulong min_keep);
  78. /// <summary>
  79. /// Modify logits by temperature
  80. /// </summary>
  81. /// <param name="ctx"></param>
  82. /// <param name="candidates"></param>
  83. /// <param name="temp"></param>
  84. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  85. public static extern void llama_sample_temperature(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, float temp);
  86. /// <summary>
  87. /// Mirostat 1.0 algorithm described in the paper https://arxiv.org/abs/2007.14966. Uses tokens instead of words.
  88. /// </summary>
  89. /// <param name="ctx"></param>
  90. /// <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>
  91. /// <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>
  92. /// <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>
  93. /// <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>
  94. /// <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>
  95. /// <returns></returns>
  96. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  97. public static extern llama_token llama_sample_token_mirostat(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, float tau, float eta, int m, ref float mu);
  98. /// <summary>
  99. /// Mirostat 2.0 algorithm described in the paper https://arxiv.org/abs/2007.14966. Uses tokens instead of words.
  100. /// </summary>
  101. /// <param name="ctx"></param>
  102. /// <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>
  103. /// <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>
  104. /// <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>
  105. /// <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>
  106. /// <returns></returns>
  107. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  108. public static extern llama_token llama_sample_token_mirostat_v2(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, float tau, float eta, ref float mu);
  109. /// <summary>
  110. /// Selects the token with the highest probability.
  111. /// </summary>
  112. /// <param name="ctx"></param>
  113. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  114. /// <returns></returns>
  115. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  116. public static extern llama_token llama_sample_token_greedy(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates);
  117. /// <summary>
  118. /// Randomly selects a token from the candidates based on their probabilities.
  119. /// </summary>
  120. /// <param name="ctx"></param>
  121. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  122. /// <returns></returns>
  123. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  124. public static extern llama_token llama_sample_token(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates);
  125. }
  126. }