logits.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #include "llama.h"
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <string>
  5. #include <vector>
  6. #include <ctype.h>
  7. #include <filesystem>
  8. static void print_usage(int, char ** argv) {
  9. printf("\nexample usage:\n");
  10. printf("\n %s -m model.gguf [-ngl n_gpu_layers] -embd-mode [prompt]\n", argv[0]);
  11. printf("\n");
  12. }
  13. int main(int argc, char ** argv) {
  14. std::string model_path;
  15. std::string prompt = "Hello, my name is";
  16. int ngl = 0;
  17. bool embedding_mode = false;
  18. {
  19. int i = 1;
  20. for (; i < argc; i++) {
  21. if (strcmp(argv[i], "-m") == 0) {
  22. if (i + 1 < argc) {
  23. model_path = argv[++i];
  24. } else {
  25. print_usage(argc, argv);
  26. return 1;
  27. }
  28. } else if (strcmp(argv[i], "-ngl") == 0) {
  29. if (i + 1 < argc) {
  30. try {
  31. ngl = std::stoi(argv[++i]);
  32. } catch (...) {
  33. print_usage(argc, argv);
  34. return 1;
  35. }
  36. } else {
  37. print_usage(argc, argv);
  38. return 1;
  39. }
  40. } else if (strcmp(argv[i], "-embd-mode") == 0) {
  41. if (i + 1 < argc) {
  42. try {
  43. embedding_mode = true;
  44. } catch (...) {
  45. print_usage(argc, argv);
  46. return 1;
  47. }
  48. } else {
  49. print_usage(argc, argv);
  50. return 1;
  51. }
  52. } else {
  53. // prompt starts here
  54. break;
  55. }
  56. }
  57. if (model_path.empty()) {
  58. print_usage(argc, argv);
  59. return 1;
  60. }
  61. if (i < argc) {
  62. prompt = argv[i++];
  63. for (; i < argc; i++) {
  64. prompt += " ";
  65. prompt += argv[i];
  66. }
  67. }
  68. }
  69. ggml_backend_load_all();
  70. llama_model_params model_params = llama_model_default_params();
  71. model_params.n_gpu_layers = ngl;
  72. llama_model * model = llama_model_load_from_file(model_path.c_str(), model_params);
  73. if (model == NULL) {
  74. fprintf(stderr , "%s: error: unable to load model\n" , __func__);
  75. return 1;
  76. }
  77. // Extract basename from model_path
  78. const char * basename = strrchr(model_path.c_str(), '/');
  79. basename = (basename == NULL) ? model_path.c_str() : basename + 1;
  80. char model_name[256];
  81. strncpy(model_name, basename, 255);
  82. model_name[255] = '\0';
  83. char * dot = strrchr(model_name, '.');
  84. if (dot != NULL && strcmp(dot, ".gguf") == 0) {
  85. *dot = '\0';
  86. }
  87. printf("Model name: %s\n", model_name);
  88. const llama_vocab * vocab = llama_model_get_vocab(model);
  89. const int n_prompt = -llama_tokenize(vocab, prompt.c_str(), prompt.size(), NULL, 0, true, true);
  90. std::vector<llama_token> prompt_tokens(n_prompt);
  91. if (llama_tokenize(vocab, prompt.c_str(), prompt.size(), prompt_tokens.data(), prompt_tokens.size(), true, true) < 0) {
  92. fprintf(stderr, "%s: error: failed to tokenize the prompt\n", __func__);
  93. return 1;
  94. }
  95. llama_context_params ctx_params = llama_context_default_params();
  96. ctx_params.n_ctx = n_prompt;
  97. ctx_params.n_batch = n_prompt;
  98. ctx_params.no_perf = false;
  99. if (embedding_mode) {
  100. ctx_params.embeddings = true;
  101. ctx_params.n_ubatch = ctx_params.n_batch;
  102. }
  103. llama_context * ctx = llama_init_from_model(model, ctx_params);
  104. if (ctx == NULL) {
  105. fprintf(stderr , "%s: error: failed to create the llama_context\n" , __func__);
  106. return 1;
  107. }
  108. printf("Input prompt: \"%s\"\n", prompt.c_str());
  109. printf("Tokenized prompt (%d tokens): ", n_prompt);
  110. for (auto id : prompt_tokens) {
  111. char buf[128];
  112. int n = llama_token_to_piece(vocab, id, buf, sizeof(buf), 0, true);
  113. if (n < 0) {
  114. fprintf(stderr, "%s: error: failed to convert token to piece\n", __func__);
  115. return 1;
  116. }
  117. std::string s(buf, n);
  118. printf("%s", s.c_str());
  119. }
  120. printf("\n");
  121. llama_batch batch = llama_batch_get_one(prompt_tokens.data(), prompt_tokens.size());
  122. if (llama_decode(ctx, batch)) {
  123. fprintf(stderr, "%s : failed to eval\n", __func__);
  124. return 1;
  125. }
  126. float * logits;
  127. int n_logits;
  128. const char * type;
  129. if (embedding_mode) {
  130. logits = llama_get_embeddings(ctx);
  131. n_logits = llama_model_n_embd(model) * batch.n_tokens;
  132. type = "-embeddings";
  133. printf("Embeddings size: %d\n", n_logits);
  134. } else {
  135. logits = llama_get_logits_ith(ctx, batch.n_tokens - 1);
  136. n_logits = llama_vocab_n_tokens(vocab);
  137. type = "";
  138. printf("Vocab size: %d\n", n_logits);
  139. }
  140. std::filesystem::create_directory("data");
  141. // Save logits to binary file
  142. char bin_filename[512];
  143. snprintf(bin_filename, sizeof(bin_filename), "data/llamacpp-%s%s.bin", model_name, type);
  144. printf("Saving logits to %s\n", bin_filename);
  145. FILE * f = fopen(bin_filename, "wb");
  146. if (f == NULL) {
  147. fprintf(stderr, "%s: error: failed to open binary output file\n", __func__);
  148. return 1;
  149. }
  150. fwrite(logits, sizeof(float), n_logits, f);
  151. fclose(f);
  152. // Also save as text for debugging
  153. char txt_filename[512];
  154. snprintf(txt_filename, sizeof(txt_filename), "data/llamacpp-%s%s.txt", model_name, type);
  155. f = fopen(txt_filename, "w");
  156. if (f == NULL) {
  157. fprintf(stderr, "%s: error: failed to open text output file\n", __func__);
  158. return 1;
  159. }
  160. for (int i = 0; i < n_logits; i++) {
  161. fprintf(f, "%d: %.6f\n", i, logits[i]); // Added index and changed format
  162. }
  163. fclose(f);
  164. // Print first and last 10 logits for quick verification
  165. printf("First 10 logits: ");
  166. for (int i = 0; i < 10 && i < n_logits; i++) {
  167. printf("%.6f ", logits[i]);
  168. }
  169. printf("\n");
  170. printf("Last 10 logits: ");
  171. for (int i = n_logits - 10; i < n_logits; i++) {
  172. if (i >= 0) printf("%.6f ", logits[i]);
  173. }
  174. printf("\n\n");
  175. printf("Logits saved to %s\n", bin_filename);
  176. printf("Logits saved to %s\n", txt_filename);
  177. llama_free(ctx);
  178. llama_model_free(model);
  179. return 0;
  180. }