embd-input-lib.cpp 7.3 KB

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