arg.cpp 153 KB

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