sampling.h 4.4 KB

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