1
0

embd-input-lib.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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, "%s\n", get_system_info(params).c_str());
  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(llama_get_model(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. llama_batch batch = { int32_t(n_eval), nullptr, (input+i*n_emb), nullptr, nullptr, nullptr, n_past, 1, 0, };
  67. if (llama_decode(ctx, batch)) {
  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_decode(ctx, llama_batch_get_one(&tokens[i], n_eval, n_past, 0))) {
  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. llama_sampling_params & sparams = params.sampling_params;
  112. // int n_ctx = llama_n_ctx(ctx);
  113. // out of user input, sample next token
  114. const float temp = sparams.temp;
  115. const int32_t top_k = sparams.top_k <= 0 ? llama_n_vocab(llama_get_model(ctx)) : sparams.top_k;
  116. const float top_p = sparams.top_p;
  117. const float tfs_z = sparams.tfs_z;
  118. const float typical_p = sparams.typical_p;
  119. // const int32_t repeat_last_n = params.repeat_last_n < 0 ? n_ctx : params.repeat_last_n;
  120. // const float repeat_penalty = params.repeat_penalty;
  121. // const float alpha_presence = params.presence_penalty;
  122. // const float alpha_frequency = params.frequency_penalty;
  123. const int mirostat = sparams.mirostat;
  124. const float mirostat_tau = sparams.mirostat_tau;
  125. const float mirostat_eta = sparams.mirostat_eta;
  126. // const bool penalize_nl = params.penalize_nl;
  127. llama_token id = 0;
  128. {
  129. auto logits = llama_get_logits(ctx);
  130. auto n_vocab = llama_n_vocab(llama_get_model(ctx));
  131. // Apply params.logit_bias map
  132. for (auto it = sparams.logit_bias.begin(); it != sparams.logit_bias.end(); it++) {
  133. logits[it->first] += it->second;
  134. }
  135. std::vector<llama_token_data> candidates;
  136. candidates.reserve(n_vocab);
  137. for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
  138. candidates.emplace_back(llama_token_data{token_id, logits[token_id], 0.0f});
  139. }
  140. llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
  141. // TODO: Apply penalties
  142. // float nl_logit = logits[llama_token_nl(ctx)];
  143. // auto last_n_repeat = std::min(std::min((int)last_n_tokens.size(), repeat_last_n), n_ctx);
  144. // llama_sample_repetition_penalty(ctx, &candidates_p,
  145. // last_n_tokens.data() + last_n_tokens.size() - last_n_repeat,
  146. // last_n_repeat, repeat_penalty);
  147. // llama_sample_frequency_and_presence_penalties(ctx, &candidates_p,
  148. // last_n_tokens.data() + last_n_tokens.size() - last_n_repeat,
  149. // last_n_repeat, alpha_frequency, alpha_presence);
  150. // if (!penalize_nl) {
  151. // logits[llama_token_nl(ctx)] = nl_logit;
  152. // }
  153. if (temp <= 0) {
  154. // Greedy sampling
  155. id = llama_sample_token_greedy(ctx, &candidates_p);
  156. } else {
  157. if (mirostat == 1) {
  158. static float mirostat_mu = 2.0f * mirostat_tau;
  159. const int mirostat_m = 100;
  160. llama_sample_temp(ctx, &candidates_p, temp);
  161. id = llama_sample_token_mirostat(ctx, &candidates_p, mirostat_tau, mirostat_eta, mirostat_m, &mirostat_mu);
  162. } else if (mirostat == 2) {
  163. static float mirostat_mu = 2.0f * mirostat_tau;
  164. llama_sample_temp(ctx, &candidates_p, temp);
  165. id = llama_sample_token_mirostat_v2(ctx, &candidates_p, mirostat_tau, mirostat_eta, &mirostat_mu);
  166. } else {
  167. // Temperature sampling
  168. llama_sample_top_k(ctx, &candidates_p, top_k, 1);
  169. llama_sample_tail_free(ctx, &candidates_p, tfs_z, 1);
  170. llama_sample_typical(ctx, &candidates_p, typical_p, 1);
  171. llama_sample_top_p(ctx, &candidates_p, top_p, 1);
  172. llama_sample_temp(ctx, &candidates_p, temp);
  173. id = llama_sample_token(ctx, &candidates_p);
  174. }
  175. }
  176. }
  177. return id;
  178. }
  179. const char * sampling(struct MyModel * mymodel) {
  180. llama_context * ctx = mymodel->ctx;
  181. int id = sampling_id(mymodel);
  182. static std::string ret;
  183. if (id == llama_token_eos(ctx)) {
  184. ret = "</s>";
  185. } else {
  186. ret = llama_token_to_piece(ctx, id);
  187. }
  188. eval_id(mymodel, id);
  189. return ret.c_str();
  190. }
  191. }