arg.cpp 89 KB

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