sampling.h 5.1 KB

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