sampling.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "sampling.h"
  2. llama_sampling_context::~llama_sampling_context() {
  3. for (auto & it : sequence_contexts) {
  4. if (it.second.grammar != NULL) {
  5. llama_grammar_free(it.second.grammar);
  6. it.second.grammar = NULL;
  7. }
  8. }
  9. }
  10. llama_sampling_context llama_sampling_context_init(
  11. const struct gpt_params & params,
  12. llama_grammar * grammar) {
  13. llama_sampling_context result;
  14. result.params = params.sampling_params;
  15. result.grammar = grammar;
  16. return result;
  17. }
  18. // Note: Creates the context if it doesn't exist, so this always return something.
  19. llama_sampler_sequence_context & llama_sampling_get_sequence_context(
  20. llama_sampling_context & ctx_sampling,
  21. const llama_seq_id seq) {
  22. const auto it = ctx_sampling.sequence_contexts.find(seq);
  23. if (it != ctx_sampling.sequence_contexts.end()) {
  24. return it->second;
  25. }
  26. llama_sampler_sequence_context new_ctx = {
  27. 2.0f * ctx_sampling.params.mirostat_tau,
  28. ctx_sampling.grammar != NULL ? llama_grammar_copy(ctx_sampling.grammar) : NULL,
  29. };
  30. return ctx_sampling.sequence_contexts.insert({seq, new_ctx}).first->second;
  31. }
  32. bool llama_sampling_context_reset(
  33. llama_sampling_context & ctx_sampling,
  34. const llama_seq_id seq) {
  35. const auto it = ctx_sampling.sequence_contexts.find(seq);
  36. if (it == ctx_sampling.sequence_contexts.end()) return false;
  37. if (it->second.grammar != NULL) {
  38. llama_grammar_free(it->second.grammar);
  39. it->second.grammar = NULL;
  40. }
  41. ctx_sampling.sequence_contexts.erase(it);
  42. return true;
  43. }
  44. llama_token llama_sampling_sample(
  45. struct llama_context * ctx,
  46. struct llama_context * ctx_guidance,
  47. struct llama_sampling_context & ctx_sampling,
  48. const std::vector<llama_token> & last_tokens,
  49. std::vector<llama_token_data> & candidates,
  50. const int idx,
  51. llama_seq_id seq) {
  52. const int n_ctx = llama_n_ctx(ctx);
  53. const int n_vocab = llama_n_vocab(llama_get_model(ctx));
  54. const llama_sampling_params & params = ctx_sampling.params;
  55. const float temp = params.temp;
  56. const int32_t top_k = params.top_k <= 0 ? n_vocab : params.top_k;
  57. const float top_p = params.top_p;
  58. const float tfs_z = params.tfs_z;
  59. const float typical_p = params.typical_p;
  60. const int32_t repeat_last_n = params.repeat_last_n < 0 ? n_ctx : params.repeat_last_n;
  61. const float repeat_penalty = params.repeat_penalty;
  62. const float alpha_presence = params.presence_penalty;
  63. const float alpha_frequency = params.frequency_penalty;
  64. const int mirostat = params.mirostat;
  65. const float mirostat_tau = params.mirostat_tau;
  66. const float mirostat_eta = params.mirostat_eta;
  67. const bool penalize_nl = params.penalize_nl;
  68. llama_token id = 0;
  69. float * logits = llama_get_logits_ith(ctx, idx);
  70. // Apply params.logit_bias map
  71. for (auto it = params.logit_bias.begin(); it != params.logit_bias.end(); it++) {
  72. logits[it->first] += it->second;
  73. }
  74. candidates.clear();
  75. for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
  76. candidates.emplace_back(llama_token_data{token_id, logits[token_id], 0.0f});
  77. }
  78. llama_token_data_array cur_p = { candidates.data(), candidates.size(), false };
  79. if (ctx_guidance) {
  80. llama_sample_classifier_free_guidance(ctx, &cur_p, ctx_guidance, params.cfg_scale);
  81. }
  82. // apply penalties
  83. if (!last_tokens.empty()) {
  84. const float nl_logit = logits[llama_token_nl(ctx)];
  85. const int last_n_repeat = std::min(std::min((int)last_tokens.size(), repeat_last_n), n_ctx);
  86. llama_sample_repetition_penalty(ctx, &cur_p,
  87. last_tokens.data() + last_tokens.size() - last_n_repeat,
  88. last_n_repeat, repeat_penalty);
  89. llama_sample_frequency_and_presence_penalties(ctx, &cur_p,
  90. last_tokens.data() + last_tokens.size() - last_n_repeat,
  91. last_n_repeat, alpha_frequency, alpha_presence);
  92. if (!penalize_nl) {
  93. for (size_t idx = 0; idx < cur_p.size; idx++) {
  94. if (cur_p.data[idx].id == llama_token_nl(ctx)) {
  95. cur_p.data[idx].logit = nl_logit;
  96. break;
  97. }
  98. }
  99. }
  100. }
  101. llama_sampler_sequence_context & ctx_seq = llama_sampling_get_sequence_context(ctx_sampling, seq);
  102. if (ctx_seq.grammar != NULL) {
  103. llama_sample_grammar(ctx, &cur_p, ctx_seq.grammar);
  104. }
  105. if (temp <= 0) {
  106. // Greedy sampling
  107. id = llama_sample_token_greedy(ctx, &cur_p);
  108. } else {
  109. if (mirostat == 1) {
  110. const int mirostat_m = 100;
  111. llama_sample_temp(ctx, &cur_p, temp);
  112. id = llama_sample_token_mirostat(ctx, &cur_p, mirostat_tau, mirostat_eta, mirostat_m, &ctx_seq.mirostat_mu);
  113. } else if (mirostat == 2) {
  114. llama_sample_temp(ctx, &cur_p, temp);
  115. id = llama_sample_token_mirostat_v2(ctx, &cur_p, mirostat_tau, mirostat_eta, &ctx_seq.mirostat_mu);
  116. } else {
  117. // Temperature sampling
  118. size_t min_keep = std::max(1, params.n_probs);
  119. llama_sample_top_k (ctx, &cur_p, top_k, min_keep);
  120. llama_sample_tail_free (ctx, &cur_p, tfs_z, min_keep);
  121. llama_sample_typical (ctx, &cur_p, typical_p, min_keep);
  122. llama_sample_top_p (ctx, &cur_p, top_p, min_keep);
  123. llama_sample_temp(ctx, &cur_p, temp);
  124. {
  125. const int n_top = 10;
  126. LOG("top %d candidates:\n", n_top);
  127. for (int i = 0; i < n_top; i++) {
  128. const llama_token id = cur_p.data[i].id;
  129. (void)id; // To avoid a warning that id is unused when logging is disabled.
  130. LOG(" - %5d: '%12s' (%.3f)\n", id, llama_token_to_piece(ctx, id).c_str(), cur_p.data[i].p);
  131. }
  132. }
  133. id = llama_sample_token(ctx, &cur_p);
  134. LOG("sampled token: %5d: '%s'\n", id, llama_token_to_piece(ctx, id).c_str());
  135. }
  136. }
  137. if (ctx_seq.grammar != NULL) {
  138. llama_grammar_accept_token(ctx, ctx_seq.grammar, id);
  139. }
  140. return id;
  141. }