embedding.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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_mmap = params.use_mmap;
  32. lparams.use_mlock = params.use_mlock;
  33. lparams.embedding = params.embedding;
  34. ctx = llama_init_from_file(params.model.c_str(), lparams);
  35. if (ctx == NULL) {
  36. fprintf(stderr, "%s: error: failed to load model '%s'\n", __func__, params.model.c_str());
  37. return 1;
  38. }
  39. }
  40. // print system information
  41. {
  42. fprintf(stderr, "\n");
  43. fprintf(stderr, "system_info: n_threads = %d / %d | %s\n",
  44. params.n_threads, std::thread::hardware_concurrency(), llama_print_system_info());
  45. }
  46. int n_past = 0;
  47. // Add a space in front of the first character to match OG llama tokenizer behavior
  48. params.prompt.insert(0, 1, ' ');
  49. // tokenize the prompt
  50. auto embd_inp = ::llama_tokenize(ctx, params.prompt, true);
  51. // determine newline token
  52. auto llama_token_newline = ::llama_tokenize(ctx, "\n", false);
  53. if (params.verbose_prompt) {
  54. fprintf(stderr, "\n");
  55. fprintf(stderr, "%s: prompt: '%s'\n", __func__, params.prompt.c_str());
  56. fprintf(stderr, "%s: number of tokens in prompt = %zu\n", __func__, embd_inp.size());
  57. for (int i = 0; i < (int) embd_inp.size(); i++) {
  58. fprintf(stderr, "%6d -> '%s'\n", embd_inp[i], llama_token_to_str(ctx, embd_inp[i]));
  59. }
  60. fprintf(stderr, "\n");
  61. }
  62. if (params.embedding){
  63. if (embd_inp.size() > 0) {
  64. if (llama_eval(ctx, embd_inp.data(), embd_inp.size(), n_past, params.n_threads)) {
  65. fprintf(stderr, "%s : failed to eval\n", __func__);
  66. return 1;
  67. }
  68. }
  69. const int n_embd = llama_n_embd(ctx);
  70. const auto embeddings = llama_get_embeddings(ctx);
  71. for (int i = 0; i < n_embd; i++) {
  72. printf("%f ", embeddings[i]);
  73. }
  74. printf("\n");
  75. }
  76. llama_print_timings(ctx);
  77. llama_free(ctx);
  78. return 0;
  79. }