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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. namespace LLama.Native
  3. {
  4. using llama_token = Int32;
  5. public unsafe class SamplingApi
  6. {
  7. /// <summary>
  8. /// Repetition penalty described in CTRL academic paper https://arxiv.org/abs/1909.05858, with negative logit fix.
  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"></param>
  15. public static void llama_sample_repetition_penalty(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, llama_token[] last_tokens, ulong last_tokens_size, float penalty)
  16. {
  17. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  18. NativeApi.llama_sample_repetition_penalty(ctx, ref st, last_tokens, last_tokens_size, penalty);
  19. }
  20. /// <summary>
  21. /// Frequency and presence penalties described in OpenAI API https://platform.openai.com/docs/api-reference/parameter-details.
  22. /// </summary>
  23. /// <param name="ctx"></param>
  24. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  25. /// <param name="last_tokens"></param>
  26. /// <param name="last_tokens_size"></param>
  27. /// <param name="alpha_frequency"></param>
  28. /// <param name="alpha_presence"></param>
  29. public static void llama_sample_frequency_and_presence_penalties(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, llama_token[] last_tokens, ulong last_tokens_size, float alpha_frequency, float alpha_presence)
  30. {
  31. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  32. NativeApi.llama_sample_frequency_and_presence_penalties(ctx, ref st, last_tokens, last_tokens_size, alpha_frequency, alpha_presence);
  33. }
  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. public static void llama_sample_softmax(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates)
  40. {
  41. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  42. NativeApi.llama_sample_softmax(ctx, ref st);
  43. }
  44. /// <summary>
  45. /// Top-K sampling described in academic paper "The Curious Case of Neural Text Degeneration" https://arxiv.org/abs/1904.09751
  46. /// </summary>
  47. /// <param name="ctx"></param>
  48. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  49. /// <param name="k"></param>
  50. /// <param name="min_keep"></param>
  51. public static void llama_sample_top_k(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, int k, ulong min_keep)
  52. {
  53. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  54. NativeApi.llama_sample_top_k(ctx, ref st, k, min_keep);
  55. }
  56. /// <summary>
  57. /// Nucleus sampling described in academic paper "The Curious Case of Neural Text Degeneration" https://arxiv.org/abs/1904.09751
  58. /// </summary>
  59. /// <param name="ctx"></param>
  60. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  61. /// <param name="p"></param>
  62. /// <param name="min_keep"></param>
  63. public static void llama_sample_top_p(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, float p, ulong min_keep)
  64. {
  65. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  66. NativeApi.llama_sample_top_p(ctx, ref st, p, min_keep);
  67. }
  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. public static void llama_sample_tail_free(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, float z, ulong min_keep)
  76. {
  77. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  78. NativeApi.llama_sample_tail_free(ctx, ref st, z, min_keep);
  79. }
  80. /// <summary>
  81. /// Locally Typical Sampling implementation described in the paper https://arxiv.org/abs/2202.00666.
  82. /// </summary>
  83. /// <param name="ctx"></param>
  84. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  85. /// <param name="p"></param>
  86. /// <param name="min_keep"></param>
  87. public static void llama_sample_typical(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, float p, ulong min_keep)
  88. {
  89. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  90. NativeApi.llama_sample_typical(ctx, ref st, p, min_keep);
  91. }
  92. public static void llama_sample_temperature(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, float temp)
  93. {
  94. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  95. NativeApi.llama_sample_temperature(ctx, ref st, temp);
  96. }
  97. /// <summary>
  98. /// Mirostat 1.0 algorithm described in the paper https://arxiv.org/abs/2007.14966. Uses tokens instead of words.
  99. /// </summary>
  100. /// <param name="ctx"></param>
  101. /// <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>
  102. /// <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>
  103. /// <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>
  104. /// <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>
  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. public static llama_token llama_sample_token_mirostat(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, float tau, float eta, int m, ref float mu)
  108. {
  109. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  110. fixed(float* pmu = &mu)
  111. {
  112. return NativeApi.llama_sample_token_mirostat(ctx, ref st, tau, eta, m, pmu);
  113. }
  114. }
  115. /// <summary>
  116. /// Mirostat 2.0 algorithm described in the paper https://arxiv.org/abs/2007.14966. Uses tokens instead of words.
  117. /// </summary>
  118. /// <param name="ctx"></param>
  119. /// <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>
  120. /// <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>
  121. /// <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>
  122. /// <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>
  123. /// <returns></returns>
  124. public static llama_token llama_sample_token_mirostat_v2(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, float tau, float eta, ref float mu)
  125. {
  126. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  127. fixed (float* pmu = &mu)
  128. {
  129. return NativeApi.llama_sample_token_mirostat_v2(ctx, ref st, tau, eta, pmu);
  130. }
  131. }
  132. /// <summary>
  133. /// Selects the token with the highest probability.
  134. /// </summary>
  135. /// <param name="ctx"></param>
  136. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  137. /// <returns></returns>
  138. public static llama_token llama_sample_token_greedy(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates)
  139. {
  140. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  141. return NativeApi.llama_sample_token_greedy(ctx, ref st);
  142. }
  143. /// <summary>
  144. /// Randomly selects a token from the candidates based on their probabilities.
  145. /// </summary>
  146. /// <param name="ctx"></param>
  147. /// <param name="candidates">Pointer to LLamaTokenDataArray</param>
  148. /// <returns></returns>
  149. public static llama_token llama_sample_token(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates)
  150. {
  151. using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);
  152. return NativeApi.llama_sample_token(ctx, ref st);
  153. }
  154. }
  155. }