embedding.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "common.h"
  2. #include "llama.h"
  3. int main(int argc, char ** argv) {
  4. gpt_params params;
  5. params.model = "models/llama-7B/ggml-model.bin";
  6. if (gpt_params_parse(argc, argv, params) == false) {
  7. return 1;
  8. }
  9. params.embedding = true;
  10. if (params.n_ctx > 2048) {
  11. fprintf(stderr, "%s: warning: model does not support context sizes greater than 2048 tokens (%d specified);"
  12. "expect poor results\n", __func__, params.n_ctx);
  13. }
  14. if (params.seed <= 0) {
  15. params.seed = time(NULL);
  16. }
  17. fprintf(stderr, "%s: seed = %d\n", __func__, params.seed);
  18. std::mt19937 rng(params.seed);
  19. if (params.random_prompt) {
  20. params.prompt = gpt_random_prompt(rng);
  21. }
  22. llama_context * ctx;
  23. // load the model
  24. {
  25. auto lparams = llama_context_default_params();
  26. lparams.n_ctx = params.n_ctx;
  27. lparams.n_parts = params.n_parts;
  28. lparams.seed = params.seed;
  29. lparams.f16_kv = params.memory_f16;
  30. lparams.logits_all = params.perplexity;
  31. lparams.use_mlock = params.use_mlock;
  32. lparams.embedding = params.embedding;
  33. ctx = llama_init_from_file(params.model.c_str(), lparams);
  34. if (ctx == NULL) {
  35. fprintf(stderr, "%s: error: failed to load model '%s'\n", __func__, params.model.c_str());
  36. return 1;
  37. }
  38. }
  39. // print system information
  40. {
  41. fprintf(stderr, "\n");
  42. fprintf(stderr, "system_info: n_threads = %d / %d | %s\n",
  43. params.n_threads, std::thread::hardware_concurrency(), llama_print_system_info());
  44. }
  45. int n_past = 0;
  46. // Add a space in front of the first character to match OG llama tokenizer behavior
  47. params.prompt.insert(0, 1, ' ');
  48. // tokenize the prompt
  49. auto embd_inp = ::llama_tokenize(ctx, params.prompt, true);
  50. // determine newline token
  51. auto llama_token_newline = ::llama_tokenize(ctx, "\n", false);
  52. if (params.verbose_prompt) {
  53. fprintf(stderr, "\n");
  54. fprintf(stderr, "%s: prompt: '%s'\n", __func__, params.prompt.c_str());
  55. fprintf(stderr, "%s: number of tokens in prompt = %zu\n", __func__, embd_inp.size());
  56. for (int i = 0; i < (int) embd_inp.size(); i++) {
  57. fprintf(stderr, "%6d -> '%s'\n", embd_inp[i], llama_token_to_str(ctx, embd_inp[i]));
  58. }
  59. fprintf(stderr, "\n");
  60. }
  61. if (params.embedding){
  62. if (embd_inp.size() > 0) {
  63. if (llama_eval(ctx, embd_inp.data(), embd_inp.size(), n_past, params.n_threads)) {
  64. fprintf(stderr, "%s : failed to eval\n", __func__);
  65. return 1;
  66. }
  67. }
  68. const int n_embd = llama_n_embd(ctx);
  69. const auto embeddings = llama_get_embeddings(ctx);
  70. for (int i = 0; i < n_embd; i++) {
  71. printf("%f ", embeddings[i]);
  72. }
  73. printf("\n");
  74. }
  75. llama_print_timings(ctx);
  76. llama_free(ctx);
  77. return 0;
  78. }