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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. /// Sorts candidate tokens by their logits in descending order and calculate probabilities based on logits.
  31. /// </summary>
  32. /// <param name="ctx"></param>
  33. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  34. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  35. public static extern void llama_sample_softmax(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates);
  36. /// <summary>
  37. /// Top-K sampling described in academic paper "The Curious Case of Neural Text Degeneration" https://arxiv.org/abs/1904.09751
  38. /// </summary>
  39. /// <param name="ctx"></param>
  40. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  41. /// <param name="k"></param>
  42. /// <param name="min_keep"></param>
  43. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  44. public static extern void llama_sample_top_k(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, int k, ulong min_keep);
  45. /// <summary>
  46. /// Nucleus 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="p"></param>
  51. /// <param name="min_keep"></param>
  52. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  53. public static extern void llama_sample_top_p(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, float p, ulong min_keep);
  54. /// <summary>
  55. /// Tail Free Sampling described in https://www.trentonbricken.com/Tail-Free-Sampling/.
  56. /// </summary>
  57. /// <param name="ctx"></param>
  58. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  59. /// <param name="z"></param>
  60. /// <param name="min_keep"></param>
  61. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  62. public static extern void llama_sample_tail_free(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, float z, ulong min_keep);
  63. /// <summary>
  64. /// Locally Typical Sampling implementation described in the paper https://arxiv.org/abs/2202.00666.
  65. /// </summary>
  66. /// <param name="ctx"></param>
  67. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  68. /// <param name="p"></param>
  69. /// <param name="min_keep"></param>
  70. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  71. public static extern void llama_sample_typical(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, float p, ulong min_keep);
  72. /// <summary>
  73. /// Modify logits by temperature
  74. /// </summary>
  75. /// <param name="ctx"></param>
  76. /// <param name="candidates"></param>
  77. /// <param name="temp"></param>
  78. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  79. public static extern void llama_sample_temperature(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, float temp);
  80. /// <summary>
  81. /// Mirostat 1.0 algorithm described in the paper https://arxiv.org/abs/2007.14966. Uses tokens instead of words.
  82. /// </summary>
  83. /// <param name="ctx"></param>
  84. /// <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>
  85. /// <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>
  86. /// <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>
  87. /// <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>
  88. /// <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>
  89. /// <returns></returns>
  90. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  91. public static extern llama_token llama_sample_token_mirostat(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, float tau, float eta, int m, float* mu);
  92. /// <summary>
  93. /// Mirostat 2.0 algorithm described in the paper https://arxiv.org/abs/2007.14966. Uses tokens instead of words.
  94. /// </summary>
  95. /// <param name="ctx"></param>
  96. /// <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>
  97. /// <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>
  98. /// <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>
  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. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  102. public static extern llama_token llama_sample_token_mirostat_v2(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, float tau, float eta, float* mu);
  103. /// <summary>
  104. /// Selects the token with the highest probability.
  105. /// </summary>
  106. /// <param name="ctx"></param>
  107. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  108. /// <returns></returns>
  109. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  110. public static extern llama_token llama_sample_token_greedy(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates);
  111. /// <summary>
  112. /// Randomly selects a token from the candidates based on their probabilities.
  113. /// </summary>
  114. /// <param name="ctx"></param>
  115. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  116. /// <returns></returns>
  117. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  118. public static extern llama_token llama_sample_token(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates);
  119. }
  120. }