1
0

arg.cpp 155 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522
  1. #include "arg.h"
  2. #include "chat.h"
  3. #include "common.h"
  4. #include "json-schema-to-grammar.h"
  5. #include "log.h"
  6. #include "sampling.h"
  7. #include "download.h"
  8. // fix problem with std::min and std::max
  9. #if defined(_WIN32)
  10. #define WIN32_LEAN_AND_MEAN
  11. #ifndef NOMINMAX
  12. # define NOMINMAX
  13. #endif
  14. #include <windows.h>
  15. #endif
  16. #define JSON_ASSERT GGML_ASSERT
  17. #include <nlohmann/json.hpp>
  18. #include <algorithm>
  19. #include <cinttypes>
  20. #include <climits>
  21. #include <cstdarg>
  22. #include <fstream>
  23. #include <list>
  24. #include <regex>
  25. #include <set>
  26. #include <string>
  27. #include <thread> // for hardware_concurrency
  28. #include <vector>
  29. #ifndef __EMSCRIPTEN__
  30. #ifdef __linux__
  31. #include <linux/limits.h>
  32. #elif defined(_WIN32)
  33. # if !defined(PATH_MAX)
  34. # define PATH_MAX MAX_PATH
  35. # endif
  36. #elif defined(_AIX)
  37. #include <sys/limits.h>
  38. #else
  39. #include <sys/syslimits.h>
  40. #endif
  41. #endif
  42. #define LLAMA_MAX_URL_LENGTH 2084 // Maximum URL Length in Chrome: 2083
  43. using json = nlohmann::ordered_json;
  44. using namespace common_arg_utils;
  45. static std::initializer_list<enum llama_example> mmproj_examples = {
  46. LLAMA_EXAMPLE_MTMD,
  47. LLAMA_EXAMPLE_SERVER,
  48. LLAMA_EXAMPLE_CLI,
  49. };
  50. static std::string read_file(const std::string & fname) {
  51. std::ifstream file(fname);
  52. if (!file) {
  53. throw std::runtime_error(string_format("error: failed to open file '%s'\n", fname.c_str()));
  54. }
  55. std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
  56. file.close();
  57. return content;
  58. }
  59. static const std::vector<common_arg> & get_common_arg_defs() {
  60. static const std::vector<common_arg> options = [] {
  61. common_params params;
  62. auto ctx = common_params_parser_init(params, LLAMA_EXAMPLE_SERVER, nullptr);
  63. return ctx.options;
  64. }();
  65. return options;
  66. }
  67. common_arg & common_arg::set_examples(std::initializer_list<enum llama_example> examples) {
  68. this->examples = examples;
  69. return *this;
  70. }
  71. common_arg & common_arg::set_excludes(std::initializer_list<enum llama_example> excludes) {
  72. this->excludes = excludes;
  73. return *this;
  74. }
  75. common_arg & common_arg::set_env(const char * env) {
  76. help = help + "\n(env: " + env + ")";
  77. this->env = env;
  78. return *this;
  79. }
  80. common_arg & common_arg::set_sparam() {
  81. is_sparam = true;
  82. return *this;
  83. }
  84. common_arg & common_arg::set_preset_only() {
  85. is_preset_only = true;
  86. return *this;
  87. }
  88. bool common_arg::in_example(enum llama_example ex) {
  89. return examples.find(ex) != examples.end();
  90. }
  91. bool common_arg::is_exclude(enum llama_example ex) {
  92. return excludes.find(ex) != excludes.end();
  93. }
  94. bool common_arg::get_value_from_env(std::string & output) const {
  95. if (env == nullptr) return false;
  96. if (!args_neg.empty()) {
  97. // for compatibility, we need to check LLAMA_ARG_NO_ env as well
  98. std::string neg_env = env;
  99. string_replace_all(neg_env, "LLAMA_ARG_", "LLAMA_ARG_NO_");
  100. char * neg_value = std::getenv(neg_env.c_str());
  101. if (neg_value) {
  102. output = "0"; // falsey
  103. return true;
  104. }
  105. }
  106. char * value = std::getenv(env);
  107. if (value) {
  108. output = value;
  109. return true;
  110. }
  111. return false;
  112. }
  113. bool common_arg::has_value_from_env() const {
  114. if (env != nullptr && !args_neg.empty()) {
  115. // for compatibility, we need to check LLAMA_ARG_NO_ env as well
  116. std::string neg_env = env;
  117. string_replace_all(neg_env, "LLAMA_ARG_", "LLAMA_ARG_NO_");
  118. if (std::getenv(neg_env.c_str())) {
  119. return true;
  120. }
  121. }
  122. return env != nullptr && std::getenv(env);
  123. }
  124. static std::vector<std::string> break_str_into_lines(std::string input, size_t max_char_per_line) {
  125. std::vector<std::string> result;
  126. std::istringstream iss(input);
  127. std::string line;
  128. auto add_line = [&](const std::string& l) {
  129. if (l.length() <= max_char_per_line) {
  130. result.push_back(l);
  131. } else {
  132. std::istringstream line_stream(l);
  133. std::string word, current_line;
  134. while (line_stream >> word) {
  135. if (current_line.length() + !current_line.empty() + word.length() > max_char_per_line) {
  136. if (!current_line.empty()) result.push_back(current_line);
  137. current_line = word;
  138. } else {
  139. current_line += (!current_line.empty() ? " " : "") + word;
  140. }
  141. }
  142. if (!current_line.empty()) result.push_back(current_line);
  143. }
  144. };
  145. while (std::getline(iss, line)) {
  146. add_line(line);
  147. }
  148. return result;
  149. }
  150. std::string common_arg::to_string() const {
  151. // params for printing to console
  152. const static int n_leading_spaces = 40;
  153. const static int n_char_per_line_help = 70; // TODO: detect this based on current console
  154. std::string leading_spaces(n_leading_spaces, ' ');
  155. std::ostringstream ss;
  156. auto all_args = get_args(); // also contains args_neg
  157. for (const auto & arg : all_args) {
  158. if (arg == all_args.front()) {
  159. if (all_args.size() == 1) {
  160. ss << arg;
  161. } else {
  162. // first arg is usually abbreviation, we need padding to make it more beautiful
  163. auto tmp = std::string(arg) + ", ";
  164. auto spaces = std::string(std::max(0, 7 - (int)tmp.size()), ' ');
  165. ss << tmp << spaces;
  166. }
  167. } else {
  168. ss << arg << (arg != all_args.back() ? ", " : "");
  169. }
  170. }
  171. if (value_hint) ss << " " << value_hint;
  172. if (value_hint_2) ss << " " << value_hint_2;
  173. if (ss.tellp() > n_leading_spaces - 3) {
  174. // current line is too long, add new line
  175. ss << "\n" << leading_spaces;
  176. } else {
  177. // padding between arg and help, same line
  178. ss << std::string(leading_spaces.size() - ss.tellp(), ' ');
  179. }
  180. const auto help_lines = break_str_into_lines(help, n_char_per_line_help);
  181. for (const auto & line : help_lines) {
  182. ss << (&line == &help_lines.front() ? "" : leading_spaces) << line << "\n";
  183. }
  184. return ss.str();
  185. }
  186. std::vector<std::string> common_arg::get_args() const {
  187. std::vector<std::string> result;
  188. for (const auto & arg : args) {
  189. result.push_back(std::string(arg));
  190. }
  191. for (const auto & arg : args_neg) {
  192. result.push_back(std::string(arg));
  193. }
  194. return result;
  195. }
  196. std::vector<std::string> common_arg::get_env() const {
  197. std::vector<std::string> result;
  198. if (env) {
  199. result.push_back(std::string(env));
  200. }
  201. if (!args_neg.empty() && env) {
  202. // for compatibility, we need to add LLAMA_ARG_NO_ variant
  203. std::string neg_env = env;
  204. string_replace_all(neg_env, "LLAMA_ARG_", "LLAMA_ARG_NO_");
  205. result.push_back(neg_env);
  206. }
  207. return result;
  208. }
  209. //
  210. // utils
  211. //
  212. // Helper function to parse tensor buffer override strings
  213. static void parse_tensor_buffer_overrides(const std::string & value, std::vector<llama_model_tensor_buft_override> & overrides) {
  214. std::map<std::string, ggml_backend_buffer_type_t> buft_list;
  215. for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
  216. auto * dev = ggml_backend_dev_get(i);
  217. auto * buft = ggml_backend_dev_buffer_type(dev);
  218. if (buft) {
  219. buft_list[ggml_backend_buft_name(buft)] = buft;
  220. }
  221. }
  222. for (const auto & override : string_split<std::string>(value, ',')) {
  223. std::string::size_type pos = override.find('=');
  224. if (pos == std::string::npos) {
  225. throw std::invalid_argument("invalid value");
  226. }
  227. std::string tensor_name = override.substr(0, pos);
  228. std::string buffer_type = override.substr(pos + 1);
  229. if (buft_list.find(buffer_type) == buft_list.end()) {
  230. printf("Available buffer types:\n");
  231. for (const auto & it : buft_list) {
  232. printf(" %s\n", ggml_backend_buft_name(it.second));
  233. }
  234. throw std::invalid_argument("unknown buffer type");
  235. }
  236. // keep strings alive and avoid leaking memory by storing them in a static vector
  237. static std::list<std::string> buft_overrides;
  238. buft_overrides.push_back(tensor_name);
  239. overrides.push_back({buft_overrides.back().c_str(), buft_list.at(buffer_type)});
  240. }
  241. }
  242. struct handle_model_result {
  243. bool found_mmproj = false;
  244. common_params_model mmproj;
  245. };
  246. static handle_model_result common_params_handle_model(
  247. struct common_params_model & model,
  248. const std::string & bearer_token,
  249. bool offline) {
  250. handle_model_result result;
  251. // handle pre-fill default model path and url based on hf_repo and hf_file
  252. {
  253. if (!model.docker_repo.empty()) { // Handle Docker URLs by resolving them to local paths
  254. model.path = common_docker_resolve_model(model.docker_repo);
  255. model.name = model.docker_repo; // set name for consistency
  256. } else if (!model.hf_repo.empty()) {
  257. // short-hand to avoid specifying --hf-file -> default it to --model
  258. if (model.hf_file.empty()) {
  259. if (model.path.empty()) {
  260. auto auto_detected = common_get_hf_file(model.hf_repo, bearer_token, offline);
  261. if (auto_detected.repo.empty() || auto_detected.ggufFile.empty()) {
  262. exit(1); // built without CURL, error message already printed
  263. }
  264. model.name = model.hf_repo; // repo name with tag
  265. model.hf_repo = auto_detected.repo; // repo name without tag
  266. model.hf_file = auto_detected.ggufFile;
  267. if (!auto_detected.mmprojFile.empty()) {
  268. result.found_mmproj = true;
  269. result.mmproj.hf_repo = model.hf_repo;
  270. result.mmproj.hf_file = auto_detected.mmprojFile;
  271. }
  272. } else {
  273. model.hf_file = model.path;
  274. }
  275. }
  276. std::string model_endpoint = get_model_endpoint();
  277. model.url = model_endpoint + model.hf_repo + "/resolve/main/" + model.hf_file;
  278. // make sure model path is present (for caching purposes)
  279. if (model.path.empty()) {
  280. // this is to avoid different repo having same file name, or same file name in different subdirs
  281. std::string filename = model.hf_repo + "_" + model.hf_file;
  282. // to make sure we don't have any slashes in the filename
  283. string_replace_all(filename, "/", "_");
  284. model.path = fs_get_cache_file(filename);
  285. }
  286. } else if (!model.url.empty()) {
  287. if (model.path.empty()) {
  288. auto f = string_split<std::string>(model.url, '#').front();
  289. f = string_split<std::string>(f, '?').front();
  290. model.path = fs_get_cache_file(string_split<std::string>(f, '/').back());
  291. }
  292. }
  293. }
  294. // then, download it if needed
  295. if (!model.url.empty()) {
  296. bool ok = common_download_model(model, bearer_token, offline);
  297. if (!ok) {
  298. LOG_ERR("error: failed to download model from %s\n", model.url.c_str());
  299. exit(1);
  300. }
  301. }
  302. return result;
  303. }
  304. const std::vector<ggml_type> kv_cache_types = {
  305. GGML_TYPE_F32,
  306. GGML_TYPE_F16,
  307. GGML_TYPE_BF16,
  308. GGML_TYPE_Q8_0,
  309. GGML_TYPE_Q4_0,
  310. GGML_TYPE_Q4_1,
  311. GGML_TYPE_IQ4_NL,
  312. GGML_TYPE_Q5_0,
  313. GGML_TYPE_Q5_1,
  314. };
  315. static ggml_type kv_cache_type_from_str(const std::string & s) {
  316. for (const auto & type : kv_cache_types) {
  317. if (ggml_type_name(type) == s) {
  318. return type;
  319. }
  320. }
  321. throw std::runtime_error("Unsupported cache type: " + s);
  322. }
  323. static std::string get_all_kv_cache_types() {
  324. std::ostringstream msg;
  325. for (const auto & type : kv_cache_types) {
  326. msg << ggml_type_name(type) << (&type == &kv_cache_types.back() ? "" : ", ");
  327. }
  328. return msg.str();
  329. }
  330. static bool parse_bool_value(const std::string & value) {
  331. if (is_truthy(value)) {
  332. return true;
  333. } else if (is_falsey(value)) {
  334. return false;
  335. } else {
  336. throw std::invalid_argument("invalid boolean value");
  337. }
  338. }
  339. //
  340. // CLI argument parsing functions
  341. //
  342. static bool common_params_parse_ex(int argc, char ** argv, common_params_context & ctx_arg) {
  343. common_params & params = ctx_arg.params;
  344. std::unordered_map<std::string, std::pair<common_arg *, bool>> arg_to_options;
  345. for (auto & opt : ctx_arg.options) {
  346. for (const auto & arg : opt.args) {
  347. arg_to_options[arg] = {&opt, /* is_positive */ true};
  348. }
  349. for (const auto & arg : opt.args_neg) {
  350. arg_to_options[arg] = {&opt, /* is_positive */ false};
  351. }
  352. }
  353. // handle environment variables
  354. for (auto & opt : ctx_arg.options) {
  355. std::string value;
  356. if (opt.get_value_from_env(value)) {
  357. try {
  358. if (opt.handler_void && is_truthy(value)) {
  359. opt.handler_void(params);
  360. }
  361. if (opt.handler_int) {
  362. opt.handler_int(params, std::stoi(value));
  363. }
  364. if (opt.handler_bool) {
  365. opt.handler_bool(params, parse_bool_value(value));
  366. }
  367. if (opt.handler_string) {
  368. opt.handler_string(params, value);
  369. continue;
  370. }
  371. } catch (std::exception & e) {
  372. throw std::invalid_argument(string_format(
  373. "error while handling environment variable \"%s\": %s\n\n", opt.env, e.what()));
  374. }
  375. }
  376. }
  377. // handle command line arguments
  378. auto check_arg = [&](int i) {
  379. if (i+1 >= argc) {
  380. throw std::invalid_argument("expected value for argument");
  381. }
  382. };
  383. std::set<std::string> seen_args;
  384. for (int i = 1; i < argc; i++) {
  385. const std::string arg_prefix = "--";
  386. std::string arg = argv[i];
  387. if (arg.compare(0, arg_prefix.size(), arg_prefix) == 0) {
  388. std::replace(arg.begin(), arg.end(), '_', '-');
  389. }
  390. if (arg_to_options.find(arg) == arg_to_options.end()) {
  391. throw std::invalid_argument(string_format("error: invalid argument: %s", arg.c_str()));
  392. }
  393. if (!seen_args.insert(arg).second) {
  394. LOG_WRN("DEPRECATED: argument '%s' specified multiple times, use comma-separated values instead (only last value will be used)\n", arg.c_str());
  395. }
  396. auto & tmp = arg_to_options[arg];
  397. auto opt = *tmp.first;
  398. bool is_positive = tmp.second;
  399. if (opt.has_value_from_env()) {
  400. fprintf(stderr, "warn: %s environment variable is set, but will be overwritten by command line argument %s\n", opt.env, arg.c_str());
  401. }
  402. try {
  403. if (opt.handler_void) {
  404. opt.handler_void(params);
  405. continue;
  406. }
  407. if (opt.handler_bool) {
  408. opt.handler_bool(params, is_positive);
  409. continue;
  410. }
  411. // arg with single value
  412. check_arg(i);
  413. std::string val = argv[++i];
  414. if (opt.handler_int) {
  415. opt.handler_int(params, std::stoi(val));
  416. continue;
  417. }
  418. if (opt.handler_string) {
  419. opt.handler_string(params, val);
  420. continue;
  421. }
  422. // arg with 2 values
  423. check_arg(i);
  424. std::string val2 = argv[++i];
  425. if (opt.handler_str_str) {
  426. opt.handler_str_str(params, val, val2);
  427. continue;
  428. }
  429. } catch (std::exception & e) {
  430. throw std::invalid_argument(string_format(
  431. "error while handling argument \"%s\": %s\n\n"
  432. "usage:\n%s\n\nto show complete usage, run with -h",
  433. arg.c_str(), e.what(), opt.to_string().c_str()));
  434. }
  435. }
  436. postprocess_cpu_params(params.cpuparams, nullptr);
  437. postprocess_cpu_params(params.cpuparams_batch, &params.cpuparams);
  438. postprocess_cpu_params(params.speculative.cpuparams, &params.cpuparams);
  439. postprocess_cpu_params(params.speculative.cpuparams_batch, &params.cpuparams_batch);
  440. if (params.prompt_cache_all && (params.interactive || params.interactive_first)) {
  441. throw std::invalid_argument("error: --prompt-cache-all not supported in interactive mode yet\n");
  442. }
  443. // handle model and download
  444. {
  445. auto res = common_params_handle_model(params.model, params.hf_token, params.offline);
  446. if (params.no_mmproj) {
  447. params.mmproj = {};
  448. } else if (res.found_mmproj && params.mmproj.path.empty() && params.mmproj.url.empty()) {
  449. // optionally, handle mmproj model when -hf is specified
  450. params.mmproj = res.mmproj;
  451. }
  452. // only download mmproj if the current example is using it
  453. for (auto & ex : mmproj_examples) {
  454. if (ctx_arg.ex == ex) {
  455. common_params_handle_model(params.mmproj, params.hf_token, params.offline);
  456. break;
  457. }
  458. }
  459. common_params_handle_model(params.speculative.model, params.hf_token, params.offline);
  460. common_params_handle_model(params.vocoder.model, params.hf_token, params.offline);
  461. }
  462. // model is required (except for server)
  463. // TODO @ngxson : maybe show a list of available models in CLI in this case
  464. if (params.model.path.empty() && ctx_arg.ex != LLAMA_EXAMPLE_SERVER && !params.usage && !params.completion) {
  465. throw std::invalid_argument("error: --model is required\n");
  466. }
  467. if (params.escape) {
  468. string_process_escapes(params.prompt);
  469. string_process_escapes(params.input_prefix);
  470. string_process_escapes(params.input_suffix);
  471. for (auto & antiprompt : params.antiprompt) {
  472. string_process_escapes(antiprompt);
  473. }
  474. for (auto & seq_breaker : params.sampling.dry_sequence_breakers) {
  475. string_process_escapes(seq_breaker);
  476. }
  477. for (auto & pair : params.speculative.replacements) {
  478. string_process_escapes(pair.first);
  479. string_process_escapes(pair.second);
  480. }
  481. }
  482. if (!params.kv_overrides.empty()) {
  483. params.kv_overrides.emplace_back();
  484. params.kv_overrides.back().key[0] = 0;
  485. }
  486. // pad tensor_buft_overrides for llama_params_fit:
  487. const size_t ntbo = llama_max_tensor_buft_overrides();
  488. while (params.tensor_buft_overrides.size() < ntbo) {
  489. params.tensor_buft_overrides.push_back({nullptr, nullptr});
  490. }
  491. if (!params.speculative.tensor_buft_overrides.empty()) {
  492. params.speculative.tensor_buft_overrides.push_back({nullptr, nullptr});
  493. }
  494. if (!params.chat_template.empty() && !common_chat_verify_template(params.chat_template, params.use_jinja)) {
  495. throw std::runtime_error(string_format(
  496. "error: the supplied chat template is not supported: %s%s\n",
  497. params.chat_template.c_str(),
  498. params.use_jinja ? "" : "\nnote: llama.cpp was started without --jinja, we only support commonly used templates"
  499. ));
  500. }
  501. common_log_set_verbosity_thold(params.verbosity);
  502. return true;
  503. }
  504. static void common_params_print_usage(common_params_context & ctx_arg) {
  505. auto print_options = [](std::vector<common_arg *> & options) {
  506. for (common_arg * opt : options) {
  507. printf("%s", opt->to_string().c_str());
  508. }
  509. };
  510. std::vector<common_arg *> common_options;
  511. std::vector<common_arg *> sparam_options;
  512. std::vector<common_arg *> specific_options;
  513. for (auto & opt : ctx_arg.options) {
  514. // in case multiple LLAMA_EXAMPLE_* are set, we prioritize the LLAMA_EXAMPLE_* matching current example
  515. if (opt.is_sparam) {
  516. sparam_options.push_back(&opt);
  517. } else if (opt.in_example(ctx_arg.ex)) {
  518. specific_options.push_back(&opt);
  519. } else {
  520. common_options.push_back(&opt);
  521. }
  522. }
  523. printf("----- common params -----\n\n");
  524. print_options(common_options);
  525. printf("\n\n----- sampling params -----\n\n");
  526. print_options(sparam_options);
  527. // TODO: maybe convert enum llama_example to string
  528. printf("\n\n----- example-specific params -----\n\n");
  529. print_options(specific_options);
  530. }
  531. static void common_params_print_completion(common_params_context & ctx_arg) {
  532. std::vector<common_arg *> common_options;
  533. std::vector<common_arg *> sparam_options;
  534. std::vector<common_arg *> specific_options;
  535. for (auto & opt : ctx_arg.options) {
  536. if (opt.is_sparam) {
  537. sparam_options.push_back(&opt);
  538. } else if (opt.in_example(ctx_arg.ex)) {
  539. specific_options.push_back(&opt);
  540. } else {
  541. common_options.push_back(&opt);
  542. }
  543. }
  544. printf("_llama_completions() {\n");
  545. printf(" local cur prev opts\n");
  546. printf(" COMPREPLY=()\n");
  547. printf(" cur=\"${COMP_WORDS[COMP_CWORD]}\"\n");
  548. printf(" prev=\"${COMP_WORDS[COMP_CWORD-1]}\"\n\n");
  549. printf(" opts=\"");
  550. auto print_options = [](const std::vector<common_arg *> & options) {
  551. for (const common_arg * opt : options) {
  552. for (const char * arg : opt->args) {
  553. printf("%s ", arg);
  554. }
  555. }
  556. };
  557. print_options(common_options);
  558. print_options(sparam_options);
  559. print_options(specific_options);
  560. printf("\"\n\n");
  561. printf(" case \"$prev\" in\n");
  562. printf(" --model|-m)\n");
  563. printf(" COMPREPLY=( $(compgen -f -X '!*.gguf' -- \"$cur\") $(compgen -d -- \"$cur\") )\n");
  564. printf(" return 0\n");
  565. printf(" ;;\n");
  566. printf(" --grammar-file)\n");
  567. printf(" COMPREPLY=( $(compgen -f -X '!*.gbnf' -- \"$cur\") $(compgen -d -- \"$cur\") )\n");
  568. printf(" return 0\n");
  569. printf(" ;;\n");
  570. printf(" --chat-template-file)\n");
  571. printf(" COMPREPLY=( $(compgen -f -X '!*.jinja' -- \"$cur\") $(compgen -d -- \"$cur\") )\n");
  572. printf(" return 0\n");
  573. printf(" ;;\n");
  574. printf(" *)\n");
  575. printf(" COMPREPLY=( $(compgen -W \"${opts}\" -- \"$cur\") )\n");
  576. printf(" return 0\n");
  577. printf(" ;;\n");
  578. printf(" esac\n");
  579. printf("}\n\n");
  580. std::set<std::string> executables = {
  581. "llama-batched",
  582. "llama-batched-bench",
  583. "llama-bench",
  584. "llama-cli",
  585. "llama-completion",
  586. "llama-convert-llama2c-to-ggml",
  587. "llama-cvector-generator",
  588. "llama-embedding",
  589. "llama-eval-callback",
  590. "llama-export-lora",
  591. "llama-gen-docs",
  592. "llama-gguf",
  593. "llama-gguf-hash",
  594. "llama-gguf-split",
  595. "llama-gritlm",
  596. "llama-imatrix",
  597. "llama-infill",
  598. "llama-mtmd-cli",
  599. "llama-llava-clip-quantize-cli",
  600. "llama-lookahead",
  601. "llama-lookup",
  602. "llama-lookup-create",
  603. "llama-lookup-merge",
  604. "llama-lookup-stats",
  605. "llama-parallel",
  606. "llama-passkey",
  607. "llama-perplexity",
  608. "llama-q8dot",
  609. "llama-quantize",
  610. "llama-qwen2vl-cli",
  611. "llama-retrieval",
  612. "llama-run",
  613. "llama-save-load-state",
  614. "llama-server",
  615. "llama-simple",
  616. "llama-simple-chat",
  617. "llama-speculative",
  618. "llama-speculative-simple",
  619. "llama-tokenize",
  620. "llama-tts",
  621. "llama-vdot"
  622. };
  623. for (const auto& exe : executables) {
  624. printf("complete -F _llama_completions %s\n", exe.c_str());
  625. }
  626. }
  627. static std::vector<ggml_backend_dev_t> parse_device_list(const std::string & value) {
  628. std::vector<ggml_backend_dev_t> devices;
  629. auto dev_names = string_split<std::string>(value, ',');
  630. if (dev_names.empty()) {
  631. throw std::invalid_argument("no devices specified");
  632. }
  633. if (dev_names.size() == 1 && dev_names[0] == "none") {
  634. devices.push_back(nullptr);
  635. } else {
  636. for (const auto & device : dev_names) {
  637. auto * dev = ggml_backend_dev_by_name(device.c_str());
  638. if (!dev || ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_CPU) {
  639. throw std::invalid_argument(string_format("invalid device: %s", device.c_str()));
  640. }
  641. devices.push_back(dev);
  642. }
  643. devices.push_back(nullptr);
  644. }
  645. return devices;
  646. }
  647. static void add_rpc_devices(const std::string & servers) {
  648. auto rpc_servers = string_split<std::string>(servers, ',');
  649. if (rpc_servers.empty()) {
  650. throw std::invalid_argument("no RPC servers specified");
  651. }
  652. ggml_backend_reg_t rpc_reg = ggml_backend_reg_by_name("RPC");
  653. if (!rpc_reg) {
  654. throw std::invalid_argument("failed to find RPC backend");
  655. }
  656. typedef ggml_backend_reg_t (*ggml_backend_rpc_add_server_t)(const char * endpoint);
  657. ggml_backend_rpc_add_server_t ggml_backend_rpc_add_server_fn = (ggml_backend_rpc_add_server_t) ggml_backend_reg_get_proc_address(rpc_reg, "ggml_backend_rpc_add_server");
  658. if (!ggml_backend_rpc_add_server_fn) {
  659. throw std::invalid_argument("failed to find RPC add server function");
  660. }
  661. for (const auto & server : rpc_servers) {
  662. auto reg = ggml_backend_rpc_add_server_fn(server.c_str());
  663. ggml_backend_register(reg);
  664. }
  665. }
  666. bool common_params_to_map(int argc, char ** argv, llama_example ex, std::map<common_arg, std::string> & out_map) {
  667. common_params dummy_params;
  668. common_params_context ctx_arg = common_params_parser_init(dummy_params, ex, nullptr);
  669. std::unordered_map<std::string, common_arg *> arg_to_options;
  670. for (auto & opt : ctx_arg.options) {
  671. for (const auto & arg : opt.args) {
  672. arg_to_options[arg] = &opt;
  673. }
  674. for (const auto & arg : opt.args_neg) {
  675. arg_to_options[arg] = &opt;
  676. }
  677. }
  678. // TODO @ngxson : find a way to deduplicate this code
  679. // handle command line arguments
  680. auto check_arg = [&](int i) {
  681. if (i+1 >= argc) {
  682. throw std::invalid_argument("expected value for argument");
  683. }
  684. };
  685. std::set<std::string> seen_args;
  686. for (int i = 1; i < argc; i++) {
  687. const std::string arg_prefix = "--";
  688. std::string arg = argv[i];
  689. if (arg.compare(0, arg_prefix.size(), arg_prefix) == 0) {
  690. std::replace(arg.begin(), arg.end(), '_', '-');
  691. }
  692. if (arg_to_options.find(arg) == arg_to_options.end()) {
  693. throw std::invalid_argument(string_format("error: invalid argument: %s", arg.c_str()));
  694. }
  695. if (!seen_args.insert(arg).second) {
  696. LOG_WRN("DEPRECATED: argument '%s' specified multiple times, use comma-separated values instead (only last value will be used)\n", arg.c_str());
  697. }
  698. auto opt = *arg_to_options[arg];
  699. std::string val;
  700. if (opt.value_hint == nullptr && opt.value_hint_2 == nullptr) {
  701. // bool arg (need to reverse the meaning for negative args)
  702. bool is_neg = std::find(opt.args_neg.begin(), opt.args_neg.end(), arg) != opt.args_neg.end();
  703. val = is_neg ? "0" : "1";
  704. }
  705. if (opt.value_hint != nullptr) {
  706. // arg with single value
  707. check_arg(i);
  708. val = argv[++i];
  709. }
  710. if (opt.value_hint_2 != nullptr) {
  711. // TODO: support arg with 2 values
  712. throw std::invalid_argument("error: argument with 2 values is not yet supported\n");
  713. }
  714. out_map[opt] = val;
  715. }
  716. return true;
  717. }
  718. bool common_params_parse(int argc, char ** argv, common_params & params, llama_example ex, void(*print_usage)(int, char **)) {
  719. auto ctx_arg = common_params_parser_init(params, ex, print_usage);
  720. const common_params params_org = ctx_arg.params; // the example can modify the default params
  721. try {
  722. if (!common_params_parse_ex(argc, argv, ctx_arg)) {
  723. ctx_arg.params = params_org;
  724. return false;
  725. }
  726. if (ctx_arg.params.usage) {
  727. common_params_print_usage(ctx_arg);
  728. if (ctx_arg.print_usage) {
  729. ctx_arg.print_usage(argc, argv);
  730. }
  731. exit(0);
  732. }
  733. if (ctx_arg.params.completion) {
  734. common_params_print_completion(ctx_arg);
  735. exit(0);
  736. }
  737. params.lr.init();
  738. } catch (const std::invalid_argument & ex) {
  739. fprintf(stderr, "%s\n", ex.what());
  740. ctx_arg.params = params_org;
  741. return false;
  742. } catch (std::exception & ex) {
  743. fprintf(stderr, "%s\n", ex.what());
  744. exit(1); // for other exceptions, we exit with status code 1
  745. }
  746. return true;
  747. }
  748. static std::string list_builtin_chat_templates() {
  749. std::vector<const char *> supported_tmpl;
  750. int32_t res = llama_chat_builtin_templates(nullptr, 0);
  751. supported_tmpl.resize(res);
  752. res = llama_chat_builtin_templates(supported_tmpl.data(), supported_tmpl.size());
  753. std::ostringstream msg;
  754. for (auto & tmpl : supported_tmpl) {
  755. msg << tmpl << (&tmpl == &supported_tmpl.back() ? "" : ", ");
  756. }
  757. return msg.str();
  758. }
  759. bool common_arg_utils::is_truthy(const std::string & value) {
  760. return value == "on" || value == "enabled" || value == "true" || value == "1";
  761. }
  762. bool common_arg_utils::is_falsey(const std::string & value) {
  763. return value == "off" || value == "disabled" || value == "false" || value == "0";
  764. }
  765. bool common_arg_utils::is_autoy(const std::string & value) {
  766. return value == "auto" || value == "-1";
  767. }
  768. common_params_context common_params_parser_init(common_params & params, llama_example ex, void(*print_usage)(int, char **)) {
  769. // per-example default params
  770. // we define here to make sure it's included in llama-gen-docs
  771. if (ex == LLAMA_EXAMPLE_COMPLETION) {
  772. params.use_jinja = false; // disable jinja by default
  773. } else if (ex == LLAMA_EXAMPLE_MTMD) {
  774. params.use_jinja = false; // disable jinja by default
  775. params.sampling.temp = 0.2; // lower temp by default for better quality
  776. } else if (ex == LLAMA_EXAMPLE_SERVER) {
  777. params.n_parallel = -1; // auto by default
  778. }
  779. params.use_color = tty_can_use_colors();
  780. // load dynamic backends
  781. ggml_backend_load_all();
  782. common_params_context ctx_arg(params);
  783. ctx_arg.print_usage = print_usage;
  784. ctx_arg.ex = ex;
  785. std::string sampler_type_chars;
  786. std::string sampler_type_names;
  787. for (const auto & sampler : params.sampling.samplers) {
  788. sampler_type_chars += common_sampler_type_to_chr(sampler);
  789. sampler_type_names += common_sampler_type_to_str(sampler) + ";";
  790. }
  791. if (!sampler_type_names.empty()) {
  792. sampler_type_names.pop_back(); // remove last semicolon
  793. }
  794. /**
  795. * filter options by example
  796. * rules:
  797. * - all examples inherit options from LLAMA_EXAMPLE_COMMON
  798. * - if LLAMA_EXAMPLE_* is set (other than COMMON), we only show the option in the corresponding example
  799. * - if both {LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_*,} are set, we will prioritize the LLAMA_EXAMPLE_* matching current example
  800. */
  801. auto add_opt = [&](common_arg arg) {
  802. if ((arg.in_example(ex) || arg.in_example(LLAMA_EXAMPLE_COMMON)) && !arg.is_exclude(ex)) {
  803. ctx_arg.options.push_back(std::move(arg));
  804. }
  805. };
  806. add_opt(common_arg(
  807. {"-h", "--help", "--usage"},
  808. "print usage and exit",
  809. [](common_params & params) {
  810. params.usage = true;
  811. }
  812. ));
  813. add_opt(common_arg(
  814. {"--version"},
  815. "show version and build info",
  816. [](common_params &) {
  817. fprintf(stderr, "version: %d (%s)\n", LLAMA_BUILD_NUMBER, LLAMA_COMMIT);
  818. fprintf(stderr, "built with %s for %s\n", LLAMA_COMPILER, LLAMA_BUILD_TARGET);
  819. exit(0);
  820. }
  821. ));
  822. add_opt(common_arg(
  823. {"-cl", "--cache-list"},
  824. "show list of models in cache",
  825. [](common_params &) {
  826. printf("model cache directory: %s\n", fs_get_cache_directory().c_str());
  827. auto models = common_list_cached_models();
  828. printf("number of models in cache: %zu\n", models.size());
  829. for (size_t i = 0; i < models.size(); i++) {
  830. auto & model = models[i];
  831. printf("%4d. %s\n", (int) i + 1, model.to_string().c_str());
  832. }
  833. exit(0);
  834. }
  835. ));
  836. add_opt(common_arg(
  837. {"--completion-bash"},
  838. "print source-able bash completion script for llama.cpp",
  839. [](common_params & params) {
  840. params.completion = true;
  841. }
  842. ));
  843. add_opt(common_arg(
  844. {"--verbose-prompt"},
  845. string_format("print a verbose prompt before generation (default: %s)", params.verbose_prompt ? "true" : "false"),
  846. [](common_params & params) {
  847. params.verbose_prompt = true;
  848. }
  849. ));
  850. add_opt(common_arg(
  851. {"--display-prompt"},
  852. {"--no-display-prompt"},
  853. string_format("whether to print prompt at generation (default: %s)", params.display_prompt ? "true" : "false"),
  854. [](common_params & params, bool value) {
  855. params.display_prompt = value;
  856. }
  857. ).set_examples({LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_CLI}));
  858. add_opt(common_arg(
  859. {"-co", "--color"}, "[on|off|auto]",
  860. "Colorize output to distinguish prompt and user input from generations ('on', 'off', or 'auto', default: 'auto')\n"
  861. "'auto' enables colors when output is to a terminal",
  862. [](common_params & params, const std::string & value) {
  863. if (is_truthy(value)) {
  864. params.use_color = true;
  865. } else if (is_falsey(value)) {
  866. params.use_color = false;
  867. } else if (is_autoy(value)) {
  868. params.use_color = tty_can_use_colors();
  869. } else {
  870. throw std::invalid_argument(
  871. string_format("error: unknown value for --color: '%s'\n", value.c_str()));
  872. }
  873. }
  874. ).set_examples({LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_CLI, LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_LOOKUP}));
  875. add_opt(common_arg(
  876. {"-t", "--threads"}, "N",
  877. string_format("number of CPU threads to use during generation (default: %d)", params.cpuparams.n_threads),
  878. [](common_params & params, int value) {
  879. params.cpuparams.n_threads = value;
  880. if (params.cpuparams.n_threads <= 0) {
  881. params.cpuparams.n_threads = std::thread::hardware_concurrency();
  882. }
  883. }
  884. ).set_env("LLAMA_ARG_THREADS"));
  885. add_opt(common_arg(
  886. {"-tb", "--threads-batch"}, "N",
  887. "number of threads to use during batch and prompt processing (default: same as --threads)",
  888. [](common_params & params, int value) {
  889. params.cpuparams_batch.n_threads = value;
  890. if (params.cpuparams_batch.n_threads <= 0) {
  891. params.cpuparams_batch.n_threads = std::thread::hardware_concurrency();
  892. }
  893. }
  894. ));
  895. add_opt(common_arg(
  896. {"-C", "--cpu-mask"}, "M",
  897. "CPU affinity mask: arbitrarily long hex. Complements cpu-range (default: \"\")",
  898. [](common_params & params, const std::string & mask) {
  899. params.cpuparams.mask_valid = true;
  900. if (!parse_cpu_mask(mask, params.cpuparams.cpumask)) {
  901. throw std::invalid_argument("invalid cpumask");
  902. }
  903. }
  904. ));
  905. add_opt(common_arg(
  906. {"-Cr", "--cpu-range"}, "lo-hi",
  907. "range of CPUs for affinity. Complements --cpu-mask",
  908. [](common_params & params, const std::string & range) {
  909. params.cpuparams.mask_valid = true;
  910. if (!parse_cpu_range(range, params.cpuparams.cpumask)) {
  911. throw std::invalid_argument("invalid range");
  912. }
  913. }
  914. ));
  915. add_opt(common_arg(
  916. {"--cpu-strict"}, "<0|1>",
  917. string_format("use strict CPU placement (default: %u)\n", (unsigned) params.cpuparams.strict_cpu),
  918. [](common_params & params, const std::string & value) {
  919. params.cpuparams.strict_cpu = std::stoul(value);
  920. }
  921. ));
  922. add_opt(common_arg(
  923. {"--prio"}, "N",
  924. string_format("set process/thread priority : low(-1), normal(0), medium(1), high(2), realtime(3) (default: %d)\n", params.cpuparams.priority),
  925. [](common_params & params, int prio) {
  926. if (prio < GGML_SCHED_PRIO_LOW || prio > GGML_SCHED_PRIO_REALTIME) {
  927. throw std::invalid_argument("invalid value");
  928. }
  929. params.cpuparams.priority = (enum ggml_sched_priority) prio;
  930. }
  931. ));
  932. add_opt(common_arg(
  933. {"--poll"}, "<0...100>",
  934. string_format("use polling level to wait for work (0 - no polling, default: %u)\n", (unsigned) params.cpuparams.poll),
  935. [](common_params & params, const std::string & value) {
  936. params.cpuparams.poll = std::stoul(value);
  937. }
  938. ));
  939. add_opt(common_arg(
  940. {"-Cb", "--cpu-mask-batch"}, "M",
  941. "CPU affinity mask: arbitrarily long hex. Complements cpu-range-batch (default: same as --cpu-mask)",
  942. [](common_params & params, const std::string & mask) {
  943. params.cpuparams_batch.mask_valid = true;
  944. if (!parse_cpu_mask(mask, params.cpuparams_batch.cpumask)) {
  945. throw std::invalid_argument("invalid cpumask");
  946. }
  947. }
  948. ));
  949. add_opt(common_arg(
  950. {"-Crb", "--cpu-range-batch"}, "lo-hi",
  951. "ranges of CPUs for affinity. Complements --cpu-mask-batch",
  952. [](common_params & params, const std::string & range) {
  953. params.cpuparams_batch.mask_valid = true;
  954. if (!parse_cpu_range(range, params.cpuparams_batch.cpumask)) {
  955. throw std::invalid_argument("invalid range");
  956. }
  957. }
  958. ));
  959. add_opt(common_arg(
  960. {"--cpu-strict-batch"}, "<0|1>",
  961. "use strict CPU placement (default: same as --cpu-strict)",
  962. [](common_params & params, int value) {
  963. params.cpuparams_batch.strict_cpu = value;
  964. }
  965. ));
  966. add_opt(common_arg(
  967. {"--prio-batch"}, "N",
  968. string_format("set process/thread priority : 0-normal, 1-medium, 2-high, 3-realtime (default: %d)\n", params.cpuparams_batch.priority),
  969. [](common_params & params, int prio) {
  970. if (prio < 0 || prio > 3) {
  971. throw std::invalid_argument("invalid value");
  972. }
  973. params.cpuparams_batch.priority = (enum ggml_sched_priority) prio;
  974. }
  975. ));
  976. add_opt(common_arg(
  977. {"--poll-batch"}, "<0|1>",
  978. "use polling to wait for work (default: same as --poll)",
  979. [](common_params & params, int value) {
  980. params.cpuparams_batch.poll = value;
  981. }
  982. ));
  983. add_opt(common_arg(
  984. {"-lcs", "--lookup-cache-static"}, "FNAME",
  985. "path to static lookup cache to use for lookup decoding (not updated by generation)",
  986. [](common_params & params, const std::string & value) {
  987. params.lookup_cache_static = value;
  988. }
  989. ).set_examples({LLAMA_EXAMPLE_LOOKUP}));
  990. add_opt(common_arg(
  991. {"-lcd", "--lookup-cache-dynamic"}, "FNAME",
  992. "path to dynamic lookup cache to use for lookup decoding (updated by generation)",
  993. [](common_params & params, const std::string & value) {
  994. params.lookup_cache_dynamic = value;
  995. }
  996. ).set_examples({LLAMA_EXAMPLE_LOOKUP}));
  997. add_opt(common_arg(
  998. {"-c", "--ctx-size"}, "N",
  999. string_format("size of the prompt context (default: %d, 0 = loaded from model)", params.n_ctx),
  1000. [](common_params & params, int value) {
  1001. params.n_ctx = value;
  1002. }
  1003. ).set_env("LLAMA_ARG_CTX_SIZE"));
  1004. add_opt(common_arg(
  1005. {"-n", "--predict", "--n-predict"}, "N",
  1006. string_format(
  1007. ex == LLAMA_EXAMPLE_COMPLETION
  1008. ? "number of tokens to predict (default: %d, -1 = infinity, -2 = until context filled)"
  1009. : "number of tokens to predict (default: %d, -1 = infinity)",
  1010. params.n_predict),
  1011. [](common_params & params, int value) {
  1012. params.n_predict = value;
  1013. }
  1014. ).set_env("LLAMA_ARG_N_PREDICT"));
  1015. add_opt(common_arg(
  1016. {"-b", "--batch-size"}, "N",
  1017. string_format("logical maximum batch size (default: %d)", params.n_batch),
  1018. [](common_params & params, int value) {
  1019. params.n_batch = value;
  1020. }
  1021. ).set_env("LLAMA_ARG_BATCH"));
  1022. add_opt(common_arg(
  1023. {"-ub", "--ubatch-size"}, "N",
  1024. string_format("physical maximum batch size (default: %d)", params.n_ubatch),
  1025. [](common_params & params, int value) {
  1026. params.n_ubatch = value;
  1027. }
  1028. ).set_env("LLAMA_ARG_UBATCH"));
  1029. add_opt(common_arg(
  1030. {"--keep"}, "N",
  1031. string_format("number of tokens to keep from the initial prompt (default: %d, -1 = all)", params.n_keep),
  1032. [](common_params & params, int value) {
  1033. params.n_keep = value;
  1034. }
  1035. ));
  1036. add_opt(common_arg(
  1037. {"--swa-full"},
  1038. string_format("use full-size SWA cache (default: %s)\n"
  1039. "[(more info)](https://github.com/ggml-org/llama.cpp/pull/13194#issuecomment-2868343055)", params.swa_full ? "true" : "false"),
  1040. [](common_params & params) {
  1041. params.swa_full = true;
  1042. }
  1043. ).set_env("LLAMA_ARG_SWA_FULL"));
  1044. add_opt(common_arg(
  1045. {"--ctx-checkpoints", "--swa-checkpoints"}, "N",
  1046. string_format("max number of context checkpoints to create per slot (default: %d)"
  1047. "[(more info)](https://github.com/ggml-org/llama.cpp/pull/15293)", params.n_ctx_checkpoints),
  1048. [](common_params & params, int value) {
  1049. params.n_ctx_checkpoints = value;
  1050. }
  1051. ).set_env("LLAMA_ARG_CTX_CHECKPOINTS").set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}));
  1052. add_opt(common_arg(
  1053. {"-cram", "--cache-ram"}, "N",
  1054. string_format("set the maximum cache size in MiB (default: %d, -1 - no limit, 0 - disable)"
  1055. "[(more info)](https://github.com/ggml-org/llama.cpp/pull/16391)", params.cache_ram_mib),
  1056. [](common_params & params, int value) {
  1057. params.cache_ram_mib = value;
  1058. }
  1059. ).set_env("LLAMA_ARG_CACHE_RAM").set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}));
  1060. add_opt(common_arg(
  1061. {"-kvu", "--kv-unified"},
  1062. "use single unified KV buffer shared across all sequences (default: enabled if number of slots is auto)",
  1063. [](common_params & params) {
  1064. params.kv_unified = true;
  1065. }
  1066. ).set_env("LLAMA_ARG_KV_UNIFIED").set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_PERPLEXITY}));
  1067. add_opt(common_arg(
  1068. {"--context-shift"},
  1069. {"--no-context-shift"},
  1070. string_format("whether to use context shift on infinite text generation (default: %s)", params.ctx_shift ? "enabled" : "disabled"),
  1071. [](common_params & params, bool value) {
  1072. params.ctx_shift = value;
  1073. }
  1074. ).set_examples({LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_CLI, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_IMATRIX, LLAMA_EXAMPLE_PERPLEXITY}).set_env("LLAMA_ARG_CONTEXT_SHIFT"));
  1075. add_opt(common_arg(
  1076. {"--chunks"}, "N",
  1077. string_format("max number of chunks to process (default: %d, -1 = all)", params.n_chunks),
  1078. [](common_params & params, int value) {
  1079. params.n_chunks = value;
  1080. }
  1081. ).set_examples({LLAMA_EXAMPLE_IMATRIX, LLAMA_EXAMPLE_PERPLEXITY, LLAMA_EXAMPLE_RETRIEVAL}));
  1082. add_opt(common_arg({ "-fa", "--flash-attn" }, "[on|off|auto]",
  1083. string_format("set Flash Attention use ('on', 'off', or 'auto', default: '%s')",
  1084. llama_flash_attn_type_name(params.flash_attn_type)),
  1085. [](common_params & params, const std::string & value) {
  1086. if (is_truthy(value)) {
  1087. params.flash_attn_type = LLAMA_FLASH_ATTN_TYPE_ENABLED;
  1088. } else if (is_falsey(value)) {
  1089. params.flash_attn_type = LLAMA_FLASH_ATTN_TYPE_DISABLED;
  1090. } else if (is_autoy(value)) {
  1091. params.flash_attn_type = LLAMA_FLASH_ATTN_TYPE_AUTO;
  1092. } else {
  1093. throw std::runtime_error(
  1094. string_format("error: unknown value for --flash-attn: '%s'\n", value.c_str()));
  1095. }
  1096. }).set_env("LLAMA_ARG_FLASH_ATTN"));
  1097. add_opt(common_arg(
  1098. {"-p", "--prompt"}, "PROMPT",
  1099. "prompt to start generation with; for system message, use -sys",
  1100. [](common_params & params, const std::string & value) {
  1101. params.prompt = value;
  1102. }
  1103. ).set_excludes({LLAMA_EXAMPLE_SERVER}));
  1104. add_opt(common_arg(
  1105. {"-sys", "--system-prompt"}, "PROMPT",
  1106. "system prompt to use with model (if applicable, depending on chat template)",
  1107. [](common_params & params, const std::string & value) {
  1108. params.system_prompt = value;
  1109. }
  1110. ).set_examples({LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_CLI, LLAMA_EXAMPLE_DIFFUSION, LLAMA_EXAMPLE_MTMD}));
  1111. add_opt(common_arg(
  1112. {"--perf"},
  1113. {"--no-perf"},
  1114. string_format("whether to enable internal libllama performance timings (default: %s)", params.no_perf ? "true" : "false"),
  1115. [](common_params & params, bool value) {
  1116. params.no_perf = !value;
  1117. params.sampling.no_perf = !value;
  1118. }
  1119. ).set_env("LLAMA_ARG_PERF"));
  1120. add_opt(common_arg(
  1121. {"--show-timings"},
  1122. {"--no-show-timings"},
  1123. string_format("whether to show timing information after each response (default: %s)", params.show_timings ? "true" : "false"),
  1124. [](common_params & params, bool value) {
  1125. params.show_timings = value;
  1126. }
  1127. ).set_examples({LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_SHOW_TIMINGS"));
  1128. add_opt(common_arg(
  1129. {"-f", "--file"}, "FNAME",
  1130. "a file containing the prompt (default: none)",
  1131. [](common_params & params, const std::string & value) {
  1132. params.prompt = read_file(value);
  1133. // store the external file name in params
  1134. params.prompt_file = value;
  1135. if (!params.prompt.empty() && params.prompt.back() == '\n') {
  1136. params.prompt.pop_back();
  1137. }
  1138. }
  1139. ).set_excludes({LLAMA_EXAMPLE_SERVER}));
  1140. add_opt(common_arg(
  1141. {"-sysf", "--system-prompt-file"}, "FNAME",
  1142. "a file containing the system prompt (default: none)",
  1143. [](common_params & params, const std::string & value) {
  1144. params.system_prompt = read_file(value);
  1145. if (!params.system_prompt.empty() && params.system_prompt.back() == '\n') {
  1146. params.system_prompt.pop_back();
  1147. }
  1148. }
  1149. ).set_examples({LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_CLI, LLAMA_EXAMPLE_DIFFUSION}));
  1150. add_opt(common_arg(
  1151. {"--in-file"}, "FNAME",
  1152. "an input file (use comma-separated values to specify multiple files)",
  1153. [](common_params & params, const std::string & value) {
  1154. for (const auto & item : string_split<std::string>(value, ',')) {
  1155. std::ifstream file(item);
  1156. if (!file) {
  1157. throw std::runtime_error(string_format("error: failed to open file '%s'\n", item.c_str()));
  1158. }
  1159. params.in_files.push_back(item);
  1160. }
  1161. }
  1162. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  1163. add_opt(common_arg(
  1164. {"-bf", "--binary-file"}, "FNAME",
  1165. "binary file containing the prompt (default: none)",
  1166. [](common_params & params, const std::string & value) {
  1167. std::ifstream file(value, std::ios::binary);
  1168. if (!file) {
  1169. throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
  1170. }
  1171. // store the external file name in params
  1172. params.prompt_file = value;
  1173. std::ostringstream ss;
  1174. ss << file.rdbuf();
  1175. params.prompt = ss.str();
  1176. fprintf(stderr, "Read %zu bytes from binary file %s\n", params.prompt.size(), value.c_str());
  1177. }
  1178. ).set_excludes({LLAMA_EXAMPLE_SERVER}));
  1179. add_opt(common_arg(
  1180. {"-e", "--escape"},
  1181. {"--no-escape"},
  1182. string_format("whether to process escapes sequences (\\n, \\r, \\t, \\', \\\", \\\\) (default: %s)", params.escape ? "true" : "false"),
  1183. [](common_params & params, bool value) {
  1184. params.escape = value;
  1185. }
  1186. ));
  1187. add_opt(common_arg(
  1188. {"-ptc", "--print-token-count"}, "N",
  1189. string_format("print token count every N tokens (default: %d)", params.n_print),
  1190. [](common_params & params, int value) {
  1191. params.n_print = value;
  1192. }
  1193. ).set_examples({LLAMA_EXAMPLE_COMPLETION}));
  1194. add_opt(common_arg(
  1195. {"--prompt-cache"}, "FNAME",
  1196. "file to cache prompt state for faster startup (default: none)",
  1197. [](common_params & params, const std::string & value) {
  1198. params.path_prompt_cache = value;
  1199. }
  1200. ).set_examples({LLAMA_EXAMPLE_COMPLETION}));
  1201. add_opt(common_arg(
  1202. {"--prompt-cache-all"},
  1203. "if specified, saves user input and generations to cache as well\n",
  1204. [](common_params & params) {
  1205. params.prompt_cache_all = true;
  1206. }
  1207. ).set_examples({LLAMA_EXAMPLE_COMPLETION}));
  1208. add_opt(common_arg(
  1209. {"--prompt-cache-ro"},
  1210. "if specified, uses the prompt cache but does not update it",
  1211. [](common_params & params) {
  1212. params.prompt_cache_ro = true;
  1213. }
  1214. ).set_examples({LLAMA_EXAMPLE_COMPLETION}));
  1215. add_opt(common_arg(
  1216. {"-r", "--reverse-prompt"}, "PROMPT",
  1217. "halt generation at PROMPT, return control in interactive mode\n",
  1218. [](common_params & params, const std::string & value) {
  1219. params.antiprompt.emplace_back(value);
  1220. }
  1221. ).set_examples({LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_CLI, LLAMA_EXAMPLE_SERVER}));
  1222. add_opt(common_arg(
  1223. {"-sp", "--special"},
  1224. string_format("special tokens output enabled (default: %s)", params.special ? "true" : "false"),
  1225. [](common_params & params) {
  1226. params.special = true;
  1227. }
  1228. ).set_examples({LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_CLI, LLAMA_EXAMPLE_SERVER}));
  1229. add_opt(common_arg(
  1230. {"-cnv", "--conversation"},
  1231. {"-no-cnv", "--no-conversation"},
  1232. "whether to run in conversation mode:\n"
  1233. "- does not print special tokens and suffix/prefix\n"
  1234. "- interactive mode is also enabled\n"
  1235. "(default: auto enabled if chat template is available)",
  1236. [](common_params & params, bool value) {
  1237. params.conversation_mode = value ? COMMON_CONVERSATION_MODE_ENABLED : COMMON_CONVERSATION_MODE_DISABLED;
  1238. }
  1239. ).set_examples({LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_CLI}));
  1240. add_opt(common_arg(
  1241. {"-st", "--single-turn"},
  1242. "run conversation for a single turn only, then exit when done\n"
  1243. "will not be interactive if first turn is predefined with --prompt\n"
  1244. "(default: false)",
  1245. [](common_params & params) {
  1246. params.single_turn = true;
  1247. }
  1248. ).set_examples({LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_CLI}));
  1249. add_opt(common_arg(
  1250. {"-i", "--interactive"},
  1251. string_format("run in interactive mode (default: %s)", params.interactive ? "true" : "false"),
  1252. [](common_params & params) {
  1253. params.interactive = true;
  1254. }
  1255. ).set_examples({LLAMA_EXAMPLE_COMPLETION}));
  1256. add_opt(common_arg(
  1257. {"-if", "--interactive-first"},
  1258. string_format("run in interactive mode and wait for input right away (default: %s)", params.interactive_first ? "true" : "false"),
  1259. [](common_params & params) {
  1260. params.interactive_first = true;
  1261. }
  1262. ).set_examples({LLAMA_EXAMPLE_COMPLETION}));
  1263. add_opt(common_arg(
  1264. {"-mli", "--multiline-input"},
  1265. "allows you to write or paste multiple lines without ending each in '\\'",
  1266. [](common_params & params) {
  1267. params.multiline_input = true;
  1268. }
  1269. ).set_examples({LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_CLI}));
  1270. add_opt(common_arg(
  1271. {"--in-prefix-bos"},
  1272. "prefix BOS to user inputs, preceding the `--in-prefix` string",
  1273. [](common_params & params) {
  1274. params.input_prefix_bos = true;
  1275. params.enable_chat_template = false;
  1276. }
  1277. ).set_examples({LLAMA_EXAMPLE_COMPLETION}));
  1278. add_opt(common_arg(
  1279. {"--in-prefix"}, "STRING",
  1280. "string to prefix user inputs with (default: empty)",
  1281. [](common_params & params, const std::string & value) {
  1282. params.input_prefix = value;
  1283. params.enable_chat_template = false;
  1284. }
  1285. ).set_examples({LLAMA_EXAMPLE_COMPLETION}));
  1286. add_opt(common_arg(
  1287. {"--in-suffix"}, "STRING",
  1288. "string to suffix after user inputs with (default: empty)",
  1289. [](common_params & params, const std::string & value) {
  1290. params.input_suffix = value;
  1291. params.enable_chat_template = false;
  1292. }
  1293. ).set_examples({LLAMA_EXAMPLE_COMPLETION}));
  1294. add_opt(common_arg(
  1295. {"--warmup"},
  1296. {"--no-warmup"},
  1297. string_format("whether to perform warmup with an empty run (default: %s)", params.warmup ? "enabled" : "disabled"),
  1298. [](common_params & params, bool value) {
  1299. params.warmup = value;
  1300. }
  1301. ).set_examples({LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_CLI, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_MTMD, LLAMA_EXAMPLE_EMBEDDING, LLAMA_EXAMPLE_RETRIEVAL, LLAMA_EXAMPLE_PERPLEXITY}));
  1302. add_opt(common_arg(
  1303. {"--spm-infill"},
  1304. string_format(
  1305. "use Suffix/Prefix/Middle pattern for infill (instead of Prefix/Suffix/Middle) as some models prefer this. (default: %s)",
  1306. params.spm_infill ? "enabled" : "disabled"
  1307. ),
  1308. [](common_params & params) {
  1309. params.spm_infill = true;
  1310. }
  1311. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  1312. add_opt(common_arg(
  1313. {"--samplers"}, "SAMPLERS",
  1314. string_format("samplers that will be used for generation in the order, separated by \';\'\n(default: %s)", sampler_type_names.c_str()),
  1315. [](common_params & params, const std::string & value) {
  1316. const auto sampler_names = string_split<std::string>(value, ';');
  1317. params.sampling.samplers = common_sampler_types_from_names(sampler_names, true);
  1318. params.sampling.user_sampling_config |= common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_SAMPLERS;
  1319. }
  1320. ).set_sparam());
  1321. add_opt(common_arg(
  1322. {"-s", "--seed"}, "SEED",
  1323. string_format("RNG seed (default: %d, use random seed for %d)", params.sampling.seed, LLAMA_DEFAULT_SEED),
  1324. [](common_params & params, const std::string & value) {
  1325. params.sampling.seed = std::stoul(value);
  1326. }
  1327. ).set_sparam());
  1328. add_opt(common_arg(
  1329. {"--sampler-seq", "--sampling-seq"}, "SEQUENCE",
  1330. string_format("simplified sequence for samplers that will be used (default: %s)", sampler_type_chars.c_str()),
  1331. [](common_params & params, const std::string & value) {
  1332. params.sampling.samplers = common_sampler_types_from_chars(value);
  1333. }
  1334. ).set_sparam());
  1335. add_opt(common_arg(
  1336. {"--ignore-eos"},
  1337. "ignore end of stream token and continue generating (implies --logit-bias EOS-inf)",
  1338. [](common_params & params) {
  1339. params.sampling.ignore_eos = true;
  1340. }
  1341. ).set_sparam());
  1342. add_opt(common_arg(
  1343. {"--temp"}, "N",
  1344. string_format("temperature (default: %.1f)", (double)params.sampling.temp),
  1345. [](common_params & params, const std::string & value) {
  1346. params.sampling.temp = std::stof(value);
  1347. params.sampling.temp = std::max(params.sampling.temp, 0.0f);
  1348. params.sampling.user_sampling_config |= common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_TEMP;
  1349. }
  1350. ).set_sparam());
  1351. add_opt(common_arg(
  1352. {"--top-k"}, "N",
  1353. string_format("top-k sampling (default: %d, 0 = disabled)", params.sampling.top_k),
  1354. [](common_params & params, int value) {
  1355. params.sampling.top_k = value;
  1356. params.sampling.user_sampling_config |= common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_TOP_K;
  1357. }
  1358. ).set_sparam().set_env("LLAMA_ARG_TOP_K"));
  1359. add_opt(common_arg(
  1360. {"--top-p"}, "N",
  1361. string_format("top-p sampling (default: %.1f, 1.0 = disabled)", (double)params.sampling.top_p),
  1362. [](common_params & params, const std::string & value) {
  1363. params.sampling.top_p = std::stof(value);
  1364. params.sampling.user_sampling_config |= common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_TOP_P;
  1365. }
  1366. ).set_sparam());
  1367. add_opt(common_arg(
  1368. {"--min-p"}, "N",
  1369. string_format("min-p sampling (default: %.1f, 0.0 = disabled)", (double)params.sampling.min_p),
  1370. [](common_params & params, const std::string & value) {
  1371. params.sampling.min_p = std::stof(value);
  1372. params.sampling.user_sampling_config |= common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_MIN_P;
  1373. }
  1374. ).set_sparam());
  1375. add_opt(common_arg(
  1376. {"--top-nsigma"}, "N",
  1377. string_format("top-n-sigma sampling (default: %.1f, -1.0 = disabled)", params.sampling.top_n_sigma),
  1378. [](common_params & params, const std::string & value) {
  1379. params.sampling.top_n_sigma = std::stof(value);
  1380. }
  1381. ).set_sparam());
  1382. add_opt(common_arg(
  1383. {"--xtc-probability"}, "N",
  1384. string_format("xtc probability (default: %.1f, 0.0 = disabled)", (double)params.sampling.xtc_probability),
  1385. [](common_params & params, const std::string & value) {
  1386. params.sampling.xtc_probability = std::stof(value);
  1387. params.sampling.user_sampling_config |= common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_XTC_PROBABILITY;
  1388. }
  1389. ).set_sparam());
  1390. add_opt(common_arg(
  1391. {"--xtc-threshold"}, "N",
  1392. string_format("xtc threshold (default: %.1f, 1.0 = disabled)", (double)params.sampling.xtc_threshold),
  1393. [](common_params & params, const std::string & value) {
  1394. params.sampling.xtc_threshold = std::stof(value);
  1395. params.sampling.user_sampling_config |= common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_XTC_THRESHOLD;
  1396. }
  1397. ).set_sparam());
  1398. add_opt(common_arg(
  1399. {"--typical"}, "N",
  1400. string_format("locally typical sampling, parameter p (default: %.1f, 1.0 = disabled)", (double)params.sampling.typ_p),
  1401. [](common_params & params, const std::string & value) {
  1402. params.sampling.typ_p = std::stof(value);
  1403. }
  1404. ).set_sparam());
  1405. add_opt(common_arg(
  1406. {"--repeat-last-n"}, "N",
  1407. string_format("last n tokens to consider for penalize (default: %d, 0 = disabled, -1 = ctx_size)", params.sampling.penalty_last_n),
  1408. [](common_params & params, int value) {
  1409. if (value < -1) {
  1410. throw std::runtime_error(string_format("error: invalid repeat-last-n = %d\n", value));
  1411. }
  1412. params.sampling.penalty_last_n = value;
  1413. params.sampling.n_prev = std::max(params.sampling.n_prev, params.sampling.penalty_last_n);
  1414. params.sampling.user_sampling_config |= common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_PENALTY_LAST_N;
  1415. }
  1416. ).set_sparam());
  1417. add_opt(common_arg(
  1418. {"--repeat-penalty"}, "N",
  1419. string_format("penalize repeat sequence of tokens (default: %.1f, 1.0 = disabled)", (double)params.sampling.penalty_repeat),
  1420. [](common_params & params, const std::string & value) {
  1421. params.sampling.penalty_repeat = std::stof(value);
  1422. params.sampling.user_sampling_config |= common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_PENALTY_REPEAT;
  1423. }
  1424. ).set_sparam());
  1425. add_opt(common_arg(
  1426. {"--presence-penalty"}, "N",
  1427. string_format("repeat alpha presence penalty (default: %.1f, 0.0 = disabled)", (double)params.sampling.penalty_present),
  1428. [](common_params & params, const std::string & value) {
  1429. params.sampling.penalty_present = std::stof(value);
  1430. }
  1431. ).set_sparam());
  1432. add_opt(common_arg(
  1433. {"--frequency-penalty"}, "N",
  1434. string_format("repeat alpha frequency penalty (default: %.1f, 0.0 = disabled)", (double)params.sampling.penalty_freq),
  1435. [](common_params & params, const std::string & value) {
  1436. params.sampling.penalty_freq = std::stof(value);
  1437. }
  1438. ).set_sparam());
  1439. add_opt(common_arg(
  1440. {"--dry-multiplier"}, "N",
  1441. string_format("set DRY sampling multiplier (default: %.1f, 0.0 = disabled)", (double)params.sampling.dry_multiplier),
  1442. [](common_params & params, const std::string & value) {
  1443. params.sampling.dry_multiplier = std::stof(value);
  1444. }
  1445. ).set_sparam());
  1446. add_opt(common_arg(
  1447. {"--dry-base"}, "N",
  1448. string_format("set DRY sampling base value (default: %.2f)", (double)params.sampling.dry_base),
  1449. [](common_params & params, const std::string & value) {
  1450. float potential_base = std::stof(value);
  1451. if (potential_base >= 1.0f)
  1452. {
  1453. params.sampling.dry_base = potential_base;
  1454. }
  1455. }
  1456. ).set_sparam());
  1457. add_opt(common_arg(
  1458. {"--dry-allowed-length"}, "N",
  1459. string_format("set allowed length for DRY sampling (default: %d)", params.sampling.dry_allowed_length),
  1460. [](common_params & params, int value) {
  1461. params.sampling.dry_allowed_length = value;
  1462. }
  1463. ).set_sparam());
  1464. add_opt(common_arg(
  1465. {"--dry-penalty-last-n"}, "N",
  1466. string_format("set DRY penalty for the last n tokens (default: %d, 0 = disable, -1 = context size)", params.sampling.dry_penalty_last_n),
  1467. [](common_params & params, int value) {
  1468. if (value < -1) {
  1469. throw std::runtime_error(string_format("error: invalid dry-penalty-last-n = %d\n", value));
  1470. }
  1471. params.sampling.dry_penalty_last_n = value;
  1472. }
  1473. ).set_sparam());
  1474. add_opt(common_arg(
  1475. {"--dry-sequence-breaker"}, "STRING",
  1476. 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",
  1477. params.sampling.dry_sequence_breakers.empty() ? "none" :
  1478. std::accumulate(std::next(params.sampling.dry_sequence_breakers.begin()),
  1479. params.sampling.dry_sequence_breakers.end(),
  1480. std::string("'") + (params.sampling.dry_sequence_breakers[0] == "\n" ? "\\n" : params.sampling.dry_sequence_breakers[0]) + "'",
  1481. [](const std::string& a, const std::string& b) {
  1482. std::string formatted_b = (b == "\n") ? "\\n" : b;
  1483. return a + ", '" + formatted_b + "'";
  1484. }).c_str()),
  1485. [](common_params & params, const std::string & value) {
  1486. static bool defaults_cleared = false;
  1487. if (!defaults_cleared) {
  1488. params.sampling.dry_sequence_breakers.clear();
  1489. defaults_cleared = true;
  1490. }
  1491. if (value == "none") {
  1492. params.sampling.dry_sequence_breakers.clear();
  1493. } else {
  1494. params.sampling.dry_sequence_breakers.emplace_back(value);
  1495. }
  1496. }
  1497. ).set_sparam());
  1498. add_opt(common_arg(
  1499. {"--dynatemp-range"}, "N",
  1500. string_format("dynamic temperature range (default: %.1f, 0.0 = disabled)", (double)params.sampling.dynatemp_range),
  1501. [](common_params & params, const std::string & value) {
  1502. params.sampling.dynatemp_range = std::stof(value);
  1503. }
  1504. ).set_sparam());
  1505. add_opt(common_arg(
  1506. {"--dynatemp-exp"}, "N",
  1507. string_format("dynamic temperature exponent (default: %.1f)", (double)params.sampling.dynatemp_exponent),
  1508. [](common_params & params, const std::string & value) {
  1509. params.sampling.dynatemp_exponent = std::stof(value);
  1510. }
  1511. ).set_sparam());
  1512. add_opt(common_arg(
  1513. {"--mirostat"}, "N",
  1514. string_format("use Mirostat sampling.\nTop K, Nucleus and Locally Typical samplers are ignored if used.\n"
  1515. "(default: %d, 0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0)", params.sampling.mirostat),
  1516. [](common_params & params, int value) {
  1517. params.sampling.mirostat = value;
  1518. params.sampling.user_sampling_config |= common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_MIROSTAT;
  1519. }
  1520. ).set_sparam());
  1521. add_opt(common_arg(
  1522. {"--mirostat-lr"}, "N",
  1523. string_format("Mirostat learning rate, parameter eta (default: %.1f)", (double)params.sampling.mirostat_eta),
  1524. [](common_params & params, const std::string & value) {
  1525. params.sampling.mirostat_eta = std::stof(value);
  1526. params.sampling.user_sampling_config |= common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_MIROSTAT_ETA;
  1527. }
  1528. ).set_sparam());
  1529. add_opt(common_arg(
  1530. {"--mirostat-ent"}, "N",
  1531. string_format("Mirostat target entropy, parameter tau (default: %.1f)", (double)params.sampling.mirostat_tau),
  1532. [](common_params & params, const std::string & value) {
  1533. params.sampling.mirostat_tau = std::stof(value);
  1534. params.sampling.user_sampling_config |= common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_MIROSTAT_TAU;
  1535. }
  1536. ).set_sparam());
  1537. add_opt(common_arg(
  1538. {"-l", "--logit-bias"}, "TOKEN_ID(+/-)BIAS",
  1539. "modifies the likelihood of token appearing in the completion,\n"
  1540. "i.e. `--logit-bias 15043+1` to increase likelihood of token ' Hello',\n"
  1541. "or `--logit-bias 15043-1` to decrease likelihood of token ' Hello'",
  1542. [](common_params & params, const std::string & value) {
  1543. std::stringstream ss(value);
  1544. llama_token key;
  1545. char sign;
  1546. std::string value_str;
  1547. try {
  1548. if (ss >> key && ss >> sign && std::getline(ss, value_str) && (sign == '+' || sign == '-')) {
  1549. const float bias = std::stof(value_str) * ((sign == '-') ? -1.0f : 1.0f);
  1550. params.sampling.logit_bias.push_back({key, bias});
  1551. } else {
  1552. throw std::invalid_argument("invalid input format");
  1553. }
  1554. } catch (const std::exception&) {
  1555. throw std::invalid_argument("invalid input format");
  1556. }
  1557. }
  1558. ).set_sparam());
  1559. add_opt(common_arg(
  1560. {"--grammar"}, "GRAMMAR",
  1561. string_format("BNF-like grammar to constrain generations (see samples in grammars/ dir) (default: '%s')", params.sampling.grammar.c_str()),
  1562. [](common_params & params, const std::string & value) {
  1563. params.sampling.grammar = value;
  1564. }
  1565. ).set_sparam());
  1566. add_opt(common_arg(
  1567. {"--grammar-file"}, "FNAME",
  1568. "file to read grammar from",
  1569. [](common_params & params, const std::string & value) {
  1570. params.sampling.grammar = read_file(value);
  1571. }
  1572. ).set_sparam());
  1573. add_opt(common_arg(
  1574. {"-j", "--json-schema"}, "SCHEMA",
  1575. "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",
  1576. [](common_params & params, const std::string & value) {
  1577. params.sampling.grammar = json_schema_to_grammar(json::parse(value));
  1578. }
  1579. ).set_sparam());
  1580. add_opt(common_arg(
  1581. {"-jf", "--json-schema-file"}, "FILE",
  1582. "File containing a 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",
  1583. [](common_params & params, const std::string & value) {
  1584. std::ifstream file(value);
  1585. if (!file) {
  1586. throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
  1587. }
  1588. std::string schema;
  1589. std::copy(
  1590. std::istreambuf_iterator<char>(file),
  1591. std::istreambuf_iterator<char>(),
  1592. std::back_inserter(schema)
  1593. );
  1594. params.sampling.grammar = json_schema_to_grammar(json::parse(schema));
  1595. }
  1596. ).set_sparam());
  1597. add_opt(common_arg(
  1598. {"--pooling"}, "{none,mean,cls,last,rank}",
  1599. "pooling type for embeddings, use model default if unspecified",
  1600. [](common_params & params, const std::string & value) {
  1601. /**/ if (value == "none") { params.pooling_type = LLAMA_POOLING_TYPE_NONE; }
  1602. else if (value == "mean") { params.pooling_type = LLAMA_POOLING_TYPE_MEAN; }
  1603. else if (value == "cls") { params.pooling_type = LLAMA_POOLING_TYPE_CLS; }
  1604. else if (value == "last") { params.pooling_type = LLAMA_POOLING_TYPE_LAST; }
  1605. else if (value == "rank") { params.pooling_type = LLAMA_POOLING_TYPE_RANK; }
  1606. else { throw std::invalid_argument("invalid value"); }
  1607. }
  1608. ).set_examples({LLAMA_EXAMPLE_EMBEDDING, LLAMA_EXAMPLE_RETRIEVAL, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_POOLING"));
  1609. add_opt(common_arg(
  1610. {"--attention"}, "{causal,non-causal}",
  1611. "attention type for embeddings, use model default if unspecified",
  1612. [](common_params & params, const std::string & value) {
  1613. /**/ if (value == "causal") { params.attention_type = LLAMA_ATTENTION_TYPE_CAUSAL; }
  1614. else if (value == "non-causal") { params.attention_type = LLAMA_ATTENTION_TYPE_NON_CAUSAL; }
  1615. else { throw std::invalid_argument("invalid value"); }
  1616. }
  1617. ).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
  1618. add_opt(common_arg(
  1619. {"--rope-scaling"}, "{none,linear,yarn}",
  1620. "RoPE frequency scaling method, defaults to linear unless specified by the model",
  1621. [](common_params & params, const std::string & value) {
  1622. /**/ if (value == "none") { params.rope_scaling_type = LLAMA_ROPE_SCALING_TYPE_NONE; }
  1623. else if (value == "linear") { params.rope_scaling_type = LLAMA_ROPE_SCALING_TYPE_LINEAR; }
  1624. else if (value == "yarn") { params.rope_scaling_type = LLAMA_ROPE_SCALING_TYPE_YARN; }
  1625. else { throw std::invalid_argument("invalid value"); }
  1626. }
  1627. ).set_env("LLAMA_ARG_ROPE_SCALING_TYPE"));
  1628. add_opt(common_arg(
  1629. {"--rope-scale"}, "N",
  1630. "RoPE context scaling factor, expands context by a factor of N",
  1631. [](common_params & params, const std::string & value) {
  1632. params.rope_freq_scale = 1.0f / std::stof(value);
  1633. }
  1634. ).set_env("LLAMA_ARG_ROPE_SCALE"));
  1635. add_opt(common_arg(
  1636. {"--rope-freq-base"}, "N",
  1637. "RoPE base frequency, used by NTK-aware scaling (default: loaded from model)",
  1638. [](common_params & params, const std::string & value) {
  1639. params.rope_freq_base = std::stof(value);
  1640. }
  1641. ).set_env("LLAMA_ARG_ROPE_FREQ_BASE"));
  1642. add_opt(common_arg(
  1643. {"--rope-freq-scale"}, "N",
  1644. "RoPE frequency scaling factor, expands context by a factor of 1/N",
  1645. [](common_params & params, const std::string & value) {
  1646. params.rope_freq_scale = std::stof(value);
  1647. }
  1648. ).set_env("LLAMA_ARG_ROPE_FREQ_SCALE"));
  1649. add_opt(common_arg(
  1650. {"--yarn-orig-ctx"}, "N",
  1651. string_format("YaRN: original context size of model (default: %d = model training context size)", params.yarn_orig_ctx),
  1652. [](common_params & params, int value) {
  1653. params.yarn_orig_ctx = value;
  1654. }
  1655. ).set_env("LLAMA_ARG_YARN_ORIG_CTX"));
  1656. add_opt(common_arg(
  1657. {"--yarn-ext-factor"}, "N",
  1658. string_format("YaRN: extrapolation mix factor (default: %.1f, 0.0 = full interpolation)", (double)params.yarn_ext_factor),
  1659. [](common_params & params, const std::string & value) {
  1660. params.yarn_ext_factor = std::stof(value);
  1661. }
  1662. ).set_env("LLAMA_ARG_YARN_EXT_FACTOR"));
  1663. add_opt(common_arg(
  1664. {"--yarn-attn-factor"}, "N",
  1665. string_format("YaRN: scale sqrt(t) or attention magnitude (default: %.1f)", (double)params.yarn_attn_factor),
  1666. [](common_params & params, const std::string & value) {
  1667. params.yarn_attn_factor = std::stof(value);
  1668. }
  1669. ).set_env("LLAMA_ARG_YARN_ATTN_FACTOR"));
  1670. add_opt(common_arg(
  1671. {"--yarn-beta-slow"}, "N",
  1672. string_format("YaRN: high correction dim or alpha (default: %.1f)", (double)params.yarn_beta_slow),
  1673. [](common_params & params, const std::string & value) {
  1674. params.yarn_beta_slow = std::stof(value);
  1675. }
  1676. ).set_env("LLAMA_ARG_YARN_BETA_SLOW"));
  1677. add_opt(common_arg(
  1678. {"--yarn-beta-fast"}, "N",
  1679. string_format("YaRN: low correction dim or beta (default: %.1f)", (double)params.yarn_beta_fast),
  1680. [](common_params & params, const std::string & value) {
  1681. params.yarn_beta_fast = std::stof(value);
  1682. }
  1683. ).set_env("LLAMA_ARG_YARN_BETA_FAST"));
  1684. add_opt(common_arg(
  1685. {"-gan", "--grp-attn-n"}, "N",
  1686. string_format("group-attention factor (default: %d)", params.grp_attn_n),
  1687. [](common_params & params, int value) {
  1688. params.grp_attn_n = value;
  1689. }
  1690. ).set_env("LLAMA_ARG_GRP_ATTN_N").set_examples({LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_PASSKEY}));
  1691. add_opt(common_arg(
  1692. {"-gaw", "--grp-attn-w"}, "N",
  1693. string_format("group-attention width (default: %d)", params.grp_attn_w),
  1694. [](common_params & params, int value) {
  1695. params.grp_attn_w = value;
  1696. }
  1697. ).set_env("LLAMA_ARG_GRP_ATTN_W").set_examples({LLAMA_EXAMPLE_COMPLETION}));
  1698. add_opt(common_arg(
  1699. {"-kvo", "--kv-offload"},
  1700. {"-nkvo", "--no-kv-offload"},
  1701. string_format("whether to enable KV cache offloading (default: %s)", params.no_kv_offload ? "disabled" : "enabled"),
  1702. [](common_params & params, bool value) {
  1703. params.no_kv_offload = !value;
  1704. }
  1705. ).set_env("LLAMA_ARG_KV_OFFLOAD"));
  1706. add_opt(common_arg(
  1707. {"--repack"},
  1708. {"-nr", "--no-repack"},
  1709. string_format("whether to enable weight repacking (default: %s)", params.no_extra_bufts ? "disabled" : "enabled"),
  1710. [](common_params & params, bool value) {
  1711. params.no_extra_bufts = !value;
  1712. }
  1713. ).set_env("LLAMA_ARG_REPACK"));
  1714. add_opt(common_arg(
  1715. {"--no-host"},
  1716. "bypass host buffer allowing extra buffers to be used",
  1717. [](common_params & params) {
  1718. params.no_host = true;
  1719. }
  1720. ).set_env("LLAMA_ARG_NO_HOST"));
  1721. add_opt(common_arg(
  1722. {"-ctk", "--cache-type-k"}, "TYPE",
  1723. string_format(
  1724. "KV cache data type for K\n"
  1725. "allowed values: %s\n"
  1726. "(default: %s)",
  1727. get_all_kv_cache_types().c_str(),
  1728. ggml_type_name(params.cache_type_k)
  1729. ),
  1730. [](common_params & params, const std::string & value) {
  1731. params.cache_type_k = kv_cache_type_from_str(value);
  1732. }
  1733. ).set_env("LLAMA_ARG_CACHE_TYPE_K"));
  1734. add_opt(common_arg(
  1735. {"-ctv", "--cache-type-v"}, "TYPE",
  1736. string_format(
  1737. "KV cache data type for V\n"
  1738. "allowed values: %s\n"
  1739. "(default: %s)",
  1740. get_all_kv_cache_types().c_str(),
  1741. ggml_type_name(params.cache_type_v)
  1742. ),
  1743. [](common_params & params, const std::string & value) {
  1744. params.cache_type_v = kv_cache_type_from_str(value);
  1745. }
  1746. ).set_env("LLAMA_ARG_CACHE_TYPE_V"));
  1747. add_opt(common_arg(
  1748. {"--hellaswag"},
  1749. "compute HellaSwag score over random tasks from datafile supplied with -f",
  1750. [](common_params & params) {
  1751. params.hellaswag = true;
  1752. }
  1753. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1754. add_opt(common_arg(
  1755. {"--hellaswag-tasks"}, "N",
  1756. string_format("number of tasks to use when computing the HellaSwag score (default: %zu)", params.hellaswag_tasks),
  1757. [](common_params & params, int value) {
  1758. params.hellaswag_tasks = value;
  1759. }
  1760. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1761. add_opt(common_arg(
  1762. {"--winogrande"},
  1763. "compute Winogrande score over random tasks from datafile supplied with -f",
  1764. [](common_params & params) {
  1765. params.winogrande = true;
  1766. }
  1767. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1768. add_opt(common_arg(
  1769. {"--winogrande-tasks"}, "N",
  1770. string_format("number of tasks to use when computing the Winogrande score (default: %zu)", params.winogrande_tasks),
  1771. [](common_params & params, int value) {
  1772. params.winogrande_tasks = value;
  1773. }
  1774. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1775. add_opt(common_arg(
  1776. {"--multiple-choice"},
  1777. "compute multiple choice score over random tasks from datafile supplied with -f",
  1778. [](common_params & params) {
  1779. params.multiple_choice = true;
  1780. }
  1781. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1782. add_opt(common_arg(
  1783. {"--multiple-choice-tasks"}, "N",
  1784. string_format("number of tasks to use when computing the multiple choice score (default: %zu)", params.multiple_choice_tasks),
  1785. [](common_params & params, int value) {
  1786. params.multiple_choice_tasks = value;
  1787. }
  1788. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1789. add_opt(common_arg(
  1790. {"--kl-divergence"},
  1791. "computes KL-divergence to logits provided via --kl-divergence-base",
  1792. [](common_params & params) {
  1793. params.kl_divergence = true;
  1794. }
  1795. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1796. add_opt(common_arg(
  1797. {"--save-all-logits", "--kl-divergence-base"}, "FNAME",
  1798. "set logits file",
  1799. [](common_params & params, const std::string & value) {
  1800. params.logits_file = value;
  1801. }
  1802. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1803. add_opt(common_arg(
  1804. {"--ppl-stride"}, "N",
  1805. string_format("stride for perplexity calculation (default: %d)", params.ppl_stride),
  1806. [](common_params & params, int value) {
  1807. params.ppl_stride = value;
  1808. }
  1809. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1810. add_opt(common_arg(
  1811. {"--ppl-output-type"}, "<0|1>",
  1812. string_format("output type for perplexity calculation (default: %d)", params.ppl_output_type),
  1813. [](common_params & params, int value) {
  1814. params.ppl_output_type = value;
  1815. }
  1816. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1817. add_opt(common_arg(
  1818. {"-dt", "--defrag-thold"}, "N",
  1819. string_format("KV cache defragmentation threshold (DEPRECATED)"),
  1820. [](common_params & params, const std::string & value) {
  1821. GGML_UNUSED(params);
  1822. GGML_UNUSED(value);
  1823. LOG_WRN("DEPRECATED: --defrag-thold is deprecated and no longer necessary to specify\n");
  1824. }
  1825. ).set_env("LLAMA_ARG_DEFRAG_THOLD"));
  1826. if (ex == LLAMA_EXAMPLE_SERVER) {
  1827. // this is to make sure this option appears in the server-specific section of the help message
  1828. add_opt(common_arg(
  1829. {"-np", "--parallel"}, "N",
  1830. string_format("number of server slots (default: %d, -1 = auto)", params.n_parallel),
  1831. [](common_params & params, int value) {
  1832. if (value == 0) {
  1833. throw std::invalid_argument("error: invalid value for n_parallel\n");
  1834. }
  1835. params.n_parallel = value;
  1836. }
  1837. ).set_env("LLAMA_ARG_N_PARALLEL").set_examples({LLAMA_EXAMPLE_SERVER}));
  1838. } else {
  1839. add_opt(common_arg(
  1840. {"-np", "--parallel"}, "N",
  1841. string_format("number of parallel sequences to decode (default: %d)", params.n_parallel),
  1842. [](common_params & params, int value) {
  1843. params.n_parallel = value;
  1844. }
  1845. ).set_env("LLAMA_ARG_N_PARALLEL"));
  1846. }
  1847. add_opt(common_arg(
  1848. {"-ns", "--sequences"}, "N",
  1849. string_format("number of sequences to decode (default: %d)", params.n_sequences),
  1850. [](common_params & params, int value) {
  1851. params.n_sequences = value;
  1852. }
  1853. ).set_examples({LLAMA_EXAMPLE_PARALLEL}));
  1854. add_opt(common_arg(
  1855. {"-cb", "--cont-batching"},
  1856. {"-nocb", "--no-cont-batching"},
  1857. string_format("whether to enable continuous batching (a.k.a dynamic batching) (default: %s)", params.cont_batching ? "enabled" : "disabled"),
  1858. [](common_params & params, bool value) {
  1859. params.cont_batching = value;
  1860. }
  1861. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CONT_BATCHING"));
  1862. add_opt(common_arg(
  1863. {"-mm", "--mmproj"}, "FILE",
  1864. "path to a multimodal projector file. see tools/mtmd/README.md\n"
  1865. "note: if -hf is used, this argument can be omitted",
  1866. [](common_params & params, const std::string & value) {
  1867. params.mmproj.path = value;
  1868. }
  1869. ).set_examples(mmproj_examples).set_env("LLAMA_ARG_MMPROJ"));
  1870. add_opt(common_arg(
  1871. {"-mmu", "--mmproj-url"}, "URL",
  1872. "URL to a multimodal projector file. see tools/mtmd/README.md",
  1873. [](common_params & params, const std::string & value) {
  1874. params.mmproj.url = value;
  1875. }
  1876. ).set_examples(mmproj_examples).set_env("LLAMA_ARG_MMPROJ_URL"));
  1877. add_opt(common_arg(
  1878. {"--mmproj-auto"},
  1879. {"--no-mmproj", "--no-mmproj-auto"},
  1880. string_format("whether to use multimodal projector file (if available), useful when using -hf (default: %s)", params.no_mmproj ? "disabled" : "enabled"),
  1881. [](common_params & params, bool value) {
  1882. params.no_mmproj = !value;
  1883. }
  1884. ).set_examples(mmproj_examples).set_env("LLAMA_ARG_MMPROJ_AUTO"));
  1885. add_opt(common_arg(
  1886. {"--mmproj-offload"},
  1887. {"--no-mmproj-offload"},
  1888. string_format("whether to enable GPU offloading for multimodal projector (default: %s)", params.mmproj_use_gpu ? "enabled" : "disabled"),
  1889. [](common_params & params, bool value) {
  1890. params.mmproj_use_gpu = value;
  1891. }
  1892. ).set_examples(mmproj_examples).set_env("LLAMA_ARG_MMPROJ_OFFLOAD"));
  1893. add_opt(common_arg(
  1894. {"--image", "--audio"}, "FILE",
  1895. "path to an image or audio file. use with multimodal models, use comma-separated values for multiple files\n",
  1896. [](common_params & params, const std::string & value) {
  1897. for (const auto & item : string_split<std::string>(value, ',')) {
  1898. params.image.emplace_back(item);
  1899. }
  1900. }
  1901. ).set_examples({LLAMA_EXAMPLE_MTMD, LLAMA_EXAMPLE_CLI}));
  1902. add_opt(common_arg(
  1903. {"--image-min-tokens"}, "N",
  1904. "minimum number of tokens each image can take, only used by vision models with dynamic resolution (default: read from model)",
  1905. [](common_params & params, int value) {
  1906. params.image_min_tokens = value;
  1907. }
  1908. ).set_examples(mmproj_examples).set_env("LLAMA_ARG_IMAGE_MIN_TOKENS"));
  1909. add_opt(common_arg(
  1910. {"--image-max-tokens"}, "N",
  1911. "maximum number of tokens each image can take, only used by vision models with dynamic resolution (default: read from model)",
  1912. [](common_params & params, int value) {
  1913. params.image_max_tokens = value;
  1914. }
  1915. ).set_examples(mmproj_examples).set_env("LLAMA_ARG_IMAGE_MAX_TOKENS"));
  1916. if (llama_supports_rpc()) {
  1917. add_opt(common_arg(
  1918. {"--rpc"}, "SERVERS",
  1919. "comma separated list of RPC servers",
  1920. [](common_params & params, const std::string & value) {
  1921. add_rpc_devices(value);
  1922. GGML_UNUSED(params);
  1923. }
  1924. ).set_env("LLAMA_ARG_RPC"));
  1925. }
  1926. add_opt(common_arg(
  1927. {"--mlock"},
  1928. "force system to keep model in RAM rather than swapping or compressing",
  1929. [](common_params & params) {
  1930. params.use_mlock = true;
  1931. }
  1932. ).set_env("LLAMA_ARG_MLOCK"));
  1933. add_opt(common_arg(
  1934. {"--mmap"},
  1935. {"--no-mmap"},
  1936. string_format("whether to memory-map model (if disabled, slower load but may reduce pageouts if not using mlock) (default: %s)", params.use_mmap ? "enabled" : "disabled"),
  1937. [](common_params & params, bool value) {
  1938. params.use_mmap = value;
  1939. }
  1940. ).set_env("LLAMA_ARG_MMAP"));
  1941. add_opt(common_arg(
  1942. {"--numa"}, "TYPE",
  1943. "attempt optimizations that help on some NUMA systems\n"
  1944. "- distribute: spread execution evenly over all nodes\n"
  1945. "- isolate: only spawn threads on CPUs on the node that execution started on\n"
  1946. "- numactl: use the CPU map provided by numactl\n"
  1947. "if run without this previously, it is recommended to drop the system page cache before using this\n"
  1948. "see https://github.com/ggml-org/llama.cpp/issues/1437",
  1949. [](common_params & params, const std::string & value) {
  1950. /**/ if (value == "distribute" || value == "") { params.numa = GGML_NUMA_STRATEGY_DISTRIBUTE; }
  1951. else if (value == "isolate") { params.numa = GGML_NUMA_STRATEGY_ISOLATE; }
  1952. else if (value == "numactl") { params.numa = GGML_NUMA_STRATEGY_NUMACTL; }
  1953. else { throw std::invalid_argument("invalid value"); }
  1954. }
  1955. ).set_env("LLAMA_ARG_NUMA"));
  1956. add_opt(common_arg(
  1957. {"-dev", "--device"}, "<dev1,dev2,..>",
  1958. "comma-separated list of devices to use for offloading (none = don't offload)\n"
  1959. "use --list-devices to see a list of available devices",
  1960. [](common_params & params, const std::string & value) {
  1961. params.devices = parse_device_list(value);
  1962. }
  1963. ).set_env("LLAMA_ARG_DEVICE"));
  1964. add_opt(common_arg(
  1965. {"--list-devices"},
  1966. "print list of available devices and exit",
  1967. [](common_params &) {
  1968. std::vector<ggml_backend_dev_t> devices;
  1969. for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
  1970. auto * dev = ggml_backend_dev_get(i);
  1971. if (ggml_backend_dev_type(dev) != GGML_BACKEND_DEVICE_TYPE_CPU) {
  1972. devices.push_back(dev);
  1973. }
  1974. }
  1975. printf("Available devices:\n");
  1976. for (auto * dev : devices) {
  1977. size_t free, total;
  1978. ggml_backend_dev_memory(dev, &free, &total);
  1979. 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);
  1980. }
  1981. exit(0);
  1982. }
  1983. ));
  1984. add_opt(common_arg(
  1985. {"-ot", "--override-tensor"}, "<tensor name pattern>=<buffer type>,...",
  1986. "override tensor buffer type", [](common_params & params, const std::string & value) {
  1987. parse_tensor_buffer_overrides(value, params.tensor_buft_overrides);
  1988. }
  1989. ));
  1990. add_opt(common_arg(
  1991. {"-otd", "--override-tensor-draft"}, "<tensor name pattern>=<buffer type>,...",
  1992. "override tensor buffer type for draft model", [](common_params & params, const std::string & value) {
  1993. parse_tensor_buffer_overrides(value, params.speculative.tensor_buft_overrides);
  1994. }
  1995. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}));
  1996. add_opt(common_arg(
  1997. {"-cmoe", "--cpu-moe"},
  1998. "keep all Mixture of Experts (MoE) weights in the CPU",
  1999. [](common_params & params) {
  2000. params.tensor_buft_overrides.push_back(llm_ffn_exps_cpu_override());
  2001. }
  2002. ).set_env("LLAMA_ARG_CPU_MOE"));
  2003. add_opt(common_arg(
  2004. {"-ncmoe", "--n-cpu-moe"}, "N",
  2005. "keep the Mixture of Experts (MoE) weights of the first N layers in the CPU",
  2006. [](common_params & params, int value) {
  2007. if (value < 0) {
  2008. throw std::invalid_argument("invalid value");
  2009. }
  2010. for (int i = 0; i < value; ++i) {
  2011. // keep strings alive and avoid leaking memory by storing them in a static vector
  2012. static std::list<std::string> buft_overrides;
  2013. buft_overrides.push_back(llm_ffn_exps_block_regex(i));
  2014. params.tensor_buft_overrides.push_back({buft_overrides.back().c_str(), ggml_backend_cpu_buffer_type()});
  2015. }
  2016. }
  2017. ).set_env("LLAMA_ARG_N_CPU_MOE"));
  2018. add_opt(common_arg(
  2019. {"-cmoed", "--cpu-moe-draft"},
  2020. "keep all Mixture of Experts (MoE) weights in the CPU for the draft model",
  2021. [](common_params & params) {
  2022. params.speculative.tensor_buft_overrides.push_back(llm_ffn_exps_cpu_override());
  2023. }
  2024. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_CPU_MOE_DRAFT"));
  2025. add_opt(common_arg(
  2026. {"-ncmoed", "--n-cpu-moe-draft"}, "N",
  2027. "keep the Mixture of Experts (MoE) weights of the first N layers in the CPU for the draft model",
  2028. [](common_params & params, int value) {
  2029. if (value < 0) {
  2030. throw std::invalid_argument("invalid value");
  2031. }
  2032. for (int i = 0; i < value; ++i) {
  2033. static std::list<std::string> buft_overrides_draft;
  2034. buft_overrides_draft.push_back(llm_ffn_exps_block_regex(i));
  2035. params.speculative.tensor_buft_overrides.push_back({buft_overrides_draft.back().c_str(), ggml_backend_cpu_buffer_type()});
  2036. }
  2037. }
  2038. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_N_CPU_MOE_DRAFT"));
  2039. add_opt(common_arg(
  2040. {"-ngl", "--gpu-layers", "--n-gpu-layers"}, "N",
  2041. string_format("max. number of layers to store in VRAM (default: %d)", params.n_gpu_layers),
  2042. [](common_params & params, int value) {
  2043. params.n_gpu_layers = value;
  2044. if (!llama_supports_gpu_offload()) {
  2045. fprintf(stderr, "warning: no usable GPU found, --gpu-layers option will be ignored\n");
  2046. fprintf(stderr, "warning: one possible reason is that llama.cpp was compiled without GPU support\n");
  2047. fprintf(stderr, "warning: consult docs/build.md for compilation instructions\n");
  2048. }
  2049. }
  2050. ).set_env("LLAMA_ARG_N_GPU_LAYERS"));
  2051. add_opt(common_arg(
  2052. {"-sm", "--split-mode"}, "{none,layer,row}",
  2053. "how to split the model across multiple GPUs, one of:\n"
  2054. "- none: use one GPU only\n"
  2055. "- layer (default): split layers and KV across GPUs\n"
  2056. "- row: split rows across GPUs",
  2057. [](common_params & params, const std::string & value) {
  2058. std::string arg_next = value;
  2059. if (arg_next == "none") {
  2060. params.split_mode = LLAMA_SPLIT_MODE_NONE;
  2061. } else if (arg_next == "layer") {
  2062. params.split_mode = LLAMA_SPLIT_MODE_LAYER;
  2063. } else if (arg_next == "row") {
  2064. params.split_mode = LLAMA_SPLIT_MODE_ROW;
  2065. } else {
  2066. throw std::invalid_argument("invalid value");
  2067. }
  2068. if (!llama_supports_gpu_offload()) {
  2069. fprintf(stderr, "warning: llama.cpp was compiled without support for GPU offload. Setting the split mode has no effect.\n");
  2070. }
  2071. }
  2072. ).set_env("LLAMA_ARG_SPLIT_MODE"));
  2073. add_opt(common_arg(
  2074. {"-ts", "--tensor-split"}, "N0,N1,N2,...",
  2075. "fraction of the model to offload to each GPU, comma-separated list of proportions, e.g. 3,1",
  2076. [](common_params & params, const std::string & value) {
  2077. std::string arg_next = value;
  2078. // split string by , and /
  2079. const std::regex regex{ R"([,/]+)" };
  2080. std::sregex_token_iterator it{ arg_next.begin(), arg_next.end(), regex, -1 };
  2081. std::vector<std::string> split_arg{ it, {} };
  2082. if (split_arg.size() >= llama_max_devices()) {
  2083. throw std::invalid_argument(
  2084. string_format("got %d input configs, but system only has %d devices", (int)split_arg.size(), (int)llama_max_devices())
  2085. );
  2086. }
  2087. for (size_t i = 0; i < llama_max_devices(); ++i) {
  2088. if (i < split_arg.size()) {
  2089. params.tensor_split[i] = std::stof(split_arg[i]);
  2090. } else {
  2091. params.tensor_split[i] = 0.0f;
  2092. }
  2093. }
  2094. if (!llama_supports_gpu_offload()) {
  2095. fprintf(stderr, "warning: llama.cpp was compiled without support for GPU offload. Setting a tensor split has no effect.\n");
  2096. }
  2097. }
  2098. ).set_env("LLAMA_ARG_TENSOR_SPLIT"));
  2099. add_opt(common_arg(
  2100. {"-mg", "--main-gpu"}, "INDEX",
  2101. 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),
  2102. [](common_params & params, int value) {
  2103. params.main_gpu = value;
  2104. if (!llama_supports_gpu_offload()) {
  2105. fprintf(stderr, "warning: llama.cpp was compiled without support for GPU offload. Setting the main GPU has no effect.\n");
  2106. }
  2107. }
  2108. ).set_env("LLAMA_ARG_MAIN_GPU"));
  2109. add_opt(common_arg(
  2110. { "-fit", "--fit" }, "[on|off]",
  2111. string_format("whether to adjust unset arguments to fit in device memory ('on' or 'off', default: '%s')", params.fit_params ? "on" : "off"),
  2112. [](common_params & params, const std::string & value) {
  2113. if (is_truthy(value)) {
  2114. params.fit_params = true;
  2115. } else if (is_falsey(value)) {
  2116. params.fit_params = false;
  2117. } else {
  2118. throw std::runtime_error(
  2119. string_format("error: unkown value for --fit: '%s'\n", value.c_str()));
  2120. }
  2121. }
  2122. ).set_env("LLAMA_ARG_FIT"));
  2123. add_opt(common_arg(
  2124. { "-fitt", "--fit-target" }, "MiB",
  2125. string_format("target margin per device for --fit option, default: %zu", params.fit_params_target/(1024*1024)),
  2126. [](common_params & params, int value) {
  2127. params.fit_params_target = value * size_t(1024*1024);
  2128. }
  2129. ).set_env("LLAMA_ARG_FIT_TARGET"));
  2130. add_opt(common_arg(
  2131. { "-fitc", "--fit-ctx" }, "N",
  2132. string_format("minimum ctx size that can be set by --fit option, default: %" PRIu32, params.fit_params_min_ctx),
  2133. [](common_params & params, int value) {
  2134. params.fit_params_min_ctx = value;
  2135. }
  2136. ).set_env("LLAMA_ARG_FIT_CTX"));
  2137. add_opt(common_arg(
  2138. {"--check-tensors"},
  2139. string_format("check model tensor data for invalid values (default: %s)", params.check_tensors ? "true" : "false"),
  2140. [](common_params & params) {
  2141. params.check_tensors = true;
  2142. }
  2143. ));
  2144. add_opt(common_arg(
  2145. {"--override-kv"}, "KEY=TYPE:VALUE,...",
  2146. "advanced option to override model metadata by key. to specify multiple overrides, either use comma-separated or repeat this argument.\n"
  2147. "types: int, float, bool, str. example: --override-kv tokenizer.ggml.add_bos_token=bool:false,tokenizer.ggml.add_eos_token=bool:false",
  2148. [](common_params & params, const std::string & value) {
  2149. std::vector<std::string> kv_overrides;
  2150. std::string current;
  2151. bool escaping = false;
  2152. for (const char c : value) {
  2153. if (escaping) {
  2154. current.push_back(c);
  2155. escaping = false;
  2156. } else if (c == '\\') {
  2157. escaping = true;
  2158. } else if (c == ',') {
  2159. kv_overrides.push_back(current);
  2160. current.clear();
  2161. } else {
  2162. current.push_back(c);
  2163. }
  2164. }
  2165. if (escaping) {
  2166. current.push_back('\\');
  2167. }
  2168. kv_overrides.push_back(current);
  2169. for (const auto & kv_override : kv_overrides) {
  2170. if (!string_parse_kv_override(kv_override.c_str(), params.kv_overrides)) {
  2171. throw std::runtime_error(string_format("error: Invalid type for KV override: %s\n", kv_override.c_str()));
  2172. }
  2173. }
  2174. }
  2175. ));
  2176. add_opt(common_arg(
  2177. {"--op-offload"},
  2178. {"--no-op-offload"},
  2179. string_format("whether to offload host tensor operations to device (default: %s)", params.no_op_offload ? "false" : "true"),
  2180. [](common_params & params, bool value) {
  2181. params.no_op_offload = !value;
  2182. }
  2183. ));
  2184. add_opt(common_arg(
  2185. {"--lora"}, "FNAME",
  2186. "path to LoRA adapter (use comma-separated values to load multiple adapters)",
  2187. [](common_params & params, const std::string & value) {
  2188. for (const auto & item : string_split<std::string>(value, ',')) {
  2189. params.lora_adapters.push_back({ item, 1.0, "", "", nullptr });
  2190. }
  2191. }
  2192. // 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
  2193. ).set_examples({LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_EXPORT_LORA}));
  2194. add_opt(common_arg(
  2195. {"--lora-scaled"}, "FNAME:SCALE,...",
  2196. "path to LoRA adapter with user defined scaling (format: FNAME:SCALE,...)\n"
  2197. "note: use comma-separated values",
  2198. [](common_params & params, const std::string & value) {
  2199. for (const auto & item : string_split<std::string>(value, ',')) {
  2200. auto parts = string_split<std::string>(item, ':');
  2201. if (parts.size() != 2) {
  2202. throw std::invalid_argument("lora-scaled format: FNAME:SCALE");
  2203. }
  2204. params.lora_adapters.push_back({ parts[0], std::stof(parts[1]), "", "", nullptr });
  2205. }
  2206. }
  2207. // 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
  2208. ).set_examples({LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_EXPORT_LORA}));
  2209. add_opt(common_arg(
  2210. {"--control-vector"}, "FNAME",
  2211. "add a control vector\nnote: use comma-separated values to add multiple control vectors",
  2212. [](common_params & params, const std::string & value) {
  2213. for (const auto & item : string_split<std::string>(value, ',')) {
  2214. params.control_vectors.push_back({ 1.0f, item, });
  2215. }
  2216. }
  2217. ));
  2218. add_opt(common_arg(
  2219. {"--control-vector-scaled"}, "FNAME:SCALE,...",
  2220. "add a control vector with user defined scaling SCALE\n"
  2221. "note: use comma-separated values (format: FNAME:SCALE,...)",
  2222. [](common_params & params, const std::string & value) {
  2223. for (const auto & item : string_split<std::string>(value, ',')) {
  2224. auto parts = string_split<std::string>(item, ':');
  2225. if (parts.size() != 2) {
  2226. throw std::invalid_argument("control-vector-scaled format: FNAME:SCALE");
  2227. }
  2228. params.control_vectors.push_back({ std::stof(parts[1]), parts[0] });
  2229. }
  2230. }
  2231. ));
  2232. add_opt(common_arg(
  2233. {"--control-vector-layer-range"}, "START", "END",
  2234. "layer range to apply the control vector(s) to, start and end inclusive",
  2235. [](common_params & params, const std::string & start, const std::string & end) {
  2236. params.control_vector_layer_start = std::stoi(start);
  2237. params.control_vector_layer_end = std::stoi(end);
  2238. }
  2239. ));
  2240. add_opt(common_arg(
  2241. {"-a", "--alias"}, "STRING",
  2242. "set alias for model name (to be used by REST API)",
  2243. [](common_params & params, const std::string & value) {
  2244. params.model_alias = value;
  2245. }
  2246. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ALIAS"));
  2247. add_opt(common_arg(
  2248. {"-m", "--model"}, "FNAME",
  2249. ex == LLAMA_EXAMPLE_EXPORT_LORA
  2250. ? "model path from which to load base model"
  2251. : "model path to load",
  2252. [](common_params & params, const std::string & value) {
  2253. params.model.path = value;
  2254. }
  2255. ).set_examples({LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_EXPORT_LORA}).set_env("LLAMA_ARG_MODEL"));
  2256. add_opt(common_arg(
  2257. {"-mu", "--model-url"}, "MODEL_URL",
  2258. "model download url (default: unused)",
  2259. [](common_params & params, const std::string & value) {
  2260. params.model.url = value;
  2261. }
  2262. ).set_env("LLAMA_ARG_MODEL_URL"));
  2263. add_opt(common_arg(
  2264. { "-dr", "--docker-repo" }, "[<repo>/]<model>[:quant]",
  2265. "Docker Hub model repository. repo is optional, default to ai/. quant is optional, default to :latest.\n"
  2266. "example: gemma3\n"
  2267. "(default: unused)",
  2268. [](common_params & params, const std::string & value) {
  2269. params.model.docker_repo = value;
  2270. }
  2271. ).set_env("LLAMA_ARG_DOCKER_REPO"));
  2272. add_opt(common_arg(
  2273. {"-hf", "-hfr", "--hf-repo"}, "<user>/<model>[:quant]",
  2274. "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"
  2275. "mmproj is also downloaded automatically if available. to disable, add --no-mmproj\n"
  2276. "example: unsloth/phi-4-GGUF:q4_k_m\n"
  2277. "(default: unused)",
  2278. [](common_params & params, const std::string & value) {
  2279. params.model.hf_repo = value;
  2280. }
  2281. ).set_env("LLAMA_ARG_HF_REPO"));
  2282. add_opt(common_arg(
  2283. {"-hfd", "-hfrd", "--hf-repo-draft"}, "<user>/<model>[:quant]",
  2284. "Same as --hf-repo, but for the draft model (default: unused)",
  2285. [](common_params & params, const std::string & value) {
  2286. params.speculative.model.hf_repo = value;
  2287. }
  2288. ).set_env("LLAMA_ARG_HFD_REPO"));
  2289. add_opt(common_arg(
  2290. {"-hff", "--hf-file"}, "FILE",
  2291. "Hugging Face model file. If specified, it will override the quant in --hf-repo (default: unused)",
  2292. [](common_params & params, const std::string & value) {
  2293. params.model.hf_file = value;
  2294. }
  2295. ).set_env("LLAMA_ARG_HF_FILE"));
  2296. add_opt(common_arg(
  2297. {"-hfv", "-hfrv", "--hf-repo-v"}, "<user>/<model>[:quant]",
  2298. "Hugging Face model repository for the vocoder model (default: unused)",
  2299. [](common_params & params, const std::string & value) {
  2300. params.vocoder.model.hf_repo = value;
  2301. }
  2302. ).set_env("LLAMA_ARG_HF_REPO_V"));
  2303. add_opt(common_arg(
  2304. {"-hffv", "--hf-file-v"}, "FILE",
  2305. "Hugging Face model file for the vocoder model (default: unused)",
  2306. [](common_params & params, const std::string & value) {
  2307. params.vocoder.model.hf_file = value;
  2308. }
  2309. ).set_env("LLAMA_ARG_HF_FILE_V"));
  2310. add_opt(common_arg(
  2311. {"-hft", "--hf-token"}, "TOKEN",
  2312. "Hugging Face access token (default: value from HF_TOKEN environment variable)",
  2313. [](common_params & params, const std::string & value) {
  2314. params.hf_token = value;
  2315. }
  2316. ).set_env("HF_TOKEN"));
  2317. add_opt(common_arg(
  2318. {"--context-file"}, "FNAME",
  2319. "file to load context from (use comma-separated values to specify multiple files)",
  2320. [](common_params & params, const std::string & value) {
  2321. for (const auto & item : string_split<std::string>(value, ',')) {
  2322. std::ifstream file(item, std::ios::binary);
  2323. if (!file) {
  2324. throw std::runtime_error(string_format("error: failed to open file '%s'\n", item.c_str()));
  2325. }
  2326. params.context_files.push_back(item);
  2327. }
  2328. }
  2329. ).set_examples({LLAMA_EXAMPLE_RETRIEVAL}));
  2330. add_opt(common_arg(
  2331. {"--chunk-size"}, "N",
  2332. string_format("minimum length of embedded text chunks (default: %d)", params.chunk_size),
  2333. [](common_params & params, int value) {
  2334. params.chunk_size = value;
  2335. }
  2336. ).set_examples({LLAMA_EXAMPLE_RETRIEVAL}));
  2337. add_opt(common_arg(
  2338. {"--chunk-separator"}, "STRING",
  2339. string_format("separator between chunks (default: '%s')", params.chunk_separator.c_str()),
  2340. [](common_params & params, const std::string & value) {
  2341. params.chunk_separator = value;
  2342. }
  2343. ).set_examples({LLAMA_EXAMPLE_RETRIEVAL}));
  2344. add_opt(common_arg(
  2345. {"--junk"}, "N",
  2346. string_format("number of times to repeat the junk text (default: %d)", params.n_junk),
  2347. [](common_params & params, int value) {
  2348. params.n_junk = value;
  2349. }
  2350. ).set_examples({LLAMA_EXAMPLE_PASSKEY, LLAMA_EXAMPLE_PARALLEL}));
  2351. add_opt(common_arg(
  2352. {"--pos"}, "N",
  2353. string_format("position of the passkey in the junk text (default: %d)", params.i_pos),
  2354. [](common_params & params, int value) {
  2355. params.i_pos = value;
  2356. }
  2357. ).set_examples({LLAMA_EXAMPLE_PASSKEY}));
  2358. add_opt(common_arg(
  2359. {"-o", "--output", "--output-file"}, "FNAME",
  2360. string_format("output file (default: '%s')", params.out_file.c_str()),
  2361. [](common_params & params, const std::string & value) {
  2362. params.out_file = value;
  2363. }
  2364. ).set_examples({LLAMA_EXAMPLE_IMATRIX, LLAMA_EXAMPLE_CVECTOR_GENERATOR, LLAMA_EXAMPLE_EXPORT_LORA, LLAMA_EXAMPLE_TTS, LLAMA_EXAMPLE_FINETUNE}));
  2365. add_opt(common_arg(
  2366. {"-ofreq", "--output-frequency"}, "N",
  2367. string_format("output the imatrix every N iterations (default: %d)", params.n_out_freq),
  2368. [](common_params & params, int value) {
  2369. params.n_out_freq = value;
  2370. }
  2371. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  2372. add_opt(common_arg(
  2373. {"--output-format"}, "{gguf,dat}",
  2374. string_format("output format for imatrix file (default: %s)", params.imat_dat > 0 ? "dat" : "gguf"),
  2375. [](common_params & params, const std::string & value) {
  2376. /**/ if (value == "gguf") { params.imat_dat = -1; }
  2377. else if (value == "dat") { params.imat_dat = 1; }
  2378. else { throw std::invalid_argument("invalid output format"); }
  2379. }
  2380. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  2381. add_opt(common_arg(
  2382. {"--save-frequency"}, "N",
  2383. string_format("save an imatrix copy every N iterations (default: %d)", params.n_save_freq),
  2384. [](common_params & params, int value) {
  2385. params.n_save_freq = value;
  2386. }
  2387. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  2388. add_opt(common_arg(
  2389. {"--process-output"},
  2390. string_format("collect data for the output tensor (default: %s)", params.process_output ? "true" : "false"),
  2391. [](common_params & params) {
  2392. params.process_output = true;
  2393. }
  2394. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  2395. add_opt(common_arg(
  2396. {"--ppl"},
  2397. {"--no-ppl"},
  2398. string_format("whether to compute perplexity (default: %s)", params.compute_ppl ? "true" : "false"),
  2399. [](common_params & params, bool value) {
  2400. params.compute_ppl = value;
  2401. }
  2402. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  2403. add_opt(common_arg(
  2404. {"--chunk", "--from-chunk"}, "N",
  2405. string_format("start processing the input from chunk N (default: %d)", params.i_chunk),
  2406. [](common_params & params, int value) {
  2407. params.i_chunk = value;
  2408. }
  2409. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  2410. add_opt(common_arg(
  2411. {"--show-statistics"},
  2412. string_format("show imatrix statistics and then exit (default: %s)", params.show_statistics ? "true" : "false"),
  2413. [](common_params & params) {
  2414. params.show_statistics = true;
  2415. }
  2416. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  2417. add_opt(common_arg(
  2418. {"--parse-special"},
  2419. string_format("parse special tokens (chat, tool, etc) (default: %s)", params.parse_special ? "true" : "false"),
  2420. [](common_params & params) {
  2421. params.parse_special = true;
  2422. }
  2423. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  2424. add_opt(common_arg(
  2425. {"-pps"},
  2426. string_format("is the prompt shared across parallel sequences (default: %s)", params.is_pp_shared ? "true" : "false"),
  2427. [](common_params & params) {
  2428. params.is_pp_shared = true;
  2429. }
  2430. ).set_examples({LLAMA_EXAMPLE_BENCH, LLAMA_EXAMPLE_PARALLEL}));
  2431. add_opt(common_arg(
  2432. {"-tgs"},
  2433. string_format("is the text generation separated across the different sequences (default: %s)", params.is_tg_separate ? "true" : "false"),
  2434. [](common_params & params) {
  2435. params.is_tg_separate = true;
  2436. }
  2437. ).set_examples({LLAMA_EXAMPLE_BENCH, LLAMA_EXAMPLE_PARALLEL}));
  2438. add_opt(common_arg(
  2439. {"-npp"}, "n0,n1,...",
  2440. "number of prompt tokens",
  2441. [](common_params & params, const std::string & value) {
  2442. auto p = string_split<int>(value, ',');
  2443. params.n_pp.insert(params.n_pp.end(), p.begin(), p.end());
  2444. }
  2445. ).set_examples({LLAMA_EXAMPLE_BENCH}));
  2446. add_opt(common_arg(
  2447. {"-ntg"}, "n0,n1,...",
  2448. "number of text generation tokens",
  2449. [](common_params & params, const std::string & value) {
  2450. auto p = string_split<int>(value, ',');
  2451. params.n_tg.insert(params.n_tg.end(), p.begin(), p.end());
  2452. }
  2453. ).set_examples({LLAMA_EXAMPLE_BENCH}));
  2454. add_opt(common_arg(
  2455. {"-npl"}, "n0,n1,...",
  2456. "number of parallel prompts",
  2457. [](common_params & params, const std::string & value) {
  2458. auto p = string_split<int>(value, ',');
  2459. params.n_pl.insert(params.n_pl.end(), p.begin(), p.end());
  2460. }
  2461. ).set_examples({LLAMA_EXAMPLE_BENCH}));
  2462. add_opt(common_arg(
  2463. {"--embd-normalize"}, "N",
  2464. string_format("normalisation for embeddings (default: %d) (-1=none, 0=max absolute int16, 1=taxicab, 2=euclidean, >2=p-norm)", params.embd_normalize),
  2465. [](common_params & params, int value) {
  2466. params.embd_normalize = value;
  2467. }
  2468. ).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
  2469. add_opt(common_arg(
  2470. {"--embd-output-format"}, "FORMAT",
  2471. "empty = default, \"array\" = [[],[]...], \"json\" = openai style, \"json+\" = same \"json\" + cosine similarity matrix, \"raw\" = plain whitespace-delimited output (one embedding per line)",
  2472. [](common_params & params, const std::string & value) {
  2473. params.embd_out = value;
  2474. }
  2475. ).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
  2476. add_opt(common_arg(
  2477. {"--embd-separator"}, "STRING",
  2478. "separator of embeddings (default \\n) for example \"<#sep#>\"",
  2479. [](common_params & params, const std::string & value) {
  2480. params.embd_sep = value;
  2481. }
  2482. ).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
  2483. add_opt(common_arg(
  2484. {"--cls-separator"}, "STRING",
  2485. "separator of classification sequences (default \\t) for example \"<#seq#>\"",
  2486. [](common_params & params, const std::string & value) {
  2487. params.cls_sep = value;
  2488. }
  2489. ).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
  2490. add_opt(common_arg(
  2491. {"--host"}, "HOST",
  2492. string_format("ip address to listen, or bind to an UNIX socket if the address ends with .sock (default: %s)", params.hostname.c_str()),
  2493. [](common_params & params, const std::string & value) {
  2494. params.hostname = value;
  2495. }
  2496. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_HOST"));
  2497. add_opt(common_arg(
  2498. {"--port"}, "PORT",
  2499. string_format("port to listen (default: %d)", params.port),
  2500. [](common_params & params, int value) {
  2501. params.port = value;
  2502. }
  2503. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_PORT"));
  2504. add_opt(common_arg(
  2505. {"--path"}, "PATH",
  2506. string_format("path to serve static files from (default: %s)", params.public_path.c_str()),
  2507. [](common_params & params, const std::string & value) {
  2508. params.public_path = value;
  2509. }
  2510. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_STATIC_PATH"));
  2511. add_opt(common_arg(
  2512. {"--api-prefix"}, "PREFIX",
  2513. string_format("prefix path the server serves from, without the trailing slash (default: %s)", params.api_prefix.c_str()),
  2514. [](common_params & params, const std::string & value) {
  2515. params.api_prefix = value;
  2516. }
  2517. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_API_PREFIX"));
  2518. add_opt(common_arg(
  2519. {"--webui-config"}, "JSON",
  2520. "JSON that provides default WebUI settings (overrides WebUI defaults)",
  2521. [](common_params & params, const std::string & value) {
  2522. params.webui_config_json = value;
  2523. }
  2524. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_WEBUI_CONFIG"));
  2525. add_opt(common_arg(
  2526. {"--webui-config-file"}, "PATH",
  2527. "JSON file that provides default WebUI settings (overrides WebUI defaults)",
  2528. [](common_params & params, const std::string & value) {
  2529. params.webui_config_json = read_file(value);
  2530. }
  2531. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_WEBUI_CONFIG_FILE"));
  2532. add_opt(common_arg(
  2533. {"--webui"},
  2534. {"--no-webui"},
  2535. string_format("whether to enable the Web UI (default: %s)", params.webui ? "enabled" : "disabled"),
  2536. [](common_params & params, bool value) {
  2537. params.webui = value;
  2538. }
  2539. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_WEBUI"));
  2540. add_opt(common_arg(
  2541. {"--embedding", "--embeddings"},
  2542. string_format("restrict to only support embedding use case; use only with dedicated embedding models (default: %s)", params.embedding ? "enabled" : "disabled"),
  2543. [](common_params & params) {
  2544. params.embedding = true;
  2545. }
  2546. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_EMBEDDINGS"));
  2547. add_opt(common_arg(
  2548. {"--rerank", "--reranking"},
  2549. string_format("enable reranking endpoint on server (default: %s)", "disabled"),
  2550. [](common_params & params) {
  2551. params.embedding = true;
  2552. params.pooling_type = LLAMA_POOLING_TYPE_RANK;
  2553. }
  2554. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_RERANKING"));
  2555. add_opt(common_arg(
  2556. {"--api-key"}, "KEY",
  2557. "API key to use for authentication (default: none)",
  2558. [](common_params & params, const std::string & value) {
  2559. params.api_keys.push_back(value);
  2560. }
  2561. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_API_KEY"));
  2562. add_opt(common_arg(
  2563. {"--api-key-file"}, "FNAME",
  2564. "path to file containing API keys (default: none)",
  2565. [](common_params & params, const std::string & value) {
  2566. std::ifstream key_file(value);
  2567. if (!key_file) {
  2568. throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
  2569. }
  2570. std::string key;
  2571. while (std::getline(key_file, key)) {
  2572. if (!key.empty()) {
  2573. params.api_keys.push_back(key);
  2574. }
  2575. }
  2576. key_file.close();
  2577. }
  2578. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2579. add_opt(common_arg(
  2580. {"--ssl-key-file"}, "FNAME",
  2581. "path to file a PEM-encoded SSL private key",
  2582. [](common_params & params, const std::string & value) {
  2583. params.ssl_file_key = value;
  2584. }
  2585. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_SSL_KEY_FILE"));
  2586. add_opt(common_arg(
  2587. {"--ssl-cert-file"}, "FNAME",
  2588. "path to file a PEM-encoded SSL certificate",
  2589. [](common_params & params, const std::string & value) {
  2590. params.ssl_file_cert = value;
  2591. }
  2592. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_SSL_CERT_FILE"));
  2593. add_opt(common_arg(
  2594. {"--chat-template-kwargs"}, "STRING",
  2595. string_format("sets additional params for the json template parser"),
  2596. [](common_params & params, const std::string & value) {
  2597. auto parsed = json::parse(value);
  2598. for (const auto & item : parsed.items()) {
  2599. params.default_template_kwargs[item.key()] = item.value().dump();
  2600. }
  2601. }
  2602. ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_CHAT_TEMPLATE_KWARGS"));
  2603. add_opt(common_arg(
  2604. {"-to", "--timeout"}, "N",
  2605. string_format("server read/write timeout in seconds (default: %d)", params.timeout_read),
  2606. [](common_params & params, int value) {
  2607. params.timeout_read = value;
  2608. params.timeout_write = value;
  2609. }
  2610. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_TIMEOUT"));
  2611. add_opt(common_arg(
  2612. {"--threads-http"}, "N",
  2613. string_format("number of threads used to process HTTP requests (default: %d)", params.n_threads_http),
  2614. [](common_params & params, int value) {
  2615. params.n_threads_http = value;
  2616. }
  2617. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_THREADS_HTTP"));
  2618. add_opt(common_arg(
  2619. {"--cache-reuse"}, "N",
  2620. string_format(
  2621. "min chunk size to attempt reusing from the cache via KV shifting (default: %d)\n"
  2622. "[(card)](https://ggml.ai/f0.png)", params.n_cache_reuse
  2623. ),
  2624. [](common_params & params, int value) {
  2625. params.n_cache_reuse = value;
  2626. }
  2627. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CACHE_REUSE"));
  2628. add_opt(common_arg(
  2629. {"--metrics"},
  2630. string_format("enable prometheus compatible metrics endpoint (default: %s)", params.endpoint_metrics ? "enabled" : "disabled"),
  2631. [](common_params & params) {
  2632. params.endpoint_metrics = true;
  2633. }
  2634. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ENDPOINT_METRICS"));
  2635. add_opt(common_arg(
  2636. {"--props"},
  2637. string_format("enable changing global properties via POST /props (default: %s)", params.endpoint_props ? "enabled" : "disabled"),
  2638. [](common_params & params) {
  2639. params.endpoint_props = true;
  2640. }
  2641. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ENDPOINT_PROPS"));
  2642. add_opt(common_arg(
  2643. {"--slots"},
  2644. {"--no-slots"},
  2645. string_format("expose slots monitoring endpoint (default: %s)", params.endpoint_slots ? "enabled" : "disabled"),
  2646. [](common_params & params, bool value) {
  2647. params.endpoint_slots = value;
  2648. }
  2649. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ENDPOINT_SLOTS"));
  2650. add_opt(common_arg(
  2651. {"--slot-save-path"}, "PATH",
  2652. "path to save slot kv cache (default: disabled)",
  2653. [](common_params & params, const std::string & value) {
  2654. params.slot_save_path = value;
  2655. if (!fs_is_directory(params.slot_save_path)) {
  2656. throw std::invalid_argument("not a directory: " + value);
  2657. }
  2658. // if doesn't end with DIRECTORY_SEPARATOR, add it
  2659. if (!params.slot_save_path.empty() && params.slot_save_path[params.slot_save_path.size() - 1] != DIRECTORY_SEPARATOR) {
  2660. params.slot_save_path += DIRECTORY_SEPARATOR;
  2661. }
  2662. }
  2663. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2664. add_opt(common_arg(
  2665. {"--media-path"}, "PATH",
  2666. "directory for loading local media files; files can be accessed via file:// URLs using relative paths (default: disabled)",
  2667. [](common_params & params, const std::string & value) {
  2668. params.media_path = value;
  2669. if (!fs_is_directory(params.media_path)) {
  2670. throw std::invalid_argument("not a directory: " + value);
  2671. }
  2672. // if doesn't end with DIRECTORY_SEPARATOR, add it
  2673. if (!params.media_path.empty() && params.media_path[params.media_path.size() - 1] != DIRECTORY_SEPARATOR) {
  2674. params.media_path += DIRECTORY_SEPARATOR;
  2675. }
  2676. }
  2677. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2678. add_opt(common_arg(
  2679. {"--models-dir"}, "PATH",
  2680. "directory containing models for the router server (default: disabled)",
  2681. [](common_params & params, const std::string & value) {
  2682. params.models_dir = value;
  2683. }
  2684. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_MODELS_DIR"));
  2685. add_opt(common_arg(
  2686. {"--models-preset"}, "PATH",
  2687. "path to INI file containing model presets for the router server (default: disabled)",
  2688. [](common_params & params, const std::string & value) {
  2689. params.models_preset = value;
  2690. }
  2691. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_MODELS_PRESET"));
  2692. add_opt(common_arg(
  2693. {"--models-max"}, "N",
  2694. string_format("for router server, maximum number of models to load simultaneously (default: %d, 0 = unlimited)", params.models_max),
  2695. [](common_params & params, int value) {
  2696. params.models_max = value;
  2697. }
  2698. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_MODELS_MAX"));
  2699. add_opt(common_arg(
  2700. {"--models-autoload"},
  2701. {"--no-models-autoload"},
  2702. string_format("for router server, whether to automatically load models (default: %s)", params.models_autoload ? "enabled" : "disabled"),
  2703. [](common_params & params, bool value) {
  2704. params.models_autoload = value;
  2705. }
  2706. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_MODELS_AUTOLOAD"));
  2707. add_opt(common_arg(
  2708. {"--jinja"},
  2709. {"--no-jinja"},
  2710. string_format("whether to use jinja template engine for chat (default: %s)", params.use_jinja ? "enabled" : "disabled"),
  2711. [](common_params & params, bool value) {
  2712. params.use_jinja = value;
  2713. }
  2714. ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_CLI, LLAMA_EXAMPLE_MTMD}).set_env("LLAMA_ARG_JINJA"));
  2715. add_opt(common_arg(
  2716. {"--reasoning-format"}, "FORMAT",
  2717. "controls whether thought tags are allowed and/or extracted from the response, and in which format they're returned; one of:\n"
  2718. "- none: leaves thoughts unparsed in `message.content`\n"
  2719. "- deepseek: puts thoughts in `message.reasoning_content`\n"
  2720. "- deepseek-legacy: keeps `<think>` tags in `message.content` while also populating `message.reasoning_content`\n"
  2721. "(default: auto)",
  2722. [](common_params & params, const std::string & value) {
  2723. params.reasoning_format = common_reasoning_format_from_name(value);
  2724. }
  2725. ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_THINK"));
  2726. add_opt(common_arg(
  2727. {"--reasoning-budget"}, "N",
  2728. "controls the amount of thinking allowed; currently only one of: -1 for unrestricted thinking budget, or 0 to disable thinking (default: -1)",
  2729. [](common_params & params, int value) {
  2730. if (value != 0 && value != -1) { throw std::invalid_argument("invalid value"); }
  2731. params.reasoning_budget = value;
  2732. }
  2733. ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_THINK_BUDGET"));
  2734. add_opt(common_arg(
  2735. {"--chat-template"}, "JINJA_TEMPLATE",
  2736. string_format(
  2737. "set custom jinja chat template (default: template taken from model's metadata)\n"
  2738. "if suffix/prefix are specified, template will be disabled\n"
  2739. "only commonly used templates are accepted (unless --jinja is set before this flag):\n"
  2740. "list of built-in templates:\n%s", list_builtin_chat_templates().c_str()
  2741. ),
  2742. [](common_params & params, const std::string & value) {
  2743. params.chat_template = value;
  2744. }
  2745. ).set_examples({LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_CLI, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_MTMD}).set_env("LLAMA_ARG_CHAT_TEMPLATE"));
  2746. add_opt(common_arg(
  2747. {"--chat-template-file"}, "JINJA_TEMPLATE_FILE",
  2748. string_format(
  2749. "set custom jinja chat template file (default: template taken from model's metadata)\n"
  2750. "if suffix/prefix are specified, template will be disabled\n"
  2751. "only commonly used templates are accepted (unless --jinja is set before this flag):\n"
  2752. "list of built-in templates:\n%s", list_builtin_chat_templates().c_str()
  2753. ),
  2754. [](common_params & params, const std::string & value) {
  2755. params.chat_template = read_file(value);
  2756. }
  2757. ).set_examples({LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_CLI, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CHAT_TEMPLATE_FILE"));
  2758. add_opt(common_arg(
  2759. {"--prefill-assistant"},
  2760. {"--no-prefill-assistant"},
  2761. string_format(
  2762. "whether to prefill the assistant's response if the last message is an assistant message (default: prefill enabled)\n"
  2763. "when this flag is set, if the last message is an assistant message then it will be treated as a full message and not prefilled\n"
  2764. ),
  2765. [](common_params & params, bool value) {
  2766. params.prefill_assistant = value;
  2767. }
  2768. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_PREFILL_ASSISTANT"));
  2769. add_opt(common_arg(
  2770. {"-sps", "--slot-prompt-similarity"}, "SIMILARITY",
  2771. 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),
  2772. [](common_params & params, const std::string & value) {
  2773. params.slot_prompt_similarity = std::stof(value);
  2774. }
  2775. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2776. add_opt(common_arg(
  2777. {"--lora-init-without-apply"},
  2778. string_format("load LoRA adapters without applying them (apply later via POST /lora-adapters) (default: %s)", params.lora_init_without_apply ? "enabled" : "disabled"),
  2779. [](common_params & params) {
  2780. params.lora_init_without_apply = true;
  2781. }
  2782. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2783. add_opt(common_arg(
  2784. {"--simple-io"},
  2785. "use basic IO for better compatibility in subprocesses and limited consoles",
  2786. [](common_params & params) {
  2787. params.simple_io = true;
  2788. }
  2789. ).set_examples({LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_CLI}));
  2790. add_opt(common_arg(
  2791. {"--positive-file"}, "FNAME",
  2792. string_format("positive prompts file, one prompt per line (default: '%s')", params.cvector_positive_file.c_str()),
  2793. [](common_params & params, const std::string & value) {
  2794. params.cvector_positive_file = value;
  2795. }
  2796. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  2797. add_opt(common_arg(
  2798. {"--negative-file"}, "FNAME",
  2799. string_format("negative prompts file, one prompt per line (default: '%s')", params.cvector_negative_file.c_str()),
  2800. [](common_params & params, const std::string & value) {
  2801. params.cvector_negative_file = value;
  2802. }
  2803. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  2804. add_opt(common_arg(
  2805. {"--pca-batch"}, "N",
  2806. string_format("batch size used for PCA. Larger batch runs faster, but uses more memory (default: %d)", params.n_pca_batch),
  2807. [](common_params & params, int value) {
  2808. params.n_pca_batch = value;
  2809. }
  2810. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  2811. add_opt(common_arg(
  2812. {"--pca-iter"}, "N",
  2813. string_format("number of iterations used for PCA (default: %d)", params.n_pca_iterations),
  2814. [](common_params & params, int value) {
  2815. params.n_pca_iterations = value;
  2816. }
  2817. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  2818. add_opt(common_arg(
  2819. {"--method"}, "{pca, mean}",
  2820. "dimensionality reduction method to be used (default: pca)",
  2821. [](common_params & params, const std::string & value) {
  2822. /**/ if (value == "pca") { params.cvector_dimre_method = DIMRE_METHOD_PCA; }
  2823. else if (value == "mean") { params.cvector_dimre_method = DIMRE_METHOD_MEAN; }
  2824. else { throw std::invalid_argument("invalid value"); }
  2825. }
  2826. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  2827. add_opt(common_arg(
  2828. {"--output-format"}, "{md,jsonl}",
  2829. "output format for batched-bench results (default: md)",
  2830. [](common_params & params, const std::string & value) {
  2831. /**/ if (value == "jsonl") { params.batched_bench_output_jsonl = true; }
  2832. else if (value == "md") { params.batched_bench_output_jsonl = false; }
  2833. else { throw std::invalid_argument("invalid value"); }
  2834. }
  2835. ).set_examples({LLAMA_EXAMPLE_BENCH}));
  2836. add_opt(common_arg(
  2837. {"--log-disable"},
  2838. "Log disable",
  2839. [](common_params &) {
  2840. common_log_pause(common_log_main());
  2841. }
  2842. ));
  2843. add_opt(common_arg(
  2844. {"--log-file"}, "FNAME",
  2845. "Log to file",
  2846. [](common_params &, const std::string & value) {
  2847. common_log_set_file(common_log_main(), value.c_str());
  2848. }
  2849. ).set_env("LLAMA_LOG_FILE"));
  2850. add_opt(common_arg(
  2851. {"--log-colors"}, "[on|off|auto]",
  2852. "Set colored logging ('on', 'off', or 'auto', default: 'auto')\n"
  2853. "'auto' enables colors when output is to a terminal",
  2854. [](common_params &, const std::string & value) {
  2855. if (is_truthy(value)) {
  2856. common_log_set_colors(common_log_main(), LOG_COLORS_ENABLED);
  2857. } else if (is_falsey(value)) {
  2858. common_log_set_colors(common_log_main(), LOG_COLORS_DISABLED);
  2859. } else if (is_autoy(value)) {
  2860. common_log_set_colors(common_log_main(), LOG_COLORS_AUTO);
  2861. } else {
  2862. throw std::invalid_argument(
  2863. string_format("error: unknown value for --log-colors: '%s'\n", value.c_str()));
  2864. }
  2865. }
  2866. ).set_env("LLAMA_LOG_COLORS"));
  2867. add_opt(common_arg(
  2868. {"-v", "--verbose", "--log-verbose"},
  2869. "Set verbosity level to infinity (i.e. log all messages, useful for debugging)",
  2870. [](common_params & params) {
  2871. params.verbosity = INT_MAX;
  2872. }
  2873. ));
  2874. add_opt(common_arg(
  2875. {"--offline"},
  2876. "Offline mode: forces use of cache, prevents network access",
  2877. [](common_params & params) {
  2878. params.offline = true;
  2879. }
  2880. ).set_env("LLAMA_OFFLINE"));
  2881. add_opt(common_arg(
  2882. {"-lv", "--verbosity", "--log-verbosity"}, "N",
  2883. string_format("Set the verbosity threshold. Messages with a higher verbosity will be ignored. Values:\n"
  2884. " - 0: generic output\n"
  2885. " - 1: error\n"
  2886. " - 2: warning\n"
  2887. " - 3: info\n"
  2888. " - 4: debug\n"
  2889. "(default: %d)\n", params.verbosity),
  2890. [](common_params & params, int value) {
  2891. params.verbosity = value;
  2892. }
  2893. ).set_env("LLAMA_LOG_VERBOSITY"));
  2894. add_opt(common_arg(
  2895. {"--log-prefix"},
  2896. "Enable prefix in log messages",
  2897. [](common_params &) {
  2898. common_log_set_prefix(common_log_main(), true);
  2899. }
  2900. ).set_env("LLAMA_LOG_PREFIX"));
  2901. add_opt(common_arg(
  2902. {"--log-timestamps"},
  2903. "Enable timestamps in log messages",
  2904. [](common_params &) {
  2905. common_log_set_timestamps(common_log_main(), true);
  2906. }
  2907. ).set_env("LLAMA_LOG_TIMESTAMPS"));
  2908. // speculative parameters
  2909. add_opt(common_arg(
  2910. {"-td", "--threads-draft"}, "N",
  2911. "number of threads to use during generation (default: same as --threads)",
  2912. [](common_params & params, int value) {
  2913. params.speculative.cpuparams.n_threads = value;
  2914. if (params.speculative.cpuparams.n_threads <= 0) {
  2915. params.speculative.cpuparams.n_threads = std::thread::hardware_concurrency();
  2916. }
  2917. }
  2918. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}));
  2919. add_opt(common_arg(
  2920. {"-tbd", "--threads-batch-draft"}, "N",
  2921. "number of threads to use during batch and prompt processing (default: same as --threads-draft)",
  2922. [](common_params & params, int value) {
  2923. params.speculative.cpuparams_batch.n_threads = value;
  2924. if (params.speculative.cpuparams_batch.n_threads <= 0) {
  2925. params.speculative.cpuparams_batch.n_threads = std::thread::hardware_concurrency();
  2926. }
  2927. }
  2928. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}));
  2929. add_opt(common_arg(
  2930. {"-Cd", "--cpu-mask-draft"}, "M",
  2931. "Draft model CPU affinity mask. Complements cpu-range-draft (default: same as --cpu-mask)",
  2932. [](common_params & params, const std::string & mask) {
  2933. params.speculative.cpuparams.mask_valid = true;
  2934. if (!parse_cpu_mask(mask, params.speculative.cpuparams.cpumask)) {
  2935. throw std::invalid_argument("invalid cpumask");
  2936. }
  2937. }
  2938. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2939. add_opt(common_arg(
  2940. {"-Crd", "--cpu-range-draft"}, "lo-hi",
  2941. "Ranges of CPUs for affinity. Complements --cpu-mask-draft",
  2942. [](common_params & params, const std::string & range) {
  2943. params.speculative.cpuparams.mask_valid = true;
  2944. if (!parse_cpu_range(range, params.speculative.cpuparams.cpumask)) {
  2945. throw std::invalid_argument("invalid range");
  2946. }
  2947. }
  2948. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2949. add_opt(common_arg(
  2950. {"--cpu-strict-draft"}, "<0|1>",
  2951. "Use strict CPU placement for draft model (default: same as --cpu-strict)",
  2952. [](common_params & params, int value) {
  2953. params.speculative.cpuparams.strict_cpu = value;
  2954. }
  2955. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2956. add_opt(common_arg(
  2957. {"--prio-draft"}, "N",
  2958. string_format("set draft process/thread priority : 0-normal, 1-medium, 2-high, 3-realtime (default: %d)\n", params.speculative.cpuparams.priority),
  2959. [](common_params & params, int prio) {
  2960. if (prio < 0 || prio > 3) {
  2961. throw std::invalid_argument("invalid value");
  2962. }
  2963. params.speculative.cpuparams.priority = (enum ggml_sched_priority) prio;
  2964. }
  2965. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2966. add_opt(common_arg(
  2967. {"--poll-draft"}, "<0|1>",
  2968. "Use polling to wait for draft model work (default: same as --poll])",
  2969. [](common_params & params, int value) {
  2970. params.speculative.cpuparams.poll = value;
  2971. }
  2972. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2973. add_opt(common_arg(
  2974. {"-Cbd", "--cpu-mask-batch-draft"}, "M",
  2975. "Draft model CPU affinity mask. Complements cpu-range-draft (default: same as --cpu-mask)",
  2976. [](common_params & params, const std::string & mask) {
  2977. params.speculative.cpuparams_batch.mask_valid = true;
  2978. if (!parse_cpu_mask(mask, params.speculative.cpuparams_batch.cpumask)) {
  2979. throw std::invalid_argument("invalid cpumask");
  2980. }
  2981. }
  2982. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2983. add_opt(common_arg(
  2984. {"-Crbd", "--cpu-range-batch-draft"}, "lo-hi",
  2985. "Ranges of CPUs for affinity. Complements --cpu-mask-draft-batch)",
  2986. [](common_params & params, const std::string & range) {
  2987. params.speculative.cpuparams_batch.mask_valid = true;
  2988. if (!parse_cpu_range(range, params.speculative.cpuparams_batch.cpumask)) {
  2989. throw std::invalid_argument("invalid cpumask");
  2990. }
  2991. }
  2992. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2993. add_opt(common_arg(
  2994. {"--cpu-strict-batch-draft"}, "<0|1>",
  2995. "Use strict CPU placement for draft model (default: --cpu-strict-draft)",
  2996. [](common_params & params, int value) {
  2997. params.speculative.cpuparams_batch.strict_cpu = value;
  2998. }
  2999. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  3000. add_opt(common_arg(
  3001. {"--prio-batch-draft"}, "N",
  3002. string_format("set draft process/thread priority : 0-normal, 1-medium, 2-high, 3-realtime (default: %d)\n", params.speculative.cpuparams_batch.priority),
  3003. [](common_params & params, int prio) {
  3004. if (prio < 0 || prio > 3) {
  3005. throw std::invalid_argument("invalid value");
  3006. }
  3007. params.speculative.cpuparams_batch.priority = (enum ggml_sched_priority) prio;
  3008. }
  3009. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  3010. add_opt(common_arg(
  3011. {"--poll-batch-draft"}, "<0|1>",
  3012. "Use polling to wait for draft model work (default: --poll-draft)",
  3013. [](common_params & params, int value) {
  3014. params.speculative.cpuparams_batch.poll = value;
  3015. }
  3016. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  3017. add_opt(common_arg(
  3018. {"--draft", "--draft-n", "--draft-max"}, "N",
  3019. string_format("number of tokens to draft for speculative decoding (default: %d)", params.speculative.n_max),
  3020. [](common_params & params, int value) {
  3021. params.speculative.n_max = value;
  3022. }
  3023. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_LOOKUP, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_DRAFT_MAX"));
  3024. add_opt(common_arg(
  3025. {"--draft-min", "--draft-n-min"}, "N",
  3026. string_format("minimum number of draft tokens to use for speculative decoding (default: %d)", params.speculative.n_min),
  3027. [](common_params & params, int value) {
  3028. params.speculative.n_min = value;
  3029. }
  3030. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_LOOKUP, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_DRAFT_MIN"));
  3031. add_opt(common_arg(
  3032. {"--draft-p-split"}, "P",
  3033. string_format("speculative decoding split probability (default: %.1f)", (double)params.speculative.p_split),
  3034. [](common_params & params, const std::string & value) {
  3035. params.speculative.p_split = std::stof(value);
  3036. }
  3037. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}).set_env("LLAMA_ARG_DRAFT_P_SPLIT"));
  3038. add_opt(common_arg(
  3039. {"--draft-p-min"}, "P",
  3040. string_format("minimum speculative decoding probability (greedy) (default: %.1f)", (double)params.speculative.p_min),
  3041. [](common_params & params, const std::string & value) {
  3042. params.speculative.p_min = std::stof(value);
  3043. }
  3044. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_DRAFT_P_MIN"));
  3045. add_opt(common_arg(
  3046. {"-cd", "--ctx-size-draft"}, "N",
  3047. string_format("size of the prompt context for the draft model (default: %d, 0 = loaded from model)", params.speculative.n_ctx),
  3048. [](common_params & params, int value) {
  3049. params.speculative.n_ctx = value;
  3050. }
  3051. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_CTX_SIZE_DRAFT"));
  3052. add_opt(common_arg(
  3053. {"-devd", "--device-draft"}, "<dev1,dev2,..>",
  3054. "comma-separated list of devices to use for offloading the draft model (none = don't offload)\n"
  3055. "use --list-devices to see a list of available devices",
  3056. [](common_params & params, const std::string & value) {
  3057. params.speculative.devices = parse_device_list(value);
  3058. }
  3059. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}));
  3060. add_opt(common_arg(
  3061. {"-ngld", "--gpu-layers-draft", "--n-gpu-layers-draft"}, "N",
  3062. "number of layers to store in VRAM for the draft model",
  3063. [](common_params & params, int value) {
  3064. params.speculative.n_gpu_layers = value;
  3065. if (!llama_supports_gpu_offload()) {
  3066. fprintf(stderr, "warning: no usable GPU found, --gpu-layers-draft option will be ignored\n");
  3067. fprintf(stderr, "warning: one possible reason is that llama.cpp was compiled without GPU support\n");
  3068. fprintf(stderr, "warning: consult docs/build.md for compilation instructions\n");
  3069. }
  3070. }
  3071. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_N_GPU_LAYERS_DRAFT"));
  3072. add_opt(common_arg(
  3073. {"-md", "--model-draft"}, "FNAME",
  3074. "draft model for speculative decoding (default: unused)",
  3075. [](common_params & params, const std::string & value) {
  3076. params.speculative.model.path = value;
  3077. }
  3078. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_MODEL_DRAFT"));
  3079. add_opt(common_arg(
  3080. {"--spec-replace"}, "TARGET", "DRAFT",
  3081. "translate the string in TARGET into DRAFT if the draft model and main model are not compatible",
  3082. [](common_params & params, const std::string & tgt, const std::string & dft) {
  3083. params.speculative.replacements.push_back({ tgt, dft });
  3084. }
  3085. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}));
  3086. add_opt(common_arg(
  3087. {"-ctkd", "--cache-type-k-draft"}, "TYPE",
  3088. string_format(
  3089. "KV cache data type for K for the draft model\n"
  3090. "allowed values: %s\n"
  3091. "(default: %s)",
  3092. get_all_kv_cache_types().c_str(),
  3093. ggml_type_name(params.speculative.cache_type_k)
  3094. ),
  3095. [](common_params & params, const std::string & value) {
  3096. params.speculative.cache_type_k = kv_cache_type_from_str(value);
  3097. }
  3098. ).set_env("LLAMA_ARG_CACHE_TYPE_K_DRAFT"));
  3099. add_opt(common_arg(
  3100. {"-ctvd", "--cache-type-v-draft"}, "TYPE",
  3101. string_format(
  3102. "KV cache data type for V for the draft model\n"
  3103. "allowed values: %s\n"
  3104. "(default: %s)",
  3105. get_all_kv_cache_types().c_str(),
  3106. ggml_type_name(params.speculative.cache_type_v)
  3107. ),
  3108. [](common_params & params, const std::string & value) {
  3109. params.speculative.cache_type_v = kv_cache_type_from_str(value);
  3110. }
  3111. ).set_env("LLAMA_ARG_CACHE_TYPE_V_DRAFT"));
  3112. add_opt(common_arg(
  3113. {"-mv", "--model-vocoder"}, "FNAME",
  3114. "vocoder model for audio generation (default: unused)",
  3115. [](common_params & params, const std::string & value) {
  3116. params.vocoder.model.path = value;
  3117. }
  3118. ).set_examples({LLAMA_EXAMPLE_TTS, LLAMA_EXAMPLE_SERVER}));
  3119. add_opt(common_arg(
  3120. {"--tts-use-guide-tokens"},
  3121. "Use guide tokens to improve TTS word recall",
  3122. [](common_params & params) {
  3123. params.vocoder.use_guide_tokens = true;
  3124. }
  3125. ).set_examples({LLAMA_EXAMPLE_TTS, LLAMA_EXAMPLE_SERVER}));
  3126. add_opt(common_arg(
  3127. {"--tts-speaker-file"}, "FNAME",
  3128. "speaker file path for audio generation",
  3129. [](common_params & params, const std::string & value) {
  3130. params.vocoder.speaker_file = value;
  3131. }
  3132. ).set_examples({LLAMA_EXAMPLE_TTS}));
  3133. add_opt(common_arg(
  3134. {"--diffusion-steps"}, "N",
  3135. string_format("number of diffusion steps (default: %d)", params.diffusion.steps),
  3136. [](common_params & params, int value) { params.diffusion.steps = value; }
  3137. ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
  3138. add_opt(common_arg(
  3139. {"--diffusion-visual"},
  3140. string_format("enable visual diffusion mode (show progressive generation) (default: %s)", params.diffusion.visual_mode ? "true" : "false"),
  3141. [](common_params & params) { params.diffusion.visual_mode = true; }
  3142. ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
  3143. add_opt(common_arg(
  3144. {"--diffusion-eps"}, "F",
  3145. string_format("epsilon for timesteps (default: %.6f)", (double) params.diffusion.eps),
  3146. [](common_params & params, const std::string & value) { params.diffusion.eps = std::stof(value); }
  3147. ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
  3148. add_opt(common_arg(
  3149. {"--diffusion-algorithm"}, "N",
  3150. string_format("diffusion algorithm: 0=ORIGIN, 1=ENTROPY_BASED, 2=MARGIN_BASED, 3=RANDOM, 4=LOW_CONFIDENCE (default: %d)", params.diffusion.algorithm),
  3151. [](common_params & params, int value) { params.diffusion.algorithm = value; }
  3152. ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
  3153. add_opt(common_arg(
  3154. {"--diffusion-alg-temp"}, "F",
  3155. string_format("dream algorithm temperature (default: %.3f)", (double) params.diffusion.alg_temp),
  3156. [](common_params & params, const std::string & value) { params.diffusion.alg_temp = std::stof(value); }
  3157. ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
  3158. add_opt(common_arg(
  3159. {"--diffusion-block-length"}, "N",
  3160. string_format("llada block length for generation (default: %d)", params.diffusion.block_length),
  3161. [](common_params & params, int value) { params.diffusion.block_length = value; }
  3162. ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
  3163. add_opt(common_arg(
  3164. {"--diffusion-cfg-scale"}, "F",
  3165. string_format("llada classifier-free guidance scale (default: %.3f)", (double) params.diffusion.cfg_scale),
  3166. [](common_params & params, const std::string & value) { params.diffusion.cfg_scale = std::stof(value); }
  3167. ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
  3168. add_opt(common_arg(
  3169. {"--diffusion-add-gumbel-noise"}, "F",
  3170. string_format("add gumbel noise to the logits if temp > 0.0 (default: %s)", params.diffusion.add_gumbel_noise ? "true" : "false"),
  3171. [](common_params & params, const std::string & value) { params.diffusion.add_gumbel_noise = std::stof(value); }
  3172. ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
  3173. add_opt(common_arg(
  3174. { "-lr", "--learning-rate" }, "ALPHA",
  3175. string_format("adamw or sgd optimizer alpha (default: %.2g); note: sgd alpha recommended ~10x (no momentum)", (double) params.lr.lr0),
  3176. [](common_params & params, const std::string & value) { params.lr.lr0 = std::stof(value); }
  3177. ).set_examples({ LLAMA_EXAMPLE_FINETUNE }));
  3178. add_opt(common_arg({ "-lr-min", "--learning-rate-min" }, "ALPHA",
  3179. string_format("(if >0) final learning rate after decay (if -decay-epochs is set, default=%.2g)",
  3180. (double) params.lr.lr_min),
  3181. [](common_params & params, const std::string & value) { params.lr.lr_min = std::stof(value); }
  3182. ).set_examples({ LLAMA_EXAMPLE_FINETUNE }));
  3183. add_opt(common_arg(
  3184. {"-decay-epochs", "--learning-rate-decay-epochs"}, "ALPHA",
  3185. string_format("(if >0) decay learning rate to -lr-min after this many epochs (exponential decay, default=%.2g)", (double) params.lr.decay_epochs),
  3186. [](common_params & params, const std::string & value) { params.lr.decay_epochs = std::stof(value); }
  3187. ).set_examples({ LLAMA_EXAMPLE_FINETUNE }));
  3188. add_opt(common_arg(
  3189. {"-wd", "--weight-decay"}, "WD",
  3190. string_format("adamw or sgd optimizer weight decay (0 is off; recommend very small e.g. 1e-9) (default: %.2g).", (double) params.lr.wd),
  3191. [](common_params & params, const std::string & value) { params.lr.wd = std::stof(value); }
  3192. ).set_examples({ LLAMA_EXAMPLE_FINETUNE }));
  3193. add_opt(common_arg(
  3194. {"-val-split", "--val-split"}, "FRACTION",
  3195. string_format("fraction of data to use as validation set for training (default: %.2g).", (double) params.val_split),
  3196. [](common_params & params, const std::string & value) { params.val_split = std::stof(value); }
  3197. ).set_examples({ LLAMA_EXAMPLE_FINETUNE }));
  3198. add_opt(common_arg(
  3199. {"-epochs", "--epochs"}, "N",
  3200. string_format("optimizer max # of epochs (default: %d)", params.lr.epochs),
  3201. [](common_params & params, int epochs) { params.lr.epochs = epochs; }
  3202. ).set_examples({ LLAMA_EXAMPLE_FINETUNE }));
  3203. add_opt(common_arg(
  3204. {"-opt", "--optimizer"}, "sgd|adamw", "adamw or sgd",
  3205. [](common_params & params, const std::string & name) {
  3206. params.optimizer = common_opt_get_optimizer(name.c_str());
  3207. if (params.optimizer == GGML_OPT_OPTIMIZER_TYPE_COUNT) {
  3208. throw std::invalid_argument("invalid --optimizer, valid options: adamw, sgd");
  3209. }
  3210. }
  3211. ).set_examples({ LLAMA_EXAMPLE_FINETUNE }));
  3212. // presets
  3213. add_opt(common_arg(
  3214. {"--tts-oute-default"},
  3215. string_format("use default OuteTTS models (note: can download weights from the internet)"),
  3216. [](common_params & params) {
  3217. params.model.hf_repo = "OuteAI/OuteTTS-0.2-500M-GGUF";
  3218. params.model.hf_file = "OuteTTS-0.2-500M-Q8_0.gguf";
  3219. params.vocoder.model.hf_repo = "ggml-org/WavTokenizer";
  3220. params.vocoder.model.hf_file = "WavTokenizer-Large-75-F16.gguf";
  3221. }
  3222. ).set_examples({LLAMA_EXAMPLE_TTS}));
  3223. add_opt(common_arg(
  3224. {"--embd-gemma-default"},
  3225. string_format("use default EmbeddingGemma model (note: can download weights from the internet)"),
  3226. [](common_params & params) {
  3227. params.model.hf_repo = "ggml-org/embeddinggemma-300M-qat-q4_0-GGUF";
  3228. params.model.hf_file = "embeddinggemma-300M-qat-Q4_0.gguf";
  3229. params.port = 8011;
  3230. params.n_ubatch = 2048;
  3231. params.n_batch = 2048;
  3232. params.n_parallel = 32;
  3233. params.n_ctx = 2048*params.n_parallel;
  3234. params.verbose_prompt = true;
  3235. params.embedding = true;
  3236. }
  3237. ).set_examples({LLAMA_EXAMPLE_EMBEDDING, LLAMA_EXAMPLE_SERVER}));
  3238. add_opt(common_arg(
  3239. {"--fim-qwen-1.5b-default"},
  3240. string_format("use default Qwen 2.5 Coder 1.5B (note: can download weights from the internet)"),
  3241. [](common_params & params) {
  3242. params.model.hf_repo = "ggml-org/Qwen2.5-Coder-1.5B-Q8_0-GGUF";
  3243. params.model.hf_file = "qwen2.5-coder-1.5b-q8_0.gguf";
  3244. params.port = 8012;
  3245. params.n_ubatch = 1024;
  3246. params.n_batch = 1024;
  3247. params.n_ctx = 0;
  3248. params.n_cache_reuse = 256;
  3249. }
  3250. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  3251. add_opt(common_arg(
  3252. {"--fim-qwen-3b-default"},
  3253. string_format("use default Qwen 2.5 Coder 3B (note: can download weights from the internet)"),
  3254. [](common_params & params) {
  3255. params.model.hf_repo = "ggml-org/Qwen2.5-Coder-3B-Q8_0-GGUF";
  3256. params.model.hf_file = "qwen2.5-coder-3b-q8_0.gguf";
  3257. params.port = 8012;
  3258. params.n_ubatch = 1024;
  3259. params.n_batch = 1024;
  3260. params.n_ctx = 0;
  3261. params.n_cache_reuse = 256;
  3262. }
  3263. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  3264. add_opt(common_arg(
  3265. {"--fim-qwen-7b-default"},
  3266. string_format("use default Qwen 2.5 Coder 7B (note: can download weights from the internet)"),
  3267. [](common_params & params) {
  3268. params.model.hf_repo = "ggml-org/Qwen2.5-Coder-7B-Q8_0-GGUF";
  3269. params.model.hf_file = "qwen2.5-coder-7b-q8_0.gguf";
  3270. params.port = 8012;
  3271. params.n_ubatch = 1024;
  3272. params.n_batch = 1024;
  3273. params.n_ctx = 0;
  3274. params.n_cache_reuse = 256;
  3275. }
  3276. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  3277. add_opt(common_arg(
  3278. {"--fim-qwen-7b-spec"},
  3279. string_format("use Qwen 2.5 Coder 7B + 0.5B draft for speculative decoding (note: can download weights from the internet)"),
  3280. [](common_params & params) {
  3281. params.model.hf_repo = "ggml-org/Qwen2.5-Coder-7B-Q8_0-GGUF";
  3282. params.model.hf_file = "qwen2.5-coder-7b-q8_0.gguf";
  3283. params.speculative.model.hf_repo = "ggml-org/Qwen2.5-Coder-0.5B-Q8_0-GGUF";
  3284. params.speculative.model.hf_file = "qwen2.5-coder-0.5b-q8_0.gguf";
  3285. params.port = 8012;
  3286. params.n_ubatch = 1024;
  3287. params.n_batch = 1024;
  3288. params.n_ctx = 0;
  3289. params.n_cache_reuse = 256;
  3290. }
  3291. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  3292. add_opt(common_arg(
  3293. {"--fim-qwen-14b-spec"},
  3294. string_format("use Qwen 2.5 Coder 14B + 0.5B draft for speculative decoding (note: can download weights from the internet)"),
  3295. [](common_params & params) {
  3296. params.model.hf_repo = "ggml-org/Qwen2.5-Coder-14B-Q8_0-GGUF";
  3297. params.model.hf_file = "qwen2.5-coder-14b-q8_0.gguf";
  3298. params.speculative.model.hf_repo = "ggml-org/Qwen2.5-Coder-0.5B-Q8_0-GGUF";
  3299. params.speculative.model.hf_file = "qwen2.5-coder-0.5b-q8_0.gguf";
  3300. params.port = 8012;
  3301. params.n_ubatch = 1024;
  3302. params.n_batch = 1024;
  3303. params.n_ctx = 0;
  3304. params.n_cache_reuse = 256;
  3305. }
  3306. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  3307. add_opt(common_arg(
  3308. {"--fim-qwen-30b-default"},
  3309. string_format("use default Qwen 3 Coder 30B A3B Instruct (note: can download weights from the internet)"),
  3310. [](common_params & params) {
  3311. params.model.hf_repo = "ggml-org/Qwen3-Coder-30B-A3B-Instruct-Q8_0-GGUF";
  3312. params.model.hf_file = "qwen3-coder-30b-a3b-instruct-q8_0.gguf";
  3313. params.port = 8012;
  3314. params.n_ubatch = 1024;
  3315. params.n_batch = 1024;
  3316. params.n_ctx = 0;
  3317. params.n_cache_reuse = 256;
  3318. }
  3319. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  3320. add_opt(common_arg(
  3321. {"--gpt-oss-20b-default"},
  3322. string_format("use gpt-oss-20b (note: can download weights from the internet)"),
  3323. [](common_params & params) {
  3324. params.model.hf_repo = "ggml-org/gpt-oss-20b-GGUF";
  3325. params.model.hf_file = "gpt-oss-20b-mxfp4.gguf";
  3326. params.port = 8013;
  3327. params.n_ubatch = 2048;
  3328. params.n_batch = 32768;
  3329. params.n_parallel = 2;
  3330. params.n_ctx = 131072*params.n_parallel;
  3331. params.sampling.temp = 1.0f;
  3332. params.sampling.top_p = 1.0f;
  3333. params.sampling.top_k = 0;
  3334. params.sampling.min_p = 0.01f;
  3335. params.use_jinja = true;
  3336. //params.default_template_kwargs["reasoning_effort"] = "\"high\"";
  3337. }
  3338. ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}));
  3339. add_opt(common_arg(
  3340. {"--gpt-oss-120b-default"},
  3341. string_format("use gpt-oss-120b (note: can download weights from the internet)"),
  3342. [](common_params & params) {
  3343. params.model.hf_repo = "ggml-org/gpt-oss-120b-GGUF";
  3344. params.port = 8013;
  3345. params.n_ubatch = 2048;
  3346. params.n_batch = 32768;
  3347. params.n_parallel = 2;
  3348. params.n_ctx = 131072*params.n_parallel;
  3349. params.sampling.temp = 1.0f;
  3350. params.sampling.top_p = 1.0f;
  3351. params.sampling.top_k = 0;
  3352. params.sampling.min_p = 0.01f;
  3353. params.use_jinja = true;
  3354. //params.default_template_kwargs["reasoning_effort"] = "\"high\"";
  3355. }
  3356. ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}));
  3357. add_opt(common_arg(
  3358. {"--vision-gemma-4b-default"},
  3359. string_format("use Gemma 3 4B QAT (note: can download weights from the internet)"),
  3360. [](common_params & params) {
  3361. params.model.hf_repo = "ggml-org/gemma-3-4b-it-qat-GGUF";
  3362. params.port = 8014;
  3363. params.n_ctx = 0;
  3364. params.use_jinja = true;
  3365. }
  3366. ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}));
  3367. add_opt(common_arg(
  3368. {"--vision-gemma-12b-default"},
  3369. string_format("use Gemma 3 12B QAT (note: can download weights from the internet)"),
  3370. [](common_params & params) {
  3371. params.model.hf_repo = "ggml-org/gemma-3-12b-it-qat-GGUF";
  3372. params.port = 8014;
  3373. params.n_ctx = 0;
  3374. params.use_jinja = true;
  3375. }
  3376. ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}));
  3377. return ctx_arg;
  3378. }
  3379. void common_params_add_preset_options(std::vector<common_arg> & args) {
  3380. // arguments below won't be treated as CLI args, only preset options
  3381. args.push_back(common_arg(
  3382. {"load-on-startup"}, "NAME",
  3383. "in server router mode, autoload this model on startup",
  3384. [](common_params &, const std::string &) { /* unused */ }
  3385. ).set_env(COMMON_ARG_PRESET_LOAD_ON_STARTUP).set_preset_only());
  3386. // args.push_back(common_arg(
  3387. // {"pin"},
  3388. // "in server router mode, do not unload this model if models_max is exceeded",
  3389. // [](common_params &) { /* unused */ }
  3390. // ).set_preset_only());
  3391. // args.push_back(common_arg(
  3392. // {"unload-idle-seconds"}, "SECONDS",
  3393. // "in server router mode, unload models idle for more than this many seconds",
  3394. // [](common_params &, int) { /* unused */ }
  3395. // ).set_preset_only());
  3396. }