arg.cpp 107 KB

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