embd-input-lib.cpp 7.2 KB

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