arg.cpp 164 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724
  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); // built without CURL, 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. {"--dynatemp-range"}, "N",
  1614. string_format("dynamic temperature range (default: %.1f, 0.0 = disabled)", (double)params.sampling.dynatemp_range),
  1615. [](common_params & params, const std::string & value) {
  1616. params.sampling.dynatemp_range = std::stof(value);
  1617. }
  1618. ).set_sparam());
  1619. add_opt(common_arg(
  1620. {"--dynatemp-exp"}, "N",
  1621. string_format("dynamic temperature exponent (default: %.1f)", (double)params.sampling.dynatemp_exponent),
  1622. [](common_params & params, const std::string & value) {
  1623. params.sampling.dynatemp_exponent = std::stof(value);
  1624. }
  1625. ).set_sparam());
  1626. add_opt(common_arg(
  1627. {"--mirostat"}, "N",
  1628. string_format("use Mirostat sampling.\nTop K, Nucleus and Locally Typical samplers are ignored if used.\n"
  1629. "(default: %d, 0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0)", params.sampling.mirostat),
  1630. [](common_params & params, int value) {
  1631. params.sampling.mirostat = value;
  1632. params.sampling.user_sampling_config |= common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_MIROSTAT;
  1633. }
  1634. ).set_sparam());
  1635. add_opt(common_arg(
  1636. {"--mirostat-lr"}, "N",
  1637. string_format("Mirostat learning rate, parameter eta (default: %.1f)", (double)params.sampling.mirostat_eta),
  1638. [](common_params & params, const std::string & value) {
  1639. params.sampling.mirostat_eta = std::stof(value);
  1640. params.sampling.user_sampling_config |= common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_MIROSTAT_ETA;
  1641. }
  1642. ).set_sparam());
  1643. add_opt(common_arg(
  1644. {"--mirostat-ent"}, "N",
  1645. string_format("Mirostat target entropy, parameter tau (default: %.1f)", (double)params.sampling.mirostat_tau),
  1646. [](common_params & params, const std::string & value) {
  1647. params.sampling.mirostat_tau = std::stof(value);
  1648. params.sampling.user_sampling_config |= common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_MIROSTAT_TAU;
  1649. }
  1650. ).set_sparam());
  1651. add_opt(common_arg(
  1652. {"-l", "--logit-bias"}, "TOKEN_ID(+/-)BIAS",
  1653. "modifies the likelihood of token appearing in the completion,\n"
  1654. "i.e. `--logit-bias 15043+1` to increase likelihood of token ' Hello',\n"
  1655. "or `--logit-bias 15043-1` to decrease likelihood of token ' Hello'",
  1656. [](common_params & params, const std::string & value) {
  1657. std::stringstream ss(value);
  1658. llama_token key;
  1659. char sign;
  1660. std::string value_str;
  1661. try {
  1662. if (ss >> key && ss >> sign && std::getline(ss, value_str) && (sign == '+' || sign == '-')) {
  1663. const float bias = std::stof(value_str) * ((sign == '-') ? -1.0f : 1.0f);
  1664. params.sampling.logit_bias.push_back({key, bias});
  1665. } else {
  1666. throw std::invalid_argument("invalid input format");
  1667. }
  1668. } catch (const std::exception&) {
  1669. throw std::invalid_argument("invalid input format");
  1670. }
  1671. }
  1672. ).set_sparam());
  1673. add_opt(common_arg(
  1674. {"--grammar"}, "GRAMMAR",
  1675. string_format("BNF-like grammar to constrain generations (see samples in grammars/ dir) (default: '%s')", params.sampling.grammar.c_str()),
  1676. [](common_params & params, const std::string & value) {
  1677. params.sampling.grammar = value;
  1678. }
  1679. ).set_sparam());
  1680. add_opt(common_arg(
  1681. {"--grammar-file"}, "FNAME",
  1682. "file to read grammar from",
  1683. [](common_params & params, const std::string & value) {
  1684. params.sampling.grammar = read_file(value);
  1685. }
  1686. ).set_sparam());
  1687. add_opt(common_arg(
  1688. {"-j", "--json-schema"}, "SCHEMA",
  1689. "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",
  1690. [](common_params & params, const std::string & value) {
  1691. params.sampling.grammar = json_schema_to_grammar(json::parse(value));
  1692. }
  1693. ).set_sparam());
  1694. add_opt(common_arg(
  1695. {"-jf", "--json-schema-file"}, "FILE",
  1696. "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",
  1697. [](common_params & params, const std::string & value) {
  1698. std::ifstream file(value);
  1699. if (!file) {
  1700. throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
  1701. }
  1702. std::string schema;
  1703. std::copy(
  1704. std::istreambuf_iterator<char>(file),
  1705. std::istreambuf_iterator<char>(),
  1706. std::back_inserter(schema)
  1707. );
  1708. params.sampling.grammar = json_schema_to_grammar(json::parse(schema));
  1709. }
  1710. ).set_sparam());
  1711. add_opt(common_arg(
  1712. {"-bs", "--backend-sampling"},
  1713. "enable backend sampling (experimental) (default: disabled)",
  1714. [](common_params & params) {
  1715. params.sampling.backend_sampling = true;
  1716. }
  1717. ).set_sparam().set_env("LLAMA_ARG_BACKEND_SAMPLING"));
  1718. add_opt(common_arg(
  1719. {"--pooling"}, "{none,mean,cls,last,rank}",
  1720. "pooling type for embeddings, use model default if unspecified",
  1721. [](common_params & params, const std::string & value) {
  1722. /**/ if (value == "none") { params.pooling_type = LLAMA_POOLING_TYPE_NONE; }
  1723. else if (value == "mean") { params.pooling_type = LLAMA_POOLING_TYPE_MEAN; }
  1724. else if (value == "cls") { params.pooling_type = LLAMA_POOLING_TYPE_CLS; }
  1725. else if (value == "last") { params.pooling_type = LLAMA_POOLING_TYPE_LAST; }
  1726. else if (value == "rank") { params.pooling_type = LLAMA_POOLING_TYPE_RANK; }
  1727. else { throw std::invalid_argument("invalid value"); }
  1728. }
  1729. ).set_examples({LLAMA_EXAMPLE_EMBEDDING, LLAMA_EXAMPLE_RETRIEVAL, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_DEBUG}).set_env("LLAMA_ARG_POOLING"));
  1730. add_opt(common_arg(
  1731. {"--attention"}, "{causal,non-causal}",
  1732. "attention type for embeddings, use model default if unspecified",
  1733. [](common_params & params, const std::string & value) {
  1734. /**/ if (value == "causal") { params.attention_type = LLAMA_ATTENTION_TYPE_CAUSAL; }
  1735. else if (value == "non-causal") { params.attention_type = LLAMA_ATTENTION_TYPE_NON_CAUSAL; }
  1736. else { throw std::invalid_argument("invalid value"); }
  1737. }
  1738. ).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
  1739. add_opt(common_arg(
  1740. {"--rope-scaling"}, "{none,linear,yarn}",
  1741. "RoPE frequency scaling method, defaults to linear unless specified by the model",
  1742. [](common_params & params, const std::string & value) {
  1743. /**/ if (value == "none") { params.rope_scaling_type = LLAMA_ROPE_SCALING_TYPE_NONE; }
  1744. else if (value == "linear") { params.rope_scaling_type = LLAMA_ROPE_SCALING_TYPE_LINEAR; }
  1745. else if (value == "yarn") { params.rope_scaling_type = LLAMA_ROPE_SCALING_TYPE_YARN; }
  1746. else { throw std::invalid_argument("invalid value"); }
  1747. }
  1748. ).set_env("LLAMA_ARG_ROPE_SCALING_TYPE"));
  1749. add_opt(common_arg(
  1750. {"--rope-scale"}, "N",
  1751. "RoPE context scaling factor, expands context by a factor of N",
  1752. [](common_params & params, const std::string & value) {
  1753. params.rope_freq_scale = 1.0f / std::stof(value);
  1754. }
  1755. ).set_env("LLAMA_ARG_ROPE_SCALE"));
  1756. add_opt(common_arg(
  1757. {"--rope-freq-base"}, "N",
  1758. "RoPE base frequency, used by NTK-aware scaling (default: loaded from model)",
  1759. [](common_params & params, const std::string & value) {
  1760. params.rope_freq_base = std::stof(value);
  1761. }
  1762. ).set_env("LLAMA_ARG_ROPE_FREQ_BASE"));
  1763. add_opt(common_arg(
  1764. {"--rope-freq-scale"}, "N",
  1765. "RoPE frequency scaling factor, expands context by a factor of 1/N",
  1766. [](common_params & params, const std::string & value) {
  1767. params.rope_freq_scale = std::stof(value);
  1768. }
  1769. ).set_env("LLAMA_ARG_ROPE_FREQ_SCALE"));
  1770. add_opt(common_arg(
  1771. {"--yarn-orig-ctx"}, "N",
  1772. string_format("YaRN: original context size of model (default: %d = model training context size)", params.yarn_orig_ctx),
  1773. [](common_params & params, int value) {
  1774. params.yarn_orig_ctx = value;
  1775. }
  1776. ).set_env("LLAMA_ARG_YARN_ORIG_CTX"));
  1777. add_opt(common_arg(
  1778. {"--yarn-ext-factor"}, "N",
  1779. string_format("YaRN: extrapolation mix factor (default: %.1f, 0.0 = full interpolation)", (double)params.yarn_ext_factor),
  1780. [](common_params & params, const std::string & value) {
  1781. params.yarn_ext_factor = std::stof(value);
  1782. }
  1783. ).set_env("LLAMA_ARG_YARN_EXT_FACTOR"));
  1784. add_opt(common_arg(
  1785. {"--yarn-attn-factor"}, "N",
  1786. string_format("YaRN: scale sqrt(t) or attention magnitude (default: %.1f)", (double)params.yarn_attn_factor),
  1787. [](common_params & params, const std::string & value) {
  1788. params.yarn_attn_factor = std::stof(value);
  1789. }
  1790. ).set_env("LLAMA_ARG_YARN_ATTN_FACTOR"));
  1791. add_opt(common_arg(
  1792. {"--yarn-beta-slow"}, "N",
  1793. string_format("YaRN: high correction dim or alpha (default: %.1f)", (double)params.yarn_beta_slow),
  1794. [](common_params & params, const std::string & value) {
  1795. params.yarn_beta_slow = std::stof(value);
  1796. }
  1797. ).set_env("LLAMA_ARG_YARN_BETA_SLOW"));
  1798. add_opt(common_arg(
  1799. {"--yarn-beta-fast"}, "N",
  1800. string_format("YaRN: low correction dim or beta (default: %.1f)", (double)params.yarn_beta_fast),
  1801. [](common_params & params, const std::string & value) {
  1802. params.yarn_beta_fast = std::stof(value);
  1803. }
  1804. ).set_env("LLAMA_ARG_YARN_BETA_FAST"));
  1805. add_opt(common_arg(
  1806. {"-gan", "--grp-attn-n"}, "N",
  1807. string_format("group-attention factor (default: %d)", params.grp_attn_n),
  1808. [](common_params & params, int value) {
  1809. params.grp_attn_n = value;
  1810. }
  1811. ).set_env("LLAMA_ARG_GRP_ATTN_N").set_examples({LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_PASSKEY}));
  1812. add_opt(common_arg(
  1813. {"-gaw", "--grp-attn-w"}, "N",
  1814. string_format("group-attention width (default: %d)", params.grp_attn_w),
  1815. [](common_params & params, int value) {
  1816. params.grp_attn_w = value;
  1817. }
  1818. ).set_env("LLAMA_ARG_GRP_ATTN_W").set_examples({LLAMA_EXAMPLE_COMPLETION}));
  1819. add_opt(common_arg(
  1820. {"-kvo", "--kv-offload"},
  1821. {"-nkvo", "--no-kv-offload"},
  1822. string_format("whether to enable KV cache offloading (default: %s)", params.no_kv_offload ? "disabled" : "enabled"),
  1823. [](common_params & params, bool value) {
  1824. params.no_kv_offload = !value;
  1825. }
  1826. ).set_env("LLAMA_ARG_KV_OFFLOAD"));
  1827. add_opt(common_arg(
  1828. {"--repack"},
  1829. {"-nr", "--no-repack"},
  1830. string_format("whether to enable weight repacking (default: %s)", params.no_extra_bufts ? "disabled" : "enabled"),
  1831. [](common_params & params, bool value) {
  1832. params.no_extra_bufts = !value;
  1833. }
  1834. ).set_env("LLAMA_ARG_REPACK"));
  1835. add_opt(common_arg(
  1836. {"--no-host"},
  1837. "bypass host buffer allowing extra buffers to be used",
  1838. [](common_params & params) {
  1839. params.no_host = true;
  1840. }
  1841. ).set_env("LLAMA_ARG_NO_HOST"));
  1842. add_opt(common_arg(
  1843. {"-ctk", "--cache-type-k"}, "TYPE",
  1844. string_format(
  1845. "KV cache data type for K\n"
  1846. "allowed values: %s\n"
  1847. "(default: %s)",
  1848. get_all_kv_cache_types().c_str(),
  1849. ggml_type_name(params.cache_type_k)
  1850. ),
  1851. [](common_params & params, const std::string & value) {
  1852. params.cache_type_k = kv_cache_type_from_str(value);
  1853. }
  1854. ).set_env("LLAMA_ARG_CACHE_TYPE_K"));
  1855. add_opt(common_arg(
  1856. {"-ctv", "--cache-type-v"}, "TYPE",
  1857. string_format(
  1858. "KV cache data type for V\n"
  1859. "allowed values: %s\n"
  1860. "(default: %s)",
  1861. get_all_kv_cache_types().c_str(),
  1862. ggml_type_name(params.cache_type_v)
  1863. ),
  1864. [](common_params & params, const std::string & value) {
  1865. params.cache_type_v = kv_cache_type_from_str(value);
  1866. }
  1867. ).set_env("LLAMA_ARG_CACHE_TYPE_V"));
  1868. add_opt(common_arg(
  1869. {"--hellaswag"},
  1870. "compute HellaSwag score over random tasks from datafile supplied with -f",
  1871. [](common_params & params) {
  1872. params.hellaswag = true;
  1873. }
  1874. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1875. add_opt(common_arg(
  1876. {"--hellaswag-tasks"}, "N",
  1877. string_format("number of tasks to use when computing the HellaSwag score (default: %zu)", params.hellaswag_tasks),
  1878. [](common_params & params, int value) {
  1879. params.hellaswag_tasks = value;
  1880. }
  1881. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1882. add_opt(common_arg(
  1883. {"--winogrande"},
  1884. "compute Winogrande score over random tasks from datafile supplied with -f",
  1885. [](common_params & params) {
  1886. params.winogrande = true;
  1887. }
  1888. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1889. add_opt(common_arg(
  1890. {"--winogrande-tasks"}, "N",
  1891. string_format("number of tasks to use when computing the Winogrande score (default: %zu)", params.winogrande_tasks),
  1892. [](common_params & params, int value) {
  1893. params.winogrande_tasks = value;
  1894. }
  1895. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1896. add_opt(common_arg(
  1897. {"--multiple-choice"},
  1898. "compute multiple choice score over random tasks from datafile supplied with -f",
  1899. [](common_params & params) {
  1900. params.multiple_choice = true;
  1901. }
  1902. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1903. add_opt(common_arg(
  1904. {"--multiple-choice-tasks"}, "N",
  1905. string_format("number of tasks to use when computing the multiple choice score (default: %zu)", params.multiple_choice_tasks),
  1906. [](common_params & params, int value) {
  1907. params.multiple_choice_tasks = value;
  1908. }
  1909. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1910. add_opt(common_arg(
  1911. {"--kl-divergence"},
  1912. "computes KL-divergence to logits provided via --kl-divergence-base",
  1913. [](common_params & params) {
  1914. params.kl_divergence = true;
  1915. }
  1916. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1917. add_opt(common_arg(
  1918. {"--save-all-logits", "--kl-divergence-base"}, "FNAME",
  1919. "set logits file",
  1920. [](common_params & params, const std::string & value) {
  1921. params.logits_file = value;
  1922. }
  1923. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1924. add_opt(common_arg(
  1925. {"--ppl-stride"}, "N",
  1926. string_format("stride for perplexity calculation (default: %d)", params.ppl_stride),
  1927. [](common_params & params, int value) {
  1928. params.ppl_stride = value;
  1929. }
  1930. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1931. add_opt(common_arg(
  1932. {"--ppl-output-type"}, "<0|1>",
  1933. string_format("output type for perplexity calculation (default: %d)", params.ppl_output_type),
  1934. [](common_params & params, int value) {
  1935. params.ppl_output_type = value;
  1936. }
  1937. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1938. add_opt(common_arg(
  1939. {"-dt", "--defrag-thold"}, "N",
  1940. string_format("KV cache defragmentation threshold (DEPRECATED)"),
  1941. [](common_params & params, const std::string & value) {
  1942. GGML_UNUSED(params);
  1943. GGML_UNUSED(value);
  1944. LOG_WRN("DEPRECATED: --defrag-thold is deprecated and no longer necessary to specify\n");
  1945. }
  1946. ).set_env("LLAMA_ARG_DEFRAG_THOLD"));
  1947. if (ex == LLAMA_EXAMPLE_SERVER) {
  1948. // this is to make sure this option appears in the server-specific section of the help message
  1949. add_opt(common_arg(
  1950. {"-np", "--parallel"}, "N",
  1951. string_format("number of server slots (default: %d, -1 = auto)", params.n_parallel),
  1952. [](common_params & params, int value) {
  1953. if (value == 0) {
  1954. throw std::invalid_argument("error: invalid value for n_parallel\n");
  1955. }
  1956. params.n_parallel = value;
  1957. }
  1958. ).set_env("LLAMA_ARG_N_PARALLEL").set_examples({LLAMA_EXAMPLE_SERVER}));
  1959. } else {
  1960. add_opt(common_arg(
  1961. {"-np", "--parallel"}, "N",
  1962. string_format("number of parallel sequences to decode (default: %d)", params.n_parallel),
  1963. [](common_params & params, int value) {
  1964. params.n_parallel = value;
  1965. }
  1966. ).set_env("LLAMA_ARG_N_PARALLEL"));
  1967. }
  1968. add_opt(common_arg(
  1969. {"-ns", "--sequences"}, "N",
  1970. string_format("number of sequences to decode (default: %d)", params.n_sequences),
  1971. [](common_params & params, int value) {
  1972. params.n_sequences = value;
  1973. }
  1974. ).set_examples({LLAMA_EXAMPLE_PARALLEL}));
  1975. add_opt(common_arg(
  1976. {"-cb", "--cont-batching"},
  1977. {"-nocb", "--no-cont-batching"},
  1978. string_format("whether to enable continuous batching (a.k.a dynamic batching) (default: %s)", params.cont_batching ? "enabled" : "disabled"),
  1979. [](common_params & params, bool value) {
  1980. params.cont_batching = value;
  1981. }
  1982. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CONT_BATCHING"));
  1983. add_opt(common_arg(
  1984. {"-mm", "--mmproj"}, "FILE",
  1985. "path to a multimodal projector file. see tools/mtmd/README.md\n"
  1986. "note: if -hf is used, this argument can be omitted",
  1987. [](common_params & params, const std::string & value) {
  1988. params.mmproj.path = value;
  1989. }
  1990. ).set_examples(mmproj_examples).set_env("LLAMA_ARG_MMPROJ"));
  1991. add_opt(common_arg(
  1992. {"-mmu", "--mmproj-url"}, "URL",
  1993. "URL to a multimodal projector file. see tools/mtmd/README.md",
  1994. [](common_params & params, const std::string & value) {
  1995. params.mmproj.url = value;
  1996. }
  1997. ).set_examples(mmproj_examples).set_env("LLAMA_ARG_MMPROJ_URL"));
  1998. add_opt(common_arg(
  1999. {"--mmproj-auto"},
  2000. {"--no-mmproj", "--no-mmproj-auto"},
  2001. string_format("whether to use multimodal projector file (if available), useful when using -hf (default: %s)", params.no_mmproj ? "disabled" : "enabled"),
  2002. [](common_params & params, bool value) {
  2003. params.no_mmproj = !value;
  2004. }
  2005. ).set_examples(mmproj_examples).set_env("LLAMA_ARG_MMPROJ_AUTO"));
  2006. add_opt(common_arg(
  2007. {"--mmproj-offload"},
  2008. {"--no-mmproj-offload"},
  2009. string_format("whether to enable GPU offloading for multimodal projector (default: %s)", params.mmproj_use_gpu ? "enabled" : "disabled"),
  2010. [](common_params & params, bool value) {
  2011. params.mmproj_use_gpu = value;
  2012. }
  2013. ).set_examples(mmproj_examples).set_env("LLAMA_ARG_MMPROJ_OFFLOAD"));
  2014. add_opt(common_arg(
  2015. {"--image", "--audio"}, "FILE",
  2016. "path to an image or audio file. use with multimodal models, use comma-separated values for multiple files\n",
  2017. [](common_params & params, const std::string & value) {
  2018. for (const auto & item : parse_csv_row(value)) {
  2019. params.image.emplace_back(item);
  2020. }
  2021. }
  2022. ).set_examples({LLAMA_EXAMPLE_MTMD, LLAMA_EXAMPLE_CLI}));
  2023. add_opt(common_arg(
  2024. {"--image-min-tokens"}, "N",
  2025. "minimum number of tokens each image can take, only used by vision models with dynamic resolution (default: read from model)",
  2026. [](common_params & params, int value) {
  2027. params.image_min_tokens = value;
  2028. }
  2029. ).set_examples(mmproj_examples).set_env("LLAMA_ARG_IMAGE_MIN_TOKENS"));
  2030. add_opt(common_arg(
  2031. {"--image-max-tokens"}, "N",
  2032. "maximum number of tokens each image can take, only used by vision models with dynamic resolution (default: read from model)",
  2033. [](common_params & params, int value) {
  2034. params.image_max_tokens = value;
  2035. }
  2036. ).set_examples(mmproj_examples).set_env("LLAMA_ARG_IMAGE_MAX_TOKENS"));
  2037. if (llama_supports_rpc()) {
  2038. add_opt(common_arg(
  2039. {"--rpc"}, "SERVERS",
  2040. "comma separated list of RPC servers (host:port)",
  2041. [](common_params & params, const std::string & value) {
  2042. add_rpc_devices(value);
  2043. GGML_UNUSED(params);
  2044. }
  2045. ).set_env("LLAMA_ARG_RPC"));
  2046. }
  2047. add_opt(common_arg(
  2048. {"--mlock"},
  2049. "force system to keep model in RAM rather than swapping or compressing",
  2050. [](common_params & params) {
  2051. params.use_mlock = true;
  2052. }
  2053. ).set_env("LLAMA_ARG_MLOCK"));
  2054. add_opt(common_arg(
  2055. {"--mmap"},
  2056. {"--no-mmap"},
  2057. 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"),
  2058. [](common_params & params, bool value) {
  2059. params.use_mmap = value;
  2060. if (value) {
  2061. params.use_direct_io = false; // disable direct io when mmap is explicitly enabled
  2062. }
  2063. }
  2064. ).set_env("LLAMA_ARG_MMAP"));
  2065. add_opt(common_arg(
  2066. {"-dio", "--direct-io"},
  2067. {"-ndio", "--no-direct-io"},
  2068. string_format("use DirectIO if available. Takes precedence over --mmap (default: %s)", params.use_direct_io ? "enabled" : "disabled"),
  2069. [](common_params & params, bool value) {
  2070. params.use_direct_io = value;
  2071. }
  2072. ).set_env("LLAMA_ARG_DIO"));
  2073. add_opt(common_arg(
  2074. {"--numa"}, "TYPE",
  2075. "attempt optimizations that help on some NUMA systems\n"
  2076. "- distribute: spread execution evenly over all nodes\n"
  2077. "- isolate: only spawn threads on CPUs on the node that execution started on\n"
  2078. "- numactl: use the CPU map provided by numactl\n"
  2079. "if run without this previously, it is recommended to drop the system page cache before using this\n"
  2080. "see https://github.com/ggml-org/llama.cpp/issues/1437",
  2081. [](common_params & params, const std::string & value) {
  2082. /**/ if (value == "distribute" || value == "") { params.numa = GGML_NUMA_STRATEGY_DISTRIBUTE; }
  2083. else if (value == "isolate") { params.numa = GGML_NUMA_STRATEGY_ISOLATE; }
  2084. else if (value == "numactl") { params.numa = GGML_NUMA_STRATEGY_NUMACTL; }
  2085. else { throw std::invalid_argument("invalid value"); }
  2086. }
  2087. ).set_env("LLAMA_ARG_NUMA"));
  2088. add_opt(common_arg(
  2089. {"-dev", "--device"}, "<dev1,dev2,..>",
  2090. "comma-separated list of devices to use for offloading (none = don't offload)\n"
  2091. "use --list-devices to see a list of available devices",
  2092. [](common_params & params, const std::string & value) {
  2093. params.devices = parse_device_list(value);
  2094. }
  2095. ).set_env("LLAMA_ARG_DEVICE"));
  2096. add_opt(common_arg(
  2097. {"--list-devices"},
  2098. "print list of available devices and exit",
  2099. [](common_params &) {
  2100. std::vector<ggml_backend_dev_t> devices;
  2101. for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
  2102. auto * dev = ggml_backend_dev_get(i);
  2103. if (ggml_backend_dev_type(dev) != GGML_BACKEND_DEVICE_TYPE_CPU) {
  2104. devices.push_back(dev);
  2105. }
  2106. }
  2107. printf("Available devices:\n");
  2108. for (auto * dev : devices) {
  2109. size_t free, total;
  2110. ggml_backend_dev_memory(dev, &free, &total);
  2111. 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);
  2112. }
  2113. exit(0);
  2114. }
  2115. ));
  2116. add_opt(common_arg(
  2117. {"-ot", "--override-tensor"}, "<tensor name pattern>=<buffer type>,...",
  2118. "override tensor buffer type", [](common_params & params, const std::string & value) {
  2119. parse_tensor_buffer_overrides(value, params.tensor_buft_overrides);
  2120. }
  2121. ).set_env("LLAMA_ARG_OVERRIDE_TENSOR"));
  2122. add_opt(common_arg(
  2123. {"-otd", "--override-tensor-draft"}, "<tensor name pattern>=<buffer type>,...",
  2124. "override tensor buffer type for draft model", [](common_params & params, const std::string & value) {
  2125. parse_tensor_buffer_overrides(value, params.speculative.tensor_buft_overrides);
  2126. }
  2127. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}));
  2128. add_opt(common_arg(
  2129. {"-cmoe", "--cpu-moe"},
  2130. "keep all Mixture of Experts (MoE) weights in the CPU",
  2131. [](common_params & params) {
  2132. params.tensor_buft_overrides.push_back(llm_ffn_exps_cpu_override());
  2133. }
  2134. ).set_env("LLAMA_ARG_CPU_MOE"));
  2135. add_opt(common_arg(
  2136. {"-ncmoe", "--n-cpu-moe"}, "N",
  2137. "keep the Mixture of Experts (MoE) weights of the first N layers in the CPU",
  2138. [](common_params & params, int value) {
  2139. if (value < 0) {
  2140. throw std::invalid_argument("invalid value");
  2141. }
  2142. for (int i = 0; i < value; ++i) {
  2143. // keep strings alive and avoid leaking memory by storing them in a static vector
  2144. static std::list<std::string> buft_overrides;
  2145. buft_overrides.push_back(llm_ffn_exps_block_regex(i));
  2146. params.tensor_buft_overrides.push_back({buft_overrides.back().c_str(), ggml_backend_cpu_buffer_type()});
  2147. }
  2148. }
  2149. ).set_env("LLAMA_ARG_N_CPU_MOE"));
  2150. add_opt(common_arg(
  2151. {"-cmoed", "--cpu-moe-draft"},
  2152. "keep all Mixture of Experts (MoE) weights in the CPU for the draft model",
  2153. [](common_params & params) {
  2154. params.speculative.tensor_buft_overrides.push_back(llm_ffn_exps_cpu_override());
  2155. }
  2156. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_CPU_MOE_DRAFT"));
  2157. add_opt(common_arg(
  2158. {"-ncmoed", "--n-cpu-moe-draft"}, "N",
  2159. "keep the Mixture of Experts (MoE) weights of the first N layers in the CPU for the draft model",
  2160. [](common_params & params, int value) {
  2161. if (value < 0) {
  2162. throw std::invalid_argument("invalid value");
  2163. }
  2164. for (int i = 0; i < value; ++i) {
  2165. static std::list<std::string> buft_overrides_draft;
  2166. buft_overrides_draft.push_back(llm_ffn_exps_block_regex(i));
  2167. params.speculative.tensor_buft_overrides.push_back({buft_overrides_draft.back().c_str(), ggml_backend_cpu_buffer_type()});
  2168. }
  2169. }
  2170. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_N_CPU_MOE_DRAFT"));
  2171. GGML_ASSERT(params.n_gpu_layers < 0); // string_format would need to be extended for a default >= 0
  2172. add_opt(common_arg(
  2173. {"-ngl", "--gpu-layers", "--n-gpu-layers"}, "N",
  2174. 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"),
  2175. [](common_params & params, const std::string & value) {
  2176. if (value == "auto") {
  2177. params.n_gpu_layers = -1;
  2178. } else if (value == "all") {
  2179. params.n_gpu_layers = -2;
  2180. } else {
  2181. params.n_gpu_layers = std::stoi(value);
  2182. }
  2183. if (!llama_supports_gpu_offload()) {
  2184. fprintf(stderr, "warning: no usable GPU found, --gpu-layers option will be ignored\n");
  2185. fprintf(stderr, "warning: one possible reason is that llama.cpp was compiled without GPU support\n");
  2186. fprintf(stderr, "warning: consult docs/build.md for compilation instructions\n");
  2187. }
  2188. }
  2189. ).set_env("LLAMA_ARG_N_GPU_LAYERS"));
  2190. add_opt(common_arg(
  2191. {"-sm", "--split-mode"}, "{none,layer,row}",
  2192. "how to split the model across multiple GPUs, one of:\n"
  2193. "- none: use one GPU only\n"
  2194. "- layer (default): split layers and KV across GPUs\n"
  2195. "- row: split rows across GPUs",
  2196. [](common_params & params, const std::string & value) {
  2197. std::string arg_next = value;
  2198. if (arg_next == "none") {
  2199. params.split_mode = LLAMA_SPLIT_MODE_NONE;
  2200. } else if (arg_next == "layer") {
  2201. params.split_mode = LLAMA_SPLIT_MODE_LAYER;
  2202. } else if (arg_next == "row") {
  2203. params.split_mode = LLAMA_SPLIT_MODE_ROW;
  2204. } else {
  2205. throw std::invalid_argument("invalid value");
  2206. }
  2207. if (!llama_supports_gpu_offload()) {
  2208. fprintf(stderr, "warning: llama.cpp was compiled without support for GPU offload. Setting the split mode has no effect.\n");
  2209. }
  2210. }
  2211. ).set_env("LLAMA_ARG_SPLIT_MODE"));
  2212. add_opt(common_arg(
  2213. {"-ts", "--tensor-split"}, "N0,N1,N2,...",
  2214. "fraction of the model to offload to each GPU, comma-separated list of proportions, e.g. 3,1",
  2215. [](common_params & params, const std::string & value) {
  2216. std::string arg_next = value;
  2217. // split string by , and /
  2218. const std::regex regex{ R"([,/]+)" };
  2219. std::sregex_token_iterator it{ arg_next.begin(), arg_next.end(), regex, -1 };
  2220. std::vector<std::string> split_arg{ it, {} };
  2221. if (split_arg.size() >= llama_max_devices()) {
  2222. throw std::invalid_argument(
  2223. string_format("got %zu input configs, but system only has %zu devices", split_arg.size(), llama_max_devices())
  2224. );
  2225. }
  2226. for (size_t i = 0; i < llama_max_devices(); ++i) {
  2227. if (i < split_arg.size()) {
  2228. params.tensor_split[i] = std::stof(split_arg[i]);
  2229. } else {
  2230. params.tensor_split[i] = 0.0f;
  2231. }
  2232. }
  2233. if (!llama_supports_gpu_offload()) {
  2234. fprintf(stderr, "warning: llama.cpp was compiled without support for GPU offload. Setting a tensor split has no effect.\n");
  2235. }
  2236. }
  2237. ).set_env("LLAMA_ARG_TENSOR_SPLIT"));
  2238. add_opt(common_arg(
  2239. {"-mg", "--main-gpu"}, "INDEX",
  2240. 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),
  2241. [](common_params & params, int value) {
  2242. params.main_gpu = value;
  2243. if (!llama_supports_gpu_offload()) {
  2244. fprintf(stderr, "warning: llama.cpp was compiled without support for GPU offload. Setting the main GPU has no effect.\n");
  2245. }
  2246. }
  2247. ).set_env("LLAMA_ARG_MAIN_GPU"));
  2248. add_opt(common_arg(
  2249. { "-fit", "--fit" }, "[on|off]",
  2250. string_format("whether to adjust unset arguments to fit in device memory ('on' or 'off', default: '%s')", params.fit_params ? "on" : "off"),
  2251. [](common_params & params, const std::string & value) {
  2252. if (is_truthy(value)) {
  2253. params.fit_params = true;
  2254. } else if (is_falsey(value)) {
  2255. params.fit_params = false;
  2256. } else {
  2257. throw std::runtime_error(
  2258. string_format("error: unkown value for --fit: '%s'\n", value.c_str()));
  2259. }
  2260. }
  2261. ).set_env("LLAMA_ARG_FIT"));
  2262. add_opt(common_arg(
  2263. { "-fitt", "--fit-target" }, "MiB0,MiB1,MiB2,...",
  2264. string_format("target margin per device for --fit, comma-separated list of values, "
  2265. "single value is broadcast across all devices, default: %zu", params.fit_params_target[0]/(1024*1024)),
  2266. [](common_params & params, const std::string & value) {
  2267. std::string arg_next = value;
  2268. // split string by , and /
  2269. const std::regex regex{ R"([,/]+)" };
  2270. std::sregex_token_iterator it{ arg_next.begin(), arg_next.end(), regex, -1 };
  2271. std::vector<std::string> split_arg{ it, {} };
  2272. if (split_arg.size() >= llama_max_devices()) {
  2273. throw std::invalid_argument(
  2274. string_format("got %zu input configs, but system only has %zu devices", split_arg.size(), llama_max_devices())
  2275. );
  2276. }
  2277. if (split_arg.size() == 1) {
  2278. std::fill(params.fit_params_target.begin(), params.fit_params_target.end(), std::stoul(split_arg[0]) * 1024*1024);
  2279. return;
  2280. }
  2281. for (size_t i = 0; i < split_arg.size(); i++) {
  2282. params.fit_params_target[i] = std::stoul(split_arg[i]) * 1024*1024;
  2283. }
  2284. }
  2285. ).set_env("LLAMA_ARG_FIT_TARGET"));
  2286. add_opt(common_arg(
  2287. { "-fitc", "--fit-ctx" }, "N",
  2288. string_format("minimum ctx size that can be set by --fit option, default: %" PRIu32, params.fit_params_min_ctx),
  2289. [](common_params & params, int value) {
  2290. params.fit_params_min_ctx = value;
  2291. }
  2292. ).set_env("LLAMA_ARG_FIT_CTX"));
  2293. add_opt(common_arg(
  2294. {"--check-tensors"},
  2295. string_format("check model tensor data for invalid values (default: %s)", params.check_tensors ? "true" : "false"),
  2296. [](common_params & params) {
  2297. params.check_tensors = true;
  2298. }
  2299. ));
  2300. add_opt(common_arg(
  2301. {"--override-kv"}, "KEY=TYPE:VALUE,...",
  2302. "advanced option to override model metadata by key. to specify multiple overrides, either use comma-separated values.\n"
  2303. "types: int, float, bool, str. example: --override-kv tokenizer.ggml.add_bos_token=bool:false,tokenizer.ggml.add_eos_token=bool:false",
  2304. [](common_params & params, const std::string & value) {
  2305. for (const auto & item : parse_csv_row(value)) {
  2306. if (!string_parse_kv_override(item.c_str(), params.kv_overrides)) {
  2307. throw std::runtime_error(string_format("error: Invalid type for KV override: %s\n", item.c_str()));
  2308. }
  2309. }
  2310. }
  2311. ));
  2312. add_opt(common_arg(
  2313. {"--op-offload"},
  2314. {"--no-op-offload"},
  2315. string_format("whether to offload host tensor operations to device (default: %s)", params.no_op_offload ? "false" : "true"),
  2316. [](common_params & params, bool value) {
  2317. params.no_op_offload = !value;
  2318. }
  2319. ));
  2320. add_opt(common_arg(
  2321. {"--lora"}, "FNAME",
  2322. "path to LoRA adapter (use comma-separated values to load multiple adapters)",
  2323. [](common_params & params, const std::string & value) {
  2324. for (const auto & item : parse_csv_row(value)) {
  2325. params.lora_adapters.push_back({ item, 1.0, "", "", nullptr });
  2326. }
  2327. }
  2328. // 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
  2329. ).set_examples({LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_EXPORT_LORA}));
  2330. add_opt(common_arg(
  2331. {"--lora-scaled"}, "FNAME:SCALE,...",
  2332. "path to LoRA adapter with user defined scaling (format: FNAME:SCALE,...)\n"
  2333. "note: use comma-separated values",
  2334. [](common_params & params, const std::string & value) {
  2335. for (const auto & item : parse_csv_row(value)) {
  2336. auto parts = string_split<std::string>(item, ':');
  2337. if (parts.size() != 2) {
  2338. throw std::invalid_argument("lora-scaled format: FNAME:SCALE");
  2339. }
  2340. params.lora_adapters.push_back({ parts[0], std::stof(parts[1]), "", "", nullptr });
  2341. }
  2342. }
  2343. // 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
  2344. ).set_examples({LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_EXPORT_LORA}));
  2345. add_opt(common_arg(
  2346. {"--control-vector"}, "FNAME",
  2347. "add a control vector\nnote: use comma-separated values to add multiple control vectors",
  2348. [](common_params & params, const std::string & value) {
  2349. for (const auto & item : parse_csv_row(value)) {
  2350. params.control_vectors.push_back({ 1.0f, item, });
  2351. }
  2352. }
  2353. ));
  2354. add_opt(common_arg(
  2355. {"--control-vector-scaled"}, "FNAME:SCALE,...",
  2356. "add a control vector with user defined scaling SCALE\n"
  2357. "note: use comma-separated values (format: FNAME:SCALE,...)",
  2358. [](common_params & params, const std::string & value) {
  2359. for (const auto & item : parse_csv_row(value)) {
  2360. auto parts = string_split<std::string>(item, ':');
  2361. if (parts.size() != 2) {
  2362. throw std::invalid_argument("control-vector-scaled format: FNAME:SCALE");
  2363. }
  2364. params.control_vectors.push_back({ std::stof(parts[1]), parts[0] });
  2365. }
  2366. }
  2367. ));
  2368. add_opt(common_arg(
  2369. {"--control-vector-layer-range"}, "START", "END",
  2370. "layer range to apply the control vector(s) to, start and end inclusive",
  2371. [](common_params & params, const std::string & start, const std::string & end) {
  2372. params.control_vector_layer_start = std::stoi(start);
  2373. params.control_vector_layer_end = std::stoi(end);
  2374. }
  2375. ));
  2376. add_opt(common_arg(
  2377. {"-a", "--alias"}, "STRING",
  2378. "set alias for model name (to be used by REST API)",
  2379. [](common_params & params, const std::string & value) {
  2380. params.model_alias = value;
  2381. }
  2382. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ALIAS"));
  2383. add_opt(common_arg(
  2384. {"-m", "--model"}, "FNAME",
  2385. ex == LLAMA_EXAMPLE_EXPORT_LORA
  2386. ? "model path from which to load base model"
  2387. : "model path to load",
  2388. [](common_params & params, const std::string & value) {
  2389. params.model.path = value;
  2390. }
  2391. ).set_examples({LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_EXPORT_LORA}).set_env("LLAMA_ARG_MODEL"));
  2392. add_opt(common_arg(
  2393. {"-mu", "--model-url"}, "MODEL_URL",
  2394. "model download url (default: unused)",
  2395. [](common_params & params, const std::string & value) {
  2396. params.model.url = value;
  2397. }
  2398. ).set_env("LLAMA_ARG_MODEL_URL"));
  2399. add_opt(common_arg(
  2400. { "-dr", "--docker-repo" }, "[<repo>/]<model>[:quant]",
  2401. "Docker Hub model repository. repo is optional, default to ai/. quant is optional, default to :latest.\n"
  2402. "example: gemma3\n"
  2403. "(default: unused)",
  2404. [](common_params & params, const std::string & value) {
  2405. params.model.docker_repo = value;
  2406. }
  2407. ).set_env("LLAMA_ARG_DOCKER_REPO"));
  2408. add_opt(common_arg(
  2409. {"-hf", "-hfr", "--hf-repo"}, "<user>/<model>[:quant]",
  2410. "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"
  2411. "mmproj is also downloaded automatically if available. to disable, add --no-mmproj\n"
  2412. "example: unsloth/phi-4-GGUF:q4_k_m\n"
  2413. "(default: unused)",
  2414. [](common_params & params, const std::string & value) {
  2415. params.model.hf_repo = value;
  2416. }
  2417. ).set_env("LLAMA_ARG_HF_REPO"));
  2418. add_opt(common_arg(
  2419. {"-hfd", "-hfrd", "--hf-repo-draft"}, "<user>/<model>[:quant]",
  2420. "Same as --hf-repo, but for the draft model (default: unused)",
  2421. [](common_params & params, const std::string & value) {
  2422. params.speculative.model.hf_repo = value;
  2423. }
  2424. ).set_env("LLAMA_ARG_HFD_REPO"));
  2425. add_opt(common_arg(
  2426. {"-hff", "--hf-file"}, "FILE",
  2427. "Hugging Face model file. If specified, it will override the quant in --hf-repo (default: unused)",
  2428. [](common_params & params, const std::string & value) {
  2429. params.model.hf_file = value;
  2430. }
  2431. ).set_env("LLAMA_ARG_HF_FILE"));
  2432. add_opt(common_arg(
  2433. {"-hfv", "-hfrv", "--hf-repo-v"}, "<user>/<model>[:quant]",
  2434. "Hugging Face model repository for the vocoder model (default: unused)",
  2435. [](common_params & params, const std::string & value) {
  2436. params.vocoder.model.hf_repo = value;
  2437. }
  2438. ).set_env("LLAMA_ARG_HF_REPO_V"));
  2439. add_opt(common_arg(
  2440. {"-hffv", "--hf-file-v"}, "FILE",
  2441. "Hugging Face model file for the vocoder model (default: unused)",
  2442. [](common_params & params, const std::string & value) {
  2443. params.vocoder.model.hf_file = value;
  2444. }
  2445. ).set_env("LLAMA_ARG_HF_FILE_V"));
  2446. add_opt(common_arg(
  2447. {"-hft", "--hf-token"}, "TOKEN",
  2448. "Hugging Face access token (default: value from HF_TOKEN environment variable)",
  2449. [](common_params & params, const std::string & value) {
  2450. params.hf_token = value;
  2451. }
  2452. ).set_env("HF_TOKEN"));
  2453. add_opt(common_arg(
  2454. {"--context-file"}, "FNAME",
  2455. "file to load context from (use comma-separated values to specify multiple files)",
  2456. [](common_params & params, const std::string & value) {
  2457. for (const auto & item : parse_csv_row(value)) {
  2458. std::ifstream file(item, std::ios::binary);
  2459. if (!file) {
  2460. throw std::runtime_error(string_format("error: failed to open file '%s'\n", item.c_str()));
  2461. }
  2462. params.context_files.push_back(item);
  2463. }
  2464. }
  2465. ).set_examples({LLAMA_EXAMPLE_RETRIEVAL}));
  2466. add_opt(common_arg(
  2467. {"--chunk-size"}, "N",
  2468. string_format("minimum length of embedded text chunks (default: %d)", params.chunk_size),
  2469. [](common_params & params, int value) {
  2470. params.chunk_size = value;
  2471. }
  2472. ).set_examples({LLAMA_EXAMPLE_RETRIEVAL}));
  2473. add_opt(common_arg(
  2474. {"--chunk-separator"}, "STRING",
  2475. string_format("separator between chunks (default: '%s')", params.chunk_separator.c_str()),
  2476. [](common_params & params, const std::string & value) {
  2477. params.chunk_separator = value;
  2478. }
  2479. ).set_examples({LLAMA_EXAMPLE_RETRIEVAL}));
  2480. add_opt(common_arg(
  2481. {"--junk"}, "N",
  2482. string_format("number of times to repeat the junk text (default: %d)", params.n_junk),
  2483. [](common_params & params, int value) {
  2484. params.n_junk = value;
  2485. }
  2486. ).set_examples({LLAMA_EXAMPLE_PASSKEY, LLAMA_EXAMPLE_PARALLEL}));
  2487. add_opt(common_arg(
  2488. {"--pos"}, "N",
  2489. string_format("position of the passkey in the junk text (default: %d)", params.i_pos),
  2490. [](common_params & params, int value) {
  2491. params.i_pos = value;
  2492. }
  2493. ).set_examples({LLAMA_EXAMPLE_PASSKEY}));
  2494. add_opt(common_arg(
  2495. {"-o", "--output", "--output-file"}, "FNAME",
  2496. string_format("output file (default: '%s')", params.out_file.c_str()),
  2497. [](common_params & params, const std::string & value) {
  2498. params.out_file = value;
  2499. }
  2500. ).set_examples({LLAMA_EXAMPLE_IMATRIX, LLAMA_EXAMPLE_CVECTOR_GENERATOR, LLAMA_EXAMPLE_EXPORT_LORA, LLAMA_EXAMPLE_TTS, LLAMA_EXAMPLE_FINETUNE}));
  2501. add_opt(common_arg(
  2502. {"-ofreq", "--output-frequency"}, "N",
  2503. string_format("output the imatrix every N iterations (default: %d)", params.n_out_freq),
  2504. [](common_params & params, int value) {
  2505. params.n_out_freq = value;
  2506. }
  2507. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  2508. add_opt(common_arg(
  2509. {"--output-format"}, "{gguf,dat}",
  2510. string_format("output format for imatrix file (default: %s)", params.imat_dat > 0 ? "dat" : "gguf"),
  2511. [](common_params & params, const std::string & value) {
  2512. /**/ if (value == "gguf") { params.imat_dat = -1; }
  2513. else if (value == "dat") { params.imat_dat = 1; }
  2514. else { throw std::invalid_argument("invalid output format"); }
  2515. }
  2516. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  2517. add_opt(common_arg(
  2518. {"--save-frequency"}, "N",
  2519. string_format("save an imatrix copy every N iterations (default: %d)", params.n_save_freq),
  2520. [](common_params & params, int value) {
  2521. params.n_save_freq = value;
  2522. }
  2523. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  2524. add_opt(common_arg(
  2525. {"--process-output"},
  2526. string_format("collect data for the output tensor (default: %s)", params.process_output ? "true" : "false"),
  2527. [](common_params & params) {
  2528. params.process_output = true;
  2529. }
  2530. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  2531. add_opt(common_arg(
  2532. {"--ppl"},
  2533. {"--no-ppl"},
  2534. string_format("whether to compute perplexity (default: %s)", params.compute_ppl ? "true" : "false"),
  2535. [](common_params & params, bool value) {
  2536. params.compute_ppl = value;
  2537. }
  2538. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  2539. add_opt(common_arg(
  2540. {"--chunk", "--from-chunk"}, "N",
  2541. string_format("start processing the input from chunk N (default: %d)", params.i_chunk),
  2542. [](common_params & params, int value) {
  2543. params.i_chunk = value;
  2544. }
  2545. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  2546. add_opt(common_arg(
  2547. {"--show-statistics"},
  2548. string_format("show imatrix statistics and then exit (default: %s)", params.show_statistics ? "true" : "false"),
  2549. [](common_params & params) {
  2550. params.show_statistics = true;
  2551. }
  2552. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  2553. add_opt(common_arg(
  2554. {"--parse-special"},
  2555. string_format("parse special tokens (chat, tool, etc) (default: %s)", params.parse_special ? "true" : "false"),
  2556. [](common_params & params) {
  2557. params.parse_special = true;
  2558. }
  2559. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  2560. add_opt(common_arg(
  2561. {"-pps"},
  2562. string_format("is the prompt shared across parallel sequences (default: %s)", params.is_pp_shared ? "true" : "false"),
  2563. [](common_params & params) {
  2564. params.is_pp_shared = true;
  2565. }
  2566. ).set_examples({LLAMA_EXAMPLE_BENCH, LLAMA_EXAMPLE_PARALLEL}));
  2567. add_opt(common_arg(
  2568. {"-tgs"},
  2569. string_format("is the text generation separated across the different sequences (default: %s)", params.is_tg_separate ? "true" : "false"),
  2570. [](common_params & params) {
  2571. params.is_tg_separate = true;
  2572. }
  2573. ).set_examples({LLAMA_EXAMPLE_BENCH, LLAMA_EXAMPLE_PARALLEL}));
  2574. add_opt(common_arg(
  2575. {"-npp"}, "n0,n1,...",
  2576. "number of prompt tokens",
  2577. [](common_params & params, const std::string & value) {
  2578. auto p = string_split<int>(value, ',');
  2579. params.n_pp.insert(params.n_pp.end(), p.begin(), p.end());
  2580. }
  2581. ).set_examples({LLAMA_EXAMPLE_BENCH}));
  2582. add_opt(common_arg(
  2583. {"-ntg"}, "n0,n1,...",
  2584. "number of text generation tokens",
  2585. [](common_params & params, const std::string & value) {
  2586. auto p = string_split<int>(value, ',');
  2587. params.n_tg.insert(params.n_tg.end(), p.begin(), p.end());
  2588. }
  2589. ).set_examples({LLAMA_EXAMPLE_BENCH}));
  2590. add_opt(common_arg(
  2591. {"-npl"}, "n0,n1,...",
  2592. "number of parallel prompts",
  2593. [](common_params & params, const std::string & value) {
  2594. auto p = string_split<int>(value, ',');
  2595. params.n_pl.insert(params.n_pl.end(), p.begin(), p.end());
  2596. }
  2597. ).set_examples({LLAMA_EXAMPLE_BENCH}));
  2598. add_opt(common_arg(
  2599. {"--embd-normalize"}, "N",
  2600. string_format("normalisation for embeddings (default: %d) (-1=none, 0=max absolute int16, 1=taxicab, 2=euclidean, >2=p-norm)", params.embd_normalize),
  2601. [](common_params & params, int value) {
  2602. params.embd_normalize = value;
  2603. }
  2604. ).set_examples({LLAMA_EXAMPLE_EMBEDDING, LLAMA_EXAMPLE_DEBUG}));
  2605. add_opt(common_arg(
  2606. {"--embd-output-format"}, "FORMAT",
  2607. "empty = default, \"array\" = [[],[]...], \"json\" = openai style, \"json+\" = same \"json\" + cosine similarity matrix, \"raw\" = plain whitespace-delimited output (one embedding per line)",
  2608. [](common_params & params, const std::string & value) {
  2609. params.embd_out = value;
  2610. }
  2611. ).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
  2612. add_opt(common_arg(
  2613. {"--embd-separator"}, "STRING",
  2614. "separator of embeddings (default \\n) for example \"<#sep#>\"",
  2615. [](common_params & params, const std::string & value) {
  2616. params.embd_sep = value;
  2617. }
  2618. ).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
  2619. add_opt(common_arg(
  2620. {"--cls-separator"}, "STRING",
  2621. "separator of classification sequences (default \\t) for example \"<#seq#>\"",
  2622. [](common_params & params, const std::string & value) {
  2623. params.cls_sep = value;
  2624. }
  2625. ).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
  2626. add_opt(common_arg(
  2627. {"--host"}, "HOST",
  2628. string_format("ip address to listen, or bind to an UNIX socket if the address ends with .sock (default: %s)", params.hostname.c_str()),
  2629. [](common_params & params, const std::string & value) {
  2630. params.hostname = value;
  2631. }
  2632. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_HOST"));
  2633. add_opt(common_arg(
  2634. {"--port"}, "PORT",
  2635. string_format("port to listen (default: %d)", params.port),
  2636. [](common_params & params, int value) {
  2637. params.port = value;
  2638. }
  2639. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_PORT"));
  2640. add_opt(common_arg(
  2641. {"--path"}, "PATH",
  2642. string_format("path to serve static files from (default: %s)", params.public_path.c_str()),
  2643. [](common_params & params, const std::string & value) {
  2644. params.public_path = value;
  2645. }
  2646. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_STATIC_PATH"));
  2647. add_opt(common_arg(
  2648. {"--api-prefix"}, "PREFIX",
  2649. string_format("prefix path the server serves from, without the trailing slash (default: %s)", params.api_prefix.c_str()),
  2650. [](common_params & params, const std::string & value) {
  2651. params.api_prefix = value;
  2652. }
  2653. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_API_PREFIX"));
  2654. add_opt(common_arg(
  2655. {"--webui-config"}, "JSON",
  2656. "JSON that provides default WebUI settings (overrides WebUI defaults)",
  2657. [](common_params & params, const std::string & value) {
  2658. params.webui_config_json = value;
  2659. }
  2660. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_WEBUI_CONFIG"));
  2661. add_opt(common_arg(
  2662. {"--webui-config-file"}, "PATH",
  2663. "JSON file that provides default WebUI settings (overrides WebUI defaults)",
  2664. [](common_params & params, const std::string & value) {
  2665. params.webui_config_json = read_file(value);
  2666. }
  2667. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_WEBUI_CONFIG_FILE"));
  2668. add_opt(common_arg(
  2669. {"--webui"},
  2670. {"--no-webui"},
  2671. string_format("whether to enable the Web UI (default: %s)", params.webui ? "enabled" : "disabled"),
  2672. [](common_params & params, bool value) {
  2673. params.webui = value;
  2674. }
  2675. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_WEBUI"));
  2676. add_opt(common_arg(
  2677. {"--embedding", "--embeddings"},
  2678. string_format("restrict to only support embedding use case; use only with dedicated embedding models (default: %s)", params.embedding ? "enabled" : "disabled"),
  2679. [](common_params & params) {
  2680. params.embedding = true;
  2681. }
  2682. ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_DEBUG}).set_env("LLAMA_ARG_EMBEDDINGS"));
  2683. add_opt(common_arg(
  2684. {"--rerank", "--reranking"},
  2685. string_format("enable reranking endpoint on server (default: %s)", "disabled"),
  2686. [](common_params & params) {
  2687. params.embedding = true;
  2688. params.pooling_type = LLAMA_POOLING_TYPE_RANK;
  2689. }
  2690. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_RERANKING"));
  2691. add_opt(common_arg(
  2692. {"--api-key"}, "KEY",
  2693. "API key to use for authentication, multiple keys can be provided as a comma-separated list (default: none)",
  2694. [](common_params & params, const std::string & value) {
  2695. for (const auto & key : parse_csv_row(value)) {
  2696. if (!key.empty()) {
  2697. params.api_keys.push_back(key);
  2698. }
  2699. }
  2700. }
  2701. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_API_KEY"));
  2702. add_opt(common_arg(
  2703. {"--api-key-file"}, "FNAME",
  2704. "path to file containing API keys (default: none)",
  2705. [](common_params & params, const std::string & value) {
  2706. std::ifstream key_file(value);
  2707. if (!key_file) {
  2708. throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
  2709. }
  2710. std::string key;
  2711. while (std::getline(key_file, key)) {
  2712. if (!key.empty()) {
  2713. params.api_keys.push_back(key);
  2714. }
  2715. }
  2716. key_file.close();
  2717. }
  2718. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2719. add_opt(common_arg(
  2720. {"--ssl-key-file"}, "FNAME",
  2721. "path to file a PEM-encoded SSL private key",
  2722. [](common_params & params, const std::string & value) {
  2723. params.ssl_file_key = value;
  2724. }
  2725. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_SSL_KEY_FILE"));
  2726. add_opt(common_arg(
  2727. {"--ssl-cert-file"}, "FNAME",
  2728. "path to file a PEM-encoded SSL certificate",
  2729. [](common_params & params, const std::string & value) {
  2730. params.ssl_file_cert = value;
  2731. }
  2732. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_SSL_CERT_FILE"));
  2733. add_opt(common_arg(
  2734. {"--chat-template-kwargs"}, "STRING",
  2735. "sets additional params for the json template parser, must be a valid json object string, e.g. '{\"key1\":\"value1\",\"key2\":\"value2\"}'",
  2736. [](common_params & params, const std::string & value) {
  2737. auto parsed = json::parse(value);
  2738. for (const auto & item : parsed.items()) {
  2739. params.default_template_kwargs[item.key()] = item.value().dump();
  2740. }
  2741. }
  2742. ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_CHAT_TEMPLATE_KWARGS"));
  2743. add_opt(common_arg(
  2744. {"-to", "--timeout"}, "N",
  2745. string_format("server read/write timeout in seconds (default: %d)", params.timeout_read),
  2746. [](common_params & params, int value) {
  2747. params.timeout_read = value;
  2748. params.timeout_write = value;
  2749. }
  2750. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_TIMEOUT"));
  2751. add_opt(common_arg(
  2752. {"--threads-http"}, "N",
  2753. string_format("number of threads used to process HTTP requests (default: %d)", params.n_threads_http),
  2754. [](common_params & params, int value) {
  2755. params.n_threads_http = value;
  2756. }
  2757. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_THREADS_HTTP"));
  2758. add_opt(common_arg(
  2759. {"--cache-prompt"},
  2760. {"--no-cache-prompt"},
  2761. string_format("whether to enable prompt caching (default: %s)", params.cache_prompt ? "enabled" : "disabled"),
  2762. [](common_params & params, bool value) {
  2763. params.cache_prompt = value;
  2764. }
  2765. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CACHE_PROMPT"));
  2766. add_opt(common_arg(
  2767. {"--cache-reuse"}, "N",
  2768. string_format(
  2769. "min chunk size to attempt reusing from the cache via KV shifting, requires prompt caching to be enabled (default: %d)\n"
  2770. "[(card)](https://ggml.ai/f0.png)", params.n_cache_reuse
  2771. ),
  2772. [](common_params & params, int value) {
  2773. params.n_cache_reuse = value;
  2774. }
  2775. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CACHE_REUSE"));
  2776. add_opt(common_arg(
  2777. {"--metrics"},
  2778. string_format("enable prometheus compatible metrics endpoint (default: %s)", params.endpoint_metrics ? "enabled" : "disabled"),
  2779. [](common_params & params) {
  2780. params.endpoint_metrics = true;
  2781. }
  2782. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ENDPOINT_METRICS"));
  2783. add_opt(common_arg(
  2784. {"--props"},
  2785. string_format("enable changing global properties via POST /props (default: %s)", params.endpoint_props ? "enabled" : "disabled"),
  2786. [](common_params & params) {
  2787. params.endpoint_props = true;
  2788. }
  2789. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ENDPOINT_PROPS"));
  2790. add_opt(common_arg(
  2791. {"--slots"},
  2792. {"--no-slots"},
  2793. string_format("expose slots monitoring endpoint (default: %s)", params.endpoint_slots ? "enabled" : "disabled"),
  2794. [](common_params & params, bool value) {
  2795. params.endpoint_slots = value;
  2796. }
  2797. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ENDPOINT_SLOTS"));
  2798. add_opt(common_arg(
  2799. {"--slot-save-path"}, "PATH",
  2800. "path to save slot kv cache (default: disabled)",
  2801. [](common_params & params, const std::string & value) {
  2802. params.slot_save_path = value;
  2803. if (!fs_is_directory(params.slot_save_path)) {
  2804. throw std::invalid_argument("not a directory: " + value);
  2805. }
  2806. // if doesn't end with DIRECTORY_SEPARATOR, add it
  2807. if (!params.slot_save_path.empty() && params.slot_save_path[params.slot_save_path.size() - 1] != DIRECTORY_SEPARATOR) {
  2808. params.slot_save_path += DIRECTORY_SEPARATOR;
  2809. }
  2810. }
  2811. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2812. add_opt(common_arg(
  2813. {"--media-path"}, "PATH",
  2814. "directory for loading local media files; files can be accessed via file:// URLs using relative paths (default: disabled)",
  2815. [](common_params & params, const std::string & value) {
  2816. params.media_path = value;
  2817. if (!fs_is_directory(params.media_path)) {
  2818. throw std::invalid_argument("not a directory: " + value);
  2819. }
  2820. // if doesn't end with DIRECTORY_SEPARATOR, add it
  2821. if (!params.media_path.empty() && params.media_path[params.media_path.size() - 1] != DIRECTORY_SEPARATOR) {
  2822. params.media_path += DIRECTORY_SEPARATOR;
  2823. }
  2824. }
  2825. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2826. add_opt(common_arg(
  2827. {"--models-dir"}, "PATH",
  2828. "directory containing models for the router server (default: disabled)",
  2829. [](common_params & params, const std::string & value) {
  2830. params.models_dir = value;
  2831. }
  2832. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_MODELS_DIR"));
  2833. add_opt(common_arg(
  2834. {"--models-preset"}, "PATH",
  2835. "path to INI file containing model presets for the router server (default: disabled)",
  2836. [](common_params & params, const std::string & value) {
  2837. params.models_preset = value;
  2838. }
  2839. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_MODELS_PRESET"));
  2840. add_opt(common_arg(
  2841. {"--models-max"}, "N",
  2842. string_format("for router server, maximum number of models to load simultaneously (default: %d, 0 = unlimited)", params.models_max),
  2843. [](common_params & params, int value) {
  2844. params.models_max = value;
  2845. }
  2846. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_MODELS_MAX"));
  2847. add_opt(common_arg(
  2848. {"--models-autoload"},
  2849. {"--no-models-autoload"},
  2850. string_format("for router server, whether to automatically load models (default: %s)", params.models_autoload ? "enabled" : "disabled"),
  2851. [](common_params & params, bool value) {
  2852. params.models_autoload = value;
  2853. }
  2854. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_MODELS_AUTOLOAD"));
  2855. add_opt(common_arg(
  2856. {"--jinja"},
  2857. {"--no-jinja"},
  2858. string_format("whether to use jinja template engine for chat (default: %s)", params.use_jinja ? "enabled" : "disabled"),
  2859. [](common_params & params, bool value) {
  2860. params.use_jinja = value;
  2861. }
  2862. ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_CLI, LLAMA_EXAMPLE_MTMD}).set_env("LLAMA_ARG_JINJA"));
  2863. add_opt(common_arg(
  2864. {"--reasoning-format"}, "FORMAT",
  2865. "controls whether thought tags are allowed and/or extracted from the response, and in which format they're returned; one of:\n"
  2866. "- none: leaves thoughts unparsed in `message.content`\n"
  2867. "- deepseek: puts thoughts in `message.reasoning_content`\n"
  2868. "- deepseek-legacy: keeps `<think>` tags in `message.content` while also populating `message.reasoning_content`\n"
  2869. "(default: auto)",
  2870. [](common_params & params, const std::string & value) {
  2871. params.reasoning_format = common_reasoning_format_from_name(value);
  2872. }
  2873. ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_THINK"));
  2874. add_opt(common_arg(
  2875. {"--reasoning-budget"}, "N",
  2876. "controls the amount of thinking allowed; currently only one of: -1 for unrestricted thinking budget, or 0 to disable thinking (default: -1)",
  2877. [](common_params & params, int value) {
  2878. if (value != 0 && value != -1) { throw std::invalid_argument("invalid value"); }
  2879. params.reasoning_budget = value;
  2880. }
  2881. ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_THINK_BUDGET"));
  2882. add_opt(common_arg(
  2883. {"--chat-template"}, "JINJA_TEMPLATE",
  2884. string_format(
  2885. "set custom jinja chat template (default: template taken from model's metadata)\n"
  2886. "if suffix/prefix are specified, template will be disabled\n"
  2887. "only commonly used templates are accepted (unless --jinja is set before this flag):\n"
  2888. "list of built-in templates:\n%s", list_builtin_chat_templates().c_str()
  2889. ),
  2890. [](common_params & params, const std::string & value) {
  2891. params.chat_template = value;
  2892. }
  2893. ).set_examples({LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_CLI, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_MTMD}).set_env("LLAMA_ARG_CHAT_TEMPLATE"));
  2894. add_opt(common_arg(
  2895. {"--chat-template-file"}, "JINJA_TEMPLATE_FILE",
  2896. string_format(
  2897. "set custom jinja chat template file (default: template taken from model's metadata)\n"
  2898. "if suffix/prefix are specified, template will be disabled\n"
  2899. "only commonly used templates are accepted (unless --jinja is set before this flag):\n"
  2900. "list of built-in templates:\n%s", list_builtin_chat_templates().c_str()
  2901. ),
  2902. [](common_params & params, const std::string & value) {
  2903. params.chat_template = read_file(value);
  2904. }
  2905. ).set_examples({LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_CLI, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CHAT_TEMPLATE_FILE"));
  2906. add_opt(common_arg(
  2907. {"--prefill-assistant"},
  2908. {"--no-prefill-assistant"},
  2909. string_format(
  2910. "whether to prefill the assistant's response if the last message is an assistant message (default: prefill enabled)\n"
  2911. "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"
  2912. ),
  2913. [](common_params & params, bool value) {
  2914. params.prefill_assistant = value;
  2915. }
  2916. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_PREFILL_ASSISTANT"));
  2917. add_opt(common_arg(
  2918. {"-sps", "--slot-prompt-similarity"}, "SIMILARITY",
  2919. 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),
  2920. [](common_params & params, const std::string & value) {
  2921. params.slot_prompt_similarity = std::stof(value);
  2922. }
  2923. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2924. add_opt(common_arg(
  2925. {"--lora-init-without-apply"},
  2926. string_format("load LoRA adapters without applying them (apply later via POST /lora-adapters) (default: %s)", params.lora_init_without_apply ? "enabled" : "disabled"),
  2927. [](common_params & params) {
  2928. params.lora_init_without_apply = true;
  2929. }
  2930. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2931. add_opt(common_arg(
  2932. {"--sleep-idle-seconds"}, "SECONDS",
  2933. string_format("number of seconds of idleness after which the server will sleep (default: %d; -1 = disabled)", params.sleep_idle_seconds),
  2934. [](common_params & params, int value) {
  2935. if (value == 0 || value < -1) {
  2936. throw std::invalid_argument("invalid value: cannot be 0 or less than -1");
  2937. }
  2938. params.sleep_idle_seconds = value;
  2939. }
  2940. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2941. add_opt(common_arg(
  2942. {"--simple-io"},
  2943. "use basic IO for better compatibility in subprocesses and limited consoles",
  2944. [](common_params & params) {
  2945. params.simple_io = true;
  2946. }
  2947. ).set_examples({LLAMA_EXAMPLE_COMPLETION, LLAMA_EXAMPLE_CLI}));
  2948. add_opt(common_arg(
  2949. {"--positive-file"}, "FNAME",
  2950. string_format("positive prompts file, one prompt per line (default: '%s')", params.cvector_positive_file.c_str()),
  2951. [](common_params & params, const std::string & value) {
  2952. params.cvector_positive_file = value;
  2953. }
  2954. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  2955. add_opt(common_arg(
  2956. {"--negative-file"}, "FNAME",
  2957. string_format("negative prompts file, one prompt per line (default: '%s')", params.cvector_negative_file.c_str()),
  2958. [](common_params & params, const std::string & value) {
  2959. params.cvector_negative_file = value;
  2960. }
  2961. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  2962. add_opt(common_arg(
  2963. {"--pca-batch"}, "N",
  2964. string_format("batch size used for PCA. Larger batch runs faster, but uses more memory (default: %d)", params.n_pca_batch),
  2965. [](common_params & params, int value) {
  2966. params.n_pca_batch = value;
  2967. }
  2968. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  2969. add_opt(common_arg(
  2970. {"--pca-iter"}, "N",
  2971. string_format("number of iterations used for PCA (default: %d)", params.n_pca_iterations),
  2972. [](common_params & params, int value) {
  2973. params.n_pca_iterations = value;
  2974. }
  2975. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  2976. add_opt(common_arg(
  2977. {"--method"}, "{pca, mean}",
  2978. "dimensionality reduction method to be used (default: pca)",
  2979. [](common_params & params, const std::string & value) {
  2980. /**/ if (value == "pca") { params.cvector_dimre_method = DIMRE_METHOD_PCA; }
  2981. else if (value == "mean") { params.cvector_dimre_method = DIMRE_METHOD_MEAN; }
  2982. else { throw std::invalid_argument("invalid value"); }
  2983. }
  2984. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  2985. add_opt(common_arg(
  2986. {"--output-format"}, "{md,jsonl}",
  2987. "output format for batched-bench results (default: md)",
  2988. [](common_params & params, const std::string & value) {
  2989. /**/ if (value == "jsonl") { params.batched_bench_output_jsonl = true; }
  2990. else if (value == "md") { params.batched_bench_output_jsonl = false; }
  2991. else { throw std::invalid_argument("invalid value"); }
  2992. }
  2993. ).set_examples({LLAMA_EXAMPLE_BENCH}));
  2994. add_opt(common_arg(
  2995. {"--log-disable"},
  2996. "Log disable",
  2997. [](common_params &) {
  2998. common_log_pause(common_log_main());
  2999. }
  3000. ));
  3001. add_opt(common_arg(
  3002. {"--log-file"}, "FNAME",
  3003. "Log to file",
  3004. [](common_params &, const std::string & value) {
  3005. common_log_set_file(common_log_main(), value.c_str());
  3006. }
  3007. ).set_env("LLAMA_LOG_FILE"));
  3008. add_opt(common_arg(
  3009. {"--log-colors"}, "[on|off|auto]",
  3010. "Set colored logging ('on', 'off', or 'auto', default: 'auto')\n"
  3011. "'auto' enables colors when output is to a terminal",
  3012. [](common_params &, const std::string & value) {
  3013. if (is_truthy(value)) {
  3014. common_log_set_colors(common_log_main(), LOG_COLORS_ENABLED);
  3015. } else if (is_falsey(value)) {
  3016. common_log_set_colors(common_log_main(), LOG_COLORS_DISABLED);
  3017. } else if (is_autoy(value)) {
  3018. common_log_set_colors(common_log_main(), LOG_COLORS_AUTO);
  3019. } else {
  3020. throw std::invalid_argument(
  3021. string_format("error: unknown value for --log-colors: '%s'\n", value.c_str()));
  3022. }
  3023. }
  3024. ).set_env("LLAMA_LOG_COLORS"));
  3025. add_opt(common_arg(
  3026. {"-v", "--verbose", "--log-verbose"},
  3027. "Set verbosity level to infinity (i.e. log all messages, useful for debugging)",
  3028. [](common_params & params) {
  3029. params.verbosity = INT_MAX;
  3030. }
  3031. ));
  3032. add_opt(common_arg(
  3033. {"--offline"},
  3034. "Offline mode: forces use of cache, prevents network access",
  3035. [](common_params & params) {
  3036. params.offline = true;
  3037. }
  3038. ).set_env("LLAMA_OFFLINE"));
  3039. add_opt(common_arg(
  3040. {"-lv", "--verbosity", "--log-verbosity"}, "N",
  3041. string_format("Set the verbosity threshold. Messages with a higher verbosity will be ignored. Values:\n"
  3042. " - 0: generic output\n"
  3043. " - 1: error\n"
  3044. " - 2: warning\n"
  3045. " - 3: info\n"
  3046. " - 4: debug\n"
  3047. "(default: %d)\n", params.verbosity),
  3048. [](common_params & params, int value) {
  3049. params.verbosity = value;
  3050. }
  3051. ).set_env("LLAMA_LOG_VERBOSITY"));
  3052. add_opt(common_arg(
  3053. {"--log-prefix"},
  3054. "Enable prefix in log messages",
  3055. [](common_params &) {
  3056. common_log_set_prefix(common_log_main(), true);
  3057. }
  3058. ).set_env("LLAMA_LOG_PREFIX"));
  3059. add_opt(common_arg(
  3060. {"--log-timestamps"},
  3061. "Enable timestamps in log messages",
  3062. [](common_params &) {
  3063. common_log_set_timestamps(common_log_main(), true);
  3064. }
  3065. ).set_env("LLAMA_LOG_TIMESTAMPS"));
  3066. // speculative parameters
  3067. add_opt(common_arg(
  3068. {"-td", "--threads-draft"}, "N",
  3069. "number of threads to use during generation (default: same as --threads)",
  3070. [](common_params & params, int value) {
  3071. params.speculative.cpuparams.n_threads = value;
  3072. if (params.speculative.cpuparams.n_threads <= 0) {
  3073. params.speculative.cpuparams.n_threads = std::thread::hardware_concurrency();
  3074. }
  3075. }
  3076. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}));
  3077. add_opt(common_arg(
  3078. {"-tbd", "--threads-batch-draft"}, "N",
  3079. "number of threads to use during batch and prompt processing (default: same as --threads-draft)",
  3080. [](common_params & params, int value) {
  3081. params.speculative.cpuparams_batch.n_threads = value;
  3082. if (params.speculative.cpuparams_batch.n_threads <= 0) {
  3083. params.speculative.cpuparams_batch.n_threads = std::thread::hardware_concurrency();
  3084. }
  3085. }
  3086. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}));
  3087. add_opt(common_arg(
  3088. {"-Cd", "--cpu-mask-draft"}, "M",
  3089. "Draft model CPU affinity mask. Complements cpu-range-draft (default: same as --cpu-mask)",
  3090. [](common_params & params, const std::string & mask) {
  3091. params.speculative.cpuparams.mask_valid = true;
  3092. if (!parse_cpu_mask(mask, params.speculative.cpuparams.cpumask)) {
  3093. throw std::invalid_argument("invalid cpumask");
  3094. }
  3095. }
  3096. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  3097. add_opt(common_arg(
  3098. {"-Crd", "--cpu-range-draft"}, "lo-hi",
  3099. "Ranges of CPUs for affinity. Complements --cpu-mask-draft",
  3100. [](common_params & params, const std::string & range) {
  3101. params.speculative.cpuparams.mask_valid = true;
  3102. if (!parse_cpu_range(range, params.speculative.cpuparams.cpumask)) {
  3103. throw std::invalid_argument("invalid range");
  3104. }
  3105. }
  3106. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  3107. add_opt(common_arg(
  3108. {"--cpu-strict-draft"}, "<0|1>",
  3109. "Use strict CPU placement for draft model (default: same as --cpu-strict)",
  3110. [](common_params & params, int value) {
  3111. params.speculative.cpuparams.strict_cpu = value;
  3112. }
  3113. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  3114. add_opt(common_arg(
  3115. {"--prio-draft"}, "N",
  3116. string_format("set draft process/thread priority : 0-normal, 1-medium, 2-high, 3-realtime (default: %d)\n", params.speculative.cpuparams.priority),
  3117. [](common_params & params, int prio) {
  3118. if (prio < 0 || prio > 3) {
  3119. throw std::invalid_argument("invalid value");
  3120. }
  3121. params.speculative.cpuparams.priority = (enum ggml_sched_priority) prio;
  3122. }
  3123. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  3124. add_opt(common_arg(
  3125. {"--poll-draft"}, "<0|1>",
  3126. "Use polling to wait for draft model work (default: same as --poll])",
  3127. [](common_params & params, int value) {
  3128. params.speculative.cpuparams.poll = value;
  3129. }
  3130. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  3131. add_opt(common_arg(
  3132. {"-Cbd", "--cpu-mask-batch-draft"}, "M",
  3133. "Draft model CPU affinity mask. Complements cpu-range-draft (default: same as --cpu-mask)",
  3134. [](common_params & params, const std::string & mask) {
  3135. params.speculative.cpuparams_batch.mask_valid = true;
  3136. if (!parse_cpu_mask(mask, params.speculative.cpuparams_batch.cpumask)) {
  3137. throw std::invalid_argument("invalid cpumask");
  3138. }
  3139. }
  3140. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  3141. add_opt(common_arg(
  3142. {"-Crbd", "--cpu-range-batch-draft"}, "lo-hi",
  3143. "Ranges of CPUs for affinity. Complements --cpu-mask-draft-batch)",
  3144. [](common_params & params, const std::string & range) {
  3145. params.speculative.cpuparams_batch.mask_valid = true;
  3146. if (!parse_cpu_range(range, params.speculative.cpuparams_batch.cpumask)) {
  3147. throw std::invalid_argument("invalid cpumask");
  3148. }
  3149. }
  3150. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  3151. add_opt(common_arg(
  3152. {"--cpu-strict-batch-draft"}, "<0|1>",
  3153. "Use strict CPU placement for draft model (default: --cpu-strict-draft)",
  3154. [](common_params & params, int value) {
  3155. params.speculative.cpuparams_batch.strict_cpu = value;
  3156. }
  3157. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  3158. add_opt(common_arg(
  3159. {"--prio-batch-draft"}, "N",
  3160. string_format("set draft process/thread priority : 0-normal, 1-medium, 2-high, 3-realtime (default: %d)\n", params.speculative.cpuparams_batch.priority),
  3161. [](common_params & params, int prio) {
  3162. if (prio < 0 || prio > 3) {
  3163. throw std::invalid_argument("invalid value");
  3164. }
  3165. params.speculative.cpuparams_batch.priority = (enum ggml_sched_priority) prio;
  3166. }
  3167. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  3168. add_opt(common_arg(
  3169. {"--poll-batch-draft"}, "<0|1>",
  3170. "Use polling to wait for draft model work (default: --poll-draft)",
  3171. [](common_params & params, int value) {
  3172. params.speculative.cpuparams_batch.poll = value;
  3173. }
  3174. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  3175. add_opt(common_arg(
  3176. {"--draft", "--draft-n", "--draft-max"}, "N",
  3177. string_format("number of tokens to draft for speculative decoding (default: %d)", params.speculative.n_max),
  3178. [](common_params & params, int value) {
  3179. params.speculative.n_max = value;
  3180. }
  3181. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_LOOKUP, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_DRAFT_MAX"));
  3182. add_opt(common_arg(
  3183. {"--draft-min", "--draft-n-min"}, "N",
  3184. string_format("minimum number of draft tokens to use for speculative decoding (default: %d)", params.speculative.n_min),
  3185. [](common_params & params, int value) {
  3186. params.speculative.n_min = value;
  3187. }
  3188. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_LOOKUP, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_DRAFT_MIN"));
  3189. add_opt(common_arg(
  3190. {"--draft-p-split"}, "P",
  3191. string_format("speculative decoding split probability (default: %.1f)", (double)params.speculative.p_split),
  3192. [](common_params & params, const std::string & value) {
  3193. params.speculative.p_split = std::stof(value);
  3194. }
  3195. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}).set_env("LLAMA_ARG_DRAFT_P_SPLIT"));
  3196. add_opt(common_arg(
  3197. {"--draft-p-min"}, "P",
  3198. string_format("minimum speculative decoding probability (greedy) (default: %.1f)", (double)params.speculative.p_min),
  3199. [](common_params & params, const std::string & value) {
  3200. params.speculative.p_min = std::stof(value);
  3201. }
  3202. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_DRAFT_P_MIN"));
  3203. add_opt(common_arg(
  3204. {"-cd", "--ctx-size-draft"}, "N",
  3205. string_format("size of the prompt context for the draft model (default: %d, 0 = loaded from model)", params.speculative.n_ctx),
  3206. [](common_params & params, int value) {
  3207. params.speculative.n_ctx = value;
  3208. }
  3209. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_CTX_SIZE_DRAFT"));
  3210. add_opt(common_arg(
  3211. {"-devd", "--device-draft"}, "<dev1,dev2,..>",
  3212. "comma-separated list of devices to use for offloading the draft model (none = don't offload)\n"
  3213. "use --list-devices to see a list of available devices",
  3214. [](common_params & params, const std::string & value) {
  3215. params.speculative.devices = parse_device_list(value);
  3216. }
  3217. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}));
  3218. GGML_ASSERT(params.speculative.n_gpu_layers < 0); // string_format would need to be extended for a default >= 0
  3219. add_opt(common_arg(
  3220. {"-ngld", "--gpu-layers-draft", "--n-gpu-layers-draft"}, "N",
  3221. string_format("max. number of draft model layers to store in VRAM, either an exact number, 'auto', or 'all' (default: %s)",
  3222. params.speculative.n_gpu_layers == -1 ? "auto" : "all"),
  3223. [](common_params & params, const std::string & value) {
  3224. if (value == "auto") {
  3225. params.speculative.n_gpu_layers = -1;
  3226. } else if (value == "all") {
  3227. params.speculative.n_gpu_layers = -2;
  3228. } else {
  3229. params.speculative.n_gpu_layers = std::stoi(value);
  3230. }
  3231. if (!llama_supports_gpu_offload()) {
  3232. fprintf(stderr, "warning: no usable GPU found, --gpu-layers-draft option will be ignored\n");
  3233. fprintf(stderr, "warning: one possible reason is that llama.cpp was compiled without GPU support\n");
  3234. fprintf(stderr, "warning: consult docs/build.md for compilation instructions\n");
  3235. }
  3236. }
  3237. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_N_GPU_LAYERS_DRAFT"));
  3238. add_opt(common_arg(
  3239. {"-md", "--model-draft"}, "FNAME",
  3240. "draft model for speculative decoding (default: unused)",
  3241. [](common_params & params, const std::string & value) {
  3242. params.speculative.model.path = value;
  3243. }
  3244. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_MODEL_DRAFT"));
  3245. add_opt(common_arg(
  3246. {"--spec-replace"}, "TARGET", "DRAFT",
  3247. "translate the string in TARGET into DRAFT if the draft model and main model are not compatible",
  3248. [](common_params & params, const std::string & tgt, const std::string & dft) {
  3249. params.speculative.replacements.push_back({ tgt, dft });
  3250. }
  3251. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}));
  3252. add_opt(common_arg(
  3253. {"-ctkd", "--cache-type-k-draft"}, "TYPE",
  3254. string_format(
  3255. "KV cache data type for K for the draft model\n"
  3256. "allowed values: %s\n"
  3257. "(default: %s)",
  3258. get_all_kv_cache_types().c_str(),
  3259. ggml_type_name(params.speculative.cache_type_k)
  3260. ),
  3261. [](common_params & params, const std::string & value) {
  3262. params.speculative.cache_type_k = kv_cache_type_from_str(value);
  3263. }
  3264. ).set_env("LLAMA_ARG_CACHE_TYPE_K_DRAFT"));
  3265. add_opt(common_arg(
  3266. {"-ctvd", "--cache-type-v-draft"}, "TYPE",
  3267. string_format(
  3268. "KV cache data type for V for the draft model\n"
  3269. "allowed values: %s\n"
  3270. "(default: %s)",
  3271. get_all_kv_cache_types().c_str(),
  3272. ggml_type_name(params.speculative.cache_type_v)
  3273. ),
  3274. [](common_params & params, const std::string & value) {
  3275. params.speculative.cache_type_v = kv_cache_type_from_str(value);
  3276. }
  3277. ).set_env("LLAMA_ARG_CACHE_TYPE_V_DRAFT"));
  3278. add_opt(common_arg(
  3279. {"-mv", "--model-vocoder"}, "FNAME",
  3280. "vocoder model for audio generation (default: unused)",
  3281. [](common_params & params, const std::string & value) {
  3282. params.vocoder.model.path = value;
  3283. }
  3284. ).set_examples({LLAMA_EXAMPLE_TTS, LLAMA_EXAMPLE_SERVER}));
  3285. add_opt(common_arg(
  3286. {"--tts-use-guide-tokens"},
  3287. "Use guide tokens to improve TTS word recall",
  3288. [](common_params & params) {
  3289. params.vocoder.use_guide_tokens = true;
  3290. }
  3291. ).set_examples({LLAMA_EXAMPLE_TTS, LLAMA_EXAMPLE_SERVER}));
  3292. add_opt(common_arg(
  3293. {"--tts-speaker-file"}, "FNAME",
  3294. "speaker file path for audio generation",
  3295. [](common_params & params, const std::string & value) {
  3296. params.vocoder.speaker_file = value;
  3297. }
  3298. ).set_examples({LLAMA_EXAMPLE_TTS}));
  3299. add_opt(common_arg(
  3300. {"--diffusion-steps"}, "N",
  3301. string_format("number of diffusion steps (default: %d)", params.diffusion.steps),
  3302. [](common_params & params, int value) { params.diffusion.steps = value; }
  3303. ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
  3304. add_opt(common_arg(
  3305. {"--diffusion-visual"},
  3306. string_format("enable visual diffusion mode (show progressive generation) (default: %s)", params.diffusion.visual_mode ? "true" : "false"),
  3307. [](common_params & params) { params.diffusion.visual_mode = true; }
  3308. ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
  3309. add_opt(common_arg(
  3310. {"--diffusion-eps"}, "F",
  3311. string_format("epsilon for timesteps (default: %.6f)", (double) params.diffusion.eps),
  3312. [](common_params & params, const std::string & value) { params.diffusion.eps = std::stof(value); }
  3313. ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
  3314. add_opt(common_arg(
  3315. {"--diffusion-algorithm"}, "N",
  3316. string_format("diffusion algorithm: 0=ORIGIN, 1=ENTROPY_BASED, 2=MARGIN_BASED, 3=RANDOM, 4=LOW_CONFIDENCE (default: %d)", params.diffusion.algorithm),
  3317. [](common_params & params, int value) { params.diffusion.algorithm = value; }
  3318. ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
  3319. add_opt(common_arg(
  3320. {"--diffusion-alg-temp"}, "F",
  3321. string_format("dream algorithm temperature (default: %.3f)", (double) params.diffusion.alg_temp),
  3322. [](common_params & params, const std::string & value) { params.diffusion.alg_temp = std::stof(value); }
  3323. ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
  3324. add_opt(common_arg(
  3325. {"--diffusion-block-length"}, "N",
  3326. string_format("llada block length for generation (default: %d)", params.diffusion.block_length),
  3327. [](common_params & params, int value) { params.diffusion.block_length = value; }
  3328. ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
  3329. add_opt(common_arg(
  3330. {"--diffusion-cfg-scale"}, "F",
  3331. string_format("llada classifier-free guidance scale (default: %.3f)", (double) params.diffusion.cfg_scale),
  3332. [](common_params & params, const std::string & value) { params.diffusion.cfg_scale = std::stof(value); }
  3333. ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
  3334. add_opt(common_arg(
  3335. {"--diffusion-add-gumbel-noise"}, "F",
  3336. string_format("add gumbel noise to the logits if temp > 0.0 (default: %s)", params.diffusion.add_gumbel_noise ? "true" : "false"),
  3337. [](common_params & params, const std::string & value) { params.diffusion.add_gumbel_noise = std::stof(value); }
  3338. ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
  3339. add_opt(common_arg(
  3340. { "-lr", "--learning-rate" }, "ALPHA",
  3341. string_format("adamw or sgd optimizer alpha (default: %.2g); note: sgd alpha recommended ~10x (no momentum)", (double) params.lr.lr0),
  3342. [](common_params & params, const std::string & value) { params.lr.lr0 = std::stof(value); }
  3343. ).set_examples({ LLAMA_EXAMPLE_FINETUNE }));
  3344. add_opt(common_arg({ "-lr-min", "--learning-rate-min" }, "ALPHA",
  3345. string_format("(if >0) final learning rate after decay (if -decay-epochs is set, default=%.2g)",
  3346. (double) params.lr.lr_min),
  3347. [](common_params & params, const std::string & value) { params.lr.lr_min = std::stof(value); }
  3348. ).set_examples({ LLAMA_EXAMPLE_FINETUNE }));
  3349. add_opt(common_arg(
  3350. {"-decay-epochs", "--learning-rate-decay-epochs"}, "ALPHA",
  3351. string_format("(if >0) decay learning rate to -lr-min after this many epochs (exponential decay, default=%.2g)", (double) params.lr.decay_epochs),
  3352. [](common_params & params, const std::string & value) { params.lr.decay_epochs = std::stof(value); }
  3353. ).set_examples({ LLAMA_EXAMPLE_FINETUNE }));
  3354. add_opt(common_arg(
  3355. {"-wd", "--weight-decay"}, "WD",
  3356. string_format("adamw or sgd optimizer weight decay (0 is off; recommend very small e.g. 1e-9) (default: %.2g).", (double) params.lr.wd),
  3357. [](common_params & params, const std::string & value) { params.lr.wd = std::stof(value); }
  3358. ).set_examples({ LLAMA_EXAMPLE_FINETUNE }));
  3359. add_opt(common_arg(
  3360. {"-val-split", "--val-split"}, "FRACTION",
  3361. string_format("fraction of data to use as validation set for training (default: %.2g).", (double) params.val_split),
  3362. [](common_params & params, const std::string & value) { params.val_split = std::stof(value); }
  3363. ).set_examples({ LLAMA_EXAMPLE_FINETUNE }));
  3364. add_opt(common_arg(
  3365. {"-epochs", "--epochs"}, "N",
  3366. string_format("optimizer max # of epochs (default: %d)", params.lr.epochs),
  3367. [](common_params & params, int epochs) { params.lr.epochs = epochs; }
  3368. ).set_examples({ LLAMA_EXAMPLE_FINETUNE }));
  3369. add_opt(common_arg(
  3370. {"-opt", "--optimizer"}, "sgd|adamw", "adamw or sgd",
  3371. [](common_params & params, const std::string & name) {
  3372. params.optimizer = common_opt_get_optimizer(name.c_str());
  3373. if (params.optimizer == GGML_OPT_OPTIMIZER_TYPE_COUNT) {
  3374. throw std::invalid_argument("invalid --optimizer, valid options: adamw, sgd");
  3375. }
  3376. }
  3377. ).set_examples({ LLAMA_EXAMPLE_FINETUNE }));
  3378. add_opt(common_arg(
  3379. {"--save-logits"},
  3380. string_format("save final logits to files for verification (default: %s)", params.save_logits ? "true" : "false"),
  3381. [](common_params & params) {
  3382. params.save_logits = true;
  3383. }
  3384. ).set_examples({LLAMA_EXAMPLE_DEBUG}));
  3385. add_opt(common_arg(
  3386. {"--logits-output-dir"}, "PATH",
  3387. string_format("directory for saving logits output files (default: %s)", params.logits_output_dir.c_str()),
  3388. [](common_params & params, const std::string & value) {
  3389. params.logits_output_dir = value;
  3390. }
  3391. ).set_examples({LLAMA_EXAMPLE_DEBUG}));
  3392. add_opt(common_arg(
  3393. {"--tensor-filter"}, "REGEX",
  3394. "filter tensor names for debug output (regex pattern, can be specified multiple times)",
  3395. [](common_params & params, const std::string & value) {
  3396. params.tensor_filter.push_back(value);
  3397. }
  3398. ).set_examples({LLAMA_EXAMPLE_DEBUG}));
  3399. // presets
  3400. add_opt(common_arg(
  3401. {"--tts-oute-default"},
  3402. string_format("use default OuteTTS models (note: can download weights from the internet)"),
  3403. [](common_params & params) {
  3404. params.model.hf_repo = "OuteAI/OuteTTS-0.2-500M-GGUF";
  3405. params.model.hf_file = "OuteTTS-0.2-500M-Q8_0.gguf";
  3406. params.vocoder.model.hf_repo = "ggml-org/WavTokenizer";
  3407. params.vocoder.model.hf_file = "WavTokenizer-Large-75-F16.gguf";
  3408. }
  3409. ).set_examples({LLAMA_EXAMPLE_TTS}));
  3410. add_opt(common_arg(
  3411. {"--embd-gemma-default"},
  3412. string_format("use default EmbeddingGemma model (note: can download weights from the internet)"),
  3413. [](common_params & params) {
  3414. params.model.hf_repo = "ggml-org/embeddinggemma-300M-qat-q4_0-GGUF";
  3415. params.model.hf_file = "embeddinggemma-300M-qat-Q4_0.gguf";
  3416. params.port = 8011;
  3417. params.n_ubatch = 2048;
  3418. params.n_batch = 2048;
  3419. params.n_parallel = 32;
  3420. params.n_ctx = 2048*params.n_parallel;
  3421. params.verbose_prompt = true;
  3422. params.embedding = true;
  3423. }
  3424. ).set_examples({LLAMA_EXAMPLE_EMBEDDING, LLAMA_EXAMPLE_SERVER}));
  3425. add_opt(common_arg(
  3426. {"--fim-qwen-1.5b-default"},
  3427. string_format("use default Qwen 2.5 Coder 1.5B (note: can download weights from the internet)"),
  3428. [](common_params & params) {
  3429. params.model.hf_repo = "ggml-org/Qwen2.5-Coder-1.5B-Q8_0-GGUF";
  3430. params.model.hf_file = "qwen2.5-coder-1.5b-q8_0.gguf";
  3431. params.port = 8012;
  3432. params.n_ubatch = 1024;
  3433. params.n_batch = 1024;
  3434. params.n_ctx = 0;
  3435. params.n_cache_reuse = 256;
  3436. }
  3437. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  3438. add_opt(common_arg(
  3439. {"--fim-qwen-3b-default"},
  3440. string_format("use default Qwen 2.5 Coder 3B (note: can download weights from the internet)"),
  3441. [](common_params & params) {
  3442. params.model.hf_repo = "ggml-org/Qwen2.5-Coder-3B-Q8_0-GGUF";
  3443. params.model.hf_file = "qwen2.5-coder-3b-q8_0.gguf";
  3444. params.port = 8012;
  3445. params.n_ubatch = 1024;
  3446. params.n_batch = 1024;
  3447. params.n_ctx = 0;
  3448. params.n_cache_reuse = 256;
  3449. }
  3450. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  3451. add_opt(common_arg(
  3452. {"--fim-qwen-7b-default"},
  3453. string_format("use default Qwen 2.5 Coder 7B (note: can download weights from the internet)"),
  3454. [](common_params & params) {
  3455. params.model.hf_repo = "ggml-org/Qwen2.5-Coder-7B-Q8_0-GGUF";
  3456. params.model.hf_file = "qwen2.5-coder-7b-q8_0.gguf";
  3457. params.port = 8012;
  3458. params.n_ubatch = 1024;
  3459. params.n_batch = 1024;
  3460. params.n_ctx = 0;
  3461. params.n_cache_reuse = 256;
  3462. }
  3463. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  3464. add_opt(common_arg(
  3465. {"--fim-qwen-7b-spec"},
  3466. string_format("use Qwen 2.5 Coder 7B + 0.5B draft for speculative decoding (note: can download weights from the internet)"),
  3467. [](common_params & params) {
  3468. params.model.hf_repo = "ggml-org/Qwen2.5-Coder-7B-Q8_0-GGUF";
  3469. params.model.hf_file = "qwen2.5-coder-7b-q8_0.gguf";
  3470. params.speculative.model.hf_repo = "ggml-org/Qwen2.5-Coder-0.5B-Q8_0-GGUF";
  3471. params.speculative.model.hf_file = "qwen2.5-coder-0.5b-q8_0.gguf";
  3472. params.port = 8012;
  3473. params.n_ubatch = 1024;
  3474. params.n_batch = 1024;
  3475. params.n_ctx = 0;
  3476. params.n_cache_reuse = 256;
  3477. }
  3478. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  3479. add_opt(common_arg(
  3480. {"--fim-qwen-14b-spec"},
  3481. string_format("use Qwen 2.5 Coder 14B + 0.5B draft for speculative decoding (note: can download weights from the internet)"),
  3482. [](common_params & params) {
  3483. params.model.hf_repo = "ggml-org/Qwen2.5-Coder-14B-Q8_0-GGUF";
  3484. params.model.hf_file = "qwen2.5-coder-14b-q8_0.gguf";
  3485. params.speculative.model.hf_repo = "ggml-org/Qwen2.5-Coder-0.5B-Q8_0-GGUF";
  3486. params.speculative.model.hf_file = "qwen2.5-coder-0.5b-q8_0.gguf";
  3487. params.port = 8012;
  3488. params.n_ubatch = 1024;
  3489. params.n_batch = 1024;
  3490. params.n_ctx = 0;
  3491. params.n_cache_reuse = 256;
  3492. }
  3493. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  3494. add_opt(common_arg(
  3495. {"--fim-qwen-30b-default"},
  3496. string_format("use default Qwen 3 Coder 30B A3B Instruct (note: can download weights from the internet)"),
  3497. [](common_params & params) {
  3498. params.model.hf_repo = "ggml-org/Qwen3-Coder-30B-A3B-Instruct-Q8_0-GGUF";
  3499. params.model.hf_file = "qwen3-coder-30b-a3b-instruct-q8_0.gguf";
  3500. params.port = 8012;
  3501. params.n_ubatch = 1024;
  3502. params.n_batch = 1024;
  3503. params.n_ctx = 0;
  3504. params.n_cache_reuse = 256;
  3505. }
  3506. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  3507. add_opt(common_arg(
  3508. {"--gpt-oss-20b-default"},
  3509. string_format("use gpt-oss-20b (note: can download weights from the internet)"),
  3510. [](common_params & params) {
  3511. params.model.hf_repo = "ggml-org/gpt-oss-20b-GGUF";
  3512. params.model.hf_file = "gpt-oss-20b-mxfp4.gguf";
  3513. params.port = 8013;
  3514. params.n_ubatch = 2048;
  3515. params.n_batch = 32768;
  3516. params.n_parallel = 2;
  3517. params.n_ctx = 131072*params.n_parallel;
  3518. params.sampling.temp = 1.0f;
  3519. params.sampling.top_p = 1.0f;
  3520. params.sampling.top_k = 0;
  3521. params.sampling.min_p = 0.01f;
  3522. params.use_jinja = true;
  3523. //params.default_template_kwargs["reasoning_effort"] = "\"high\"";
  3524. }
  3525. ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}));
  3526. add_opt(common_arg(
  3527. {"--gpt-oss-120b-default"},
  3528. string_format("use gpt-oss-120b (note: can download weights from the internet)"),
  3529. [](common_params & params) {
  3530. params.model.hf_repo = "ggml-org/gpt-oss-120b-GGUF";
  3531. params.port = 8013;
  3532. params.n_ubatch = 2048;
  3533. params.n_batch = 32768;
  3534. params.n_parallel = 2;
  3535. params.n_ctx = 131072*params.n_parallel;
  3536. params.sampling.temp = 1.0f;
  3537. params.sampling.top_p = 1.0f;
  3538. params.sampling.top_k = 0;
  3539. params.sampling.min_p = 0.01f;
  3540. params.use_jinja = true;
  3541. //params.default_template_kwargs["reasoning_effort"] = "\"high\"";
  3542. }
  3543. ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}));
  3544. add_opt(common_arg(
  3545. {"--vision-gemma-4b-default"},
  3546. string_format("use Gemma 3 4B QAT (note: can download weights from the internet)"),
  3547. [](common_params & params) {
  3548. params.model.hf_repo = "ggml-org/gemma-3-4b-it-qat-GGUF";
  3549. params.port = 8014;
  3550. params.n_ctx = 0;
  3551. params.use_jinja = true;
  3552. }
  3553. ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}));
  3554. add_opt(common_arg(
  3555. {"--vision-gemma-12b-default"},
  3556. string_format("use Gemma 3 12B QAT (note: can download weights from the internet)"),
  3557. [](common_params & params) {
  3558. params.model.hf_repo = "ggml-org/gemma-3-12b-it-qat-GGUF";
  3559. params.port = 8014;
  3560. params.n_ctx = 0;
  3561. params.use_jinja = true;
  3562. }
  3563. ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}));
  3564. return ctx_arg;
  3565. }
  3566. void common_params_add_preset_options(std::vector<common_arg> & args) {
  3567. // arguments below won't be treated as CLI args, only preset options
  3568. args.push_back(common_arg(
  3569. {"load-on-startup"}, "NAME",
  3570. "in server router mode, autoload this model on startup",
  3571. [](common_params &, const std::string &) { /* unused */ }
  3572. ).set_env(COMMON_ARG_PRESET_LOAD_ON_STARTUP).set_preset_only());
  3573. args.push_back(common_arg(
  3574. {"stop-timeout"}, "SECONDS",
  3575. "in server router mode, force-kill model instance after this many seconds of graceful shutdown",
  3576. [](common_params &, int) { /* unused */ }
  3577. ).set_env(COMMON_ARG_PRESET_STOP_TIMEOUT).set_preset_only());
  3578. // args.push_back(common_arg(
  3579. // {"pin"},
  3580. // "in server router mode, do not unload this model if models_max is exceeded",
  3581. // [](common_params &) { /* unused */ }
  3582. // ).set_preset_only());
  3583. }