common.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include "common.h"
  2. #include "ggml.h"
  3. #include <cassert>
  4. #include <cstring>
  5. #include <fstream>
  6. #include <string>
  7. #include <iterator>
  8. #include <algorithm>
  9. #if defined(_MSC_VER) || defined(__MINGW32__)
  10. #include <malloc.h> // using malloc.h with MSC/MINGW
  11. #elif !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__)
  12. #include <alloca.h>
  13. #endif
  14. bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
  15. // determine sensible default number of threads.
  16. // std::thread::hardware_concurrency may not be equal to the number of cores, or may return 0.
  17. #ifdef __linux__
  18. std::ifstream cpuinfo("/proc/cpuinfo");
  19. params.n_threads = std::count(std::istream_iterator<std::string>(cpuinfo),
  20. std::istream_iterator<std::string>(),
  21. std::string("processor"));
  22. #endif
  23. if (params.n_threads == 0) {
  24. params.n_threads = std::max(1, (int32_t) std::thread::hardware_concurrency());
  25. }
  26. bool invalid_param = false;
  27. std::string arg;
  28. for (int i = 1; i < argc; i++) {
  29. arg = argv[i];
  30. if (arg == "-s" || arg == "--seed") {
  31. if (++i >= argc) {
  32. invalid_param = true;
  33. break;
  34. }
  35. params.seed = std::stoi(argv[i]);
  36. } else if (arg == "-t" || arg == "--threads") {
  37. if (++i >= argc) {
  38. invalid_param = true;
  39. break;
  40. }
  41. params.n_threads = std::stoi(argv[i]);
  42. } else if (arg == "-p" || arg == "--prompt") {
  43. if (++i >= argc) {
  44. invalid_param = true;
  45. break;
  46. }
  47. params.prompt = argv[i];
  48. } else if (arg == "-f" || arg == "--file") {
  49. if (++i >= argc) {
  50. invalid_param = true;
  51. break;
  52. }
  53. std::ifstream file(argv[i]);
  54. std::copy(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), back_inserter(params.prompt));
  55. if (params.prompt.back() == '\n') {
  56. params.prompt.pop_back();
  57. }
  58. } else if (arg == "-n" || arg == "--n_predict") {
  59. if (++i >= argc) {
  60. invalid_param = true;
  61. break;
  62. }
  63. params.n_predict = std::stoi(argv[i]);
  64. } else if (arg == "--top_k") {
  65. if (++i >= argc) {
  66. invalid_param = true;
  67. break;
  68. }
  69. params.top_k = std::stoi(argv[i]);
  70. } else if (arg == "-c" || arg == "--ctx_size") {
  71. if (++i >= argc) {
  72. invalid_param = true;
  73. break;
  74. }
  75. params.n_ctx = std::stoi(argv[i]);
  76. } else if (arg == "--memory_f32") {
  77. params.memory_f16 = false;
  78. } else if (arg == "--top_p") {
  79. if (++i >= argc) {
  80. invalid_param = true;
  81. break;
  82. }
  83. params.top_p = std::stof(argv[i]);
  84. } else if (arg == "--temp") {
  85. if (++i >= argc) {
  86. invalid_param = true;
  87. break;
  88. }
  89. params.temp = std::stof(argv[i]);
  90. } else if (arg == "--repeat_last_n") {
  91. if (++i >= argc) {
  92. invalid_param = true;
  93. break;
  94. }
  95. params.repeat_last_n = std::stoi(argv[i]);
  96. } else if (arg == "--repeat_penalty") {
  97. if (++i >= argc) {
  98. invalid_param = true;
  99. break;
  100. }
  101. params.repeat_penalty = std::stof(argv[i]);
  102. } else if (arg == "-b" || arg == "--batch_size") {
  103. if (++i >= argc) {
  104. invalid_param = true;
  105. break;
  106. }
  107. params.n_batch = std::stoi(argv[i]);
  108. params.n_batch = std::min(512, params.n_batch);
  109. } else if (arg == "--keep") {
  110. if (++i >= argc) {
  111. invalid_param = true;
  112. break;
  113. }
  114. params.n_keep = std::stoi(argv[i]);
  115. } else if (arg == "-m" || arg == "--model") {
  116. if (++i >= argc) {
  117. invalid_param = true;
  118. break;
  119. }
  120. params.model = argv[i];
  121. } else if (arg == "-i" || arg == "--interactive") {
  122. params.interactive = true;
  123. } else if (arg == "--embedding") {
  124. params.embedding = true;
  125. } else if (arg == "--interactive-start") {
  126. params.interactive = true;
  127. } else if (arg == "--interactive-first") {
  128. params.interactive_start = true;
  129. } else if (arg == "-ins" || arg == "--instruct") {
  130. params.instruct = true;
  131. } else if (arg == "--color") {
  132. params.use_color = true;
  133. } else if (arg == "--mlock") {
  134. params.use_mlock = true;
  135. } else if (arg == "--mtest") {
  136. params.mem_test = true;
  137. } else if (arg == "--verbose-prompt") {
  138. params.verbose_prompt = true;
  139. } else if (arg == "-r" || arg == "--reverse-prompt") {
  140. if (++i >= argc) {
  141. invalid_param = true;
  142. break;
  143. }
  144. params.antiprompt.push_back(argv[i]);
  145. } else if (arg == "--perplexity") {
  146. params.perplexity = true;
  147. } else if (arg == "--ignore-eos") {
  148. params.ignore_eos = true;
  149. } else if (arg == "--n_parts") {
  150. if (++i >= argc) {
  151. invalid_param = true;
  152. break;
  153. }
  154. params.n_parts = std::stoi(argv[i]);
  155. } else if (arg == "-h" || arg == "--help") {
  156. gpt_print_usage(argc, argv, params);
  157. exit(0);
  158. } else if (arg == "--random-prompt") {
  159. params.random_prompt = true;
  160. } else if (arg == "--in-prefix") {
  161. if (++i >= argc) {
  162. invalid_param = true;
  163. break;
  164. }
  165. params.input_prefix = argv[i];
  166. } else {
  167. fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
  168. gpt_print_usage(argc, argv, params);
  169. exit(1);
  170. }
  171. }
  172. if (invalid_param) {
  173. fprintf(stderr, "error: invalid parameter for argument: %s\n", arg.c_str());
  174. gpt_print_usage(argc, argv, params);
  175. exit(1);
  176. }
  177. return true;
  178. }
  179. void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {
  180. fprintf(stderr, "usage: %s [options]\n", argv[0]);
  181. fprintf(stderr, "\n");
  182. fprintf(stderr, "options:\n");
  183. fprintf(stderr, " -h, --help show this help message and exit\n");
  184. fprintf(stderr, " -i, --interactive run in interactive mode\n");
  185. fprintf(stderr, " --interactive-first run in interactive mode and wait for input right away\n");
  186. fprintf(stderr, " -ins, --instruct run in instruction mode (use with Alpaca models)\n");
  187. fprintf(stderr, " -r PROMPT, --reverse-prompt PROMPT\n");
  188. fprintf(stderr, " run in interactive mode and poll user input upon seeing PROMPT (can be\n");
  189. fprintf(stderr, " specified more than once for multiple prompts).\n");
  190. fprintf(stderr, " --color colorise output to distinguish prompt and user input from generations\n");
  191. fprintf(stderr, " -s SEED, --seed SEED RNG seed (default: -1, use random seed for <= 0)\n");
  192. fprintf(stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n", params.n_threads);
  193. fprintf(stderr, " -p PROMPT, --prompt PROMPT\n");
  194. fprintf(stderr, " prompt to start generation with (default: empty)\n");
  195. fprintf(stderr, " --random-prompt start with a randomized prompt.\n");
  196. fprintf(stderr, " --in-prefix STRING string to prefix user inputs with (default: empty)\n");
  197. fprintf(stderr, " -f FNAME, --file FNAME\n");
  198. fprintf(stderr, " prompt file to start generation.\n");
  199. fprintf(stderr, " -n N, --n_predict N number of tokens to predict (default: %d, -1 - infinity)\n", params.n_predict);
  200. fprintf(stderr, " --top_k N top-k sampling (default: %d)\n", params.top_k);
  201. fprintf(stderr, " --top_p N top-p sampling (default: %.1f)\n", params.top_p);
  202. fprintf(stderr, " --repeat_last_n N last n tokens to consider for penalize (default: %d)\n", params.repeat_last_n);
  203. fprintf(stderr, " --repeat_penalty N penalize repeat sequence of tokens (default: %.1f)\n", params.repeat_penalty);
  204. fprintf(stderr, " -c N, --ctx_size N size of the prompt context (default: %d)\n", params.n_ctx);
  205. fprintf(stderr, " --ignore-eos ignore end of stream token and continue generating\n");
  206. fprintf(stderr, " --memory_f32 use f32 instead of f16 for memory key+value\n");
  207. fprintf(stderr, " --temp N temperature (default: %.1f)\n", params.temp);
  208. fprintf(stderr, " --n_parts N number of model parts (default: -1 = determine from dimensions)\n");
  209. fprintf(stderr, " -b N, --batch_size N batch size for prompt processing (default: %d)\n", params.n_batch);
  210. fprintf(stderr, " --perplexity compute perplexity over the prompt\n");
  211. fprintf(stderr, " --keep number of tokens to keep from the initial prompt\n");
  212. if (ggml_mlock_supported()) {
  213. fprintf(stderr, " --mlock force system to keep model in RAM rather than swapping or compressing\n");
  214. }
  215. fprintf(stderr, " --mtest compute maximum memory usage\n");
  216. fprintf(stderr, " --verbose-prompt print prompt before generation\n");
  217. fprintf(stderr, " -m FNAME, --model FNAME\n");
  218. fprintf(stderr, " model path (default: %s)\n", params.model.c_str());
  219. fprintf(stderr, "\n");
  220. }
  221. std::string gpt_random_prompt(std::mt19937 & rng) {
  222. const int r = rng() % 10;
  223. switch (r) {
  224. case 0: return "So";
  225. case 1: return "Once upon a time";
  226. case 2: return "When";
  227. case 3: return "The";
  228. case 4: return "After";
  229. case 5: return "If";
  230. case 6: return "import";
  231. case 7: return "He";
  232. case 8: return "She";
  233. case 9: return "They";
  234. default: return "To";
  235. }
  236. return "The";
  237. }
  238. // TODO: not great allocating this every time
  239. std::vector<llama_token> llama_tokenize(struct llama_context * ctx, const std::string & text, bool add_bos) {
  240. // initialize to prompt numer of chars, since n_tokens <= n_prompt_chars
  241. std::vector<llama_token> res(text.size() + (int)add_bos);
  242. int n = llama_tokenize(ctx, text.c_str(), res.data(), res.size(), add_bos);
  243. assert(n >= 0);
  244. res.resize(n);
  245. return res;
  246. }