common.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. #include "common.h"
  2. #include "build-info.h"
  3. #include "llama.h"
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <cmath>
  7. #include <cstring>
  8. #include <ctime>
  9. #include <fstream>
  10. #include <iterator>
  11. #include <iostream>
  12. #include <regex>
  13. #include <sstream>
  14. #include <string>
  15. #include <unordered_set>
  16. #include <vector>
  17. #if defined(__APPLE__) && defined(__MACH__)
  18. #include <sys/types.h>
  19. #include <sys/sysctl.h>
  20. #endif
  21. #if defined(_WIN32)
  22. #define WIN32_LEAN_AND_MEAN
  23. #define NOMINMAX
  24. #include <codecvt>
  25. #include <locale>
  26. #include <windows.h>
  27. #include <fcntl.h>
  28. #include <io.h>
  29. #else
  30. #include <sys/ioctl.h>
  31. #include <sys/stat.h>
  32. #include <unistd.h>
  33. #endif
  34. #if defined(_MSC_VER)
  35. #pragma warning(disable: 4244 4267) // possible loss of data
  36. #endif
  37. int32_t get_num_physical_cores() {
  38. #ifdef __linux__
  39. // enumerate the set of thread siblings, num entries is num cores
  40. std::unordered_set<std::string> siblings;
  41. for (uint32_t cpu=0; cpu < UINT32_MAX; ++cpu) {
  42. std::ifstream thread_siblings("/sys/devices/system/cpu"
  43. + std::to_string(cpu) + "/topology/thread_siblings");
  44. if (!thread_siblings.is_open()) {
  45. break; // no more cpus
  46. }
  47. std::string line;
  48. if (std::getline(thread_siblings, line)) {
  49. siblings.insert(line);
  50. }
  51. }
  52. if (siblings.size() > 0) {
  53. return static_cast<int32_t>(siblings.size());
  54. }
  55. #elif defined(__APPLE__) && defined(__MACH__)
  56. int32_t num_physical_cores;
  57. size_t len = sizeof(num_physical_cores);
  58. int result = sysctlbyname("hw.perflevel0.physicalcpu", &num_physical_cores, &len, NULL, 0);
  59. if (result == 0) {
  60. return num_physical_cores;
  61. }
  62. result = sysctlbyname("hw.physicalcpu", &num_physical_cores, &len, NULL, 0);
  63. if (result == 0) {
  64. return num_physical_cores;
  65. }
  66. #elif defined(_WIN32)
  67. //TODO: Implement
  68. #endif
  69. unsigned int n_threads = std::thread::hardware_concurrency();
  70. return n_threads > 0 ? (n_threads <= 4 ? n_threads : n_threads / 2) : 4;
  71. }
  72. void process_escapes(std::string& input) {
  73. std::size_t input_len = input.length();
  74. std::size_t output_idx = 0;
  75. for (std::size_t input_idx = 0; input_idx < input_len; ++input_idx) {
  76. if (input[input_idx] == '\\' && input_idx + 1 < input_len) {
  77. switch (input[++input_idx]) {
  78. case 'n': input[output_idx++] = '\n'; break;
  79. case 'r': input[output_idx++] = '\r'; break;
  80. case 't': input[output_idx++] = '\t'; break;
  81. case '\'': input[output_idx++] = '\''; break;
  82. case '\"': input[output_idx++] = '\"'; break;
  83. case '\\': input[output_idx++] = '\\'; break;
  84. default: input[output_idx++] = '\\';
  85. input[output_idx++] = input[input_idx]; break;
  86. }
  87. } else {
  88. input[output_idx++] = input[input_idx];
  89. }
  90. }
  91. input.resize(output_idx);
  92. }
  93. bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
  94. bool invalid_param = false;
  95. std::string arg;
  96. gpt_params default_params;
  97. const std::string arg_prefix = "--";
  98. for (int i = 1; i < argc; i++) {
  99. arg = argv[i];
  100. if (arg.compare(0, arg_prefix.size(), arg_prefix) == 0) {
  101. std::replace(arg.begin(), arg.end(), '_', '-');
  102. }
  103. if (arg == "-s" || arg == "--seed") {
  104. if (++i >= argc) {
  105. invalid_param = true;
  106. break;
  107. }
  108. params.seed = std::stoul(argv[i]);
  109. } else if (arg == "-t" || arg == "--threads") {
  110. if (++i >= argc) {
  111. invalid_param = true;
  112. break;
  113. }
  114. params.n_threads = std::stoi(argv[i]);
  115. if (params.n_threads <= 0) {
  116. params.n_threads = std::thread::hardware_concurrency();
  117. }
  118. } else if (arg == "-p" || arg == "--prompt") {
  119. if (++i >= argc) {
  120. invalid_param = true;
  121. break;
  122. }
  123. params.prompt = argv[i];
  124. } else if (arg == "-e" || arg == "--escape") {
  125. params.escape = true;
  126. } else if (arg == "--prompt-cache") {
  127. if (++i >= argc) {
  128. invalid_param = true;
  129. break;
  130. }
  131. params.path_prompt_cache = argv[i];
  132. } else if (arg == "--prompt-cache-all") {
  133. params.prompt_cache_all = true;
  134. } else if (arg == "--prompt-cache-ro") {
  135. params.prompt_cache_ro = true;
  136. } else if (arg == "-f" || arg == "--file") {
  137. if (++i >= argc) {
  138. invalid_param = true;
  139. break;
  140. }
  141. std::ifstream file(argv[i]);
  142. if (!file) {
  143. fprintf(stderr, "error: failed to open file '%s'\n", argv[i]);
  144. invalid_param = true;
  145. break;
  146. }
  147. std::copy(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), back_inserter(params.prompt));
  148. if (params.prompt.back() == '\n') {
  149. params.prompt.pop_back();
  150. }
  151. } else if (arg == "-n" || arg == "--n-predict") {
  152. if (++i >= argc) {
  153. invalid_param = true;
  154. break;
  155. }
  156. params.n_predict = std::stoi(argv[i]);
  157. } else if (arg == "--top-k") {
  158. if (++i >= argc) {
  159. invalid_param = true;
  160. break;
  161. }
  162. params.top_k = std::stoi(argv[i]);
  163. } else if (arg == "-c" || arg == "--ctx-size") {
  164. if (++i >= argc) {
  165. invalid_param = true;
  166. break;
  167. }
  168. params.n_ctx = std::stoi(argv[i]);
  169. } else if (arg == "--rope-freq-base") {
  170. if (++i >= argc) {
  171. invalid_param = true;
  172. break;
  173. }
  174. params.rope_freq_base = std::stof(argv[i]);
  175. } else if (arg == "--rope-freq-scale") {
  176. if (++i >= argc) {
  177. invalid_param = true;
  178. break;
  179. }
  180. params.rope_freq_scale = std::stof(argv[i]);
  181. } else if (arg == "--rope-scale") {
  182. if (++i >= argc) {
  183. invalid_param = true;
  184. break;
  185. }
  186. params.rope_freq_scale = 1.0f/std::stof(argv[i]);
  187. } else if (arg == "--memory-f32") {
  188. params.memory_f16 = false;
  189. } else if (arg == "--top-p") {
  190. if (++i >= argc) {
  191. invalid_param = true;
  192. break;
  193. }
  194. params.top_p = std::stof(argv[i]);
  195. } else if (arg == "--temp") {
  196. if (++i >= argc) {
  197. invalid_param = true;
  198. break;
  199. }
  200. params.temp = std::stof(argv[i]);
  201. } else if (arg == "--tfs") {
  202. if (++i >= argc) {
  203. invalid_param = true;
  204. break;
  205. }
  206. params.tfs_z = std::stof(argv[i]);
  207. } else if (arg == "--typical") {
  208. if (++i >= argc) {
  209. invalid_param = true;
  210. break;
  211. }
  212. params.typical_p = std::stof(argv[i]);
  213. } else if (arg == "--repeat-last-n") {
  214. if (++i >= argc) {
  215. invalid_param = true;
  216. break;
  217. }
  218. params.repeat_last_n = std::stoi(argv[i]);
  219. } else if (arg == "--repeat-penalty") {
  220. if (++i >= argc) {
  221. invalid_param = true;
  222. break;
  223. }
  224. params.repeat_penalty = std::stof(argv[i]);
  225. } else if (arg == "--frequency-penalty") {
  226. if (++i >= argc) {
  227. invalid_param = true;
  228. break;
  229. }
  230. params.frequency_penalty = std::stof(argv[i]);
  231. } else if (arg == "--presence-penalty") {
  232. if (++i >= argc) {
  233. invalid_param = true;
  234. break;
  235. }
  236. params.presence_penalty = std::stof(argv[i]);
  237. } else if (arg == "--mirostat") {
  238. if (++i >= argc) {
  239. invalid_param = true;
  240. break;
  241. }
  242. params.mirostat = std::stoi(argv[i]);
  243. } else if (arg == "--mirostat-lr") {
  244. if (++i >= argc) {
  245. invalid_param = true;
  246. break;
  247. }
  248. params.mirostat_eta = std::stof(argv[i]);
  249. } else if (arg == "--mirostat-ent") {
  250. if (++i >= argc) {
  251. invalid_param = true;
  252. break;
  253. }
  254. params.mirostat_tau = std::stof(argv[i]);
  255. } else if (arg == "--cfg-negative-prompt") {
  256. if (++i >= argc) {
  257. invalid_param = true;
  258. break;
  259. }
  260. params.cfg_negative_prompt = argv[i];
  261. } else if (arg == "--cfg-negative-prompt-file") {
  262. if (++i >= argc) {
  263. invalid_param = true;
  264. break;
  265. }
  266. std::ifstream file(argv[i]);
  267. if (!file) {
  268. fprintf(stderr, "error: failed to open file '%s'\n", argv[i]);
  269. invalid_param = true;
  270. break;
  271. }
  272. std::copy(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), back_inserter(params.cfg_negative_prompt));
  273. if (params.cfg_negative_prompt.back() == '\n') {
  274. params.cfg_negative_prompt.pop_back();
  275. }
  276. } else if (arg == "--cfg-scale") {
  277. if (++i >= argc) {
  278. invalid_param = true;
  279. break;
  280. }
  281. params.cfg_scale = std::stof(argv[i]);
  282. } else if (arg == "-b" || arg == "--batch-size") {
  283. if (++i >= argc) {
  284. invalid_param = true;
  285. break;
  286. }
  287. params.n_batch = std::stoi(argv[i]);
  288. } else if (arg == "--keep") {
  289. if (++i >= argc) {
  290. invalid_param = true;
  291. break;
  292. }
  293. params.n_keep = std::stoi(argv[i]);
  294. } else if (arg == "--chunks") {
  295. if (++i >= argc) {
  296. invalid_param = true;
  297. break;
  298. }
  299. params.n_chunks = std::stoi(argv[i]);
  300. } else if (arg == "-m" || arg == "--model") {
  301. if (++i >= argc) {
  302. invalid_param = true;
  303. break;
  304. }
  305. params.model = argv[i];
  306. } else if (arg == "-a" || arg == "--alias") {
  307. if (++i >= argc) {
  308. invalid_param = true;
  309. break;
  310. }
  311. params.model_alias = argv[i];
  312. } else if (arg == "--lora") {
  313. if (++i >= argc) {
  314. invalid_param = true;
  315. break;
  316. }
  317. params.lora_adapter = argv[i];
  318. params.use_mmap = false;
  319. } else if (arg == "--lora-base") {
  320. if (++i >= argc) {
  321. invalid_param = true;
  322. break;
  323. }
  324. params.lora_base = argv[i];
  325. } else if (arg == "-i" || arg == "--interactive") {
  326. params.interactive = true;
  327. } else if (arg == "--embedding") {
  328. params.embedding = true;
  329. } else if (arg == "--interactive-first") {
  330. params.interactive_first = true;
  331. } else if (arg == "-ins" || arg == "--instruct") {
  332. params.instruct = true;
  333. } else if (arg == "--multiline-input") {
  334. params.multiline_input = true;
  335. } else if (arg == "--simple-io") {
  336. params.simple_io = true;
  337. } else if (arg == "--color") {
  338. params.use_color = true;
  339. } else if (arg == "--mlock") {
  340. params.use_mlock = true;
  341. } else if (arg == "--gpu-layers" || arg == "-ngl" || arg == "--n-gpu-layers") {
  342. if (++i >= argc) {
  343. invalid_param = true;
  344. break;
  345. }
  346. #ifdef LLAMA_SUPPORTS_GPU_OFFLOAD
  347. params.n_gpu_layers = std::stoi(argv[i]);
  348. #else
  349. fprintf(stderr, "warning: not compiled with GPU offload support, --n-gpu-layers option will be ignored\n");
  350. fprintf(stderr, "warning: see main README.md for information on enabling GPU BLAS support\n");
  351. #endif
  352. } else if (arg == "--main-gpu" || arg == "-mg") {
  353. if (++i >= argc) {
  354. invalid_param = true;
  355. break;
  356. }
  357. #ifdef GGML_USE_CUBLAS
  358. params.main_gpu = std::stoi(argv[i]);
  359. #else
  360. fprintf(stderr, "warning: llama.cpp was compiled without cuBLAS. It is not possible to set a main GPU.\n");
  361. #endif
  362. } else if (arg == "--tensor-split" || arg == "-ts") {
  363. if (++i >= argc) {
  364. invalid_param = true;
  365. break;
  366. }
  367. #ifdef GGML_USE_CUBLAS
  368. std::string arg_next = argv[i];
  369. // split string by , and /
  370. const std::regex regex{R"([,/]+)"};
  371. std::sregex_token_iterator it{arg_next.begin(), arg_next.end(), regex, -1};
  372. std::vector<std::string> split_arg{it, {}};
  373. GGML_ASSERT(split_arg.size() <= LLAMA_MAX_DEVICES);
  374. for (size_t i = 0; i < LLAMA_MAX_DEVICES; ++i) {
  375. if (i < split_arg.size()) {
  376. params.tensor_split[i] = std::stof(split_arg[i]);
  377. } else {
  378. params.tensor_split[i] = 0.0f;
  379. }
  380. }
  381. #else
  382. fprintf(stderr, "warning: llama.cpp was compiled without cuBLAS. It is not possible to set a tensor split.\n");
  383. #endif // GGML_USE_CUBLAS
  384. } else if (arg == "--no-mul-mat-q" || arg == "-nommq") {
  385. #ifdef GGML_USE_CUBLAS
  386. params.mul_mat_q = false;
  387. #else
  388. fprintf(stderr, "warning: llama.cpp was compiled without cuBLAS. Disabling mul_mat_q kernels has no effect.\n");
  389. #endif // GGML_USE_CUBLAS
  390. } else if (arg == "--low-vram" || arg == "-lv") {
  391. #ifdef GGML_USE_CUBLAS
  392. params.low_vram = true;
  393. #else
  394. fprintf(stderr, "warning: llama.cpp was compiled without cuBLAS. It is not possible to set lower vram usage.\n");
  395. #endif // GGML_USE_CUBLAS
  396. } else if (arg == "--no-mmap") {
  397. params.use_mmap = false;
  398. } else if (arg == "--mtest") {
  399. params.mem_test = true;
  400. } else if (arg == "--numa") {
  401. params.numa = true;
  402. } else if (arg == "--export") {
  403. params.export_cgraph = true;
  404. } else if (arg == "--verbose-prompt") {
  405. params.verbose_prompt = true;
  406. } else if (arg == "-r" || arg == "--reverse-prompt") {
  407. if (++i >= argc) {
  408. invalid_param = true;
  409. break;
  410. }
  411. params.antiprompt.push_back(argv[i]);
  412. } else if (arg == "-ld" || arg == "--logdir") {
  413. if (++i >= argc) {
  414. invalid_param = true;
  415. break;
  416. }
  417. params.logdir = argv[i];
  418. if (params.logdir.back() != DIRECTORY_SEPARATOR) {
  419. params.logdir += DIRECTORY_SEPARATOR;
  420. }
  421. } else if (arg == "--perplexity") {
  422. params.perplexity = true;
  423. } else if (arg == "--ppl-stride") {
  424. if (++i >= argc) {
  425. invalid_param = true;
  426. break;
  427. }
  428. params.ppl_stride = std::stoi(argv[i]);
  429. } else if (arg == "--ppl-output-type") {
  430. if (++i >= argc) {
  431. invalid_param = true;
  432. break;
  433. }
  434. params.ppl_output_type = std::stoi(argv[i]);
  435. } else if (arg == "--hellaswag") {
  436. params.hellaswag = true;
  437. } else if (arg == "--hellaswag-tasks") {
  438. if (++i >= argc) {
  439. invalid_param = true;
  440. break;
  441. }
  442. params.hellaswag_tasks = std::stoi(argv[i]);
  443. } else if (arg == "--ignore-eos") {
  444. params.ignore_eos = true;
  445. } else if (arg == "--no-penalize-nl") {
  446. params.penalize_nl = false;
  447. } else if (arg == "-l" || arg == "--logit-bias") {
  448. if (++i >= argc) {
  449. invalid_param = true;
  450. break;
  451. }
  452. std::stringstream ss(argv[i]);
  453. llama_token key;
  454. char sign;
  455. std::string value_str;
  456. try {
  457. if (ss >> key && ss >> sign && std::getline(ss, value_str) && (sign == '+' || sign == '-')) {
  458. params.logit_bias[key] = std::stof(value_str) * ((sign == '-') ? -1.0f : 1.0f);
  459. } else {
  460. throw std::exception();
  461. }
  462. } catch (const std::exception&) {
  463. invalid_param = true;
  464. break;
  465. }
  466. } else if (arg == "-h" || arg == "--help") {
  467. gpt_print_usage(argc, argv, default_params);
  468. exit(0);
  469. } else if (arg == "--random-prompt") {
  470. params.random_prompt = true;
  471. } else if (arg == "--in-prefix-bos") {
  472. params.input_prefix_bos = true;
  473. } else if (arg == "--in-prefix") {
  474. if (++i >= argc) {
  475. invalid_param = true;
  476. break;
  477. }
  478. params.input_prefix = argv[i];
  479. } else if (arg == "--in-suffix") {
  480. if (++i >= argc) {
  481. invalid_param = true;
  482. break;
  483. }
  484. params.input_suffix = argv[i];
  485. } else if (arg == "--grammar") {
  486. if (++i >= argc) {
  487. invalid_param = true;
  488. break;
  489. }
  490. params.grammar = argv[i];
  491. } else if (arg == "--grammar-file") {
  492. if (++i >= argc) {
  493. invalid_param = true;
  494. break;
  495. }
  496. std::ifstream file(argv[i]);
  497. if (!file) {
  498. fprintf(stderr, "error: failed to open file '%s'\n", argv[i]);
  499. invalid_param = true;
  500. break;
  501. }
  502. std::copy(
  503. std::istreambuf_iterator<char>(file),
  504. std::istreambuf_iterator<char>(),
  505. std::back_inserter(params.grammar)
  506. );
  507. } else {
  508. fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
  509. gpt_print_usage(argc, argv, default_params);
  510. exit(1);
  511. }
  512. }
  513. if (invalid_param) {
  514. fprintf(stderr, "error: invalid parameter for argument: %s\n", arg.c_str());
  515. gpt_print_usage(argc, argv, default_params);
  516. exit(1);
  517. }
  518. if (params.prompt_cache_all &&
  519. (params.interactive || params.interactive_first ||
  520. params.instruct)) {
  521. fprintf(stderr, "error: --prompt-cache-all not supported in interactive mode yet\n");
  522. gpt_print_usage(argc, argv, default_params);
  523. exit(1);
  524. }
  525. if (params.escape) {
  526. process_escapes(params.prompt);
  527. process_escapes(params.input_prefix);
  528. process_escapes(params.input_suffix);
  529. }
  530. return true;
  531. }
  532. void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {
  533. fprintf(stdout, "usage: %s [options]\n", argv[0]);
  534. fprintf(stdout, "\n");
  535. fprintf(stdout, "options:\n");
  536. fprintf(stdout, " -h, --help show this help message and exit\n");
  537. fprintf(stdout, " -i, --interactive run in interactive mode\n");
  538. fprintf(stdout, " --interactive-first run in interactive mode and wait for input right away\n");
  539. fprintf(stdout, " -ins, --instruct run in instruction mode (use with Alpaca models)\n");
  540. fprintf(stdout, " --multiline-input allows you to write or paste multiple lines without ending each in '\\'\n");
  541. fprintf(stdout, " -r PROMPT, --reverse-prompt PROMPT\n");
  542. fprintf(stdout, " halt generation at PROMPT, return control in interactive mode\n");
  543. fprintf(stdout, " (can be specified more than once for multiple prompts).\n");
  544. fprintf(stdout, " --color colorise output to distinguish prompt and user input from generations\n");
  545. fprintf(stdout, " -s SEED, --seed SEED RNG seed (default: -1, use random seed for < 0)\n");
  546. fprintf(stdout, " -t N, --threads N number of threads to use during computation (default: %d)\n", params.n_threads);
  547. fprintf(stdout, " -p PROMPT, --prompt PROMPT\n");
  548. fprintf(stdout, " prompt to start generation with (default: empty)\n");
  549. fprintf(stdout, " -e, --escape process prompt escapes sequences (\\n, \\r, \\t, \\', \\\", \\\\)\n");
  550. fprintf(stdout, " --prompt-cache FNAME file to cache prompt state for faster startup (default: none)\n");
  551. fprintf(stdout, " --prompt-cache-all if specified, saves user input and generations to cache as well.\n");
  552. fprintf(stdout, " not supported with --interactive or other interactive options\n");
  553. fprintf(stdout, " --prompt-cache-ro if specified, uses the prompt cache but does not update it.\n");
  554. fprintf(stdout, " --random-prompt start with a randomized prompt.\n");
  555. fprintf(stdout, " --in-prefix-bos prefix BOS to user inputs, preceding the `--in-prefix` string\n");
  556. fprintf(stdout, " --in-prefix STRING string to prefix user inputs with (default: empty)\n");
  557. fprintf(stdout, " --in-suffix STRING string to suffix after user inputs with (default: empty)\n");
  558. fprintf(stdout, " -f FNAME, --file FNAME\n");
  559. fprintf(stdout, " prompt file to start generation.\n");
  560. fprintf(stdout, " -n N, --n-predict N number of tokens to predict (default: %d, -1 = infinity, -2 = until context filled)\n", params.n_predict);
  561. fprintf(stdout, " -c N, --ctx-size N size of the prompt context (default: %d)\n", params.n_ctx);
  562. fprintf(stdout, " -b N, --batch-size N batch size for prompt processing (default: %d)\n", params.n_batch);
  563. fprintf(stdout, " --top-k N top-k sampling (default: %d, 0 = disabled)\n", params.top_k);
  564. fprintf(stdout, " --top-p N top-p sampling (default: %.1f, 1.0 = disabled)\n", (double)params.top_p);
  565. fprintf(stdout, " --tfs N tail free sampling, parameter z (default: %.1f, 1.0 = disabled)\n", (double)params.tfs_z);
  566. fprintf(stdout, " --typical N locally typical sampling, parameter p (default: %.1f, 1.0 = disabled)\n", (double)params.typical_p);
  567. fprintf(stdout, " --repeat-last-n N last n tokens to consider for penalize (default: %d, 0 = disabled, -1 = ctx_size)\n", params.repeat_last_n);
  568. fprintf(stdout, " --repeat-penalty N penalize repeat sequence of tokens (default: %.1f, 1.0 = disabled)\n", (double)params.repeat_penalty);
  569. fprintf(stdout, " --presence-penalty N repeat alpha presence penalty (default: %.1f, 0.0 = disabled)\n", (double)params.presence_penalty);
  570. fprintf(stdout, " --frequency-penalty N repeat alpha frequency penalty (default: %.1f, 0.0 = disabled)\n", (double)params.frequency_penalty);
  571. fprintf(stdout, " --mirostat N use Mirostat sampling.\n");
  572. fprintf(stdout, " Top K, Nucleus, Tail Free and Locally Typical samplers are ignored if used.\n");
  573. fprintf(stdout, " (default: %d, 0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0)\n", params.mirostat);
  574. fprintf(stdout, " --mirostat-lr N Mirostat learning rate, parameter eta (default: %.1f)\n", (double)params.mirostat_eta);
  575. fprintf(stdout, " --mirostat-ent N Mirostat target entropy, parameter tau (default: %.1f)\n", (double)params.mirostat_tau);
  576. fprintf(stdout, " -l TOKEN_ID(+/-)BIAS, --logit-bias TOKEN_ID(+/-)BIAS\n");
  577. fprintf(stdout, " modifies the likelihood of token appearing in the completion,\n");
  578. fprintf(stdout, " i.e. `--logit-bias 15043+1` to increase likelihood of token ' Hello',\n");
  579. fprintf(stdout, " or `--logit-bias 15043-1` to decrease likelihood of token ' Hello'\n");
  580. fprintf(stdout, " --grammar GRAMMAR BNF-like grammar to constrain generations (see samples in grammars/ dir)\n");
  581. fprintf(stdout, " --grammar-file FNAME file to read grammar from\n");
  582. fprintf(stdout, " --cfg-negative-prompt PROMPT\n");
  583. fprintf(stdout, " negative prompt to use for guidance. (default: empty)\n");
  584. fprintf(stdout, " --cfg-negative-prompt-file FNAME\n");
  585. fprintf(stdout, " negative prompt file to use for guidance. (default: empty)\n");
  586. fprintf(stdout, " --cfg-scale N strength of guidance (default: %f, 1.0 = disable)\n", params.cfg_scale);
  587. fprintf(stdout, " --rope-scale N RoPE context linear scaling factor, inverse of --rope-freq-scale (default: %g)\n", 1.0f/params.rope_freq_scale);
  588. fprintf(stdout, " --rope-freq-base N RoPE base frequency, used by NTK-aware scaling (default: %.1f)\n", params.rope_freq_base);
  589. fprintf(stdout, " --rope-freq-scale N RoPE frequency linear scaling factor, inverse of --rope-scale (default: %g)\n", params.rope_freq_scale);
  590. fprintf(stdout, " --ignore-eos ignore end of stream token and continue generating (implies --logit-bias 2-inf)\n");
  591. fprintf(stdout, " --no-penalize-nl do not penalize newline token\n");
  592. fprintf(stdout, " --memory-f32 use f32 instead of f16 for memory key+value (default: disabled)\n");
  593. fprintf(stdout, " not recommended: doubles context memory required and no measurable increase in quality\n");
  594. fprintf(stdout, " --temp N temperature (default: %.1f)\n", (double)params.temp);
  595. fprintf(stdout, " --perplexity compute perplexity over each ctx window of the prompt\n");
  596. fprintf(stdout, " --hellaswag compute HellaSwag score over random tasks from datafile supplied with -f\n");
  597. fprintf(stdout, " --hellaswag-tasks N number of tasks to use when computing the HellaSwag score (default: %zu)\n", params.hellaswag_tasks);
  598. fprintf(stdout, " --keep N number of tokens to keep from the initial prompt (default: %d, -1 = all)\n", params.n_keep);
  599. fprintf(stdout, " --chunks N max number of chunks to process (default: %d, -1 = all)\n", params.n_chunks);
  600. if (llama_mlock_supported()) {
  601. fprintf(stdout, " --mlock force system to keep model in RAM rather than swapping or compressing\n");
  602. }
  603. if (llama_mmap_supported()) {
  604. fprintf(stdout, " --no-mmap do not memory-map model (slower load but may reduce pageouts if not using mlock)\n");
  605. }
  606. fprintf(stdout, " --numa attempt optimizations that help on some NUMA systems\n");
  607. fprintf(stdout, " if run without this previously, it is recommended to drop the system page cache before using this\n");
  608. fprintf(stdout, " see https://github.com/ggerganov/llama.cpp/issues/1437\n");
  609. #ifdef LLAMA_SUPPORTS_GPU_OFFLOAD
  610. fprintf(stdout, " -ngl N, --n-gpu-layers N\n");
  611. fprintf(stdout, " number of layers to store in VRAM\n");
  612. fprintf(stdout, " -ts SPLIT --tensor-split SPLIT\n");
  613. fprintf(stdout, " how to split tensors across multiple GPUs, comma-separated list of proportions, e.g. 3,1\n");
  614. fprintf(stdout, " -mg i, --main-gpu i the GPU to use for scratch and small tensors\n");
  615. fprintf(stdout, " -lv, --low-vram don't allocate VRAM scratch buffer\n");
  616. #ifdef GGML_USE_CUBLAS
  617. fprintf(stdout, " -nommq, --no-mul-mat-q\n");
  618. fprintf(stdout, " use " GGML_CUBLAS_NAME " instead of custom mul_mat_q " GGML_CUDA_NAME " kernels.\n");
  619. fprintf(stdout, " Not recommended since this is both slower and uses more VRAM.\n");
  620. #endif // GGML_USE_CUBLAS
  621. #endif
  622. fprintf(stdout, " --mtest compute maximum memory usage\n");
  623. fprintf(stdout, " --export export the computation graph to 'llama.ggml'\n");
  624. fprintf(stdout, " --verbose-prompt print prompt before generation\n");
  625. fprintf(stderr, " --simple-io use basic IO for better compatibility in subprocesses and limited consoles\n");
  626. fprintf(stdout, " --lora FNAME apply LoRA adapter (implies --no-mmap)\n");
  627. fprintf(stdout, " --lora-base FNAME optional model to use as a base for the layers modified by the LoRA adapter\n");
  628. fprintf(stdout, " -m FNAME, --model FNAME\n");
  629. fprintf(stdout, " model path (default: %s)\n", params.model.c_str());
  630. fprintf(stdout, " -ld LOGDIR, --logdir LOGDIR\n");
  631. fprintf(stdout, " path under which to save YAML logs (no logging if unset)\n");
  632. fprintf(stdout, "\n");
  633. }
  634. std::string gpt_random_prompt(std::mt19937 & rng) {
  635. const int r = rng() % 10;
  636. switch (r) {
  637. case 0: return "So";
  638. case 1: return "Once upon a time";
  639. case 2: return "When";
  640. case 3: return "The";
  641. case 4: return "After";
  642. case 5: return "If";
  643. case 6: return "import";
  644. case 7: return "He";
  645. case 8: return "She";
  646. case 9: return "They";
  647. default: return "To";
  648. }
  649. return "The";
  650. }
  651. //
  652. // Model utils
  653. //
  654. struct llama_context_params llama_context_params_from_gpt_params(const gpt_params & params) {
  655. auto lparams = llama_context_default_params();
  656. lparams.n_ctx = params.n_ctx;
  657. lparams.n_batch = params.n_batch;
  658. lparams.n_gpu_layers = params.n_gpu_layers;
  659. lparams.main_gpu = params.main_gpu;
  660. lparams.tensor_split = params.tensor_split;
  661. lparams.low_vram = params.low_vram;
  662. lparams.mul_mat_q = params.mul_mat_q;
  663. lparams.seed = params.seed;
  664. lparams.f16_kv = params.memory_f16;
  665. lparams.use_mmap = params.use_mmap;
  666. lparams.use_mlock = params.use_mlock;
  667. lparams.logits_all = params.perplexity;
  668. lparams.embedding = params.embedding;
  669. lparams.rope_freq_base = params.rope_freq_base;
  670. lparams.rope_freq_scale = params.rope_freq_scale;
  671. return lparams;
  672. }
  673. std::tuple<struct llama_model *, struct llama_context *> llama_init_from_gpt_params(gpt_params & params) {
  674. auto lparams = llama_context_params_from_gpt_params(params);
  675. llama_model * model = llama_load_model_from_file(params.model.c_str(), lparams);
  676. if (model == NULL) {
  677. fprintf(stderr, "%s: error: failed to load model '%s'\n", __func__, params.model.c_str());
  678. return std::make_tuple(nullptr, nullptr);
  679. }
  680. llama_context * lctx = llama_new_context_with_model(model, lparams);
  681. if (lctx == NULL) {
  682. fprintf(stderr, "%s: error: failed to create context with model '%s'\n", __func__, params.model.c_str());
  683. llama_free_model(model);
  684. return std::make_tuple(nullptr, nullptr);
  685. }
  686. if (!params.lora_adapter.empty()) {
  687. int err = llama_model_apply_lora_from_file(model,
  688. params.lora_adapter.c_str(),
  689. params.lora_base.empty() ? NULL : params.lora_base.c_str(),
  690. params.n_threads);
  691. if (err != 0) {
  692. fprintf(stderr, "%s: error: failed to apply lora adapter\n", __func__);
  693. llama_free(lctx);
  694. llama_free_model(model);
  695. return std::make_tuple(nullptr, nullptr);
  696. }
  697. }
  698. if (params.ignore_eos) {
  699. params.logit_bias[llama_token_eos(lctx)] = -INFINITY;
  700. }
  701. return std::make_tuple(model, lctx);
  702. }
  703. //
  704. // Vocab utils
  705. //
  706. std::vector<llama_token> llama_tokenize(
  707. struct llama_context * ctx,
  708. const std::string & text,
  709. bool add_bos) {
  710. // upper limit for the number of tokens
  711. int n_tokens = text.length() + add_bos;
  712. std::vector<llama_token> result(n_tokens);
  713. n_tokens = llama_tokenize(ctx, text.c_str(), result.data(), result.size(), add_bos);
  714. if (n_tokens < 0) {
  715. result.resize(-n_tokens);
  716. int check = llama_tokenize(ctx, text.c_str(), result.data(), result.size(), add_bos);
  717. GGML_ASSERT(check == -n_tokens);
  718. } else {
  719. result.resize(n_tokens);
  720. }
  721. return result;
  722. }
  723. std::string llama_token_to_piece(const struct llama_context * ctx, llama_token token) {
  724. std::vector<char> result(8, 0);
  725. const int n_tokens = llama_token_to_piece(ctx, token, result.data(), result.size());
  726. if (n_tokens < 0) {
  727. result.resize(-n_tokens);
  728. int check = llama_token_to_piece(ctx, token, result.data(), result.size());
  729. GGML_ASSERT(check == -n_tokens);
  730. } else {
  731. result.resize(n_tokens);
  732. }
  733. return std::string(result.data(), result.size());
  734. }
  735. std::string llama_detokenize_spm(llama_context * ctx, const std::vector<llama_token> & tokens) {
  736. const llama_token bos_id = llama_token_bos(ctx);
  737. std::string piece;
  738. std::string result;
  739. for (size_t i = 0; i < tokens.size(); ++i) {
  740. piece = llama_token_to_piece(ctx, tokens[i]);
  741. // remove the leading space of the first non-BOS token
  742. if (((tokens[0] == bos_id && i == 1) || (tokens[0] != bos_id && i == 0)) && piece[0] == ' ') {
  743. piece = piece.substr(1);
  744. }
  745. result += piece;
  746. }
  747. return result;
  748. }
  749. std::string llama_detokenize_bpe(llama_context * ctx, const std::vector<llama_token> & tokens) {
  750. std::string piece;
  751. std::string result;
  752. for (size_t i = 0; i < tokens.size(); ++i) {
  753. piece = llama_token_to_piece(ctx, tokens[i]);
  754. result += piece;
  755. }
  756. return result;
  757. }
  758. // returns true if successful, false otherwise
  759. bool create_directory_with_parents(const std::string & path) {
  760. #ifdef _WIN32
  761. std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
  762. std::wstring wpath = converter.from_bytes(path);
  763. // if the path already exists, check whether it's a directory
  764. const DWORD attributes = GetFileAttributesW(wpath.c_str());
  765. if ((attributes != INVALID_FILE_ATTRIBUTES) && (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
  766. return true;
  767. }
  768. size_t pos_slash = 0;
  769. // process path from front to back, procedurally creating directories
  770. while ((pos_slash = path.find('\\', pos_slash)) != std::string::npos) {
  771. const std::wstring subpath = wpath.substr(0, pos_slash);
  772. const wchar_t * test = subpath.c_str();
  773. const bool success = CreateDirectoryW(test, NULL);
  774. if (!success) {
  775. const DWORD error = GetLastError();
  776. // if the path already exists, ensure that it's a directory
  777. if (error == ERROR_ALREADY_EXISTS) {
  778. const DWORD attributes = GetFileAttributesW(subpath.c_str());
  779. if (attributes == INVALID_FILE_ATTRIBUTES || !(attributes & FILE_ATTRIBUTE_DIRECTORY)) {
  780. return false;
  781. }
  782. } else {
  783. return false;
  784. }
  785. }
  786. pos_slash += 1;
  787. }
  788. return true;
  789. #else
  790. // if the path already exists, check whether it's a directory
  791. struct stat info;
  792. if (stat(path.c_str(), &info) == 0) {
  793. return S_ISDIR(info.st_mode);
  794. }
  795. size_t pos_slash = 1; // skip leading slashes for directory creation
  796. // process path from front to back, procedurally creating directories
  797. while ((pos_slash = path.find('/', pos_slash)) != std::string::npos) {
  798. const std::string subpath = path.substr(0, pos_slash);
  799. struct stat info;
  800. // if the path already exists, ensure that it's a directory
  801. if (stat(subpath.c_str(), &info) == 0) {
  802. if (!S_ISDIR(info.st_mode)) {
  803. return false;
  804. }
  805. } else {
  806. // create parent directories
  807. const int ret = mkdir(subpath.c_str(), 0755);
  808. if (ret != 0) {
  809. return false;
  810. }
  811. }
  812. pos_slash += 1;
  813. }
  814. return true;
  815. #endif // _WIN32
  816. }
  817. void dump_vector_float_yaml(FILE * stream, const char * prop_name, const std::vector<float> & data) {
  818. if (data.empty()) {
  819. fprintf(stream, "%s:\n", prop_name);
  820. return;
  821. }
  822. fprintf(stream, "%s: [", prop_name);
  823. for (size_t i = 0; i < data.size() - 1; ++i) {
  824. fprintf(stream, "%e, ", data[i]);
  825. }
  826. fprintf(stream, "%e]\n", data.back());
  827. }
  828. void dump_vector_int_yaml(FILE * stream, const char * prop_name, const std::vector<int> & data) {
  829. if (data.empty()) {
  830. fprintf(stream, "%s:\n", prop_name);
  831. return;
  832. }
  833. fprintf(stream, "%s: [", prop_name);
  834. for (size_t i = 0; i < data.size() - 1; ++i) {
  835. fprintf(stream, "%d, ", data[i]);
  836. }
  837. fprintf(stream, "%d]\n", data.back());
  838. }
  839. void dump_string_yaml_multiline(FILE * stream, const char * prop_name, const char * data) {
  840. std::string data_str(data == NULL ? "" : data);
  841. if (data_str.empty()) {
  842. fprintf(stream, "%s:\n", prop_name);
  843. return;
  844. }
  845. size_t pos_start = 0;
  846. size_t pos_found = 0;
  847. if (!data_str.empty() && (std::isspace(data_str[0]) || std::isspace(data_str.back()))) {
  848. data_str = std::regex_replace(data_str, std::regex("\n"), "\\n");
  849. data_str = std::regex_replace(data_str, std::regex("\""), "\\\"");
  850. data_str = "\"" + data_str + "\"";
  851. fprintf(stream, "%s: %s\n", prop_name, data_str.c_str());
  852. return;
  853. }
  854. if (data_str.find('\n') == std::string::npos) {
  855. fprintf(stream, "%s: %s\n", prop_name, data_str.c_str());
  856. return;
  857. }
  858. fprintf(stream, "%s: |\n", prop_name);
  859. while ((pos_found = data_str.find('\n', pos_start)) != std::string::npos) {
  860. fprintf(stream, " %s\n", data_str.substr(pos_start, pos_found-pos_start).c_str());
  861. pos_start = pos_found + 1;
  862. }
  863. }
  864. std::string get_sortable_timestamp() {
  865. using clock = std::chrono::system_clock;
  866. const clock::time_point current_time = clock::now();
  867. const time_t as_time_t = clock::to_time_t(current_time);
  868. char timestamp_no_ns[100];
  869. std::strftime(timestamp_no_ns, 100, "%Y_%m_%d-%H_%M_%S", std::localtime(&as_time_t));
  870. const int64_t ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
  871. current_time.time_since_epoch() % 1000000000).count();
  872. char timestamp_ns[10];
  873. snprintf(timestamp_ns, 11, "%09ld", ns);
  874. return std::string(timestamp_no_ns) + "." + std::string(timestamp_ns);
  875. }
  876. void dump_non_result_info_yaml(FILE * stream, const gpt_params & params, const llama_context * lctx,
  877. const std::string & timestamp, const std::vector<int> & prompt_tokens, const char * model_desc) {
  878. fprintf(stream, "build_commit: %s\n", BUILD_COMMIT);
  879. fprintf(stream, "build_number: %d\n", BUILD_NUMBER);
  880. fprintf(stream, "cpu_has_arm_fma: %s\n", ggml_cpu_has_arm_fma() ? "true" : "false");
  881. fprintf(stream, "cpu_has_avx: %s\n", ggml_cpu_has_avx() ? "true" : "false");
  882. fprintf(stream, "cpu_has_avx2: %s\n", ggml_cpu_has_avx2() ? "true" : "false");
  883. fprintf(stream, "cpu_has_avx512: %s\n", ggml_cpu_has_avx512() ? "true" : "false");
  884. fprintf(stream, "cpu_has_avx512_vbmi: %s\n", ggml_cpu_has_avx512_vbmi() ? "true" : "false");
  885. fprintf(stream, "cpu_has_avx512_vnni: %s\n", ggml_cpu_has_avx512_vnni() ? "true" : "false");
  886. fprintf(stream, "cpu_has_blas: %s\n", ggml_cpu_has_blas() ? "true" : "false");
  887. fprintf(stream, "cpu_has_cublas: %s\n", ggml_cpu_has_cublas() ? "true" : "false");
  888. fprintf(stream, "cpu_has_clblast: %s\n", ggml_cpu_has_clblast() ? "true" : "false");
  889. fprintf(stream, "cpu_has_fma: %s\n", ggml_cpu_has_fma() ? "true" : "false");
  890. fprintf(stream, "cpu_has_gpublas: %s\n", ggml_cpu_has_gpublas() ? "true" : "false");
  891. fprintf(stream, "cpu_has_neon: %s\n", ggml_cpu_has_neon() ? "true" : "false");
  892. fprintf(stream, "cpu_has_f16c: %s\n", ggml_cpu_has_f16c() ? "true" : "false");
  893. fprintf(stream, "cpu_has_fp16_va: %s\n", ggml_cpu_has_fp16_va() ? "true" : "false");
  894. fprintf(stream, "cpu_has_wasm_simd: %s\n", ggml_cpu_has_wasm_simd() ? "true" : "false");
  895. fprintf(stream, "cpu_has_blas: %s\n", ggml_cpu_has_blas() ? "true" : "false");
  896. fprintf(stream, "cpu_has_sse3: %s\n", ggml_cpu_has_sse3() ? "true" : "false");
  897. fprintf(stream, "cpu_has_vsx: %s\n", ggml_cpu_has_vsx() ? "true" : "false");
  898. #ifdef NDEBUG
  899. fprintf(stream, "debug: false\n");
  900. #else
  901. fprintf(stream, "debug: true\n");
  902. #endif // NDEBUG
  903. fprintf(stream, "model_desc: %s\n", model_desc);
  904. fprintf(stream, "n_vocab: %d # output size of the final layer, 32001 for some models\n", llama_n_vocab(lctx));
  905. #ifdef __OPTIMIZE__
  906. fprintf(stream, "optimize: true\n");
  907. #else
  908. fprintf(stream, "optimize: false\n");
  909. #endif // __OPTIMIZE__
  910. fprintf(stream, "time: %s\n", timestamp.c_str());
  911. fprintf(stream, "\n");
  912. fprintf(stream, "###############\n");
  913. fprintf(stream, "# User Inputs #\n");
  914. fprintf(stream, "###############\n");
  915. fprintf(stream, "\n");
  916. fprintf(stream, "alias: %s # default: unknown\n", params.model_alias.c_str());
  917. fprintf(stream, "batch_size: %d # default: 512\n", params.n_batch);
  918. dump_string_yaml_multiline(stream, "cfg_negative_prompt", params.cfg_negative_prompt.c_str());
  919. fprintf(stream, "cfg_scale: %f # default: 1.0\n", params.cfg_scale);
  920. fprintf(stream, "chunks: %d # default: -1 (unlimited)\n", params.n_chunks);
  921. fprintf(stream, "color: %s # default: false\n", params.use_color ? "true" : "false");
  922. fprintf(stream, "ctx_size: %d # default: 512\n", params.n_ctx);
  923. fprintf(stream, "escape: %s # default: false\n", params.escape ? "true" : "false");
  924. fprintf(stream, "export: %s # default: false\n", params.export_cgraph ? "true" : "false");
  925. fprintf(stream, "file: # never logged, see prompt instead. Can still be specified for input.\n");
  926. fprintf(stream, "frequency_penalty: %f # default: 0.0 \n", params.frequency_penalty);
  927. dump_string_yaml_multiline(stream, "grammar", params.grammar.c_str());
  928. fprintf(stream, "grammar-file: # never logged, see grammar instead. Can still be specified for input.\n");
  929. fprintf(stream, "hellaswag: %s # default: false\n", params.hellaswag ? "true" : "false");
  930. fprintf(stream, "hellaswag_tasks: %ld # default: 400\n", params.hellaswag_tasks);
  931. const auto logit_bias_eos = params.logit_bias.find(llama_token_eos(lctx));
  932. const bool ignore_eos = logit_bias_eos != params.logit_bias.end() && logit_bias_eos->second == -INFINITY;
  933. fprintf(stream, "ignore_eos: %s # default: false\n", ignore_eos ? "true" : "false");
  934. dump_string_yaml_multiline(stream, "in_prefix", params.input_prefix.c_str());
  935. fprintf(stream, "in_prefix_bos: %s # default: false\n", params.input_prefix_bos ? "true" : "false");
  936. dump_string_yaml_multiline(stream, "in_suffix", params.input_prefix.c_str());
  937. fprintf(stream, "instruct: %s # default: false\n", params.instruct ? "true" : "false");
  938. fprintf(stream, "interactive: %s # default: false\n", params.interactive ? "true" : "false");
  939. fprintf(stream, "interactive_first: %s # default: false\n", params.interactive_first ? "true" : "false");
  940. fprintf(stream, "keep: %d # default: 0\n", params.n_keep);
  941. fprintf(stream, "logdir: %s # default: unset (no logging)\n", params.logdir.c_str());
  942. fprintf(stream, "logit_bias:\n");
  943. for (std::pair<llama_token, float> lb : params.logit_bias) {
  944. if (ignore_eos && lb.first == logit_bias_eos->first) {
  945. continue;
  946. }
  947. fprintf(stream, " %d: %f", lb.first, lb.second);
  948. }
  949. fprintf(stream, "lora: %s\n", params.lora_adapter.c_str());
  950. fprintf(stream, "lora_base: %s\n", params.lora_base.c_str());
  951. fprintf(stream, "low_vram: %s # default: false\n", params.low_vram ? "true" : "false");
  952. fprintf(stream, "main_gpu: %d # default: 0\n", params.main_gpu);
  953. fprintf(stream, "memory_f32: %s # default: false\n", !params.memory_f16 ? "true" : "false");
  954. fprintf(stream, "mirostat: %d # default: 0 (disabled)\n", params.mirostat);
  955. fprintf(stream, "mirostat_ent: %f # default: 5.0\n", params.mirostat_tau);
  956. fprintf(stream, "mirostat_lr: %f # default: 0.1\n", params.mirostat_eta);
  957. fprintf(stream, "mlock: %s # default: false\n", params.use_mlock ? "true" : "false");
  958. fprintf(stream, "model: %s # default: models/7B/ggml-model.bin\n", params.model.c_str());
  959. fprintf(stream, "mtest: %s # default: false\n", params.mem_test ? "true" : "false");
  960. fprintf(stream, "multiline_input: %s # default: false\n", params.multiline_input ? "true" : "false");
  961. fprintf(stream, "n_gpu_layers: %d # default: 0\n", params.n_gpu_layers);
  962. fprintf(stream, "n_predict: %d # default: -1 (unlimited)\n", params.n_predict);
  963. fprintf(stream, "n_probs: %d # only used by server binary, default: 0\n", params.n_probs);
  964. fprintf(stream, "no_mmap: %s # default: false\n", !params.use_mmap ? "true" : "false");
  965. fprintf(stream, "no_mul_mat_q: %s # default: false\n", !params.mul_mat_q ? "true" : "false");
  966. fprintf(stream, "no_penalize_nl: %s # default: false\n", !params.penalize_nl ? "true" : "false");
  967. fprintf(stream, "numa: %s # default: false\n", params.numa ? "true" : "false");
  968. fprintf(stream, "ppl_output_type: %d # default: 0\n", params.ppl_output_type);
  969. fprintf(stream, "ppl_stride: %d # default: 0\n", params.ppl_stride);
  970. fprintf(stream, "presence_penalty: %f # default: 0.0\n", params.presence_penalty);
  971. dump_string_yaml_multiline(stream, "prompt", params.prompt.c_str());
  972. fprintf(stream, "prompt_cache: %s\n", params.path_prompt_cache.c_str());
  973. fprintf(stream, "prompt_cache_all: %s # default: false\n", params.prompt_cache_all ? "true" : "false");
  974. fprintf(stream, "prompt_cache_ro: %s # default: false\n", params.prompt_cache_ro ? "true" : "false");
  975. dump_vector_int_yaml(stream, "prompt_tokens", prompt_tokens);
  976. fprintf(stream, "random_prompt: %s # default: false\n", params.random_prompt ? "true" : "false");
  977. fprintf(stream, "repeat_penalty: %f # default: 1.1\n", params.repeat_penalty);
  978. fprintf(stream, "reverse_prompt:\n");
  979. for (std::string ap : params.antiprompt) {
  980. size_t pos = 0;
  981. while ((pos = ap.find('\n', pos)) != std::string::npos) {
  982. ap.replace(pos, 1, "\\n");
  983. pos += 1;
  984. }
  985. fprintf(stream, " - %s\n", ap.c_str());
  986. }
  987. fprintf(stream, "rope_freq_base: %f # default: 10000.0\n", params.rope_freq_base);
  988. fprintf(stream, "rope_freq_scale: %f # default: 1.0\n", params.rope_freq_scale);
  989. fprintf(stream, "seed: %d # default: -1 (random seed)\n", params.seed);
  990. fprintf(stream, "simple_io: %s # default: false\n", params.simple_io ? "true" : "false");
  991. fprintf(stream, "temp: %f # default: 0.8\n", params.temp);
  992. const std::vector<float> tensor_split_vector(params.tensor_split, params.tensor_split + LLAMA_MAX_DEVICES);
  993. dump_vector_float_yaml(stream, "tensor_split", tensor_split_vector);
  994. fprintf(stream, "tfs: %f # default: 1.0\n", params.tfs_z);
  995. fprintf(stream, "threads: %d # default: %d\n", params.n_threads, std::thread::hardware_concurrency());
  996. fprintf(stream, "top_k: %d # default: 40\n", params.top_k);
  997. fprintf(stream, "top_p: %f # default: 0.95\n", params.top_p);
  998. fprintf(stream, "typical_p: %f # default: 1.0\n", params.typical_p);
  999. fprintf(stream, "verbose_prompt: %s # default: false\n", params.verbose_prompt ? "true" : "false");
  1000. }