arg.cpp 154 KB

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