utils.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. bool invalid_param = false;
  26. std::string arg;
  27. for (int i = 1; i < argc; i++) {
  28. arg = argv[i];
  29. if (arg == "-s" || arg == "--seed") {
  30. if (++i >= argc) {
  31. invalid_param = true;
  32. break;
  33. }
  34. params.seed = std::stoi(argv[i]);
  35. } else if (arg == "-t" || arg == "--threads") {
  36. if (++i >= argc) {
  37. invalid_param = true;
  38. break;
  39. }
  40. params.n_threads = std::stoi(argv[i]);
  41. } else if (arg == "-p" || arg == "--prompt") {
  42. if (++i >= argc) {
  43. invalid_param = true;
  44. break;
  45. }
  46. params.prompt = argv[i];
  47. } else if (arg == "-f" || arg == "--file") {
  48. if (++i >= argc) {
  49. invalid_param = true;
  50. break;
  51. }
  52. std::ifstream file(argv[i]);
  53. std::copy(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), back_inserter(params.prompt));
  54. if (params.prompt.back() == '\n') {
  55. params.prompt.pop_back();
  56. }
  57. } else if (arg == "-n" || arg == "--n_predict") {
  58. if (++i >= argc) {
  59. invalid_param = true;
  60. break;
  61. }
  62. params.n_predict = std::stoi(argv[i]);
  63. } else if (arg == "--top_k") {
  64. if (++i >= argc) {
  65. invalid_param = true;
  66. break;
  67. }
  68. params.top_k = std::stoi(argv[i]);
  69. } else if (arg == "-c" || arg == "--ctx_size") {
  70. if (++i >= argc) {
  71. invalid_param = true;
  72. break;
  73. }
  74. params.n_ctx = std::stoi(argv[i]);
  75. } else if (arg == "--memory_f16") {
  76. params.memory_f16 = true;
  77. } else if (arg == "--top_p") {
  78. if (++i >= argc) {
  79. invalid_param = true;
  80. break;
  81. }
  82. params.top_p = std::stof(argv[i]);
  83. } else if (arg == "--temp") {
  84. if (++i >= argc) {
  85. invalid_param = true;
  86. break;
  87. }
  88. params.temp = std::stof(argv[i]);
  89. } else if (arg == "--repeat_last_n") {
  90. if (++i >= argc) {
  91. invalid_param = true;
  92. break;
  93. }
  94. params.repeat_last_n = std::stoi(argv[i]);
  95. } else if (arg == "--repeat_penalty") {
  96. if (++i >= argc) {
  97. invalid_param = true;
  98. break;
  99. }
  100. params.repeat_penalty = std::stof(argv[i]);
  101. } else if (arg == "-b" || arg == "--batch_size") {
  102. if (++i >= argc) {
  103. invalid_param = true;
  104. break;
  105. }
  106. params.n_batch = std::stoi(argv[i]);
  107. } else if (arg == "-m" || arg == "--model") {
  108. if (++i >= argc) {
  109. invalid_param = true;
  110. break;
  111. }
  112. params.model = argv[i];
  113. } else if (arg == "-i" || arg == "--interactive") {
  114. params.interactive = true;
  115. } else if (arg == "--embedding") {
  116. params.embedding = true;
  117. } else if (arg == "--interactive-start") {
  118. params.interactive = true;
  119. } else if (arg == "--interactive-first") {
  120. params.interactive_start = true;
  121. } else if (arg == "-ins" || arg == "--instruct") {
  122. params.instruct = true;
  123. } else if (arg == "--color") {
  124. params.use_color = true;
  125. } else if (arg == "-r" || arg == "--reverse-prompt") {
  126. if (++i >= argc) {
  127. invalid_param = true;
  128. break;
  129. }
  130. params.antiprompt.push_back(argv[i]);
  131. } else if (arg == "--perplexity") {
  132. params.perplexity = true;
  133. } else if (arg == "--ignore-eos") {
  134. params.ignore_eos = true;
  135. } else if (arg == "--n_parts") {
  136. if (++i >= argc) {
  137. invalid_param = true;
  138. break;
  139. }
  140. params.n_parts = std::stoi(argv[i]);
  141. } else if (arg == "-h" || arg == "--help") {
  142. gpt_print_usage(argc, argv, params);
  143. exit(0);
  144. } else if (arg == "--random-prompt") {
  145. params.random_prompt = true;
  146. } else {
  147. fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
  148. gpt_print_usage(argc, argv, params);
  149. exit(1);
  150. }
  151. }
  152. if (invalid_param) {
  153. fprintf(stderr, "error: invalid parameter for argument: %s\n", arg.c_str());
  154. gpt_print_usage(argc, argv, params);
  155. exit(1);
  156. }
  157. return true;
  158. }
  159. void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {
  160. fprintf(stderr, "usage: %s [options]\n", argv[0]);
  161. fprintf(stderr, "\n");
  162. fprintf(stderr, "options:\n");
  163. fprintf(stderr, " -h, --help show this help message and exit\n");
  164. fprintf(stderr, " -i, --interactive run in interactive mode\n");
  165. fprintf(stderr, " --interactive-first run in interactive mode and wait for input right away\n");
  166. fprintf(stderr, " -ins, --instruct run in instruction mode (use with Alpaca models)\n");
  167. fprintf(stderr, " -r PROMPT, --reverse-prompt PROMPT\n");
  168. fprintf(stderr, " run in interactive mode and poll user input upon seeing PROMPT (can be\n");
  169. fprintf(stderr, " specified more than once for multiple prompts).\n");
  170. fprintf(stderr, " --color colorise output to distinguish prompt and user input from generations\n");
  171. fprintf(stderr, " -s SEED, --seed SEED RNG seed (default: -1, use random seed for <= 0)\n");
  172. fprintf(stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n", params.n_threads);
  173. fprintf(stderr, " -p PROMPT, --prompt PROMPT\n");
  174. fprintf(stderr, " prompt to start generation with (default: empty)\n");
  175. fprintf(stderr, " --random-prompt start with a randomized prompt.\n");
  176. fprintf(stderr, " -f FNAME, --file FNAME\n");
  177. fprintf(stderr, " prompt file to start generation.\n");
  178. fprintf(stderr, " -n N, --n_predict N number of tokens to predict (default: %d)\n", params.n_predict);
  179. fprintf(stderr, " --top_k N top-k sampling (default: %d)\n", params.top_k);
  180. fprintf(stderr, " --top_p N top-p sampling (default: %.1f)\n", params.top_p);
  181. fprintf(stderr, " --repeat_last_n N last n tokens to consider for penalize (default: %d)\n", params.repeat_last_n);
  182. fprintf(stderr, " --repeat_penalty N penalize repeat sequence of tokens (default: %.1f)\n", params.repeat_penalty);
  183. fprintf(stderr, " -c N, --ctx_size N size of the prompt context (default: %d)\n", params.n_ctx);
  184. fprintf(stderr, " --ignore-eos ignore end of stream token and continue generating\n");
  185. fprintf(stderr, " --memory_f16 use f16 instead of f32 for memory key+value\n");
  186. fprintf(stderr, " --temp N temperature (default: %.1f)\n", params.temp);
  187. fprintf(stderr, " --n_parts N number of model parts (default: -1 = determine from dimensions)\n");
  188. fprintf(stderr, " -b N, --batch_size N batch size for prompt processing (default: %d)\n", params.n_batch);
  189. fprintf(stderr, " --perplexity compute perplexity over the prompt\n");
  190. fprintf(stderr, " -m FNAME, --model FNAME\n");
  191. fprintf(stderr, " model path (default: %s)\n", params.model.c_str());
  192. fprintf(stderr, "\n");
  193. }
  194. std::string gpt_random_prompt(std::mt19937 & rng) {
  195. const int r = rng() % 10;
  196. switch (r) {
  197. case 0: return "So";
  198. case 1: return "Once upon a time";
  199. case 2: return "When";
  200. case 3: return "The";
  201. case 4: return "After";
  202. case 5: return "If";
  203. case 6: return "import";
  204. case 7: return "He";
  205. case 8: return "She";
  206. case 9: return "They";
  207. default: return "To";
  208. }
  209. return "The";
  210. }
  211. // TODO: not great allocating this every time
  212. std::vector<llama_token> llama_tokenize(struct llama_context * ctx, const std::string & text, bool add_bos) {
  213. // initialize to prompt numer of chars, since n_tokens <= n_prompt_chars
  214. std::vector<llama_token> res(text.size() + (int)add_bos);
  215. int n = llama_tokenize(ctx, text.c_str(), res.data(), res.size(), add_bos);
  216. assert(n >= 0);
  217. res.resize(n);
  218. return res;
  219. }