arg.cpp 139 KB

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