arg.cpp 112 KB

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