sampling.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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; // 1.0 = disabled
  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 grammar; // optional BNF-like grammar to constrain sampling
  26. // Classifier-Free Guidance
  27. // https://arxiv.org/abs/2306.17806
  28. std::string cfg_negative_prompt; // string to help guidance
  29. float cfg_scale = 1.f; // how strong is guidance
  30. std::unordered_map<llama_token, float> logit_bias; // logit bias for specific tokens
  31. } llama_sampling_params;
  32. // general sampler context
  33. // TODO: move to llama.h
  34. struct llama_sampling_context {
  35. // parameters that will be used for sampling
  36. llama_sampling_params params;
  37. // mirostat sampler state
  38. float mirostat_mu;
  39. llama_grammar * grammar;
  40. // internal
  41. grammar_parser::parse_state parsed_grammar;
  42. // TODO: replace with ring-buffer
  43. std::vector<llama_token> prev;
  44. std::vector<llama_token_data> cur;
  45. };
  46. #include "common.h"
  47. // Create a new sampling context instance.
  48. struct llama_sampling_context * llama_sampling_init(const struct llama_sampling_params & params);
  49. void llama_sampling_free(struct llama_sampling_context * ctx);
  50. // Reset the sampler context
  51. // - clear prev tokens
  52. // - reset grammar
  53. void llama_sampling_reset(llama_sampling_context * ctx);
  54. // Copy the sampler context
  55. void llama_sampling_cp(llama_sampling_context * src, llama_sampling_context * dst);
  56. // Get the last sampled token
  57. llama_token llama_sampling_last(llama_sampling_context * ctx);
  58. // Get a string representation of the last sampled tokens
  59. std::string llama_sampling_prev_str(llama_sampling_context * ctx_sampling, llama_context * ctx_main, int n);
  60. // Print sampling parameters into a string
  61. std::string llama_sampling_print(const llama_sampling_params & params);
  62. // this is a common sampling function used across the examples for convenience
  63. // it can serve as a starting point for implementing your own sampling function
  64. // Note: When using multiple sequences, it is the caller's responsibility to call
  65. // llama_sampling_reset when a sequence ends
  66. //
  67. // required:
  68. // - ctx_main: context to use for sampling
  69. // - ctx_sampling: sampling-specific context
  70. //
  71. // optional:
  72. // - ctx_cfg: context to use for classifier-free guidance
  73. // - idx: sample from llama_get_logits_ith(ctx, idx)
  74. //
  75. // returns:
  76. // - token: sampled token
  77. // - candidates: vector of candidate tokens
  78. //
  79. llama_token llama_sampling_sample(
  80. struct llama_sampling_context * ctx_sampling,
  81. struct llama_context * ctx_main,
  82. struct llama_context * ctx_cfg,
  83. int idx = 0);
  84. void llama_sampling_accept(
  85. struct llama_sampling_context * ctx_sampling,
  86. struct llama_context * ctx_main,
  87. llama_token id,
  88. bool apply_grammar);