logits.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.pooling_type = LLAMA_POOLING_TYPE_NONE;
  102. ctx_params.n_ubatch = ctx_params.n_batch;
  103. }
  104. llama_context * ctx = llama_init_from_model(model, ctx_params);
  105. if (ctx == NULL) {
  106. fprintf(stderr , "%s: error: failed to create the llama_context\n" , __func__);
  107. return 1;
  108. }
  109. printf("Input prompt: \"%s\"\n", prompt.c_str());
  110. printf("Tokenized prompt (%d tokens): ", n_prompt);
  111. for (auto id : prompt_tokens) {
  112. char buf[128];
  113. int n = llama_token_to_piece(vocab, id, buf, sizeof(buf), 0, true);
  114. if (n < 0) {
  115. fprintf(stderr, "%s: error: failed to convert token to piece\n", __func__);
  116. return 1;
  117. }
  118. std::string s(buf, n);
  119. printf("%s", s.c_str());
  120. }
  121. printf("\n");
  122. llama_batch batch = llama_batch_get_one(prompt_tokens.data(), prompt_tokens.size());
  123. if (llama_decode(ctx, batch)) {
  124. fprintf(stderr, "%s : failed to eval\n", __func__);
  125. return 1;
  126. }
  127. float * logits;
  128. int n_logits;
  129. const char * type;
  130. if (embedding_mode) {
  131. logits = llama_get_embeddings(ctx);
  132. n_logits = llama_model_n_embd(model) * batch.n_tokens;
  133. type = "-embeddings";
  134. const int n_embd = llama_model_n_embd(model);
  135. const int n_embd_count = batch.n_tokens;
  136. printf("Embedding dimension: %d\n", n_embd);
  137. printf("\n");
  138. // Print embeddings in the specified format
  139. for (int j = 0; j < n_embd_count; j++) {
  140. printf("embedding %d: ", j);
  141. // Print first 3 values
  142. for (int i = 0; i < 3 && i < n_embd; i++) {
  143. printf("%9.6f ", logits[j * n_embd + i]);
  144. }
  145. printf(" ... ");
  146. // Print last 3 values
  147. for (int i = n_embd - 3; i < n_embd; i++) {
  148. if (i >= 0) {
  149. printf("%9.6f ", logits[j * n_embd + i]);
  150. }
  151. }
  152. printf("\n");
  153. }
  154. printf("\n");
  155. printf("Embeddings size: %d\n", n_logits);
  156. } else {
  157. logits = llama_get_logits_ith(ctx, batch.n_tokens - 1);
  158. n_logits = llama_vocab_n_tokens(vocab);
  159. type = "";
  160. printf("Vocab size: %d\n", n_logits);
  161. }
  162. std::filesystem::create_directory("data");
  163. // Save logits to binary file
  164. char bin_filename[512];
  165. snprintf(bin_filename, sizeof(bin_filename), "data/llamacpp-%s%s.bin", model_name, type);
  166. printf("Saving logits to %s\n", bin_filename);
  167. FILE * f = fopen(bin_filename, "wb");
  168. if (f == NULL) {
  169. fprintf(stderr, "%s: error: failed to open binary output file\n", __func__);
  170. return 1;
  171. }
  172. fwrite(logits, sizeof(float), n_logits, f);
  173. fclose(f);
  174. // Also save as text for debugging
  175. char txt_filename[512];
  176. snprintf(txt_filename, sizeof(txt_filename), "data/llamacpp-%s%s.txt", model_name, type);
  177. f = fopen(txt_filename, "w");
  178. if (f == NULL) {
  179. fprintf(stderr, "%s: error: failed to open text output file\n", __func__);
  180. return 1;
  181. }
  182. for (int i = 0; i < n_logits; i++) {
  183. fprintf(f, "%d: %.6f\n", i, logits[i]);
  184. }
  185. fclose(f);
  186. if (!embedding_mode) {
  187. printf("First 10 logits: ");
  188. for (int i = 0; i < 10 && i < n_logits; i++) {
  189. printf("%.6f ", logits[i]);
  190. }
  191. printf("\n");
  192. printf("Last 10 logits: ");
  193. for (int i = n_logits - 10; i < n_logits; i++) {
  194. if (i >= 0) printf("%.6f ", logits[i]);
  195. }
  196. printf("\n\n");
  197. }
  198. printf("Logits saved to %s\n", bin_filename);
  199. printf("Logits saved to %s\n", txt_filename);
  200. llama_free(ctx);
  201. llama_model_free(model);
  202. return 0;
  203. }