arg.cpp 91 KB

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