sampling.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #pragma once
  2. #include "llama.h"
  3. #include "grammar-parser.h"
  4. #include <random>
  5. #include <string>
  6. #include <unordered_map>
  7. #include <vector>
  8. // sampler types
  9. enum class llama_sampler_type : char {
  10. TOP_K = 'k',
  11. TOP_P = 'p',
  12. MIN_P = 'm',
  13. TFS_Z = 'f',
  14. TYPICAL_P = 'y',
  15. TEMPERATURE = 't'
  16. };
  17. // sampling parameters
  18. typedef struct llama_sampling_params {
  19. int32_t n_prev = 64; // number of previous tokens to remember
  20. int32_t n_probs = 0; // if greater than 0, output the probabilities of top n_probs tokens.
  21. int32_t min_keep = 0; // 0 = disabled, otherwise samplers should return at least min_keep tokens
  22. int32_t top_k = 40; // <= 0 to use vocab size
  23. float top_p = 0.95f; // 1.0 = disabled
  24. float min_p = 0.05f; // 0.0 = disabled
  25. float tfs_z = 1.00f; // 1.0 = disabled
  26. float typical_p = 1.00f; // 1.0 = disabled
  27. float temp = 0.80f; // <= 0.0 to sample greedily, 0.0 to not output probabilities
  28. float dynatemp_range = 0.00f; // 0.0 = disabled
  29. float dynatemp_exponent = 1.00f; // controls how entropy maps to temperature in dynamic temperature sampler
  30. int32_t penalty_last_n = 64; // last n tokens to penalize (0 = disable penalty, -1 = context size)
  31. float penalty_repeat = 1.00f; // 1.0 = disabled
  32. float penalty_freq = 0.00f; // 0.0 = disabled
  33. float penalty_present = 0.00f; // 0.0 = disabled
  34. int32_t mirostat = 0; // 0 = disabled, 1 = mirostat, 2 = mirostat 2.0
  35. float mirostat_tau = 5.00f; // target entropy
  36. float mirostat_eta = 0.10f; // learning rate
  37. bool penalize_nl = false; // consider newlines as a repeatable token
  38. uint32_t seed = LLAMA_DEFAULT_SEED; // the seed used to initialize llama_sampling_context
  39. std::vector<llama_sampler_type> samplers_sequence = {
  40. llama_sampler_type::TOP_K,
  41. llama_sampler_type::TFS_Z,
  42. llama_sampler_type::TYPICAL_P,
  43. llama_sampler_type::TOP_P,
  44. llama_sampler_type::MIN_P,
  45. llama_sampler_type::TEMPERATURE
  46. };
  47. std::string grammar; // optional BNF-like grammar to constrain sampling
  48. // Classifier-Free Guidance
  49. // https://arxiv.org/abs/2306.17806
  50. std::string cfg_negative_prompt; // string to help guidance
  51. float cfg_scale = 1.f; // how strong is guidance
  52. std::unordered_map<llama_token, float> logit_bias; // logit bias for specific tokens
  53. std::vector<llama_token> penalty_prompt_tokens;
  54. bool use_penalty_prompt_tokens = false;
  55. } llama_sampling_params;
  56. // general sampler context
  57. // TODO: move to llama.h
  58. struct llama_sampling_context {
  59. // parameters that will be used for sampling
  60. llama_sampling_params params;
  61. // mirostat sampler state
  62. float mirostat_mu;
  63. llama_grammar * grammar;
  64. // internal
  65. grammar_parser::parse_state parsed_grammar;
  66. // TODO: replace with ring-buffer
  67. std::vector<llama_token> prev;
  68. std::vector<llama_token_data> cur;
  69. size_t n_valid; // Number of correct top tokens with correct probabilities.
  70. std::mt19937 rng;
  71. };
  72. #include "common.h"
  73. // Create a new sampling context instance.
  74. struct llama_sampling_context * llama_sampling_init(const struct llama_sampling_params & params);
  75. void llama_sampling_free(struct llama_sampling_context * ctx);
  76. // Reset the sampler context
  77. // - clear prev tokens
  78. // - reset grammar
  79. void llama_sampling_reset(llama_sampling_context * ctx);
  80. // Set the sampler seed
  81. void llama_sampling_set_rng_seed(struct llama_sampling_context * ctx, uint32_t seed);
  82. // Copy the sampler context
  83. void llama_sampling_cp(llama_sampling_context * src, llama_sampling_context * dst);
  84. // Get the last sampled token
  85. llama_token llama_sampling_last(llama_sampling_context * ctx);
  86. // Get a string representation of the last sampled tokens
  87. std::string llama_sampling_prev_str(llama_sampling_context * ctx_sampling, llama_context * ctx_main, int n);
  88. // Print sampling parameters into a string
  89. std::string llama_sampling_print(const llama_sampling_params & params);
  90. // Print sampling order into a string
  91. std::string llama_sampling_order_print(const llama_sampling_params & params);
  92. // this is a common sampling function used across the examples for convenience
  93. // it can serve as a starting point for implementing your own sampling function
  94. // Note: When using multiple sequences, it is the caller's responsibility to call
  95. // llama_sampling_reset when a sequence ends
  96. //
  97. // required:
  98. // - ctx_main: context to use for sampling
  99. // - ctx_sampling: sampling-specific context
  100. //
  101. // optional:
  102. // - ctx_cfg: context to use for classifier-free guidance
  103. // - idx: sample from llama_get_logits_ith(ctx, idx)
  104. //
  105. // returns:
  106. // - token: sampled token
  107. // - candidates: vector of candidate tokens
  108. //
  109. llama_token llama_sampling_sample(
  110. struct llama_sampling_context * ctx_sampling,
  111. struct llama_context * ctx_main,
  112. struct llama_context * ctx_cfg,
  113. int idx = -1);
  114. // Prepares and adjusts the set of token candidates for sampling based on penalties, biases, and sampling parameters.
  115. llama_token_data_array llama_sampling_prepare(
  116. struct llama_sampling_context * ctx_sampling,
  117. struct llama_context * ctx_main,
  118. struct llama_context * ctx_cfg,
  119. int idx = 0,
  120. bool apply_grammar = true,
  121. std::vector<float> * original_logits = nullptr);
  122. void llama_sampling_accept(
  123. struct llama_sampling_context * ctx_sampling,
  124. struct llama_context * ctx_main,
  125. llama_token id,
  126. bool apply_grammar);