common.cpp 48 KB

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