utils.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "utils.h"
  2. #include <cassert>
  3. #include <cstring>
  4. #include <fstream>
  5. #include <string>
  6. #include <iterator>
  7. #include <algorithm>
  8. #if defined(_MSC_VER) || defined(__MINGW32__)
  9. #include <malloc.h> // using malloc.h with MSC/MINGW
  10. #elif !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__)
  11. #include <alloca.h>
  12. #endif
  13. bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
  14. // determine sensible default number of threads.
  15. // std::thread::hardware_concurrency may not be equal to the number of cores, or may return 0.
  16. #ifdef __linux__
  17. std::ifstream cpuinfo("/proc/cpuinfo");
  18. params.n_threads = std::count(std::istream_iterator<std::string>(cpuinfo),
  19. std::istream_iterator<std::string>(),
  20. std::string("processor"));
  21. #endif
  22. if (params.n_threads == 0) {
  23. params.n_threads = std::max(1, (int32_t) std::thread::hardware_concurrency());
  24. }
  25. for (int i = 1; i < argc; i++) {
  26. std::string arg = argv[i];
  27. if (arg == "-s" || arg == "--seed") {
  28. params.seed = std::stoi(argv[++i]);
  29. } else if (arg == "-t" || arg == "--threads") {
  30. params.n_threads = std::stoi(argv[++i]);
  31. } else if (arg == "-p" || arg == "--prompt") {
  32. params.prompt = argv[++i];
  33. } else if (arg == "-f" || arg == "--file") {
  34. std::ifstream file(argv[++i]);
  35. std::copy(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), back_inserter(params.prompt));
  36. if (params.prompt.back() == '\n') {
  37. params.prompt.pop_back();
  38. }
  39. } else if (arg == "-n" || arg == "--n_predict") {
  40. params.n_predict = std::stoi(argv[++i]);
  41. } else if (arg == "--top_k") {
  42. params.top_k = std::stoi(argv[++i]);
  43. } else if (arg == "-c" || arg == "--ctx_size") {
  44. params.n_ctx = std::stoi(argv[++i]);
  45. } else if (arg == "--memory_f16") {
  46. params.memory_f16 = true;
  47. } else if (arg == "--top_p") {
  48. params.top_p = std::stof(argv[++i]);
  49. } else if (arg == "--temp") {
  50. params.temp = std::stof(argv[++i]);
  51. } else if (arg == "--repeat_last_n") {
  52. params.repeat_last_n = std::stoi(argv[++i]);
  53. } else if (arg == "--repeat_penalty") {
  54. params.repeat_penalty = std::stof(argv[++i]);
  55. } else if (arg == "-b" || arg == "--batch_size") {
  56. params.n_batch = std::stoi(argv[++i]);
  57. } else if (arg == "-m" || arg == "--model") {
  58. params.model = argv[++i];
  59. } else if (arg == "-i" || arg == "--interactive") {
  60. params.interactive = true;
  61. } else if (arg == "-ins" || arg == "--instruct") {
  62. params.instruct = true;
  63. } else if (arg == "--color") {
  64. params.use_color = true;
  65. } else if (arg == "-r" || arg == "--reverse-prompt") {
  66. params.antiprompt.push_back(argv[++i]);
  67. } else if (arg == "--perplexity") {
  68. params.perplexity = true;
  69. } else if (arg == "--ignore-eos") {
  70. params.ignore_eos = true;
  71. } else if (arg == "--n_parts") {
  72. params.n_parts = std::stoi(argv[++i]);
  73. } else if (arg == "-h" || arg == "--help") {
  74. gpt_print_usage(argc, argv, params);
  75. exit(0);
  76. } else if (arg == "--random-prompt") {
  77. params.random_prompt = true;
  78. } else {
  79. fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
  80. gpt_print_usage(argc, argv, params);
  81. exit(0);
  82. }
  83. }
  84. return true;
  85. }
  86. void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {
  87. fprintf(stderr, "usage: %s [options]\n", argv[0]);
  88. fprintf(stderr, "\n");
  89. fprintf(stderr, "options:\n");
  90. fprintf(stderr, " -h, --help show this help message and exit\n");
  91. fprintf(stderr, " -i, --interactive run in interactive mode\n");
  92. fprintf(stderr, " -ins, --instruct run in instruction mode (use with Alpaca models)\n");
  93. fprintf(stderr, " -r PROMPT, --reverse-prompt PROMPT\n");
  94. fprintf(stderr, " in interactive mode, poll user input upon seeing PROMPT (can be\n");
  95. fprintf(stderr, " specified more than once for multiple prompts).\n");
  96. fprintf(stderr, " --color colorise output to distinguish prompt and user input from generations\n");
  97. fprintf(stderr, " -s SEED, --seed SEED RNG seed (default: -1)\n");
  98. fprintf(stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n", params.n_threads);
  99. fprintf(stderr, " -p PROMPT, --prompt PROMPT\n");
  100. fprintf(stderr, " prompt to start generation with (default: empty)\n");
  101. fprintf(stderr, " --random-prompt start with a randomized prompt.\n");
  102. fprintf(stderr, " -f FNAME, --file FNAME\n");
  103. fprintf(stderr, " prompt file to start generation.\n");
  104. fprintf(stderr, " -n N, --n_predict N number of tokens to predict (default: %d)\n", params.n_predict);
  105. fprintf(stderr, " --top_k N top-k sampling (default: %d)\n", params.top_k);
  106. fprintf(stderr, " --top_p N top-p sampling (default: %.1f)\n", params.top_p);
  107. fprintf(stderr, " --repeat_last_n N last n tokens to consider for penalize (default: %d)\n", params.repeat_last_n);
  108. fprintf(stderr, " --repeat_penalty N penalize repeat sequence of tokens (default: %.1f)\n", params.repeat_penalty);
  109. fprintf(stderr, " -c N, --ctx_size N size of the prompt context (default: %d)\n", params.n_ctx);
  110. fprintf(stderr, " --ignore-eos ignore end of stream token and continue generating\n");
  111. fprintf(stderr, " --memory_f16 use f16 instead of f32 for memory key+value\n");
  112. fprintf(stderr, " --temp N temperature (default: %.1f)\n", params.temp);
  113. fprintf(stderr, " --n_parts N number of model parts (default: -1 = determine from dimensions)\n");
  114. fprintf(stderr, " -b N, --batch_size N batch size for prompt processing (default: %d)\n", params.n_batch);
  115. fprintf(stderr, " --perplexity compute perplexity over the prompt\n");
  116. fprintf(stderr, " -m FNAME, --model FNAME\n");
  117. fprintf(stderr, " model path (default: %s)\n", params.model.c_str());
  118. fprintf(stderr, "\n");
  119. }
  120. std::string gpt_random_prompt(std::mt19937 & rng) {
  121. const int r = rng() % 10;
  122. switch (r) {
  123. case 0: return "So";
  124. case 1: return "Once upon a time";
  125. case 2: return "When";
  126. case 3: return "The";
  127. case 4: return "After";
  128. case 5: return "If";
  129. case 6: return "import";
  130. case 7: return "He";
  131. case 8: return "She";
  132. case 9: return "They";
  133. default: return "To";
  134. }
  135. return "The";
  136. }
  137. // TODO: not great allocating this every time
  138. std::vector<llama_token> llama_tokenize(struct llama_context * ctx, const std::string & text, bool add_bos) {
  139. std::vector<llama_token> res(8096);
  140. int n = llama_tokenize(ctx, text.c_str(), res.data(), res.size(), add_bos);
  141. res.resize(n);
  142. return res;
  143. }