sampling.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 top_k = 40; // <= 0 to use vocab size
  10. float top_p = 0.95f; // 1.0 = disabled
  11. float tfs_z = 1.00f; // 1.0 = disabled
  12. float typical_p = 1.00f; // 1.0 = disabled
  13. float temp = 0.80f; // 1.0 = disabled
  14. float repeat_penalty = 1.10f; // 1.0 = disabled
  15. int32_t repeat_last_n = 64; // last n tokens to penalize (0 = disable penalty, -1 = context size)
  16. float frequency_penalty = 0.00f; // 0.0 = disabled
  17. float presence_penalty = 0.00f; // 0.0 = disabled
  18. int32_t mirostat = 0; // 0 = disabled, 1 = mirostat, 2 = mirostat 2.0
  19. float mirostat_tau = 5.00f; // target entropy
  20. float mirostat_eta = 0.10f; // learning rate
  21. bool penalize_nl = true; // consider newlines as a repeatable token
  22. int32_t n_probs = 0; // if greater than 0, output the probabilities of top n_probs tokens.
  23. // Classifier-Free Guidance
  24. // https://arxiv.org/abs/2306.17806
  25. std::string cfg_negative_prompt; // string to help guidance
  26. float cfg_scale = 1.f; // How strong is guidance
  27. std::unordered_map<llama_token, float> logit_bias; // logit bias for specific tokens
  28. } llama_sampling_params;
  29. // general sampler context
  30. // TODO: move to llama.h
  31. struct llama_sampling_context {
  32. // parameters that will be used for sampling
  33. llama_sampling_params params;
  34. // mirostat sampler state
  35. float mirostat_mu;
  36. llama_grammar * grammar;
  37. // internal
  38. grammar_parser::parse_state parsed_grammar;
  39. // TODO: replace with ring-buffer
  40. std::vector<llama_token> prev;
  41. std::vector<llama_token_data> cur;
  42. };
  43. #include "common.h"
  44. // Create a new sampling context instance.
  45. struct llama_sampling_context * llama_sampling_init(const struct gpt_params & params);
  46. void llama_sampling_free(struct llama_sampling_context * ctx);
  47. // Reset the sampler context
  48. // - clear prev tokens
  49. // - reset grammar
  50. void llama_sampling_reset(llama_sampling_context * ctx);
  51. // Copy the sampler context
  52. void llama_sampling_cp(llama_sampling_context * src, llama_sampling_context * dst);
  53. // this is a common sampling function used across the examples for convenience
  54. // it can serve as a starting point for implementing your own sampling function
  55. // Note: When using multiple sequences, it is the caller's responsibility to call
  56. // llama_sampling_reset when a sequence ends
  57. //
  58. // required:
  59. // - ctx_main: context to use for sampling
  60. // - ctx_sampling: sampling-specific context
  61. //
  62. // optional:
  63. // - ctx_cfg: context to use for classifier-free guidance
  64. // - idx: sample from llama_get_logits_ith(ctx, idx)
  65. //
  66. // returns:
  67. // - token: sampled token
  68. // - candidates: vector of candidate tokens
  69. //
  70. llama_token llama_sampling_sample(
  71. struct llama_sampling_context * ctx_sampling,
  72. struct llama_context * ctx_main,
  73. struct llama_context * ctx_cfg,
  74. int idx = 0);
  75. void llama_sampling_accept(
  76. struct llama_sampling_context * ctx_sampling,
  77. struct llama_context * ctx_main,
  78. llama_token id);