arg.cpp 165 KB

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