embd-input-lib.cpp 7.2 KB

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