embedding.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "build-info.h"
  2. #include "common.h"
  3. #include "llama.h"
  4. #include <ctime>
  5. #if defined(_MSC_VER)
  6. #pragma warning(disable: 4244 4267) // possible loss of data
  7. #endif
  8. int main(int argc, char ** argv) {
  9. gpt_params params;
  10. if (!gpt_params_parse(argc, argv, params)) {
  11. return 1;
  12. }
  13. params.embedding = true;
  14. print_build_info();
  15. if (params.seed == LLAMA_DEFAULT_SEED) {
  16. params.seed = time(NULL);
  17. }
  18. fprintf(stderr, "%s: seed = %u\n", __func__, params.seed);
  19. std::mt19937 rng(params.seed);
  20. if (params.random_prompt) {
  21. params.prompt = gpt_random_prompt(rng);
  22. }
  23. llama_backend_init(params.numa);
  24. llama_model * model;
  25. llama_context * ctx;
  26. // load the model
  27. std::tie(model, ctx) = llama_init_from_gpt_params(params);
  28. if (model == NULL) {
  29. fprintf(stderr, "%s: error: unable to load model\n", __func__);
  30. return 1;
  31. }
  32. const int n_ctx_train = llama_n_ctx_train(model);
  33. const int n_ctx = llama_n_ctx(ctx);
  34. if (n_ctx > n_ctx_train) {
  35. fprintf(stderr, "%s: warning: model was trained on only %d context tokens (%d specified)\n",
  36. __func__, n_ctx_train, n_ctx);
  37. }
  38. // print system information
  39. {
  40. fprintf(stderr, "\n");
  41. fprintf(stderr, "%s\n", get_system_info(params).c_str());
  42. }
  43. int n_past = 0;
  44. // tokenize the prompt
  45. auto embd_inp = ::llama_tokenize(ctx, params.prompt, true);
  46. if (params.verbose_prompt) {
  47. fprintf(stderr, "\n");
  48. fprintf(stderr, "%s: prompt: '%s'\n", __func__, params.prompt.c_str());
  49. fprintf(stderr, "%s: number of tokens in prompt = %zu\n", __func__, embd_inp.size());
  50. for (int i = 0; i < (int) embd_inp.size(); i++) {
  51. fprintf(stderr, "%6d -> '%s'\n", embd_inp[i], llama_token_to_piece(ctx, embd_inp[i]).c_str());
  52. }
  53. fprintf(stderr, "\n");
  54. }
  55. if (embd_inp.size() > (size_t)n_ctx) {
  56. fprintf(stderr, "%s: error: prompt is longer than the context window (%zu tokens, n_ctx = %d)\n",
  57. __func__, embd_inp.size(), n_ctx);
  58. return 1;
  59. }
  60. while (!embd_inp.empty()) {
  61. int n_tokens = std::min(params.n_batch, (int) embd_inp.size());
  62. if (llama_decode(ctx, llama_batch_get_one(embd_inp.data(), n_tokens, n_past, 0))) {
  63. fprintf(stderr, "%s : failed to eval\n", __func__);
  64. return 1;
  65. }
  66. n_past += n_tokens;
  67. embd_inp.erase(embd_inp.begin(), embd_inp.begin() + n_tokens);
  68. }
  69. const int n_embd = llama_n_embd(model);
  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. llama_print_timings(ctx);
  76. llama_free(ctx);
  77. llama_free_model(model);
  78. llama_backend_free();
  79. return 0;
  80. }