arg.cpp 140 KB

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