sampling.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * llama.cpp - commit 8962422b1c6f9b8b15f5aeaea42600bcc2d44177 - do not edit this file
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023-2024 The ggml authors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. #pragma once
  27. #include "llama.h"
  28. #include "grammar-parser.h"
  29. #include <random>
  30. #include <string>
  31. #include <unordered_map>
  32. #include <vector>
  33. // sampler types
  34. enum class llama_sampler_type : char {
  35. TOP_K = 'k',
  36. TOP_P = 'p',
  37. MIN_P = 'm',
  38. TFS_Z = 'f',
  39. TYPICAL_P = 'y',
  40. TEMPERATURE = 't'
  41. };
  42. // sampling parameters
  43. typedef struct llama_sampling_params {
  44. int32_t n_prev = 64; // number of previous tokens to remember
  45. int32_t n_probs = 0; // if greater than 0, output the probabilities of top n_probs tokens.
  46. int32_t min_keep = 0; // 0 = disabled, otherwise samplers should return at least min_keep tokens
  47. int32_t top_k = 40; // <= 0 to use vocab size
  48. float top_p = 0.95f; // 1.0 = disabled
  49. float min_p = 0.05f; // 0.0 = disabled
  50. float tfs_z = 1.00f; // 1.0 = disabled
  51. float typical_p = 1.00f; // 1.0 = disabled
  52. float temp = 0.80f; // <= 0.0 to sample greedily, 0.0 to not output probabilities
  53. float dynatemp_range = 0.00f; // 0.0 = disabled
  54. float dynatemp_exponent = 1.00f; // controls how entropy maps to temperature in dynamic temperature sampler
  55. int32_t penalty_last_n = 64; // last n tokens to penalize (0 = disable penalty, -1 = context size)
  56. float penalty_repeat = 1.00f; // 1.0 = disabled
  57. float penalty_freq = 0.00f; // 0.0 = disabled
  58. float penalty_present = 0.00f; // 0.0 = disabled
  59. int32_t mirostat = 0; // 0 = disabled, 1 = mirostat, 2 = mirostat 2.0
  60. float mirostat_tau = 5.00f; // target entropy
  61. float mirostat_eta = 0.10f; // learning rate
  62. bool penalize_nl = false; // consider newlines as a repeatable token
  63. uint32_t seed = LLAMA_DEFAULT_SEED; // the seed used to initialize llama_sampling_context
  64. std::vector<llama_sampler_type> samplers_sequence = {
  65. llama_sampler_type::TOP_K,
  66. llama_sampler_type::TFS_Z,
  67. llama_sampler_type::TYPICAL_P,
  68. llama_sampler_type::TOP_P,
  69. llama_sampler_type::MIN_P,
  70. llama_sampler_type::TEMPERATURE
  71. };
  72. std::string grammar; // optional BNF-like grammar to constrain sampling
  73. // Classifier-Free Guidance
  74. // https://arxiv.org/abs/2306.17806
  75. std::string cfg_negative_prompt; // string to help guidance
  76. float cfg_scale = 1.f; // how strong is guidance
  77. std::unordered_map<llama_token, float> logit_bias; // logit bias for specific tokens
  78. std::vector<llama_token> penalty_prompt_tokens;
  79. bool use_penalty_prompt_tokens = false;
  80. } llama_sampling_params;
  81. // general sampler context
  82. // TODO: move to llama.h
  83. struct llama_sampling_context {
  84. // parameters that will be used for sampling
  85. llama_sampling_params params;
  86. // mirostat sampler state
  87. float mirostat_mu;
  88. llama_grammar * grammar;
  89. // internal
  90. grammar_parser::parse_state parsed_grammar;
  91. // TODO: replace with ring-buffer
  92. std::vector<llama_token> prev;
  93. std::vector<llama_token_data> cur;
  94. size_t n_valid; // Number of correct top tokens with correct probabilities.
  95. std::mt19937 rng;
  96. };
  97. #include "common.h"
  98. // Create a new sampling context instance.
  99. struct llama_sampling_context * llama_sampling_init(const struct llama_sampling_params & params);
  100. void llama_sampling_free(struct llama_sampling_context * ctx);
  101. // Reset the sampler context
  102. // - clear prev tokens
  103. // - reset grammar
  104. void llama_sampling_reset(llama_sampling_context * ctx);
  105. // Set the sampler seed
  106. void llama_sampling_set_rng_seed(struct llama_sampling_context * ctx, uint32_t seed);
  107. // Copy the sampler context
  108. void llama_sampling_cp(llama_sampling_context * src, llama_sampling_context * dst);
  109. // Get the last sampled token
  110. llama_token llama_sampling_last(llama_sampling_context * ctx);
  111. // Get a string representation of the last sampled tokens
  112. std::string llama_sampling_prev_str(llama_sampling_context * ctx_sampling, llama_context * ctx_main, int n);
  113. // Print sampling parameters into a string
  114. std::string llama_sampling_print(const llama_sampling_params & params);
  115. // Print sampling order into a string
  116. std::string llama_sampling_order_print(const llama_sampling_params & params);
  117. std::string llama_sampling_type_to_str(llama_sampler_type sampler_type);
  118. std::vector<llama_sampler_type> llama_sampling_types_from_names(const std::vector<std::string> & names, bool allow_alt_names);
  119. std::vector<llama_sampler_type> llama_sampling_types_from_chars(const std::string & names_string);
  120. // this is a common sampling function used across the examples for convenience
  121. // it can serve as a starting point for implementing your own sampling function
  122. // Note: When using multiple sequences, it is the caller's responsibility to call
  123. // llama_sampling_reset when a sequence ends
  124. //
  125. // required:
  126. // - ctx_main: context to use for sampling
  127. // - ctx_sampling: sampling-specific context
  128. //
  129. // optional:
  130. // - ctx_cfg: context to use for classifier-free guidance
  131. // - idx: sample from llama_get_logits_ith(ctx, idx)
  132. //
  133. // returns:
  134. // - token: sampled token
  135. // - candidates: vector of candidate tokens
  136. //
  137. llama_token llama_sampling_sample(
  138. struct llama_sampling_context * ctx_sampling,
  139. struct llama_context * ctx_main,
  140. struct llama_context * ctx_cfg,
  141. int idx = -1);
  142. // Prepares and adjusts the set of token candidates for sampling based on penalties, biases, and sampling parameters.
  143. llama_token_data_array llama_sampling_prepare(
  144. struct llama_sampling_context * ctx_sampling,
  145. struct llama_context * ctx_main,
  146. struct llama_context * ctx_cfg,
  147. int idx = 0,
  148. bool apply_grammar = true,
  149. std::vector<float> * original_logits = nullptr);
  150. void llama_sampling_accept(
  151. struct llama_sampling_context * ctx_sampling,
  152. struct llama_context * ctx_main,
  153. llama_token id,
  154. bool apply_grammar);