embd-input-lib.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include "build-info.h"
  2. #include "common.h"
  3. #include "embd-input.h"
  4. #include <cassert>
  5. #include <cinttypes>
  6. #include <cmath>
  7. #include <cstdio>
  8. #include <cstring>
  9. #include <ctime>
  10. #include <fstream>
  11. #include <iostream>
  12. #include <string>
  13. #include <vector>
  14. static llama_context ** g_ctx;
  15. extern "C" {
  16. struct MyModel* create_mymodel(int argc, char ** argv) {
  17. gpt_params params;
  18. if (!gpt_params_parse(argc, argv, params)) {
  19. return nullptr;
  20. }
  21. print_build_info();
  22. if (params.seed == LLAMA_DEFAULT_SEED) {
  23. params.seed = uint32_t(time(NULL));
  24. }
  25. fprintf(stderr, "%s: seed = %d\n", __func__, params.seed);
  26. llama_backend_init(params.numa);
  27. llama_model * model;
  28. llama_context * ctx;
  29. g_ctx = &ctx;
  30. // load the model and apply lora adapter, if any
  31. std::tie(model, ctx) = llama_init_from_gpt_params(params);
  32. if (model == NULL) {
  33. fprintf(stderr, "%s: error: unable to load model\n", __func__);
  34. return nullptr;
  35. }
  36. // print system information
  37. {
  38. fprintf(stderr, "\n");
  39. fprintf(stderr, "system_info: n_threads = %d / %d | %s\n",
  40. params.n_threads, std::thread::hardware_concurrency(), llama_print_system_info());
  41. }
  42. struct MyModel * ret = new MyModel();
  43. ret->ctx = ctx;
  44. ret->params = params;
  45. ret->n_past = 0;
  46. // printf("ctx: %d\n", ret->ctx);
  47. return ret;
  48. }
  49. void free_mymodel(struct MyModel * mymodel) {
  50. llama_context * ctx = mymodel->ctx;
  51. llama_print_timings(ctx);
  52. llama_free(ctx);
  53. delete mymodel;
  54. }
  55. bool eval_float(void * model, float * input, int N){
  56. MyModel * mymodel = (MyModel*)model;
  57. llama_context * ctx = mymodel->ctx;
  58. gpt_params params = mymodel->params;
  59. int n_emb = llama_n_embd(ctx);
  60. int n_past = mymodel->n_past;
  61. int n_batch = N; // params.n_batch;
  62. for (int i = 0; i < (int) N; i += n_batch) {
  63. int n_eval = (int) N - i;
  64. if (n_eval > n_batch) {
  65. n_eval = n_batch;
  66. }
  67. if (llama_eval_embd(ctx, (input+i*n_emb), n_eval, n_past, params.n_threads)) {
  68. fprintf(stderr, "%s : failed to eval\n", __func__);
  69. return false;
  70. }
  71. n_past += n_eval;
  72. }
  73. mymodel->n_past = n_past;
  74. return true;
  75. }
  76. bool eval_tokens(void * model, std::vector<llama_token> tokens) {
  77. MyModel * mymodel = (MyModel* )model;
  78. llama_context * ctx;
  79. ctx = mymodel->ctx;
  80. gpt_params params = mymodel->params;
  81. int n_past = mymodel->n_past;
  82. for (int i = 0; i < (int) tokens.size(); i += params.n_batch) {
  83. int n_eval = (int) tokens.size() - i;
  84. if (n_eval > params.n_batch) {
  85. n_eval = params.n_batch;
  86. }
  87. if (llama_eval(ctx, &tokens[i], n_eval, n_past, params.n_threads)) {
  88. fprintf(stderr, "%s : failed to eval\n", __func__);
  89. return false;
  90. }
  91. n_past += n_eval;
  92. }
  93. mymodel->n_past = n_past;
  94. return true;
  95. }
  96. bool eval_id(struct MyModel* mymodel, int id) {
  97. std::vector<llama_token> tokens;
  98. tokens.push_back(id);
  99. return eval_tokens(mymodel, tokens);
  100. }
  101. bool eval_string(struct MyModel * mymodel,const char* str){
  102. llama_context * ctx = mymodel->ctx;
  103. std::string str2 = str;
  104. std::vector<llama_token> embd_inp = ::llama_tokenize(ctx, str2, true);
  105. eval_tokens(mymodel, embd_inp);
  106. return true;
  107. }
  108. llama_token sampling_id(struct MyModel* mymodel) {
  109. llama_context* ctx = mymodel->ctx;
  110. gpt_params params = mymodel->params;
  111. // int n_ctx = llama_n_ctx(ctx);
  112. // out of user input, sample next token
  113. const float temp = params.temp;
  114. const int32_t top_k = params.top_k <= 0 ? llama_n_vocab(ctx) : params.top_k;
  115. const float top_p = params.top_p;
  116. const float tfs_z = params.tfs_z;
  117. const float typical_p = params.typical_p;
  118. // const int32_t repeat_last_n = params.repeat_last_n < 0 ? n_ctx : params.repeat_last_n;
  119. // const float repeat_penalty = params.repeat_penalty;
  120. // const float alpha_presence = params.presence_penalty;
  121. // const float alpha_frequency = params.frequency_penalty;
  122. const int mirostat = params.mirostat;
  123. const float mirostat_tau = params.mirostat_tau;
  124. const float mirostat_eta = params.mirostat_eta;
  125. // const bool penalize_nl = params.penalize_nl;
  126. llama_token id = 0;
  127. {
  128. auto logits = llama_get_logits(ctx);
  129. auto n_vocab = llama_n_vocab(ctx);
  130. // Apply params.logit_bias map
  131. for (auto it = params.logit_bias.begin(); it != params.logit_bias.end(); it++) {
  132. logits[it->first] += it->second;
  133. }
  134. std::vector<llama_token_data> candidates;
  135. candidates.reserve(n_vocab);
  136. for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
  137. candidates.emplace_back(llama_token_data{token_id, logits[token_id], 0.0f});
  138. }
  139. llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
  140. // TODO: Apply penalties
  141. // float nl_logit = logits[llama_token_nl(ctx)];
  142. // auto last_n_repeat = std::min(std::min((int)last_n_tokens.size(), repeat_last_n), n_ctx);
  143. // llama_sample_repetition_penalty(ctx, &candidates_p,
  144. // last_n_tokens.data() + last_n_tokens.size() - last_n_repeat,
  145. // last_n_repeat, repeat_penalty);
  146. // llama_sample_frequency_and_presence_penalties(ctx, &candidates_p,
  147. // last_n_tokens.data() + last_n_tokens.size() - last_n_repeat,
  148. // last_n_repeat, alpha_frequency, alpha_presence);
  149. // if (!penalize_nl) {
  150. // logits[llama_token_nl(ctx)] = nl_logit;
  151. // }
  152. if (temp <= 0) {
  153. // Greedy sampling
  154. id = llama_sample_token_greedy(ctx, &candidates_p);
  155. } else {
  156. if (mirostat == 1) {
  157. static float mirostat_mu = 2.0f * mirostat_tau;
  158. const int mirostat_m = 100;
  159. llama_sample_temperature(ctx, &candidates_p, temp);
  160. id = llama_sample_token_mirostat(ctx, &candidates_p, mirostat_tau, mirostat_eta, mirostat_m, &mirostat_mu);
  161. } else if (mirostat == 2) {
  162. static float mirostat_mu = 2.0f * mirostat_tau;
  163. llama_sample_temperature(ctx, &candidates_p, temp);
  164. id = llama_sample_token_mirostat_v2(ctx, &candidates_p, mirostat_tau, mirostat_eta, &mirostat_mu);
  165. } else {
  166. // Temperature sampling
  167. llama_sample_top_k(ctx, &candidates_p, top_k, 1);
  168. llama_sample_tail_free(ctx, &candidates_p, tfs_z, 1);
  169. llama_sample_typical(ctx, &candidates_p, typical_p, 1);
  170. llama_sample_top_p(ctx, &candidates_p, top_p, 1);
  171. llama_sample_temperature(ctx, &candidates_p, temp);
  172. id = llama_sample_token(ctx, &candidates_p);
  173. }
  174. }
  175. }
  176. return id;
  177. }
  178. const char * sampling(struct MyModel * mymodel) {
  179. llama_context * ctx = mymodel->ctx;
  180. int id = sampling_id(mymodel);
  181. static std::string ret;
  182. if (id == llama_token_eos(ctx)) {
  183. ret = "</s>";
  184. } else {
  185. ret = llama_token_to_piece(ctx, id);
  186. }
  187. eval_id(mymodel, id);
  188. return ret.c_str();
  189. }
  190. }