arg.cpp 156 KB

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