1
0

llava-utils.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #pragma once
  2. // this one and clip lib will be eventually merged to a single lib, let's keep it this way for now
  3. #include "common.h"
  4. #include "llama.h"
  5. #include <cstdio>
  6. #include <cstdlib>
  7. #include <vector>
  8. inline bool eval_image_embd(llama_context * ctx_llama, float * embd, int N, int n_batch, int * n_past) {
  9. int n_embd = llama_n_embd(llama_get_model(ctx_llama));
  10. for (int i = 0; i < N; i += n_batch) {
  11. int n_eval = N - i;
  12. if (n_eval > n_batch) {
  13. n_eval = n_batch;
  14. }
  15. llama_batch batch = {int32_t(n_eval), nullptr, (embd+i*n_embd), nullptr, nullptr, nullptr, nullptr, *n_past, 1, 0, };
  16. if (llama_decode(ctx_llama, batch)) {
  17. fprintf(stderr, "%s : failed to eval\n", __func__);
  18. return false;
  19. }
  20. *n_past += n_eval;
  21. }
  22. return true;
  23. }
  24. inline bool eval_tokens(struct llama_context * ctx_llama, std::vector<llama_token> tokens, int n_batch, int * n_past) {
  25. int N = (int) tokens.size();
  26. for (int i = 0; i < N; i += n_batch) {
  27. int n_eval = (int) tokens.size() - i;
  28. if (n_eval > n_batch) {
  29. n_eval = n_batch;
  30. }
  31. if (llama_decode(ctx_llama, llama_batch_get_one(&tokens[i], n_eval, *n_past, 0))) {
  32. fprintf(stderr, "%s : failed to eval\n", __func__);
  33. return false;
  34. }
  35. *n_past += n_eval;
  36. }
  37. return true;
  38. }
  39. inline bool eval_id(struct llama_context * ctx_llama, int id, int * n_past) {
  40. std::vector<llama_token> tokens;
  41. tokens.push_back(id);
  42. return eval_tokens(ctx_llama, tokens, 1, n_past);
  43. }
  44. inline bool eval_string(struct llama_context * ctx_llama, const char* str, int n_batch, int * n_past, bool add_bos){
  45. std::string str2 = str;
  46. std::vector<llama_token> embd_inp = ::llama_tokenize(ctx_llama, str2, add_bos);
  47. eval_tokens(ctx_llama, embd_inp, n_batch, n_past);
  48. return true;
  49. }
  50. // TODO: use common/sampling.h
  51. inline llama_token sample_id(llama_context * ctx_llama, gpt_params & params) {
  52. auto & sparams = params.sparams;
  53. // out of user input, sample next token
  54. const float temp = sparams.temp;
  55. const int32_t top_k = sparams.top_k <= 0 ? llama_n_vocab(llama_get_model(ctx_llama)) : sparams.top_k;
  56. const float top_p = sparams.top_p;
  57. const float tfs_z = sparams.tfs_z;
  58. const float typical_p = sparams.typical_p;
  59. // const int32_t repeat_last_n = sparams.repeat_last_n < 0 ? n_ctx : sparams.repeat_last_n;
  60. // const float repeat_penalty = sparams.repeat_penalty;
  61. // const float alpha_presence = sparams.presence_penalty;
  62. // const float alpha_frequency = sparams.frequency_penalty;
  63. const int mirostat = sparams.mirostat;
  64. const float mirostat_tau = sparams.mirostat_tau;
  65. const float mirostat_eta = sparams.mirostat_eta;
  66. // const bool penalize_nl = sparams.penalize_nl;
  67. llama_token id = 0;
  68. {
  69. auto logits = llama_get_logits(ctx_llama);
  70. auto n_vocab = llama_n_vocab(llama_get_model(ctx_llama));
  71. // Apply params.logit_bias map
  72. for (auto it = sparams.logit_bias.begin(); it != sparams.logit_bias.end(); it++) {
  73. logits[it->first] += it->second;
  74. }
  75. std::vector<llama_token_data> candidates;
  76. candidates.reserve(n_vocab);
  77. for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
  78. candidates.emplace_back(llama_token_data{token_id, logits[token_id], 0.0f});
  79. }
  80. llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
  81. // TODO: Apply penalties
  82. // float nl_logit = logits[llama_token_nl(ctx)];
  83. // auto last_n_repeat = std::min(std::min((int)last_n_tokens.size(), repeat_last_n), n_ctx);
  84. // llama_sample_repetition_penalty(ctx, &candidates_p,
  85. // last_n_tokens.data() + last_n_tokens.size() - last_n_repeat,
  86. // last_n_repeat, repeat_penalty);
  87. // llama_sample_frequency_and_presence_penalties(ctx, &candidates_p,
  88. // last_n_tokens.data() + last_n_tokens.size() - last_n_repeat,
  89. // last_n_repeat, alpha_frequency, alpha_presence);
  90. // if (!penalize_nl) {
  91. // logits[llama_token_nl(ctx)] = nl_logit;
  92. // }
  93. if (temp <= 0) {
  94. // Greedy sampling
  95. id = llama_sample_token_greedy(ctx_llama, &candidates_p);
  96. } else {
  97. if (mirostat == 1) {
  98. static float mirostat_mu = 2.0f * mirostat_tau;
  99. const int mirostat_m = 100;
  100. llama_sample_temp(ctx_llama, &candidates_p, temp);
  101. id = llama_sample_token_mirostat(ctx_llama, &candidates_p, mirostat_tau, mirostat_eta, mirostat_m, &mirostat_mu);
  102. } else if (mirostat == 2) {
  103. static float mirostat_mu = 2.0f * mirostat_tau;
  104. llama_sample_temp(ctx_llama, &candidates_p, temp);
  105. id = llama_sample_token_mirostat_v2(ctx_llama, &candidates_p, mirostat_tau, mirostat_eta, &mirostat_mu);
  106. } else {
  107. // Temperature sampling
  108. llama_sample_top_k(ctx_llama, &candidates_p, top_k, 1);
  109. llama_sample_tail_free(ctx_llama, &candidates_p, tfs_z, 1);
  110. llama_sample_typical(ctx_llama, &candidates_p, typical_p, 1);
  111. llama_sample_top_p(ctx_llama, &candidates_p, top_p, 1);
  112. llama_sample_temp(ctx_llama, &candidates_p, temp);
  113. id = llama_sample_token(ctx_llama, &candidates_p);
  114. }
  115. }
  116. }
  117. return id;
  118. }
  119. inline const char * sample(struct llama_context * ctx_llama, gpt_params & params, int * n_past) {
  120. int id = sample_id(ctx_llama, params);
  121. static std::string ret;
  122. if (id == llama_token_eos(llama_get_model(ctx_llama))) {
  123. ret = "</s>";
  124. } else {
  125. ret = llama_token_to_piece(ctx_llama, id);
  126. }
  127. eval_id(ctx_llama, id, n_past);
  128. return ret.c_str();
  129. }