common.cpp 48 KB

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