sampling.h 5.0 KB

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