arg.cpp 163 KB

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