arg.cpp 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144
  1. #include "arg.h"
  2. #include "log.h"
  3. #include "sampling.h"
  4. #include <algorithm>
  5. #include <climits>
  6. #include <cstdarg>
  7. #include <fstream>
  8. #include <regex>
  9. #include <set>
  10. #include <string>
  11. #include <thread>
  12. #include <vector>
  13. #include "json-schema-to-grammar.h"
  14. using json = nlohmann::ordered_json;
  15. common_arg & common_arg::set_examples(std::initializer_list<enum llama_example> examples) {
  16. this->examples = std::move(examples);
  17. return *this;
  18. }
  19. common_arg & common_arg::set_env(const char * env) {
  20. help = help + "\n(env: " + env + ")";
  21. this->env = env;
  22. return *this;
  23. }
  24. common_arg & common_arg::set_sparam() {
  25. is_sparam = true;
  26. return *this;
  27. }
  28. bool common_arg::in_example(enum llama_example ex) {
  29. return examples.find(ex) != examples.end();
  30. }
  31. bool common_arg::get_value_from_env(std::string & output) {
  32. if (env == nullptr) return false;
  33. char * value = std::getenv(env);
  34. if (value) {
  35. output = value;
  36. return true;
  37. }
  38. return false;
  39. }
  40. bool common_arg::has_value_from_env() {
  41. return env != nullptr && std::getenv(env);
  42. }
  43. static std::vector<std::string> break_str_into_lines(std::string input, size_t max_char_per_line) {
  44. std::vector<std::string> result;
  45. std::istringstream iss(input);
  46. std::string line;
  47. auto add_line = [&](const std::string& l) {
  48. if (l.length() <= max_char_per_line) {
  49. result.push_back(l);
  50. } else {
  51. std::istringstream line_stream(l);
  52. std::string word, current_line;
  53. while (line_stream >> word) {
  54. if (current_line.length() + !current_line.empty() + word.length() > max_char_per_line) {
  55. if (!current_line.empty()) result.push_back(current_line);
  56. current_line = word;
  57. } else {
  58. current_line += (!current_line.empty() ? " " : "") + word;
  59. }
  60. }
  61. if (!current_line.empty()) result.push_back(current_line);
  62. }
  63. };
  64. while (std::getline(iss, line)) {
  65. add_line(line);
  66. }
  67. return result;
  68. }
  69. std::string common_arg::to_string() {
  70. // params for printing to console
  71. const static int n_leading_spaces = 40;
  72. const static int n_char_per_line_help = 70; // TODO: detect this based on current console
  73. std::string leading_spaces(n_leading_spaces, ' ');
  74. std::ostringstream ss;
  75. for (const auto arg : args) {
  76. if (arg == args.front()) {
  77. if (args.size() == 1) {
  78. ss << arg;
  79. } else {
  80. // first arg is usually abbreviation, we need padding to make it more beautiful
  81. auto tmp = std::string(arg) + ", ";
  82. auto spaces = std::string(std::max(0, 7 - (int)tmp.size()), ' ');
  83. ss << tmp << spaces;
  84. }
  85. } else {
  86. ss << arg << (arg != args.back() ? ", " : "");
  87. }
  88. }
  89. if (value_hint) ss << " " << value_hint;
  90. if (value_hint_2) ss << " " << value_hint_2;
  91. if (ss.tellp() > n_leading_spaces - 3) {
  92. // current line is too long, add new line
  93. ss << "\n" << leading_spaces;
  94. } else {
  95. // padding between arg and help, same line
  96. ss << std::string(leading_spaces.size() - ss.tellp(), ' ');
  97. }
  98. const auto help_lines = break_str_into_lines(help, n_char_per_line_help);
  99. for (const auto & line : help_lines) {
  100. ss << (&line == &help_lines.front() ? "" : leading_spaces) << line << "\n";
  101. }
  102. return ss.str();
  103. }
  104. //
  105. // utils
  106. //
  107. static void common_params_handle_model_default(common_params & params) {
  108. if (!params.hf_repo.empty()) {
  109. // short-hand to avoid specifying --hf-file -> default it to --model
  110. if (params.hf_file.empty()) {
  111. if (params.model.empty()) {
  112. throw std::invalid_argument("error: --hf-repo requires either --hf-file or --model\n");
  113. }
  114. params.hf_file = params.model;
  115. } else if (params.model.empty()) {
  116. // this is to avoid different repo having same file name, or same file name in different subdirs
  117. std::string filename = params.hf_repo + "_" + params.hf_file;
  118. // to make sure we don't have any slashes in the filename
  119. string_replace_all(filename, "/", "_");
  120. params.model = fs_get_cache_file(filename);
  121. }
  122. } else if (!params.model_url.empty()) {
  123. if (params.model.empty()) {
  124. auto f = string_split<std::string>(params.model_url, '#').front();
  125. f = string_split<std::string>(f, '?').front();
  126. params.model = fs_get_cache_file(string_split<std::string>(f, '/').back());
  127. }
  128. } else if (params.model.empty()) {
  129. params.model = DEFAULT_MODEL_PATH;
  130. }
  131. }
  132. //
  133. // CLI argument parsing functions
  134. //
  135. static bool common_params_parse_ex(int argc, char ** argv, common_params_context & ctx_arg) {
  136. std::string arg;
  137. const std::string arg_prefix = "--";
  138. common_params & params = ctx_arg.params;
  139. std::unordered_map<std::string, common_arg *> arg_to_options;
  140. for (auto & opt : ctx_arg.options) {
  141. for (const auto & arg : opt.args) {
  142. arg_to_options[arg] = &opt;
  143. }
  144. }
  145. // handle environment variables
  146. for (auto & opt : ctx_arg.options) {
  147. std::string value;
  148. if (opt.get_value_from_env(value)) {
  149. try {
  150. if (opt.handler_void && (value == "1" || value == "true")) {
  151. opt.handler_void(params);
  152. }
  153. if (opt.handler_int) {
  154. opt.handler_int(params, std::stoi(value));
  155. }
  156. if (opt.handler_string) {
  157. opt.handler_string(params, value);
  158. continue;
  159. }
  160. } catch (std::exception & e) {
  161. throw std::invalid_argument(string_format(
  162. "error while handling environment variable \"%s\": %s\n\n", opt.env, e.what()));
  163. }
  164. }
  165. }
  166. // handle command line arguments
  167. auto check_arg = [&](int i) {
  168. if (i+1 >= argc) {
  169. throw std::invalid_argument("expected value for argument");
  170. }
  171. };
  172. for (int i = 1; i < argc; i++) {
  173. const std::string arg_prefix = "--";
  174. std::string arg = argv[i];
  175. if (arg.compare(0, arg_prefix.size(), arg_prefix) == 0) {
  176. std::replace(arg.begin(), arg.end(), '_', '-');
  177. }
  178. if (arg_to_options.find(arg) == arg_to_options.end()) {
  179. throw std::invalid_argument(string_format("error: invalid argument: %s", arg.c_str()));
  180. }
  181. auto opt = *arg_to_options[arg];
  182. if (opt.has_value_from_env()) {
  183. fprintf(stderr, "warn: %s environment variable is set, but will be overwritten by command line argument %s\n", opt.env, arg.c_str());
  184. }
  185. try {
  186. if (opt.handler_void) {
  187. opt.handler_void(params);
  188. continue;
  189. }
  190. // arg with single value
  191. check_arg(i);
  192. std::string val = argv[++i];
  193. if (opt.handler_int) {
  194. opt.handler_int(params, std::stoi(val));
  195. continue;
  196. }
  197. if (opt.handler_string) {
  198. opt.handler_string(params, val);
  199. continue;
  200. }
  201. // arg with 2 values
  202. check_arg(i);
  203. std::string val2 = argv[++i];
  204. if (opt.handler_str_str) {
  205. opt.handler_str_str(params, val, val2);
  206. continue;
  207. }
  208. } catch (std::exception & e) {
  209. throw std::invalid_argument(string_format(
  210. "error while handling argument \"%s\": %s\n\n"
  211. "usage:\n%s\n\nto show complete usage, run with -h",
  212. arg.c_str(), e.what(), arg_to_options[arg]->to_string().c_str()));
  213. }
  214. }
  215. postprocess_cpu_params(params.cpuparams, nullptr);
  216. postprocess_cpu_params(params.cpuparams_batch, &params.cpuparams);
  217. postprocess_cpu_params(params.speculative.cpuparams, &params.cpuparams);
  218. postprocess_cpu_params(params.speculative.cpuparams_batch, &params.cpuparams_batch);
  219. if (params.prompt_cache_all && (params.interactive || params.interactive_first)) {
  220. throw std::invalid_argument("error: --prompt-cache-all not supported in interactive mode yet\n");
  221. }
  222. common_params_handle_model_default(params);
  223. if (params.escape) {
  224. string_process_escapes(params.prompt);
  225. string_process_escapes(params.input_prefix);
  226. string_process_escapes(params.input_suffix);
  227. for (auto & antiprompt : params.antiprompt) {
  228. string_process_escapes(antiprompt);
  229. }
  230. for (auto & seq_breaker : params.sampling.dry_sequence_breakers) {
  231. string_process_escapes(seq_breaker);
  232. }
  233. }
  234. if (!params.kv_overrides.empty()) {
  235. params.kv_overrides.emplace_back();
  236. params.kv_overrides.back().key[0] = 0;
  237. }
  238. if (params.reranking && params.embedding) {
  239. throw std::invalid_argument("error: either --embedding or --reranking can be specified, but not both");
  240. }
  241. return true;
  242. }
  243. static void common_params_print_usage(common_params_context & ctx_arg) {
  244. auto print_options = [](std::vector<common_arg *> & options) {
  245. for (common_arg * opt : options) {
  246. printf("%s", opt->to_string().c_str());
  247. }
  248. };
  249. std::vector<common_arg *> common_options;
  250. std::vector<common_arg *> sparam_options;
  251. std::vector<common_arg *> specific_options;
  252. for (auto & opt : ctx_arg.options) {
  253. // in case multiple LLAMA_EXAMPLE_* are set, we prioritize the LLAMA_EXAMPLE_* matching current example
  254. if (opt.is_sparam) {
  255. sparam_options.push_back(&opt);
  256. } else if (opt.in_example(ctx_arg.ex)) {
  257. specific_options.push_back(&opt);
  258. } else {
  259. common_options.push_back(&opt);
  260. }
  261. }
  262. printf("----- common params -----\n\n");
  263. print_options(common_options);
  264. printf("\n\n----- sampling params -----\n\n");
  265. print_options(sparam_options);
  266. // TODO: maybe convert enum llama_example to string
  267. printf("\n\n----- example-specific params -----\n\n");
  268. print_options(specific_options);
  269. }
  270. static std::vector<ggml_backend_dev_t> parse_device_list(const std::string & value) {
  271. std::vector<ggml_backend_dev_t> devices;
  272. auto dev_names = string_split<std::string>(value, ',');
  273. if (dev_names.empty()) {
  274. throw std::invalid_argument("no devices specified");
  275. }
  276. if (dev_names.size() == 1 && dev_names[0] == "none") {
  277. devices.push_back(nullptr);
  278. } else {
  279. for (const auto & device : dev_names) {
  280. auto * dev = ggml_backend_dev_by_name(device.c_str());
  281. if (!dev || ggml_backend_dev_type(dev) != GGML_BACKEND_DEVICE_TYPE_GPU) {
  282. throw std::invalid_argument(string_format("invalid device: %s", device.c_str()));
  283. }
  284. devices.push_back(dev);
  285. }
  286. devices.push_back(nullptr);
  287. }
  288. return devices;
  289. }
  290. bool common_params_parse(int argc, char ** argv, common_params & params, llama_example ex, void(*print_usage)(int, char **)) {
  291. auto ctx_arg = common_params_parser_init(params, ex, print_usage);
  292. const common_params params_org = ctx_arg.params; // the example can modify the default params
  293. try {
  294. if (!common_params_parse_ex(argc, argv, ctx_arg)) {
  295. ctx_arg.params = params_org;
  296. return false;
  297. }
  298. if (ctx_arg.params.usage) {
  299. common_params_print_usage(ctx_arg);
  300. if (ctx_arg.print_usage) {
  301. ctx_arg.print_usage(argc, argv);
  302. }
  303. exit(0);
  304. }
  305. } catch (const std::invalid_argument & ex) {
  306. fprintf(stderr, "%s\n", ex.what());
  307. ctx_arg.params = params_org;
  308. return false;
  309. }
  310. return true;
  311. }
  312. static std::string list_builtin_chat_templates() {
  313. std::vector<const char *> supported_tmpl;
  314. int32_t res = llama_chat_builtin_templates(nullptr, 0);
  315. supported_tmpl.resize(res);
  316. res = llama_chat_builtin_templates(supported_tmpl.data(), supported_tmpl.size());
  317. std::ostringstream msg;
  318. for (auto & tmpl : supported_tmpl) {
  319. msg << tmpl << (&tmpl == &supported_tmpl.back() ? "" : ", ");
  320. }
  321. return msg.str();
  322. }
  323. common_params_context common_params_parser_init(common_params & params, llama_example ex, void(*print_usage)(int, char **)) {
  324. // load dynamic backends
  325. ggml_backend_load_all();
  326. common_params_context ctx_arg(params);
  327. ctx_arg.print_usage = print_usage;
  328. ctx_arg.ex = ex;
  329. std::string sampler_type_chars;
  330. std::string sampler_type_names;
  331. for (const auto & sampler : params.sampling.samplers) {
  332. sampler_type_chars += common_sampler_type_to_chr(sampler);
  333. sampler_type_names += common_sampler_type_to_str(sampler) + ";";
  334. }
  335. sampler_type_names.pop_back();
  336. /**
  337. * filter options by example
  338. * rules:
  339. * - all examples inherit options from LLAMA_EXAMPLE_COMMON
  340. * - if LLAMA_EXAMPLE_* is set (other than COMMON), we only show the option in the corresponding example
  341. * - if both {LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_*,} are set, we will prioritize the LLAMA_EXAMPLE_* matching current example
  342. */
  343. auto add_opt = [&](common_arg arg) {
  344. if (arg.in_example(ex) || arg.in_example(LLAMA_EXAMPLE_COMMON)) {
  345. ctx_arg.options.push_back(std::move(arg));
  346. }
  347. };
  348. add_opt(common_arg(
  349. {"-h", "--help", "--usage"},
  350. "print usage and exit",
  351. [](common_params & params) {
  352. params.usage = true;
  353. }
  354. ));
  355. add_opt(common_arg(
  356. {"--version"},
  357. "show version and build info",
  358. [](common_params &) {
  359. fprintf(stderr, "version: %d (%s)\n", LLAMA_BUILD_NUMBER, LLAMA_COMMIT);
  360. fprintf(stderr, "built with %s for %s\n", LLAMA_COMPILER, LLAMA_BUILD_TARGET);
  361. exit(0);
  362. }
  363. ));
  364. add_opt(common_arg(
  365. {"--verbose-prompt"},
  366. string_format("print a verbose prompt before generation (default: %s)", params.verbose_prompt ? "true" : "false"),
  367. [](common_params & params) {
  368. params.verbose_prompt = true;
  369. }
  370. ));
  371. add_opt(common_arg(
  372. {"--no-display-prompt"},
  373. string_format("don't print prompt at generation (default: %s)", !params.display_prompt ? "true" : "false"),
  374. [](common_params & params) {
  375. params.display_prompt = false;
  376. }
  377. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  378. add_opt(common_arg(
  379. {"-co", "--color"},
  380. string_format("colorise output to distinguish prompt and user input from generations (default: %s)", params.use_color ? "true" : "false"),
  381. [](common_params & params) {
  382. params.use_color = true;
  383. }
  384. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_INFILL, LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_LOOKUP}));
  385. add_opt(common_arg(
  386. {"-t", "--threads"}, "N",
  387. string_format("number of threads to use during generation (default: %d)", params.cpuparams.n_threads),
  388. [](common_params & params, int value) {
  389. params.cpuparams.n_threads = value;
  390. if (params.cpuparams.n_threads <= 0) {
  391. params.cpuparams.n_threads = std::thread::hardware_concurrency();
  392. }
  393. }
  394. ).set_env("LLAMA_ARG_THREADS"));
  395. add_opt(common_arg(
  396. {"-tb", "--threads-batch"}, "N",
  397. "number of threads to use during batch and prompt processing (default: same as --threads)",
  398. [](common_params & params, int value) {
  399. params.cpuparams_batch.n_threads = value;
  400. if (params.cpuparams_batch.n_threads <= 0) {
  401. params.cpuparams_batch.n_threads = std::thread::hardware_concurrency();
  402. }
  403. }
  404. ));
  405. add_opt(common_arg(
  406. {"-C", "--cpu-mask"}, "M",
  407. "CPU affinity mask: arbitrarily long hex. Complements cpu-range (default: \"\")",
  408. [](common_params & params, const std::string & mask) {
  409. params.cpuparams.mask_valid = true;
  410. if (!parse_cpu_mask(mask, params.cpuparams.cpumask)) {
  411. throw std::invalid_argument("invalid cpumask");
  412. }
  413. }
  414. ));
  415. add_opt(common_arg(
  416. {"-Cr", "--cpu-range"}, "lo-hi",
  417. "range of CPUs for affinity. Complements --cpu-mask",
  418. [](common_params & params, const std::string & range) {
  419. params.cpuparams.mask_valid = true;
  420. if (!parse_cpu_range(range, params.cpuparams.cpumask)) {
  421. throw std::invalid_argument("invalid range");
  422. }
  423. }
  424. ));
  425. add_opt(common_arg(
  426. {"--cpu-strict"}, "<0|1>",
  427. string_format("use strict CPU placement (default: %u)\n", (unsigned) params.cpuparams.strict_cpu),
  428. [](common_params & params, const std::string & value) {
  429. params.cpuparams.strict_cpu = std::stoul(value);
  430. }
  431. ));
  432. add_opt(common_arg(
  433. {"--prio"}, "N",
  434. string_format("set process/thread priority : 0-normal, 1-medium, 2-high, 3-realtime (default: %d)\n", params.cpuparams.priority),
  435. [](common_params & params, int prio) {
  436. if (prio < 0 || prio > 3) {
  437. throw std::invalid_argument("invalid value");
  438. }
  439. params.cpuparams.priority = (enum ggml_sched_priority) prio;
  440. }
  441. ));
  442. add_opt(common_arg(
  443. {"--poll"}, "<0...100>",
  444. string_format("use polling level to wait for work (0 - no polling, default: %u)\n", (unsigned) params.cpuparams.poll),
  445. [](common_params & params, const std::string & value) {
  446. params.cpuparams.poll = std::stoul(value);
  447. }
  448. ));
  449. add_opt(common_arg(
  450. {"-Cb", "--cpu-mask-batch"}, "M",
  451. "CPU affinity mask: arbitrarily long hex. Complements cpu-range-batch (default: same as --cpu-mask)",
  452. [](common_params & params, const std::string & mask) {
  453. params.cpuparams_batch.mask_valid = true;
  454. if (!parse_cpu_mask(mask, params.cpuparams_batch.cpumask)) {
  455. throw std::invalid_argument("invalid cpumask");
  456. }
  457. }
  458. ));
  459. add_opt(common_arg(
  460. {"-Crb", "--cpu-range-batch"}, "lo-hi",
  461. "ranges of CPUs for affinity. Complements --cpu-mask-batch",
  462. [](common_params & params, const std::string & range) {
  463. params.cpuparams_batch.mask_valid = true;
  464. if (!parse_cpu_range(range, params.cpuparams_batch.cpumask)) {
  465. throw std::invalid_argument("invalid range");
  466. }
  467. }
  468. ));
  469. add_opt(common_arg(
  470. {"--cpu-strict-batch"}, "<0|1>",
  471. "use strict CPU placement (default: same as --cpu-strict)",
  472. [](common_params & params, int value) {
  473. params.cpuparams_batch.strict_cpu = value;
  474. }
  475. ));
  476. add_opt(common_arg(
  477. {"--prio-batch"}, "N",
  478. string_format("set process/thread priority : 0-normal, 1-medium, 2-high, 3-realtime (default: %d)\n", params.cpuparams_batch.priority),
  479. [](common_params & params, int prio) {
  480. if (prio < 0 || prio > 3) {
  481. throw std::invalid_argument("invalid value");
  482. }
  483. params.cpuparams_batch.priority = (enum ggml_sched_priority) prio;
  484. }
  485. ));
  486. add_opt(common_arg(
  487. {"--poll-batch"}, "<0|1>",
  488. "use polling to wait for work (default: same as --poll)",
  489. [](common_params & params, int value) {
  490. params.cpuparams_batch.poll = value;
  491. }
  492. ));
  493. add_opt(common_arg(
  494. {"-lcs", "--lookup-cache-static"}, "FNAME",
  495. "path to static lookup cache to use for lookup decoding (not updated by generation)",
  496. [](common_params & params, const std::string & value) {
  497. params.lookup_cache_static = value;
  498. }
  499. ).set_examples({LLAMA_EXAMPLE_LOOKUP}));
  500. add_opt(common_arg(
  501. {"-lcd", "--lookup-cache-dynamic"}, "FNAME",
  502. "path to dynamic lookup cache to use for lookup decoding (updated by generation)",
  503. [](common_params & params, const std::string & value) {
  504. params.lookup_cache_dynamic = value;
  505. }
  506. ).set_examples({LLAMA_EXAMPLE_LOOKUP}));
  507. add_opt(common_arg(
  508. {"-c", "--ctx-size"}, "N",
  509. string_format("size of the prompt context (default: %d, 0 = loaded from model)", params.n_ctx),
  510. [](common_params & params, int value) {
  511. params.n_ctx = value;
  512. }
  513. ).set_env("LLAMA_ARG_CTX_SIZE"));
  514. add_opt(common_arg(
  515. {"-n", "--predict", "--n-predict"}, "N",
  516. string_format("number of tokens to predict (default: %d, -1 = infinity, -2 = until context filled)", params.n_predict),
  517. [](common_params & params, int value) {
  518. params.n_predict = value;
  519. }
  520. ).set_env("LLAMA_ARG_N_PREDICT"));
  521. add_opt(common_arg(
  522. {"-b", "--batch-size"}, "N",
  523. string_format("logical maximum batch size (default: %d)", params.n_batch),
  524. [](common_params & params, int value) {
  525. params.n_batch = value;
  526. }
  527. ).set_env("LLAMA_ARG_BATCH"));
  528. add_opt(common_arg(
  529. {"-ub", "--ubatch-size"}, "N",
  530. string_format("physical maximum batch size (default: %d)", params.n_ubatch),
  531. [](common_params & params, int value) {
  532. params.n_ubatch = value;
  533. }
  534. ).set_env("LLAMA_ARG_UBATCH"));
  535. add_opt(common_arg(
  536. {"--keep"}, "N",
  537. string_format("number of tokens to keep from the initial prompt (default: %d, -1 = all)", params.n_keep),
  538. [](common_params & params, int value) {
  539. params.n_keep = value;
  540. }
  541. ));
  542. add_opt(common_arg(
  543. {"--no-context-shift"},
  544. string_format("disables context shift on inifinite text generation (default: %s)", params.ctx_shift ? "disabled" : "enabled"),
  545. [](common_params & params) {
  546. params.ctx_shift = false;
  547. }
  548. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_IMATRIX}).set_env("LLAMA_ARG_NO_CONTEXT_SHIFT"));
  549. add_opt(common_arg(
  550. {"--chunks"}, "N",
  551. string_format("max number of chunks to process (default: %d, -1 = all)", params.n_chunks),
  552. [](common_params & params, int value) {
  553. params.n_chunks = value;
  554. }
  555. ).set_examples({LLAMA_EXAMPLE_IMATRIX, LLAMA_EXAMPLE_PERPLEXITY, LLAMA_EXAMPLE_RETRIEVAL}));
  556. add_opt(common_arg(
  557. {"-fa", "--flash-attn"},
  558. string_format("enable Flash Attention (default: %s)", params.flash_attn ? "enabled" : "disabled"),
  559. [](common_params & params) {
  560. params.flash_attn = true;
  561. }
  562. ).set_env("LLAMA_ARG_FLASH_ATTN"));
  563. add_opt(common_arg(
  564. {"-p", "--prompt"}, "PROMPT",
  565. ex == LLAMA_EXAMPLE_MAIN
  566. ? "prompt to start generation with\nif -cnv is set, this will be used as system prompt"
  567. : "prompt to start generation with",
  568. [](common_params & params, const std::string & value) {
  569. params.prompt = value;
  570. }
  571. ));
  572. add_opt(common_arg(
  573. {"--no-perf"},
  574. string_format("disable internal libllama performance timings (default: %s)", params.no_perf ? "true" : "false"),
  575. [](common_params & params) {
  576. params.no_perf = true;
  577. params.sampling.no_perf = true;
  578. }
  579. ).set_env("LLAMA_ARG_NO_PERF"));
  580. add_opt(common_arg(
  581. {"-f", "--file"}, "FNAME",
  582. "a file containing the prompt (default: none)",
  583. [](common_params & params, const std::string & value) {
  584. std::ifstream file(value);
  585. if (!file) {
  586. throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
  587. }
  588. // store the external file name in params
  589. params.prompt_file = value;
  590. std::copy(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), back_inserter(params.prompt));
  591. if (!params.prompt.empty() && params.prompt.back() == '\n') {
  592. params.prompt.pop_back();
  593. }
  594. }
  595. ));
  596. add_opt(common_arg(
  597. {"--in-file"}, "FNAME",
  598. "an input file (repeat to specify multiple files)",
  599. [](common_params & params, const std::string & value) {
  600. std::ifstream file(value);
  601. if (!file) {
  602. throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
  603. }
  604. params.in_files.push_back(value);
  605. }
  606. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  607. add_opt(common_arg(
  608. {"-bf", "--binary-file"}, "FNAME",
  609. "binary file containing the prompt (default: none)",
  610. [](common_params & params, const std::string & value) {
  611. std::ifstream file(value, std::ios::binary);
  612. if (!file) {
  613. throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
  614. }
  615. // store the external file name in params
  616. params.prompt_file = value;
  617. std::ostringstream ss;
  618. ss << file.rdbuf();
  619. params.prompt = ss.str();
  620. fprintf(stderr, "Read %zu bytes from binary file %s\n", params.prompt.size(), value.c_str());
  621. }
  622. ));
  623. add_opt(common_arg(
  624. {"-e", "--escape"},
  625. string_format("process escapes sequences (\\n, \\r, \\t, \\', \\\", \\\\) (default: %s)", params.escape ? "true" : "false"),
  626. [](common_params & params) {
  627. params.escape = true;
  628. }
  629. ));
  630. add_opt(common_arg(
  631. {"--no-escape"},
  632. "do not process escape sequences",
  633. [](common_params & params) {
  634. params.escape = false;
  635. }
  636. ));
  637. add_opt(common_arg(
  638. {"-ptc", "--print-token-count"}, "N",
  639. string_format("print token count every N tokens (default: %d)", params.n_print),
  640. [](common_params & params, int value) {
  641. params.n_print = value;
  642. }
  643. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  644. add_opt(common_arg(
  645. {"--prompt-cache"}, "FNAME",
  646. "file to cache prompt state for faster startup (default: none)",
  647. [](common_params & params, const std::string & value) {
  648. params.path_prompt_cache = value;
  649. }
  650. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  651. add_opt(common_arg(
  652. {"--prompt-cache-all"},
  653. "if specified, saves user input and generations to cache as well\n",
  654. [](common_params & params) {
  655. params.prompt_cache_all = true;
  656. }
  657. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  658. add_opt(common_arg(
  659. {"--prompt-cache-ro"},
  660. "if specified, uses the prompt cache but does not update it",
  661. [](common_params & params) {
  662. params.prompt_cache_ro = true;
  663. }
  664. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  665. add_opt(common_arg(
  666. {"-r", "--reverse-prompt"}, "PROMPT",
  667. "halt generation at PROMPT, return control in interactive mode\n",
  668. [](common_params & params, const std::string & value) {
  669. params.antiprompt.emplace_back(value);
  670. }
  671. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  672. add_opt(common_arg(
  673. {"-sp", "--special"},
  674. string_format("special tokens output enabled (default: %s)", params.special ? "true" : "false"),
  675. [](common_params & params) {
  676. params.special = true;
  677. }
  678. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_SERVER}));
  679. add_opt(common_arg(
  680. {"-cnv", "--conversation"},
  681. string_format(
  682. "run in conversation mode:\n"
  683. "- does not print special tokens and suffix/prefix\n"
  684. "- interactive mode is also enabled\n"
  685. "(default: %s)",
  686. params.conversation ? "true" : "false"
  687. ),
  688. [](common_params & params) {
  689. params.conversation = true;
  690. }
  691. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  692. add_opt(common_arg(
  693. {"-i", "--interactive"},
  694. string_format("run in interactive mode (default: %s)", params.interactive ? "true" : "false"),
  695. [](common_params & params) {
  696. params.interactive = true;
  697. }
  698. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  699. add_opt(common_arg(
  700. {"-if", "--interactive-first"},
  701. string_format("run in interactive mode and wait for input right away (default: %s)", params.interactive_first ? "true" : "false"),
  702. [](common_params & params) {
  703. params.interactive_first = true;
  704. }
  705. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  706. add_opt(common_arg(
  707. {"-mli", "--multiline-input"},
  708. "allows you to write or paste multiple lines without ending each in '\\'",
  709. [](common_params & params) {
  710. params.multiline_input = true;
  711. }
  712. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  713. add_opt(common_arg(
  714. {"--in-prefix-bos"},
  715. "prefix BOS to user inputs, preceding the `--in-prefix` string",
  716. [](common_params & params) {
  717. params.input_prefix_bos = true;
  718. params.enable_chat_template = false;
  719. }
  720. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  721. add_opt(common_arg(
  722. {"--in-prefix"}, "STRING",
  723. "string to prefix user inputs with (default: empty)",
  724. [](common_params & params, const std::string & value) {
  725. params.input_prefix = value;
  726. params.enable_chat_template = false;
  727. }
  728. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_INFILL}));
  729. add_opt(common_arg(
  730. {"--in-suffix"}, "STRING",
  731. "string to suffix after user inputs with (default: empty)",
  732. [](common_params & params, const std::string & value) {
  733. params.input_suffix = value;
  734. params.enable_chat_template = false;
  735. }
  736. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_INFILL}));
  737. add_opt(common_arg(
  738. {"--no-warmup"},
  739. "skip warming up the model with an empty run",
  740. [](common_params & params) {
  741. params.warmup = false;
  742. }
  743. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_SERVER}));
  744. add_opt(common_arg(
  745. {"--spm-infill"},
  746. string_format(
  747. "use Suffix/Prefix/Middle pattern for infill (instead of Prefix/Suffix/Middle) as some models prefer this. (default: %s)",
  748. params.spm_infill ? "enabled" : "disabled"
  749. ),
  750. [](common_params & params) {
  751. params.spm_infill = true;
  752. }
  753. ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_INFILL}));
  754. add_opt(common_arg(
  755. {"--samplers"}, "SAMPLERS",
  756. string_format("samplers that will be used for generation in the order, separated by \';\'\n(default: %s)", sampler_type_names.c_str()),
  757. [](common_params & params, const std::string & value) {
  758. const auto sampler_names = string_split<std::string>(value, ';');
  759. params.sampling.samplers = common_sampler_types_from_names(sampler_names, true);
  760. }
  761. ).set_sparam());
  762. add_opt(common_arg(
  763. {"-s", "--seed"}, "SEED",
  764. string_format("RNG seed (default: %d, use random seed for %d)", params.sampling.seed, LLAMA_DEFAULT_SEED),
  765. [](common_params & params, const std::string & value) {
  766. params.sampling.seed = std::stoul(value);
  767. }
  768. ).set_sparam());
  769. add_opt(common_arg(
  770. {"--sampling-seq"}, "SEQUENCE",
  771. string_format("simplified sequence for samplers that will be used (default: %s)", sampler_type_chars.c_str()),
  772. [](common_params & params, const std::string & value) {
  773. params.sampling.samplers = common_sampler_types_from_chars(value);
  774. }
  775. ).set_sparam());
  776. add_opt(common_arg(
  777. {"--ignore-eos"},
  778. "ignore end of stream token and continue generating (implies --logit-bias EOS-inf)",
  779. [](common_params & params) {
  780. params.sampling.ignore_eos = true;
  781. }
  782. ).set_sparam());
  783. add_opt(common_arg(
  784. {"--penalize-nl"},
  785. string_format("penalize newline tokens (default: %s)", params.sampling.penalize_nl ? "true" : "false"),
  786. [](common_params & params) {
  787. params.sampling.penalize_nl = true;
  788. }
  789. ).set_sparam());
  790. add_opt(common_arg(
  791. {"--temp"}, "N",
  792. string_format("temperature (default: %.1f)", (double)params.sampling.temp),
  793. [](common_params & params, const std::string & value) {
  794. params.sampling.temp = std::stof(value);
  795. params.sampling.temp = std::max(params.sampling.temp, 0.0f);
  796. }
  797. ).set_sparam());
  798. add_opt(common_arg(
  799. {"--top-k"}, "N",
  800. string_format("top-k sampling (default: %d, 0 = disabled)", params.sampling.top_k),
  801. [](common_params & params, int value) {
  802. params.sampling.top_k = value;
  803. }
  804. ).set_sparam());
  805. add_opt(common_arg(
  806. {"--top-p"}, "N",
  807. string_format("top-p sampling (default: %.1f, 1.0 = disabled)", (double)params.sampling.top_p),
  808. [](common_params & params, const std::string & value) {
  809. params.sampling.top_p = std::stof(value);
  810. }
  811. ).set_sparam());
  812. add_opt(common_arg(
  813. {"--min-p"}, "N",
  814. string_format("min-p sampling (default: %.1f, 0.0 = disabled)", (double)params.sampling.min_p),
  815. [](common_params & params, const std::string & value) {
  816. params.sampling.min_p = std::stof(value);
  817. }
  818. ).set_sparam());
  819. add_opt(common_arg(
  820. {"--xtc-probability"}, "N",
  821. string_format("xtc probability (default: %.1f, 0.0 = disabled)", (double)params.sampling.xtc_probability),
  822. [](common_params & params, const std::string & value) {
  823. params.sampling.xtc_probability = std::stof(value);
  824. }
  825. ).set_sparam());
  826. add_opt(common_arg(
  827. {"--xtc-threshold"}, "N",
  828. string_format("xtc threshold (default: %.1f, 1.0 = disabled)", (double)params.sampling.xtc_threshold),
  829. [](common_params & params, const std::string & value) {
  830. params.sampling.xtc_threshold = std::stof(value);
  831. }
  832. ).set_sparam());
  833. add_opt(common_arg(
  834. {"--typical"}, "N",
  835. string_format("locally typical sampling, parameter p (default: %.1f, 1.0 = disabled)", (double)params.sampling.typ_p),
  836. [](common_params & params, const std::string & value) {
  837. params.sampling.typ_p = std::stof(value);
  838. }
  839. ).set_sparam());
  840. add_opt(common_arg(
  841. {"--repeat-last-n"}, "N",
  842. string_format("last n tokens to consider for penalize (default: %d, 0 = disabled, -1 = ctx_size)", params.sampling.penalty_last_n),
  843. [](common_params & params, int value) {
  844. params.sampling.penalty_last_n = value;
  845. params.sampling.n_prev = std::max(params.sampling.n_prev, params.sampling.penalty_last_n);
  846. }
  847. ).set_sparam());
  848. add_opt(common_arg(
  849. {"--repeat-penalty"}, "N",
  850. string_format("penalize repeat sequence of tokens (default: %.1f, 1.0 = disabled)", (double)params.sampling.penalty_repeat),
  851. [](common_params & params, const std::string & value) {
  852. params.sampling.penalty_repeat = std::stof(value);
  853. }
  854. ).set_sparam());
  855. add_opt(common_arg(
  856. {"--presence-penalty"}, "N",
  857. string_format("repeat alpha presence penalty (default: %.1f, 0.0 = disabled)", (double)params.sampling.penalty_present),
  858. [](common_params & params, const std::string & value) {
  859. params.sampling.penalty_present = std::stof(value);
  860. }
  861. ).set_sparam());
  862. add_opt(common_arg(
  863. {"--frequency-penalty"}, "N",
  864. string_format("repeat alpha frequency penalty (default: %.1f, 0.0 = disabled)", (double)params.sampling.penalty_freq),
  865. [](common_params & params, const std::string & value) {
  866. params.sampling.penalty_freq = std::stof(value);
  867. }
  868. ).set_sparam());
  869. add_opt(common_arg(
  870. {"--dry-multiplier"}, "N",
  871. string_format("set DRY sampling multiplier (default: %.1f, 0.0 = disabled)", (double)params.sampling.dry_multiplier),
  872. [](common_params & params, const std::string & value) {
  873. params.sampling.dry_multiplier = std::stof(value);
  874. }
  875. ).set_sparam());
  876. add_opt(common_arg(
  877. {"--dry-base"}, "N",
  878. string_format("set DRY sampling base value (default: %.2f)", (double)params.sampling.dry_base),
  879. [](common_params & params, const std::string & value) {
  880. float potential_base = std::stof(value);
  881. if (potential_base >= 1.0f)
  882. {
  883. params.sampling.dry_base = potential_base;
  884. }
  885. }
  886. ).set_sparam());
  887. add_opt(common_arg(
  888. {"--dry-allowed-length"}, "N",
  889. string_format("set allowed length for DRY sampling (default: %d)", params.sampling.dry_allowed_length),
  890. [](common_params & params, int value) {
  891. params.sampling.dry_allowed_length = value;
  892. }
  893. ).set_sparam());
  894. add_opt(common_arg(
  895. {"--dry-penalty-last-n"}, "N",
  896. string_format("set DRY penalty for the last n tokens (default: %d, 0 = disable, -1 = context size)", params.sampling.dry_penalty_last_n),
  897. [](common_params & params, int value) {
  898. params.sampling.dry_penalty_last_n = value;
  899. }
  900. ).set_sparam());
  901. add_opt(common_arg(
  902. {"--dry-sequence-breaker"}, "STRING",
  903. string_format("add sequence breaker for DRY sampling, clearing out default breakers (%s) in the process; use \"none\" to not use any sequence breakers\n",
  904. params.sampling.dry_sequence_breakers.empty() ? "none" :
  905. std::accumulate(std::next(params.sampling.dry_sequence_breakers.begin()),
  906. params.sampling.dry_sequence_breakers.end(),
  907. std::string("'") + (params.sampling.dry_sequence_breakers[0] == "\n" ? "\\n" : params.sampling.dry_sequence_breakers[0]) + "'",
  908. [](const std::string& a, const std::string& b) {
  909. std::string formatted_b = (b == "\n") ? "\\n" : b;
  910. return a + ", '" + formatted_b + "'";
  911. }).c_str()),
  912. [](common_params & params, const std::string & value) {
  913. static bool defaults_cleared = false;
  914. if (!defaults_cleared) {
  915. params.sampling.dry_sequence_breakers.clear();
  916. defaults_cleared = true;
  917. }
  918. if (value == "none") {
  919. params.sampling.dry_sequence_breakers.clear();
  920. } else {
  921. params.sampling.dry_sequence_breakers.emplace_back(value);
  922. }
  923. }
  924. ).set_sparam());
  925. add_opt(common_arg(
  926. {"--dynatemp-range"}, "N",
  927. string_format("dynamic temperature range (default: %.1f, 0.0 = disabled)", (double)params.sampling.dynatemp_range),
  928. [](common_params & params, const std::string & value) {
  929. params.sampling.dynatemp_range = std::stof(value);
  930. }
  931. ).set_sparam());
  932. add_opt(common_arg(
  933. {"--dynatemp-exp"}, "N",
  934. string_format("dynamic temperature exponent (default: %.1f)", (double)params.sampling.dynatemp_exponent),
  935. [](common_params & params, const std::string & value) {
  936. params.sampling.dynatemp_exponent = std::stof(value);
  937. }
  938. ).set_sparam());
  939. add_opt(common_arg(
  940. {"--mirostat"}, "N",
  941. string_format("use Mirostat sampling.\nTop K, Nucleus and Locally Typical samplers are ignored if used.\n"
  942. "(default: %d, 0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0)", params.sampling.mirostat),
  943. [](common_params & params, int value) {
  944. params.sampling.mirostat = value;
  945. }
  946. ).set_sparam());
  947. add_opt(common_arg(
  948. {"--mirostat-lr"}, "N",
  949. string_format("Mirostat learning rate, parameter eta (default: %.1f)", (double)params.sampling.mirostat_eta),
  950. [](common_params & params, const std::string & value) {
  951. params.sampling.mirostat_eta = std::stof(value);
  952. }
  953. ).set_sparam());
  954. add_opt(common_arg(
  955. {"--mirostat-ent"}, "N",
  956. string_format("Mirostat target entropy, parameter tau (default: %.1f)", (double)params.sampling.mirostat_tau),
  957. [](common_params & params, const std::string & value) {
  958. params.sampling.mirostat_tau = std::stof(value);
  959. }
  960. ).set_sparam());
  961. add_opt(common_arg(
  962. {"-l", "--logit-bias"}, "TOKEN_ID(+/-)BIAS",
  963. "modifies the likelihood of token appearing in the completion,\n"
  964. "i.e. `--logit-bias 15043+1` to increase likelihood of token ' Hello',\n"
  965. "or `--logit-bias 15043-1` to decrease likelihood of token ' Hello'",
  966. [](common_params & params, const std::string & value) {
  967. std::stringstream ss(value);
  968. llama_token key;
  969. char sign;
  970. std::string value_str;
  971. try {
  972. if (ss >> key && ss >> sign && std::getline(ss, value_str) && (sign == '+' || sign == '-')) {
  973. const float bias = std::stof(value_str) * ((sign == '-') ? -1.0f : 1.0f);
  974. params.sampling.logit_bias.push_back({key, bias});
  975. } else {
  976. throw std::invalid_argument("invalid input format");
  977. }
  978. } catch (const std::exception&) {
  979. throw std::invalid_argument("invalid input format");
  980. }
  981. }
  982. ).set_sparam());
  983. add_opt(common_arg(
  984. {"--grammar"}, "GRAMMAR",
  985. string_format("BNF-like grammar to constrain generations (see samples in grammars/ dir) (default: '%s')", params.sampling.grammar.c_str()),
  986. [](common_params & params, const std::string & value) {
  987. params.sampling.grammar = value;
  988. }
  989. ).set_sparam());
  990. add_opt(common_arg(
  991. {"--grammar-file"}, "FNAME",
  992. "file to read grammar from",
  993. [](common_params & params, const std::string & value) {
  994. std::ifstream file(value);
  995. if (!file) {
  996. throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
  997. }
  998. std::copy(
  999. std::istreambuf_iterator<char>(file),
  1000. std::istreambuf_iterator<char>(),
  1001. std::back_inserter(params.sampling.grammar)
  1002. );
  1003. }
  1004. ).set_sparam());
  1005. add_opt(common_arg(
  1006. {"-j", "--json-schema"}, "SCHEMA",
  1007. "JSON schema to constrain generations (https://json-schema.org/), e.g. `{}` for any JSON object\nFor schemas w/ external $refs, use --grammar + example/json_schema_to_grammar.py instead",
  1008. [](common_params & params, const std::string & value) {
  1009. params.sampling.grammar = json_schema_to_grammar(json::parse(value));
  1010. }
  1011. ).set_sparam());
  1012. add_opt(common_arg(
  1013. {"--pooling"}, "{none,mean,cls,last,rank}",
  1014. "pooling type for embeddings, use model default if unspecified",
  1015. [](common_params & params, const std::string & value) {
  1016. /**/ if (value == "none") { params.pooling_type = LLAMA_POOLING_TYPE_NONE; }
  1017. else if (value == "mean") { params.pooling_type = LLAMA_POOLING_TYPE_MEAN; }
  1018. else if (value == "cls") { params.pooling_type = LLAMA_POOLING_TYPE_CLS; }
  1019. else if (value == "last") { params.pooling_type = LLAMA_POOLING_TYPE_LAST; }
  1020. else if (value == "rank") { params.pooling_type = LLAMA_POOLING_TYPE_RANK; }
  1021. else { throw std::invalid_argument("invalid value"); }
  1022. }
  1023. ).set_examples({LLAMA_EXAMPLE_EMBEDDING, LLAMA_EXAMPLE_RETRIEVAL, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_POOLING"));
  1024. add_opt(common_arg(
  1025. {"--attention"}, "{causal,non-causal}",
  1026. "attention type for embeddings, use model default if unspecified",
  1027. [](common_params & params, const std::string & value) {
  1028. /**/ if (value == "causal") { params.attention_type = LLAMA_ATTENTION_TYPE_CAUSAL; }
  1029. else if (value == "non-causal") { params.attention_type = LLAMA_ATTENTION_TYPE_NON_CAUSAL; }
  1030. else { throw std::invalid_argument("invalid value"); }
  1031. }
  1032. ).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
  1033. add_opt(common_arg(
  1034. {"--rope-scaling"}, "{none,linear,yarn}",
  1035. "RoPE frequency scaling method, defaults to linear unless specified by the model",
  1036. [](common_params & params, const std::string & value) {
  1037. /**/ if (value == "none") { params.rope_scaling_type = LLAMA_ROPE_SCALING_TYPE_NONE; }
  1038. else if (value == "linear") { params.rope_scaling_type = LLAMA_ROPE_SCALING_TYPE_LINEAR; }
  1039. else if (value == "yarn") { params.rope_scaling_type = LLAMA_ROPE_SCALING_TYPE_YARN; }
  1040. else { throw std::invalid_argument("invalid value"); }
  1041. }
  1042. ).set_env("LLAMA_ARG_ROPE_SCALING_TYPE"));
  1043. add_opt(common_arg(
  1044. {"--rope-scale"}, "N",
  1045. "RoPE context scaling factor, expands context by a factor of N",
  1046. [](common_params & params, const std::string & value) {
  1047. params.rope_freq_scale = 1.0f / std::stof(value);
  1048. }
  1049. ).set_env("LLAMA_ARG_ROPE_SCALE"));
  1050. add_opt(common_arg(
  1051. {"--rope-freq-base"}, "N",
  1052. "RoPE base frequency, used by NTK-aware scaling (default: loaded from model)",
  1053. [](common_params & params, const std::string & value) {
  1054. params.rope_freq_base = std::stof(value);
  1055. }
  1056. ).set_env("LLAMA_ARG_ROPE_FREQ_BASE"));
  1057. add_opt(common_arg(
  1058. {"--rope-freq-scale"}, "N",
  1059. "RoPE frequency scaling factor, expands context by a factor of 1/N",
  1060. [](common_params & params, const std::string & value) {
  1061. params.rope_freq_scale = std::stof(value);
  1062. }
  1063. ).set_env("LLAMA_ARG_ROPE_FREQ_SCALE"));
  1064. add_opt(common_arg(
  1065. {"--yarn-orig-ctx"}, "N",
  1066. string_format("YaRN: original context size of model (default: %d = model training context size)", params.yarn_orig_ctx),
  1067. [](common_params & params, int value) {
  1068. params.yarn_orig_ctx = value;
  1069. }
  1070. ).set_env("LLAMA_ARG_YARN_ORIG_CTX"));
  1071. add_opt(common_arg(
  1072. {"--yarn-ext-factor"}, "N",
  1073. string_format("YaRN: extrapolation mix factor (default: %.1f, 0.0 = full interpolation)", (double)params.yarn_ext_factor),
  1074. [](common_params & params, const std::string & value) {
  1075. params.yarn_ext_factor = std::stof(value);
  1076. }
  1077. ).set_env("LLAMA_ARG_YARN_EXT_FACTOR"));
  1078. add_opt(common_arg(
  1079. {"--yarn-attn-factor"}, "N",
  1080. string_format("YaRN: scale sqrt(t) or attention magnitude (default: %.1f)", (double)params.yarn_attn_factor),
  1081. [](common_params & params, const std::string & value) {
  1082. params.yarn_attn_factor = std::stof(value);
  1083. }
  1084. ).set_env("LLAMA_ARG_YARN_ATTN_FACTOR"));
  1085. add_opt(common_arg(
  1086. {"--yarn-beta-slow"}, "N",
  1087. string_format("YaRN: high correction dim or alpha (default: %.1f)", (double)params.yarn_beta_slow),
  1088. [](common_params & params, const std::string & value) {
  1089. params.yarn_beta_slow = std::stof(value);
  1090. }
  1091. ).set_env("LLAMA_ARG_YARN_BETA_SLOW"));
  1092. add_opt(common_arg(
  1093. {"--yarn-beta-fast"}, "N",
  1094. string_format("YaRN: low correction dim or beta (default: %.1f)", (double)params.yarn_beta_fast),
  1095. [](common_params & params, const std::string & value) {
  1096. params.yarn_beta_fast = std::stof(value);
  1097. }
  1098. ).set_env("LLAMA_ARG_YARN_BETA_FAST"));
  1099. add_opt(common_arg(
  1100. {"-gan", "--grp-attn-n"}, "N",
  1101. string_format("group-attention factor (default: %d)", params.grp_attn_n),
  1102. [](common_params & params, int value) {
  1103. params.grp_attn_n = value;
  1104. }
  1105. ).set_env("LLAMA_ARG_GRP_ATTN_N").set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_PASSKEY}));
  1106. add_opt(common_arg(
  1107. {"-gaw", "--grp-attn-w"}, "N",
  1108. string_format("group-attention width (default: %d)", params.grp_attn_w),
  1109. [](common_params & params, int value) {
  1110. params.grp_attn_w = value;
  1111. }
  1112. ).set_env("LLAMA_ARG_GRP_ATTN_W").set_examples({LLAMA_EXAMPLE_MAIN}));
  1113. add_opt(common_arg(
  1114. {"-dkvc", "--dump-kv-cache"},
  1115. "verbose print of the KV cache",
  1116. [](common_params & params) {
  1117. params.dump_kv_cache = true;
  1118. }
  1119. ));
  1120. add_opt(common_arg(
  1121. {"-nkvo", "--no-kv-offload"},
  1122. "disable KV offload",
  1123. [](common_params & params) {
  1124. params.no_kv_offload = true;
  1125. }
  1126. ).set_env("LLAMA_ARG_NO_KV_OFFLOAD"));
  1127. add_opt(common_arg(
  1128. {"-ctk", "--cache-type-k"}, "TYPE",
  1129. string_format("KV cache data type for K (default: %s)", params.cache_type_k.c_str()),
  1130. [](common_params & params, const std::string & value) {
  1131. // TODO: get the type right here
  1132. params.cache_type_k = value;
  1133. }
  1134. ).set_env("LLAMA_ARG_CACHE_TYPE_K"));
  1135. add_opt(common_arg(
  1136. {"-ctv", "--cache-type-v"}, "TYPE",
  1137. string_format("KV cache data type for V (default: %s)", params.cache_type_v.c_str()),
  1138. [](common_params & params, const std::string & value) {
  1139. // TODO: get the type right here
  1140. params.cache_type_v = value;
  1141. }
  1142. ).set_env("LLAMA_ARG_CACHE_TYPE_V"));
  1143. add_opt(common_arg(
  1144. {"--perplexity", "--all-logits"},
  1145. string_format("return logits for all tokens in the batch (default: %s)", params.logits_all ? "true" : "false"),
  1146. [](common_params & params) {
  1147. params.logits_all = true;
  1148. }
  1149. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1150. add_opt(common_arg(
  1151. {"--hellaswag"},
  1152. "compute HellaSwag score over random tasks from datafile supplied with -f",
  1153. [](common_params & params) {
  1154. params.hellaswag = true;
  1155. }
  1156. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1157. add_opt(common_arg(
  1158. {"--hellaswag-tasks"}, "N",
  1159. string_format("number of tasks to use when computing the HellaSwag score (default: %zu)", params.hellaswag_tasks),
  1160. [](common_params & params, int value) {
  1161. params.hellaswag_tasks = value;
  1162. }
  1163. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1164. add_opt(common_arg(
  1165. {"--winogrande"},
  1166. "compute Winogrande score over random tasks from datafile supplied with -f",
  1167. [](common_params & params) {
  1168. params.winogrande = true;
  1169. }
  1170. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1171. add_opt(common_arg(
  1172. {"--winogrande-tasks"}, "N",
  1173. string_format("number of tasks to use when computing the Winogrande score (default: %zu)", params.winogrande_tasks),
  1174. [](common_params & params, int value) {
  1175. params.winogrande_tasks = value;
  1176. }
  1177. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1178. add_opt(common_arg(
  1179. {"--multiple-choice"},
  1180. "compute multiple choice score over random tasks from datafile supplied with -f",
  1181. [](common_params & params) {
  1182. params.multiple_choice = true;
  1183. }
  1184. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1185. add_opt(common_arg(
  1186. {"--multiple-choice-tasks"}, "N",
  1187. string_format("number of tasks to use when computing the multiple choice score (default: %zu)", params.multiple_choice_tasks),
  1188. [](common_params & params, int value) {
  1189. params.multiple_choice_tasks = value;
  1190. }
  1191. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1192. add_opt(common_arg(
  1193. {"--kl-divergence"},
  1194. "computes KL-divergence to logits provided via --kl-divergence-base",
  1195. [](common_params & params) {
  1196. params.kl_divergence = true;
  1197. }
  1198. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1199. add_opt(common_arg(
  1200. {"--save-all-logits", "--kl-divergence-base"}, "FNAME",
  1201. "set logits file",
  1202. [](common_params & params, const std::string & value) {
  1203. params.logits_file = value;
  1204. }
  1205. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1206. add_opt(common_arg(
  1207. {"--ppl-stride"}, "N",
  1208. string_format("stride for perplexity calculation (default: %d)", params.ppl_stride),
  1209. [](common_params & params, int value) {
  1210. params.ppl_stride = value;
  1211. }
  1212. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1213. add_opt(common_arg(
  1214. {"--ppl-output-type"}, "<0|1>",
  1215. string_format("output type for perplexity calculation (default: %d)", params.ppl_output_type),
  1216. [](common_params & params, int value) {
  1217. params.ppl_output_type = value;
  1218. }
  1219. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1220. add_opt(common_arg(
  1221. {"-dt", "--defrag-thold"}, "N",
  1222. string_format("KV cache defragmentation threshold (default: %.1f, < 0 - disabled)", (double)params.defrag_thold),
  1223. [](common_params & params, const std::string & value) {
  1224. params.defrag_thold = std::stof(value);
  1225. }
  1226. ).set_env("LLAMA_ARG_DEFRAG_THOLD"));
  1227. add_opt(common_arg(
  1228. {"-np", "--parallel"}, "N",
  1229. string_format("number of parallel sequences to decode (default: %d)", params.n_parallel),
  1230. [](common_params & params, int value) {
  1231. params.n_parallel = value;
  1232. }
  1233. ).set_env("LLAMA_ARG_N_PARALLEL"));
  1234. add_opt(common_arg(
  1235. {"-ns", "--sequences"}, "N",
  1236. string_format("number of sequences to decode (default: %d)", params.n_sequences),
  1237. [](common_params & params, int value) {
  1238. params.n_sequences = value;
  1239. }
  1240. ).set_examples({LLAMA_EXAMPLE_PARALLEL}));
  1241. add_opt(common_arg(
  1242. {"-cb", "--cont-batching"},
  1243. string_format("enable continuous batching (a.k.a dynamic batching) (default: %s)", params.cont_batching ? "enabled" : "disabled"),
  1244. [](common_params & params) {
  1245. params.cont_batching = true;
  1246. }
  1247. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CONT_BATCHING"));
  1248. add_opt(common_arg(
  1249. {"-nocb", "--no-cont-batching"},
  1250. "disable continuous batching",
  1251. [](common_params & params) {
  1252. params.cont_batching = false;
  1253. }
  1254. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_NO_CONT_BATCHING"));
  1255. add_opt(common_arg(
  1256. {"--mmproj"}, "FILE",
  1257. "path to a multimodal projector file for LLaVA. see examples/llava/README.md",
  1258. [](common_params & params, const std::string & value) {
  1259. params.mmproj = value;
  1260. }
  1261. ).set_examples({LLAMA_EXAMPLE_LLAVA}));
  1262. add_opt(common_arg(
  1263. {"--image"}, "FILE",
  1264. "path to an image file. use with multimodal models. Specify multiple times for batching",
  1265. [](common_params & params, const std::string & value) {
  1266. params.image.emplace_back(value);
  1267. }
  1268. ).set_examples({LLAMA_EXAMPLE_LLAVA}));
  1269. if (llama_supports_rpc()) {
  1270. add_opt(common_arg(
  1271. {"--rpc"}, "SERVERS",
  1272. "comma separated list of RPC servers",
  1273. [](common_params & params, const std::string & value) {
  1274. params.rpc_servers = value;
  1275. }
  1276. ).set_env("LLAMA_ARG_RPC"));
  1277. }
  1278. add_opt(common_arg(
  1279. {"--mlock"},
  1280. "force system to keep model in RAM rather than swapping or compressing",
  1281. [](common_params & params) {
  1282. params.use_mlock = true;
  1283. }
  1284. ).set_env("LLAMA_ARG_MLOCK"));
  1285. add_opt(common_arg(
  1286. {"--no-mmap"},
  1287. "do not memory-map model (slower load but may reduce pageouts if not using mlock)",
  1288. [](common_params & params) {
  1289. params.use_mmap = false;
  1290. }
  1291. ).set_env("LLAMA_ARG_NO_MMAP"));
  1292. add_opt(common_arg(
  1293. {"--numa"}, "TYPE",
  1294. "attempt optimizations that help on some NUMA systems\n"
  1295. "- distribute: spread execution evenly over all nodes\n"
  1296. "- isolate: only spawn threads on CPUs on the node that execution started on\n"
  1297. "- numactl: use the CPU map provided by numactl\n"
  1298. "if run without this previously, it is recommended to drop the system page cache before using this\n"
  1299. "see https://github.com/ggerganov/llama.cpp/issues/1437",
  1300. [](common_params & params, const std::string & value) {
  1301. /**/ if (value == "distribute" || value == "") { params.numa = GGML_NUMA_STRATEGY_DISTRIBUTE; }
  1302. else if (value == "isolate") { params.numa = GGML_NUMA_STRATEGY_ISOLATE; }
  1303. else if (value == "numactl") { params.numa = GGML_NUMA_STRATEGY_NUMACTL; }
  1304. else { throw std::invalid_argument("invalid value"); }
  1305. }
  1306. ).set_env("LLAMA_ARG_NUMA"));
  1307. add_opt(common_arg(
  1308. {"-dev", "--device"}, "<dev1,dev2,..>",
  1309. "comma-separated list of devices to use for offloading (none = don't offload)\n"
  1310. "use --list-devices to see a list of available devices",
  1311. [](common_params & params, const std::string & value) {
  1312. params.devices = parse_device_list(value);
  1313. }
  1314. ).set_env("LLAMA_ARG_DEVICE"));
  1315. add_opt(common_arg(
  1316. {"--list-devices"},
  1317. "print list of available devices and exit",
  1318. [](common_params &) {
  1319. printf("Available devices:\n");
  1320. for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
  1321. auto * dev = ggml_backend_dev_get(i);
  1322. if (ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_GPU) {
  1323. size_t free, total;
  1324. ggml_backend_dev_memory(dev, &free, &total);
  1325. printf(" %s: %s (%zu MiB, %zu MiB free)\n", ggml_backend_dev_name(dev), ggml_backend_dev_description(dev), total / 1024 / 1024, free / 1024 / 1024);
  1326. }
  1327. }
  1328. exit(0);
  1329. }
  1330. ));
  1331. add_opt(common_arg(
  1332. {"-ngl", "--gpu-layers", "--n-gpu-layers"}, "N",
  1333. "number of layers to store in VRAM",
  1334. [](common_params & params, int value) {
  1335. params.n_gpu_layers = value;
  1336. if (!llama_supports_gpu_offload()) {
  1337. fprintf(stderr, "warning: no usable GPU found, --gpu-layers option will be ignored\n");
  1338. fprintf(stderr, "warning: one possible reason is that llama.cpp was compiled without GPU support\n");
  1339. fprintf(stderr, "warning: consult docs/build.md for compilation instructions\n");
  1340. }
  1341. }
  1342. ).set_env("LLAMA_ARG_N_GPU_LAYERS"));
  1343. add_opt(common_arg(
  1344. {"-sm", "--split-mode"}, "{none,layer,row}",
  1345. "how to split the model across multiple GPUs, one of:\n"
  1346. "- none: use one GPU only\n"
  1347. "- layer (default): split layers and KV across GPUs\n"
  1348. "- row: split rows across GPUs",
  1349. [](common_params & params, const std::string & value) {
  1350. std::string arg_next = value;
  1351. if (arg_next == "none") {
  1352. params.split_mode = LLAMA_SPLIT_MODE_NONE;
  1353. } else if (arg_next == "layer") {
  1354. params.split_mode = LLAMA_SPLIT_MODE_LAYER;
  1355. } else if (arg_next == "row") {
  1356. params.split_mode = LLAMA_SPLIT_MODE_ROW;
  1357. } else {
  1358. throw std::invalid_argument("invalid value");
  1359. }
  1360. if (!llama_supports_gpu_offload()) {
  1361. fprintf(stderr, "warning: llama.cpp was compiled without support for GPU offload. Setting the split mode has no effect.\n");
  1362. }
  1363. }
  1364. ).set_env("LLAMA_ARG_SPLIT_MODE"));
  1365. add_opt(common_arg(
  1366. {"-ts", "--tensor-split"}, "N0,N1,N2,...",
  1367. "fraction of the model to offload to each GPU, comma-separated list of proportions, e.g. 3,1",
  1368. [](common_params & params, const std::string & value) {
  1369. std::string arg_next = value;
  1370. // split string by , and /
  1371. const std::regex regex{ R"([,/]+)" };
  1372. std::sregex_token_iterator it{ arg_next.begin(), arg_next.end(), regex, -1 };
  1373. std::vector<std::string> split_arg{ it, {} };
  1374. if (split_arg.size() >= llama_max_devices()) {
  1375. throw std::invalid_argument(
  1376. string_format("got %d input configs, but system only has %d devices", (int)split_arg.size(), (int)llama_max_devices())
  1377. );
  1378. }
  1379. for (size_t i = 0; i < llama_max_devices(); ++i) {
  1380. if (i < split_arg.size()) {
  1381. params.tensor_split[i] = std::stof(split_arg[i]);
  1382. } else {
  1383. params.tensor_split[i] = 0.0f;
  1384. }
  1385. }
  1386. if (!llama_supports_gpu_offload()) {
  1387. fprintf(stderr, "warning: llama.cpp was compiled without support for GPU offload. Setting a tensor split has no effect.\n");
  1388. }
  1389. }
  1390. ).set_env("LLAMA_ARG_TENSOR_SPLIT"));
  1391. add_opt(common_arg(
  1392. {"-mg", "--main-gpu"}, "INDEX",
  1393. string_format("the GPU to use for the model (with split-mode = none), or for intermediate results and KV (with split-mode = row) (default: %d)", params.main_gpu),
  1394. [](common_params & params, int value) {
  1395. params.main_gpu = value;
  1396. if (!llama_supports_gpu_offload()) {
  1397. fprintf(stderr, "warning: llama.cpp was compiled without support for GPU offload. Setting the main GPU has no effect.\n");
  1398. }
  1399. }
  1400. ).set_env("LLAMA_ARG_MAIN_GPU"));
  1401. add_opt(common_arg(
  1402. {"--check-tensors"},
  1403. string_format("check model tensor data for invalid values (default: %s)", params.check_tensors ? "true" : "false"),
  1404. [](common_params & params) {
  1405. params.check_tensors = true;
  1406. }
  1407. ));
  1408. add_opt(common_arg(
  1409. {"--override-kv"}, "KEY=TYPE:VALUE",
  1410. "advanced option to override model metadata by key. may be specified multiple times.\n"
  1411. "types: int, float, bool, str. example: --override-kv tokenizer.ggml.add_bos_token=bool:false",
  1412. [](common_params & params, const std::string & value) {
  1413. if (!string_parse_kv_override(value.c_str(), params.kv_overrides)) {
  1414. throw std::runtime_error(string_format("error: Invalid type for KV override: %s\n", value.c_str()));
  1415. }
  1416. }
  1417. ));
  1418. add_opt(common_arg(
  1419. {"--lora"}, "FNAME",
  1420. "path to LoRA adapter (can be repeated to use multiple adapters)",
  1421. [](common_params & params, const std::string & value) {
  1422. params.lora_adapters.push_back({ std::string(value), 1.0 });
  1423. }
  1424. // we define this arg on both COMMON and EXPORT_LORA, so when showing help message of export-lora, it will be categorized as "example-specific" arg
  1425. ).set_examples({LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_EXPORT_LORA}));
  1426. add_opt(common_arg(
  1427. {"--lora-scaled"}, "FNAME", "SCALE",
  1428. "path to LoRA adapter with user defined scaling (can be repeated to use multiple adapters)",
  1429. [](common_params & params, const std::string & fname, const std::string & scale) {
  1430. params.lora_adapters.push_back({ fname, std::stof(scale) });
  1431. }
  1432. // we define this arg on both COMMON and EXPORT_LORA, so when showing help message of export-lora, it will be categorized as "example-specific" arg
  1433. ).set_examples({LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_EXPORT_LORA}));
  1434. add_opt(common_arg(
  1435. {"--control-vector"}, "FNAME",
  1436. "add a control vector\nnote: this argument can be repeated to add multiple control vectors",
  1437. [](common_params & params, const std::string & value) {
  1438. params.control_vectors.push_back({ 1.0f, value, });
  1439. }
  1440. ));
  1441. add_opt(common_arg(
  1442. {"--control-vector-scaled"}, "FNAME", "SCALE",
  1443. "add a control vector with user defined scaling SCALE\n"
  1444. "note: this argument can be repeated to add multiple scaled control vectors",
  1445. [](common_params & params, const std::string & fname, const std::string & scale) {
  1446. params.control_vectors.push_back({ std::stof(scale), fname });
  1447. }
  1448. ));
  1449. add_opt(common_arg(
  1450. {"--control-vector-layer-range"}, "START", "END",
  1451. "layer range to apply the control vector(s) to, start and end inclusive",
  1452. [](common_params & params, const std::string & start, const std::string & end) {
  1453. params.control_vector_layer_start = std::stoi(start);
  1454. params.control_vector_layer_end = std::stoi(end);
  1455. }
  1456. ));
  1457. add_opt(common_arg(
  1458. {"-a", "--alias"}, "STRING",
  1459. "set alias for model name (to be used by REST API)",
  1460. [](common_params & params, const std::string & value) {
  1461. params.model_alias = value;
  1462. }
  1463. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ALIAS"));
  1464. add_opt(common_arg(
  1465. {"-m", "--model"}, "FNAME",
  1466. ex == LLAMA_EXAMPLE_EXPORT_LORA
  1467. ? std::string("model path from which to load base model")
  1468. : string_format(
  1469. "model path (default: `models/$filename` with filename from `--hf-file` "
  1470. "or `--model-url` if set, otherwise %s)", DEFAULT_MODEL_PATH
  1471. ),
  1472. [](common_params & params, const std::string & value) {
  1473. params.model = value;
  1474. }
  1475. ).set_examples({LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_EXPORT_LORA}).set_env("LLAMA_ARG_MODEL"));
  1476. add_opt(common_arg(
  1477. {"-mu", "--model-url"}, "MODEL_URL",
  1478. "model download url (default: unused)",
  1479. [](common_params & params, const std::string & value) {
  1480. params.model_url = value;
  1481. }
  1482. ).set_env("LLAMA_ARG_MODEL_URL"));
  1483. add_opt(common_arg(
  1484. {"-hfr", "--hf-repo"}, "REPO",
  1485. "Hugging Face model repository (default: unused)",
  1486. [](common_params & params, const std::string & value) {
  1487. params.hf_repo = value;
  1488. }
  1489. ).set_env("LLAMA_ARG_HF_REPO"));
  1490. add_opt(common_arg(
  1491. {"-hff", "--hf-file"}, "FILE",
  1492. "Hugging Face model file (default: unused)",
  1493. [](common_params & params, const std::string & value) {
  1494. params.hf_file = value;
  1495. }
  1496. ).set_env("LLAMA_ARG_HF_FILE"));
  1497. add_opt(common_arg(
  1498. {"-hft", "--hf-token"}, "TOKEN",
  1499. "Hugging Face access token (default: value from HF_TOKEN environment variable)",
  1500. [](common_params & params, const std::string & value) {
  1501. params.hf_token = value;
  1502. }
  1503. ).set_env("HF_TOKEN"));
  1504. add_opt(common_arg(
  1505. {"--context-file"}, "FNAME",
  1506. "file to load context from (repeat to specify multiple files)",
  1507. [](common_params & params, const std::string & value) {
  1508. std::ifstream file(value, std::ios::binary);
  1509. if (!file) {
  1510. throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
  1511. }
  1512. params.context_files.push_back(value);
  1513. }
  1514. ).set_examples({LLAMA_EXAMPLE_RETRIEVAL}));
  1515. add_opt(common_arg(
  1516. {"--chunk-size"}, "N",
  1517. string_format("minimum length of embedded text chunks (default: %d)", params.chunk_size),
  1518. [](common_params & params, int value) {
  1519. params.chunk_size = value;
  1520. }
  1521. ).set_examples({LLAMA_EXAMPLE_RETRIEVAL}));
  1522. add_opt(common_arg(
  1523. {"--chunk-separator"}, "STRING",
  1524. string_format("separator between chunks (default: '%s')", params.chunk_separator.c_str()),
  1525. [](common_params & params, const std::string & value) {
  1526. params.chunk_separator = value;
  1527. }
  1528. ).set_examples({LLAMA_EXAMPLE_RETRIEVAL}));
  1529. add_opt(common_arg(
  1530. {"--junk"}, "N",
  1531. string_format("number of times to repeat the junk text (default: %d)", params.n_junk),
  1532. [](common_params & params, int value) {
  1533. params.n_junk = value;
  1534. }
  1535. ).set_examples({LLAMA_EXAMPLE_PASSKEY}));
  1536. add_opt(common_arg(
  1537. {"--pos"}, "N",
  1538. string_format("position of the passkey in the junk text (default: %d)", params.i_pos),
  1539. [](common_params & params, int value) {
  1540. params.i_pos = value;
  1541. }
  1542. ).set_examples({LLAMA_EXAMPLE_PASSKEY}));
  1543. add_opt(common_arg(
  1544. {"-o", "--output", "--output-file"}, "FNAME",
  1545. string_format("output file (default: '%s')",
  1546. ex == LLAMA_EXAMPLE_EXPORT_LORA
  1547. ? params.lora_outfile.c_str()
  1548. : ex == LLAMA_EXAMPLE_CVECTOR_GENERATOR
  1549. ? params.cvector_outfile.c_str()
  1550. : params.out_file.c_str()),
  1551. [](common_params & params, const std::string & value) {
  1552. params.out_file = value;
  1553. params.cvector_outfile = value;
  1554. params.lora_outfile = value;
  1555. }
  1556. ).set_examples({LLAMA_EXAMPLE_IMATRIX, LLAMA_EXAMPLE_CVECTOR_GENERATOR, LLAMA_EXAMPLE_EXPORT_LORA}));
  1557. add_opt(common_arg(
  1558. {"-ofreq", "--output-frequency"}, "N",
  1559. string_format("output the imatrix every N iterations (default: %d)", params.n_out_freq),
  1560. [](common_params & params, int value) {
  1561. params.n_out_freq = value;
  1562. }
  1563. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  1564. add_opt(common_arg(
  1565. {"--save-frequency"}, "N",
  1566. string_format("save an imatrix copy every N iterations (default: %d)", params.n_save_freq),
  1567. [](common_params & params, int value) {
  1568. params.n_save_freq = value;
  1569. }
  1570. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  1571. add_opt(common_arg(
  1572. {"--process-output"},
  1573. string_format("collect data for the output tensor (default: %s)", params.process_output ? "true" : "false"),
  1574. [](common_params & params) {
  1575. params.process_output = true;
  1576. }
  1577. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  1578. add_opt(common_arg(
  1579. {"--no-ppl"},
  1580. string_format("do not compute perplexity (default: %s)", params.compute_ppl ? "true" : "false"),
  1581. [](common_params & params) {
  1582. params.compute_ppl = false;
  1583. }
  1584. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  1585. add_opt(common_arg(
  1586. {"--chunk", "--from-chunk"}, "N",
  1587. string_format("start processing the input from chunk N (default: %d)", params.i_chunk),
  1588. [](common_params & params, int value) {
  1589. params.i_chunk = value;
  1590. }
  1591. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  1592. add_opt(common_arg(
  1593. {"-pps"},
  1594. string_format("is the prompt shared across parallel sequences (default: %s)", params.is_pp_shared ? "true" : "false"),
  1595. [](common_params & params) {
  1596. params.is_pp_shared = true;
  1597. }
  1598. ).set_examples({LLAMA_EXAMPLE_BENCH}));
  1599. add_opt(common_arg(
  1600. {"-npp"}, "n0,n1,...",
  1601. "number of prompt tokens",
  1602. [](common_params & params, const std::string & value) {
  1603. auto p = string_split<int>(value, ',');
  1604. params.n_pp.insert(params.n_pp.end(), p.begin(), p.end());
  1605. }
  1606. ).set_examples({LLAMA_EXAMPLE_BENCH}));
  1607. add_opt(common_arg(
  1608. {"-ntg"}, "n0,n1,...",
  1609. "number of text generation tokens",
  1610. [](common_params & params, const std::string & value) {
  1611. auto p = string_split<int>(value, ',');
  1612. params.n_tg.insert(params.n_tg.end(), p.begin(), p.end());
  1613. }
  1614. ).set_examples({LLAMA_EXAMPLE_BENCH}));
  1615. add_opt(common_arg(
  1616. {"-npl"}, "n0,n1,...",
  1617. "number of parallel prompts",
  1618. [](common_params & params, const std::string & value) {
  1619. auto p = string_split<int>(value, ',');
  1620. params.n_pl.insert(params.n_pl.end(), p.begin(), p.end());
  1621. }
  1622. ).set_examples({LLAMA_EXAMPLE_BENCH}));
  1623. add_opt(common_arg(
  1624. {"--embd-normalize"}, "N",
  1625. string_format("normalisation for embeddings (default: %d) (-1=none, 0=max absolute int16, 1=taxicab, 2=euclidean, >2=p-norm)", params.embd_normalize),
  1626. [](common_params & params, int value) {
  1627. params.embd_normalize = value;
  1628. }
  1629. ).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
  1630. add_opt(common_arg(
  1631. {"--embd-output-format"}, "FORMAT",
  1632. "empty = default, \"array\" = [[],[]...], \"json\" = openai style, \"json+\" = same \"json\" + cosine similarity matrix",
  1633. [](common_params & params, const std::string & value) {
  1634. params.embd_out = value;
  1635. }
  1636. ).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
  1637. add_opt(common_arg(
  1638. {"--embd-separator"}, "STRING",
  1639. "separator of embeddings (default \\n) for example \"<#sep#>\"",
  1640. [](common_params & params, const std::string & value) {
  1641. params.embd_sep = value;
  1642. }
  1643. ).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
  1644. add_opt(common_arg(
  1645. {"--host"}, "HOST",
  1646. string_format("ip address to listen (default: %s)", params.hostname.c_str()),
  1647. [](common_params & params, const std::string & value) {
  1648. params.hostname = value;
  1649. }
  1650. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_HOST"));
  1651. add_opt(common_arg(
  1652. {"--port"}, "PORT",
  1653. string_format("port to listen (default: %d)", params.port),
  1654. [](common_params & params, int value) {
  1655. params.port = value;
  1656. }
  1657. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_PORT"));
  1658. add_opt(common_arg(
  1659. {"--path"}, "PATH",
  1660. string_format("path to serve static files from (default: %s)", params.public_path.c_str()),
  1661. [](common_params & params, const std::string & value) {
  1662. params.public_path = value;
  1663. }
  1664. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_STATIC_PATH"));
  1665. add_opt(common_arg(
  1666. {"--no-webui"},
  1667. string_format("Disable the Web UI (default: %s)", params.webui ? "enabled" : "disabled"),
  1668. [](common_params & params) {
  1669. params.webui = false;
  1670. }
  1671. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_NO_WEBUI"));
  1672. add_opt(common_arg(
  1673. {"--embedding", "--embeddings"},
  1674. string_format("restrict to only support embedding use case; use only with dedicated embedding models (default: %s)", params.embedding ? "enabled" : "disabled"),
  1675. [](common_params & params) {
  1676. params.embedding = true;
  1677. }
  1678. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_EMBEDDINGS"));
  1679. add_opt(common_arg(
  1680. {"--reranking", "--rerank"},
  1681. string_format("enable reranking endpoint on server (default: %s)", params.reranking ? "enabled" : "disabled"),
  1682. [](common_params & params) {
  1683. params.reranking = true;
  1684. }
  1685. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_RERANKING"));
  1686. add_opt(common_arg(
  1687. {"--api-key"}, "KEY",
  1688. "API key to use for authentication (default: none)",
  1689. [](common_params & params, const std::string & value) {
  1690. params.api_keys.push_back(value);
  1691. }
  1692. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_API_KEY"));
  1693. add_opt(common_arg(
  1694. {"--api-key-file"}, "FNAME",
  1695. "path to file containing API keys (default: none)",
  1696. [](common_params & params, const std::string & value) {
  1697. std::ifstream key_file(value);
  1698. if (!key_file) {
  1699. throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
  1700. }
  1701. std::string key;
  1702. while (std::getline(key_file, key)) {
  1703. if (!key.empty()) {
  1704. params.api_keys.push_back(key);
  1705. }
  1706. }
  1707. key_file.close();
  1708. }
  1709. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  1710. add_opt(common_arg(
  1711. {"--ssl-key-file"}, "FNAME",
  1712. "path to file a PEM-encoded SSL private key",
  1713. [](common_params & params, const std::string & value) {
  1714. params.ssl_file_key = value;
  1715. }
  1716. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_SSL_KEY_FILE"));
  1717. add_opt(common_arg(
  1718. {"--ssl-cert-file"}, "FNAME",
  1719. "path to file a PEM-encoded SSL certificate",
  1720. [](common_params & params, const std::string & value) {
  1721. params.ssl_file_cert = value;
  1722. }
  1723. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_SSL_CERT_FILE"));
  1724. add_opt(common_arg(
  1725. {"-to", "--timeout"}, "N",
  1726. string_format("server read/write timeout in seconds (default: %d)", params.timeout_read),
  1727. [](common_params & params, int value) {
  1728. params.timeout_read = value;
  1729. params.timeout_write = value;
  1730. }
  1731. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_TIMEOUT"));
  1732. add_opt(common_arg(
  1733. {"--threads-http"}, "N",
  1734. string_format("number of threads used to process HTTP requests (default: %d)", params.n_threads_http),
  1735. [](common_params & params, int value) {
  1736. params.n_threads_http = value;
  1737. }
  1738. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_THREADS_HTTP"));
  1739. add_opt(common_arg(
  1740. {"--cache-reuse"}, "N",
  1741. string_format("min chunk size to attempt reusing from the cache via KV shifting (default: %d)", params.n_cache_reuse),
  1742. [](common_params & params, int value) {
  1743. params.n_cache_reuse = value;
  1744. }
  1745. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CACHE_REUSE"));
  1746. add_opt(common_arg(
  1747. {"--metrics"},
  1748. string_format("enable prometheus compatible metrics endpoint (default: %s)", params.endpoint_metrics ? "enabled" : "disabled"),
  1749. [](common_params & params) {
  1750. params.endpoint_metrics = true;
  1751. }
  1752. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ENDPOINT_METRICS"));
  1753. add_opt(common_arg(
  1754. {"--slots"},
  1755. string_format("enable slots monitoring endpoint (default: %s)", params.endpoint_slots ? "enabled" : "disabled"),
  1756. [](common_params & params) {
  1757. params.endpoint_slots = true;
  1758. }
  1759. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ENDPOINT_SLOTS"));
  1760. add_opt(common_arg(
  1761. {"--props"},
  1762. string_format("enable changing global properties via POST /props (default: %s)", params.endpoint_props ? "enabled" : "disabled"),
  1763. [](common_params & params) {
  1764. params.endpoint_props = true;
  1765. }
  1766. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ENDPOINT_PROPS"));
  1767. add_opt(common_arg(
  1768. {"--no-slots"},
  1769. "disables slots monitoring endpoint",
  1770. [](common_params & params) {
  1771. params.endpoint_slots = false;
  1772. }
  1773. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_NO_ENDPOINT_SLOTS"));
  1774. add_opt(common_arg(
  1775. {"--slot-save-path"}, "PATH",
  1776. "path to save slot kv cache (default: disabled)",
  1777. [](common_params & params, const std::string & value) {
  1778. params.slot_save_path = value;
  1779. // if doesn't end with DIRECTORY_SEPARATOR, add it
  1780. if (!params.slot_save_path.empty() && params.slot_save_path[params.slot_save_path.size() - 1] != DIRECTORY_SEPARATOR) {
  1781. params.slot_save_path += DIRECTORY_SEPARATOR;
  1782. }
  1783. }
  1784. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  1785. add_opt(common_arg(
  1786. {"--chat-template"}, "JINJA_TEMPLATE",
  1787. string_format(
  1788. "set custom jinja chat template (default: template taken from model's metadata)\n"
  1789. "if suffix/prefix are specified, template will be disabled\n"
  1790. "list of built-in templates:\n%s", list_builtin_chat_templates().c_str()
  1791. ),
  1792. [](common_params & params, const std::string & value) {
  1793. if (!common_chat_verify_template(value)) {
  1794. throw std::runtime_error(string_format(
  1795. "error: the supplied chat template is not supported: %s\n"
  1796. "note: llama.cpp does not use jinja parser, we only support commonly used templates\n",
  1797. value.c_str()
  1798. ));
  1799. }
  1800. params.chat_template = value;
  1801. }
  1802. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CHAT_TEMPLATE"));
  1803. add_opt(common_arg(
  1804. {"-sps", "--slot-prompt-similarity"}, "SIMILARITY",
  1805. string_format("how much the prompt of a request must match the prompt of a slot in order to use that slot (default: %.2f, 0.0 = disabled)\n", params.slot_prompt_similarity),
  1806. [](common_params & params, const std::string & value) {
  1807. params.slot_prompt_similarity = std::stof(value);
  1808. }
  1809. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  1810. add_opt(common_arg(
  1811. {"--lora-init-without-apply"},
  1812. string_format("load LoRA adapters without applying them (apply later via POST /lora-adapters) (default: %s)", params.lora_init_without_apply ? "enabled" : "disabled"),
  1813. [](common_params & params) {
  1814. params.lora_init_without_apply = true;
  1815. }
  1816. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  1817. add_opt(common_arg(
  1818. {"--simple-io"},
  1819. "use basic IO for better compatibility in subprocesses and limited consoles",
  1820. [](common_params & params) {
  1821. params.simple_io = true;
  1822. }
  1823. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_INFILL}));
  1824. add_opt(common_arg(
  1825. {"--positive-file"}, "FNAME",
  1826. string_format("positive prompts file, one prompt per line (default: '%s')", params.cvector_positive_file.c_str()),
  1827. [](common_params & params, const std::string & value) {
  1828. params.cvector_positive_file = value;
  1829. }
  1830. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  1831. add_opt(common_arg(
  1832. {"--negative-file"}, "FNAME",
  1833. string_format("negative prompts file, one prompt per line (default: '%s')", params.cvector_negative_file.c_str()),
  1834. [](common_params & params, const std::string & value) {
  1835. params.cvector_negative_file = value;
  1836. }
  1837. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  1838. add_opt(common_arg(
  1839. {"--pca-batch"}, "N",
  1840. string_format("batch size used for PCA. Larger batch runs faster, but uses more memory (default: %d)", params.n_pca_batch),
  1841. [](common_params & params, int value) {
  1842. params.n_pca_batch = value;
  1843. }
  1844. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  1845. add_opt(common_arg(
  1846. {"--pca-iter"}, "N",
  1847. string_format("number of iterations used for PCA (default: %d)", params.n_pca_iterations),
  1848. [](common_params & params, int value) {
  1849. params.n_pca_iterations = value;
  1850. }
  1851. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  1852. add_opt(common_arg(
  1853. {"--method"}, "{pca, mean}",
  1854. "dimensionality reduction method to be used (default: pca)",
  1855. [](common_params & params, const std::string & value) {
  1856. /**/ if (value == "pca") { params.cvector_dimre_method = DIMRE_METHOD_PCA; }
  1857. else if (value == "mean") { params.cvector_dimre_method = DIMRE_METHOD_MEAN; }
  1858. else { throw std::invalid_argument("invalid value"); }
  1859. }
  1860. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  1861. add_opt(common_arg(
  1862. {"--output-format"}, "{md,jsonl}",
  1863. "output format for batched-bench results (default: md)",
  1864. [](common_params & params, const std::string & value) {
  1865. /**/ if (value == "jsonl") { params.batched_bench_output_jsonl = true; }
  1866. else if (value == "md") { params.batched_bench_output_jsonl = false; }
  1867. else { std::invalid_argument("invalid value"); }
  1868. }
  1869. ).set_examples({LLAMA_EXAMPLE_BENCH}));
  1870. add_opt(common_arg(
  1871. {"--log-disable"},
  1872. "Log disable",
  1873. [](common_params &) {
  1874. common_log_pause(common_log_main());
  1875. }
  1876. ));
  1877. add_opt(common_arg(
  1878. {"--log-file"}, "FNAME",
  1879. "Log to file",
  1880. [](common_params &, const std::string & value) {
  1881. common_log_set_file(common_log_main(), value.c_str());
  1882. }
  1883. ));
  1884. add_opt(common_arg(
  1885. {"--log-colors"},
  1886. "Enable colored logging",
  1887. [](common_params &) {
  1888. common_log_set_colors(common_log_main(), true);
  1889. }
  1890. ).set_env("LLAMA_LOG_COLORS"));
  1891. add_opt(common_arg(
  1892. {"-v", "--verbose", "--log-verbose"},
  1893. "Set verbosity level to infinity (i.e. log all messages, useful for debugging)",
  1894. [](common_params & params) {
  1895. params.verbosity = INT_MAX;
  1896. common_log_set_verbosity_thold(INT_MAX);
  1897. }
  1898. ));
  1899. add_opt(common_arg(
  1900. {"-lv", "--verbosity", "--log-verbosity"}, "N",
  1901. "Set the verbosity threshold. Messages with a higher verbosity will be ignored.",
  1902. [](common_params & params, int value) {
  1903. params.verbosity = value;
  1904. common_log_set_verbosity_thold(value);
  1905. }
  1906. ).set_env("LLAMA_LOG_VERBOSITY"));
  1907. add_opt(common_arg(
  1908. {"--log-prefix"},
  1909. "Enable prefx in log messages",
  1910. [](common_params &) {
  1911. common_log_set_prefix(common_log_main(), true);
  1912. }
  1913. ).set_env("LLAMA_LOG_PREFIX"));
  1914. add_opt(common_arg(
  1915. {"--log-timestamps"},
  1916. "Enable timestamps in log messages",
  1917. [](common_params &) {
  1918. common_log_set_timestamps(common_log_main(), true);
  1919. }
  1920. ).set_env("LLAMA_LOG_TIMESTAMPS"));
  1921. // speculative parameters
  1922. add_opt(common_arg(
  1923. {"-td", "--threads-draft"}, "N",
  1924. "number of threads to use during generation (default: same as --threads)",
  1925. [](common_params & params, int value) {
  1926. params.speculative.cpuparams.n_threads = value;
  1927. if (params.speculative.cpuparams.n_threads <= 0) {
  1928. params.speculative.cpuparams.n_threads = std::thread::hardware_concurrency();
  1929. }
  1930. }
  1931. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  1932. add_opt(common_arg(
  1933. {"-tbd", "--threads-batch-draft"}, "N",
  1934. "number of threads to use during batch and prompt processing (default: same as --threads-draft)",
  1935. [](common_params & params, int value) {
  1936. params.speculative.cpuparams_batch.n_threads = value;
  1937. if (params.speculative.cpuparams_batch.n_threads <= 0) {
  1938. params.speculative.cpuparams_batch.n_threads = std::thread::hardware_concurrency();
  1939. }
  1940. }
  1941. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  1942. add_opt(common_arg(
  1943. {"-Cd", "--cpu-mask-draft"}, "M",
  1944. "Draft model CPU affinity mask. Complements cpu-range-draft (default: same as --cpu-mask)",
  1945. [](common_params & params, const std::string & mask) {
  1946. params.speculative.cpuparams.mask_valid = true;
  1947. if (!parse_cpu_mask(mask, params.speculative.cpuparams.cpumask)) {
  1948. throw std::invalid_argument("invalid cpumask");
  1949. }
  1950. }
  1951. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  1952. add_opt(common_arg(
  1953. {"-Crd", "--cpu-range-draft"}, "lo-hi",
  1954. "Ranges of CPUs for affinity. Complements --cpu-mask-draft",
  1955. [](common_params & params, const std::string & range) {
  1956. params.speculative.cpuparams.mask_valid = true;
  1957. if (!parse_cpu_range(range, params.speculative.cpuparams.cpumask)) {
  1958. throw std::invalid_argument("invalid range");
  1959. }
  1960. }
  1961. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  1962. add_opt(common_arg(
  1963. {"--cpu-strict-draft"}, "<0|1>",
  1964. "Use strict CPU placement for draft model (default: same as --cpu-strict)",
  1965. [](common_params & params, int value) {
  1966. params.speculative.cpuparams.strict_cpu = value;
  1967. }
  1968. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  1969. add_opt(common_arg(
  1970. {"--prio-draft"}, "N",
  1971. string_format("set draft process/thread priority : 0-normal, 1-medium, 2-high, 3-realtime (default: %d)\n", params.speculative.cpuparams.priority),
  1972. [](common_params & params, int prio) {
  1973. if (prio < 0 || prio > 3) {
  1974. throw std::invalid_argument("invalid value");
  1975. }
  1976. params.speculative.cpuparams.priority = (enum ggml_sched_priority) prio;
  1977. }
  1978. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  1979. add_opt(common_arg(
  1980. {"--poll-draft"}, "<0|1>",
  1981. "Use polling to wait for draft model work (default: same as --poll])",
  1982. [](common_params & params, int value) {
  1983. params.speculative.cpuparams.poll = value;
  1984. }
  1985. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  1986. add_opt(common_arg(
  1987. {"-Cbd", "--cpu-mask-batch-draft"}, "M",
  1988. "Draft model CPU affinity mask. Complements cpu-range-draft (default: same as --cpu-mask)",
  1989. [](common_params & params, const std::string & mask) {
  1990. params.speculative.cpuparams_batch.mask_valid = true;
  1991. if (!parse_cpu_mask(mask, params.speculative.cpuparams_batch.cpumask)) {
  1992. throw std::invalid_argument("invalid cpumask");
  1993. }
  1994. }
  1995. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  1996. add_opt(common_arg(
  1997. {"-Crbd", "--cpu-range-batch-draft"}, "lo-hi",
  1998. "Ranges of CPUs for affinity. Complements --cpu-mask-draft-batch)",
  1999. [](common_params & params, const std::string & range) {
  2000. params.speculative.cpuparams_batch.mask_valid = true;
  2001. if (!parse_cpu_range(range, params.speculative.cpuparams_batch.cpumask)) {
  2002. throw std::invalid_argument("invalid cpumask");
  2003. }
  2004. }
  2005. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2006. add_opt(common_arg(
  2007. {"--cpu-strict-batch-draft"}, "<0|1>",
  2008. "Use strict CPU placement for draft model (default: --cpu-strict-draft)",
  2009. [](common_params & params, int value) {
  2010. params.speculative.cpuparams_batch.strict_cpu = value;
  2011. }
  2012. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2013. add_opt(common_arg(
  2014. {"--prio-batch-draft"}, "N",
  2015. string_format("set draft process/thread priority : 0-normal, 1-medium, 2-high, 3-realtime (default: %d)\n", params.speculative.cpuparams_batch.priority),
  2016. [](common_params & params, int prio) {
  2017. if (prio < 0 || prio > 3) {
  2018. throw std::invalid_argument("invalid value");
  2019. }
  2020. params.speculative.cpuparams_batch.priority = (enum ggml_sched_priority) prio;
  2021. }
  2022. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2023. add_opt(common_arg(
  2024. {"--poll-batch-draft"}, "<0|1>",
  2025. "Use polling to wait for draft model work (default: --poll-draft)",
  2026. [](common_params & params, int value) {
  2027. params.speculative.cpuparams_batch.poll = value;
  2028. }
  2029. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2030. add_opt(common_arg(
  2031. {"--draft-max", "--draft", "--draft-n"}, "N",
  2032. string_format("number of tokens to draft for speculative decoding (default: %d)", params.speculative.n_max),
  2033. [](common_params & params, int value) {
  2034. params.speculative.n_max = value;
  2035. }
  2036. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_LOOKUP, LLAMA_EXAMPLE_SERVER}));
  2037. add_opt(common_arg(
  2038. {"--draft-min", "--draft-n-min"}, "N",
  2039. string_format("minimum number of draft tokens to use for speculative decoding (default: %d)", params.speculative.n_min),
  2040. [](common_params & params, int value) {
  2041. params.speculative.n_min = value;
  2042. }
  2043. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_LOOKUP, LLAMA_EXAMPLE_SERVER}));
  2044. add_opt(common_arg(
  2045. {"--draft-p-split"}, "P",
  2046. string_format("speculative decoding split probability (default: %.1f)", (double)params.speculative.p_split),
  2047. [](common_params & params, const std::string & value) {
  2048. params.speculative.p_split = std::stof(value);
  2049. }
  2050. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2051. add_opt(common_arg(
  2052. {"--draft-p-min"}, "P",
  2053. string_format("minimum speculative decoding probability (greedy) (default: %.1f)", (double)params.speculative.p_min),
  2054. [](common_params & params, const std::string & value) {
  2055. params.speculative.p_min = std::stof(value);
  2056. }
  2057. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}));
  2058. add_opt(common_arg(
  2059. {"-cd", "--ctx-size-draft"}, "N",
  2060. string_format("size of the prompt context for the draft model (default: %d, 0 = loaded from model)", params.speculative.n_ctx),
  2061. [](common_params & params, int value) {
  2062. params.speculative.n_ctx = value;
  2063. }
  2064. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}));
  2065. add_opt(common_arg(
  2066. {"-devd", "--device-draft"}, "<dev1,dev2,..>",
  2067. "comma-separated list of devices to use for offloading the draft model (none = don't offload)\n"
  2068. "use --list-devices to see a list of available devices",
  2069. [](common_params & params, const std::string & value) {
  2070. params.speculative.devices = parse_device_list(value);
  2071. }
  2072. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}));
  2073. add_opt(common_arg(
  2074. {"-ngld", "--gpu-layers-draft", "--n-gpu-layers-draft"}, "N",
  2075. "number of layers to store in VRAM for the draft model",
  2076. [](common_params & params, int value) {
  2077. params.speculative.n_gpu_layers = value;
  2078. if (!llama_supports_gpu_offload()) {
  2079. fprintf(stderr, "warning: no usable GPU found, --gpu-layers-draft option will be ignored\n");
  2080. fprintf(stderr, "warning: one possible reason is that llama.cpp was compiled without GPU support\n");
  2081. fprintf(stderr, "warning: consult docs/build.md for compilation instructions\n");
  2082. }
  2083. }
  2084. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}));
  2085. add_opt(common_arg(
  2086. {"-md", "--model-draft"}, "FNAME",
  2087. "draft model for speculative decoding (default: unused)",
  2088. [](common_params & params, const std::string & value) {
  2089. params.speculative.model = value;
  2090. }
  2091. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}));
  2092. return ctx_arg;
  2093. }