arg.cpp 111 KB

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