arg.cpp 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606
  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("number of tokens to predict (default: %d, -1 = infinity, -2 = until context filled)", params.n_predict),
  707. [](common_params & params, int value) {
  708. params.n_predict = value;
  709. }
  710. ).set_env("LLAMA_ARG_N_PREDICT"));
  711. add_opt(common_arg(
  712. {"-b", "--batch-size"}, "N",
  713. string_format("logical maximum batch size (default: %d)", params.n_batch),
  714. [](common_params & params, int value) {
  715. params.n_batch = value;
  716. }
  717. ).set_env("LLAMA_ARG_BATCH"));
  718. add_opt(common_arg(
  719. {"-ub", "--ubatch-size"}, "N",
  720. string_format("physical maximum batch size (default: %d)", params.n_ubatch),
  721. [](common_params & params, int value) {
  722. params.n_ubatch = value;
  723. }
  724. ).set_env("LLAMA_ARG_UBATCH"));
  725. add_opt(common_arg(
  726. {"--keep"}, "N",
  727. string_format("number of tokens to keep from the initial prompt (default: %d, -1 = all)", params.n_keep),
  728. [](common_params & params, int value) {
  729. params.n_keep = value;
  730. }
  731. ));
  732. add_opt(common_arg(
  733. {"--no-context-shift"},
  734. string_format("disables context shift on infinite text generation (default: %s)", params.ctx_shift ? "disabled" : "enabled"),
  735. [](common_params & params) {
  736. params.ctx_shift = false;
  737. }
  738. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_IMATRIX, LLAMA_EXAMPLE_PERPLEXITY}).set_env("LLAMA_ARG_NO_CONTEXT_SHIFT"));
  739. add_opt(common_arg(
  740. {"--chunks"}, "N",
  741. string_format("max number of chunks to process (default: %d, -1 = all)", params.n_chunks),
  742. [](common_params & params, int value) {
  743. params.n_chunks = value;
  744. }
  745. ).set_examples({LLAMA_EXAMPLE_IMATRIX, LLAMA_EXAMPLE_PERPLEXITY, LLAMA_EXAMPLE_RETRIEVAL}));
  746. add_opt(common_arg(
  747. {"-fa", "--flash-attn"},
  748. string_format("enable Flash Attention (default: %s)", params.flash_attn ? "enabled" : "disabled"),
  749. [](common_params & params) {
  750. params.flash_attn = true;
  751. }
  752. ).set_env("LLAMA_ARG_FLASH_ATTN"));
  753. add_opt(common_arg(
  754. {"-p", "--prompt"}, "PROMPT",
  755. "prompt to start generation with; for system message, use -sys",
  756. [](common_params & params, const std::string & value) {
  757. params.prompt = value;
  758. }
  759. ).set_excludes({LLAMA_EXAMPLE_SERVER}));
  760. add_opt(common_arg(
  761. {"-sys", "--system-prompt"}, "PROMPT",
  762. "system prompt to use with model (if applicable, depending on chat template)",
  763. [](common_params & params, const std::string & value) {
  764. params.system_prompt = value;
  765. }
  766. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  767. add_opt(common_arg(
  768. {"--no-perf"},
  769. string_format("disable internal libllama performance timings (default: %s)", params.no_perf ? "true" : "false"),
  770. [](common_params & params) {
  771. params.no_perf = true;
  772. params.sampling.no_perf = true;
  773. }
  774. ).set_env("LLAMA_ARG_NO_PERF"));
  775. add_opt(common_arg(
  776. {"-f", "--file"}, "FNAME",
  777. "a file containing the prompt (default: none)",
  778. [](common_params & params, const std::string & value) {
  779. std::ifstream file(value);
  780. if (!file) {
  781. throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
  782. }
  783. // store the external file name in params
  784. params.prompt_file = value;
  785. std::copy(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), back_inserter(params.prompt));
  786. if (!params.prompt.empty() && params.prompt.back() == '\n') {
  787. params.prompt.pop_back();
  788. }
  789. }
  790. ).set_excludes({LLAMA_EXAMPLE_SERVER}));
  791. add_opt(common_arg(
  792. {"--in-file"}, "FNAME",
  793. "an input file (repeat to specify multiple files)",
  794. [](common_params & params, const std::string & value) {
  795. std::ifstream file(value);
  796. if (!file) {
  797. throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
  798. }
  799. params.in_files.push_back(value);
  800. }
  801. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  802. add_opt(common_arg(
  803. {"-bf", "--binary-file"}, "FNAME",
  804. "binary file containing the prompt (default: none)",
  805. [](common_params & params, const std::string & value) {
  806. std::ifstream file(value, std::ios::binary);
  807. if (!file) {
  808. throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
  809. }
  810. // store the external file name in params
  811. params.prompt_file = value;
  812. std::ostringstream ss;
  813. ss << file.rdbuf();
  814. params.prompt = ss.str();
  815. fprintf(stderr, "Read %zu bytes from binary file %s\n", params.prompt.size(), value.c_str());
  816. }
  817. ).set_excludes({LLAMA_EXAMPLE_SERVER}));
  818. add_opt(common_arg(
  819. {"-e", "--escape"},
  820. string_format("process escapes sequences (\\n, \\r, \\t, \\', \\\", \\\\) (default: %s)", params.escape ? "true" : "false"),
  821. [](common_params & params) {
  822. params.escape = true;
  823. }
  824. ));
  825. add_opt(common_arg(
  826. {"--no-escape"},
  827. "do not process escape sequences",
  828. [](common_params & params) {
  829. params.escape = false;
  830. }
  831. ));
  832. add_opt(common_arg(
  833. {"-ptc", "--print-token-count"}, "N",
  834. string_format("print token count every N tokens (default: %d)", params.n_print),
  835. [](common_params & params, int value) {
  836. params.n_print = value;
  837. }
  838. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  839. add_opt(common_arg(
  840. {"--prompt-cache"}, "FNAME",
  841. "file to cache prompt state for faster startup (default: none)",
  842. [](common_params & params, const std::string & value) {
  843. params.path_prompt_cache = value;
  844. }
  845. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  846. add_opt(common_arg(
  847. {"--prompt-cache-all"},
  848. "if specified, saves user input and generations to cache as well\n",
  849. [](common_params & params) {
  850. params.prompt_cache_all = true;
  851. }
  852. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  853. add_opt(common_arg(
  854. {"--prompt-cache-ro"},
  855. "if specified, uses the prompt cache but does not update it",
  856. [](common_params & params) {
  857. params.prompt_cache_ro = true;
  858. }
  859. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  860. add_opt(common_arg(
  861. {"-r", "--reverse-prompt"}, "PROMPT",
  862. "halt generation at PROMPT, return control in interactive mode\n",
  863. [](common_params & params, const std::string & value) {
  864. params.antiprompt.emplace_back(value);
  865. }
  866. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  867. add_opt(common_arg(
  868. {"-sp", "--special"},
  869. string_format("special tokens output enabled (default: %s)", params.special ? "true" : "false"),
  870. [](common_params & params) {
  871. params.special = true;
  872. }
  873. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_SERVER}));
  874. add_opt(common_arg(
  875. {"-cnv", "--conversation"},
  876. "run in conversation mode:\n"
  877. "- does not print special tokens and suffix/prefix\n"
  878. "- interactive mode is also enabled\n"
  879. "(default: auto enabled if chat template is available)",
  880. [](common_params & params) {
  881. params.conversation_mode = COMMON_CONVERSATION_MODE_ENABLED;
  882. }
  883. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  884. add_opt(common_arg(
  885. {"-no-cnv", "--no-conversation"},
  886. "force disable conversation mode (default: false)",
  887. [](common_params & params) {
  888. params.conversation_mode = COMMON_CONVERSATION_MODE_DISABLED;
  889. }
  890. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  891. add_opt(common_arg(
  892. {"-st", "--single-turn"},
  893. "run conversation for a single turn only, then exit when done\n"
  894. "will not be interactive if first turn is predefined with --prompt\n"
  895. "(default: false)",
  896. [](common_params & params) {
  897. params.single_turn = true;
  898. }
  899. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  900. add_opt(common_arg(
  901. {"-i", "--interactive"},
  902. string_format("run in interactive mode (default: %s)", params.interactive ? "true" : "false"),
  903. [](common_params & params) {
  904. params.interactive = true;
  905. }
  906. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  907. add_opt(common_arg(
  908. {"-if", "--interactive-first"},
  909. string_format("run in interactive mode and wait for input right away (default: %s)", params.interactive_first ? "true" : "false"),
  910. [](common_params & params) {
  911. params.interactive_first = true;
  912. }
  913. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  914. add_opt(common_arg(
  915. {"-mli", "--multiline-input"},
  916. "allows you to write or paste multiple lines without ending each in '\\'",
  917. [](common_params & params) {
  918. params.multiline_input = true;
  919. }
  920. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  921. add_opt(common_arg(
  922. {"--in-prefix-bos"},
  923. "prefix BOS to user inputs, preceding the `--in-prefix` string",
  924. [](common_params & params) {
  925. params.input_prefix_bos = true;
  926. params.enable_chat_template = false;
  927. }
  928. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  929. add_opt(common_arg(
  930. {"--in-prefix"}, "STRING",
  931. "string to prefix user inputs with (default: empty)",
  932. [](common_params & params, const std::string & value) {
  933. params.input_prefix = value;
  934. params.enable_chat_template = false;
  935. }
  936. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_INFILL}));
  937. add_opt(common_arg(
  938. {"--in-suffix"}, "STRING",
  939. "string to suffix after user inputs with (default: empty)",
  940. [](common_params & params, const std::string & value) {
  941. params.input_suffix = value;
  942. params.enable_chat_template = false;
  943. }
  944. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_INFILL}));
  945. add_opt(common_arg(
  946. {"--no-warmup"},
  947. "skip warming up the model with an empty run",
  948. [](common_params & params) {
  949. params.warmup = false;
  950. }
  951. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_EMBEDDING}));
  952. add_opt(common_arg(
  953. {"--spm-infill"},
  954. string_format(
  955. "use Suffix/Prefix/Middle pattern for infill (instead of Prefix/Suffix/Middle) as some models prefer this. (default: %s)",
  956. params.spm_infill ? "enabled" : "disabled"
  957. ),
  958. [](common_params & params) {
  959. params.spm_infill = true;
  960. }
  961. ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_INFILL}));
  962. add_opt(common_arg(
  963. {"--samplers"}, "SAMPLERS",
  964. string_format("samplers that will be used for generation in the order, separated by \';\'\n(default: %s)", sampler_type_names.c_str()),
  965. [](common_params & params, const std::string & value) {
  966. const auto sampler_names = string_split<std::string>(value, ';');
  967. params.sampling.samplers = common_sampler_types_from_names(sampler_names, true);
  968. }
  969. ).set_sparam());
  970. add_opt(common_arg(
  971. {"-s", "--seed"}, "SEED",
  972. string_format("RNG seed (default: %d, use random seed for %d)", params.sampling.seed, LLAMA_DEFAULT_SEED),
  973. [](common_params & params, const std::string & value) {
  974. params.sampling.seed = std::stoul(value);
  975. }
  976. ).set_sparam());
  977. add_opt(common_arg(
  978. {"--sampling-seq", "--sampler-seq"}, "SEQUENCE",
  979. string_format("simplified sequence for samplers that will be used (default: %s)", sampler_type_chars.c_str()),
  980. [](common_params & params, const std::string & value) {
  981. params.sampling.samplers = common_sampler_types_from_chars(value);
  982. }
  983. ).set_sparam());
  984. add_opt(common_arg(
  985. {"--ignore-eos"},
  986. "ignore end of stream token and continue generating (implies --logit-bias EOS-inf)",
  987. [](common_params & params) {
  988. params.sampling.ignore_eos = true;
  989. }
  990. ).set_sparam());
  991. add_opt(common_arg(
  992. {"--temp"}, "N",
  993. string_format("temperature (default: %.1f)", (double)params.sampling.temp),
  994. [](common_params & params, const std::string & value) {
  995. params.sampling.temp = std::stof(value);
  996. params.sampling.temp = std::max(params.sampling.temp, 0.0f);
  997. }
  998. ).set_sparam());
  999. add_opt(common_arg(
  1000. {"--top-k"}, "N",
  1001. string_format("top-k sampling (default: %d, 0 = disabled)", params.sampling.top_k),
  1002. [](common_params & params, int value) {
  1003. params.sampling.top_k = value;
  1004. }
  1005. ).set_sparam());
  1006. add_opt(common_arg(
  1007. {"--top-p"}, "N",
  1008. string_format("top-p sampling (default: %.1f, 1.0 = disabled)", (double)params.sampling.top_p),
  1009. [](common_params & params, const std::string & value) {
  1010. params.sampling.top_p = std::stof(value);
  1011. }
  1012. ).set_sparam());
  1013. add_opt(common_arg(
  1014. {"--min-p"}, "N",
  1015. string_format("min-p sampling (default: %.1f, 0.0 = disabled)", (double)params.sampling.min_p),
  1016. [](common_params & params, const std::string & value) {
  1017. params.sampling.min_p = std::stof(value);
  1018. }
  1019. ).set_sparam());
  1020. add_opt(common_arg(
  1021. {"--top-nsigma"}, "N",
  1022. string_format("top-n-sigma sampling (default: %.1f, -1.0 = disabled)", params.sampling.top_n_sigma),
  1023. [](common_params & params, const std::string & value) {
  1024. params.sampling.top_n_sigma = std::stof(value);
  1025. }
  1026. ).set_examples({LLAMA_EXAMPLE_MAIN}).set_sparam());
  1027. add_opt(common_arg(
  1028. {"--xtc-probability"}, "N",
  1029. string_format("xtc probability (default: %.1f, 0.0 = disabled)", (double)params.sampling.xtc_probability),
  1030. [](common_params & params, const std::string & value) {
  1031. params.sampling.xtc_probability = std::stof(value);
  1032. }
  1033. ).set_sparam());
  1034. add_opt(common_arg(
  1035. {"--xtc-threshold"}, "N",
  1036. string_format("xtc threshold (default: %.1f, 1.0 = disabled)", (double)params.sampling.xtc_threshold),
  1037. [](common_params & params, const std::string & value) {
  1038. params.sampling.xtc_threshold = std::stof(value);
  1039. }
  1040. ).set_sparam());
  1041. add_opt(common_arg(
  1042. {"--typical"}, "N",
  1043. string_format("locally typical sampling, parameter p (default: %.1f, 1.0 = disabled)", (double)params.sampling.typ_p),
  1044. [](common_params & params, const std::string & value) {
  1045. params.sampling.typ_p = std::stof(value);
  1046. }
  1047. ).set_sparam());
  1048. add_opt(common_arg(
  1049. {"--repeat-last-n"}, "N",
  1050. string_format("last n tokens to consider for penalize (default: %d, 0 = disabled, -1 = ctx_size)", params.sampling.penalty_last_n),
  1051. [](common_params & params, int value) {
  1052. if (value < -1) {
  1053. throw std::runtime_error(string_format("error: invalid repeat-last-n = %d\n", value));
  1054. }
  1055. params.sampling.penalty_last_n = value;
  1056. params.sampling.n_prev = std::max(params.sampling.n_prev, params.sampling.penalty_last_n);
  1057. }
  1058. ).set_sparam());
  1059. add_opt(common_arg(
  1060. {"--repeat-penalty"}, "N",
  1061. string_format("penalize repeat sequence of tokens (default: %.1f, 1.0 = disabled)", (double)params.sampling.penalty_repeat),
  1062. [](common_params & params, const std::string & value) {
  1063. params.sampling.penalty_repeat = std::stof(value);
  1064. }
  1065. ).set_sparam());
  1066. add_opt(common_arg(
  1067. {"--presence-penalty"}, "N",
  1068. string_format("repeat alpha presence penalty (default: %.1f, 0.0 = disabled)", (double)params.sampling.penalty_present),
  1069. [](common_params & params, const std::string & value) {
  1070. params.sampling.penalty_present = std::stof(value);
  1071. }
  1072. ).set_sparam());
  1073. add_opt(common_arg(
  1074. {"--frequency-penalty"}, "N",
  1075. string_format("repeat alpha frequency penalty (default: %.1f, 0.0 = disabled)", (double)params.sampling.penalty_freq),
  1076. [](common_params & params, const std::string & value) {
  1077. params.sampling.penalty_freq = std::stof(value);
  1078. }
  1079. ).set_sparam());
  1080. add_opt(common_arg(
  1081. {"--dry-multiplier"}, "N",
  1082. string_format("set DRY sampling multiplier (default: %.1f, 0.0 = disabled)", (double)params.sampling.dry_multiplier),
  1083. [](common_params & params, const std::string & value) {
  1084. params.sampling.dry_multiplier = std::stof(value);
  1085. }
  1086. ).set_sparam());
  1087. add_opt(common_arg(
  1088. {"--dry-base"}, "N",
  1089. string_format("set DRY sampling base value (default: %.2f)", (double)params.sampling.dry_base),
  1090. [](common_params & params, const std::string & value) {
  1091. float potential_base = std::stof(value);
  1092. if (potential_base >= 1.0f)
  1093. {
  1094. params.sampling.dry_base = potential_base;
  1095. }
  1096. }
  1097. ).set_sparam());
  1098. add_opt(common_arg(
  1099. {"--dry-allowed-length"}, "N",
  1100. string_format("set allowed length for DRY sampling (default: %d)", params.sampling.dry_allowed_length),
  1101. [](common_params & params, int value) {
  1102. params.sampling.dry_allowed_length = value;
  1103. }
  1104. ).set_sparam());
  1105. add_opt(common_arg(
  1106. {"--dry-penalty-last-n"}, "N",
  1107. string_format("set DRY penalty for the last n tokens (default: %d, 0 = disable, -1 = context size)", params.sampling.dry_penalty_last_n),
  1108. [](common_params & params, int value) {
  1109. if (value < -1) {
  1110. throw std::runtime_error(string_format("error: invalid dry-penalty-last-n = %d\n", value));
  1111. }
  1112. params.sampling.dry_penalty_last_n = value;
  1113. }
  1114. ).set_sparam());
  1115. add_opt(common_arg(
  1116. {"--dry-sequence-breaker"}, "STRING",
  1117. 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",
  1118. params.sampling.dry_sequence_breakers.empty() ? "none" :
  1119. std::accumulate(std::next(params.sampling.dry_sequence_breakers.begin()),
  1120. params.sampling.dry_sequence_breakers.end(),
  1121. std::string("'") + (params.sampling.dry_sequence_breakers[0] == "\n" ? "\\n" : params.sampling.dry_sequence_breakers[0]) + "'",
  1122. [](const std::string& a, const std::string& b) {
  1123. std::string formatted_b = (b == "\n") ? "\\n" : b;
  1124. return a + ", '" + formatted_b + "'";
  1125. }).c_str()),
  1126. [](common_params & params, const std::string & value) {
  1127. static bool defaults_cleared = false;
  1128. if (!defaults_cleared) {
  1129. params.sampling.dry_sequence_breakers.clear();
  1130. defaults_cleared = true;
  1131. }
  1132. if (value == "none") {
  1133. params.sampling.dry_sequence_breakers.clear();
  1134. } else {
  1135. params.sampling.dry_sequence_breakers.emplace_back(value);
  1136. }
  1137. }
  1138. ).set_sparam());
  1139. add_opt(common_arg(
  1140. {"--dynatemp-range"}, "N",
  1141. string_format("dynamic temperature range (default: %.1f, 0.0 = disabled)", (double)params.sampling.dynatemp_range),
  1142. [](common_params & params, const std::string & value) {
  1143. params.sampling.dynatemp_range = std::stof(value);
  1144. }
  1145. ).set_sparam());
  1146. add_opt(common_arg(
  1147. {"--dynatemp-exp"}, "N",
  1148. string_format("dynamic temperature exponent (default: %.1f)", (double)params.sampling.dynatemp_exponent),
  1149. [](common_params & params, const std::string & value) {
  1150. params.sampling.dynatemp_exponent = std::stof(value);
  1151. }
  1152. ).set_sparam());
  1153. add_opt(common_arg(
  1154. {"--mirostat"}, "N",
  1155. string_format("use Mirostat sampling.\nTop K, Nucleus and Locally Typical samplers are ignored if used.\n"
  1156. "(default: %d, 0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0)", params.sampling.mirostat),
  1157. [](common_params & params, int value) {
  1158. params.sampling.mirostat = value;
  1159. }
  1160. ).set_sparam());
  1161. add_opt(common_arg(
  1162. {"--mirostat-lr"}, "N",
  1163. string_format("Mirostat learning rate, parameter eta (default: %.1f)", (double)params.sampling.mirostat_eta),
  1164. [](common_params & params, const std::string & value) {
  1165. params.sampling.mirostat_eta = std::stof(value);
  1166. }
  1167. ).set_sparam());
  1168. add_opt(common_arg(
  1169. {"--mirostat-ent"}, "N",
  1170. string_format("Mirostat target entropy, parameter tau (default: %.1f)", (double)params.sampling.mirostat_tau),
  1171. [](common_params & params, const std::string & value) {
  1172. params.sampling.mirostat_tau = std::stof(value);
  1173. }
  1174. ).set_sparam());
  1175. add_opt(common_arg(
  1176. {"-l", "--logit-bias"}, "TOKEN_ID(+/-)BIAS",
  1177. "modifies the likelihood of token appearing in the completion,\n"
  1178. "i.e. `--logit-bias 15043+1` to increase likelihood of token ' Hello',\n"
  1179. "or `--logit-bias 15043-1` to decrease likelihood of token ' Hello'",
  1180. [](common_params & params, const std::string & value) {
  1181. std::stringstream ss(value);
  1182. llama_token key;
  1183. char sign;
  1184. std::string value_str;
  1185. try {
  1186. if (ss >> key && ss >> sign && std::getline(ss, value_str) && (sign == '+' || sign == '-')) {
  1187. const float bias = std::stof(value_str) * ((sign == '-') ? -1.0f : 1.0f);
  1188. params.sampling.logit_bias.push_back({key, bias});
  1189. } else {
  1190. throw std::invalid_argument("invalid input format");
  1191. }
  1192. } catch (const std::exception&) {
  1193. throw std::invalid_argument("invalid input format");
  1194. }
  1195. }
  1196. ).set_sparam());
  1197. add_opt(common_arg(
  1198. {"--grammar"}, "GRAMMAR",
  1199. string_format("BNF-like grammar to constrain generations (see samples in grammars/ dir) (default: '%s')", params.sampling.grammar.c_str()),
  1200. [](common_params & params, const std::string & value) {
  1201. params.sampling.grammar = value;
  1202. }
  1203. ).set_sparam());
  1204. add_opt(common_arg(
  1205. {"--grammar-file"}, "FNAME",
  1206. "file to read grammar from",
  1207. [](common_params & params, const std::string & value) {
  1208. std::ifstream file(value);
  1209. if (!file) {
  1210. throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
  1211. }
  1212. std::copy(
  1213. std::istreambuf_iterator<char>(file),
  1214. std::istreambuf_iterator<char>(),
  1215. std::back_inserter(params.sampling.grammar)
  1216. );
  1217. }
  1218. ).set_sparam());
  1219. add_opt(common_arg(
  1220. {"-j", "--json-schema"}, "SCHEMA",
  1221. "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",
  1222. [](common_params & params, const std::string & value) {
  1223. params.sampling.grammar = json_schema_to_grammar(json::parse(value));
  1224. }
  1225. ).set_sparam());
  1226. add_opt(common_arg(
  1227. {"--pooling"}, "{none,mean,cls,last,rank}",
  1228. "pooling type for embeddings, use model default if unspecified",
  1229. [](common_params & params, const std::string & value) {
  1230. /**/ if (value == "none") { params.pooling_type = LLAMA_POOLING_TYPE_NONE; }
  1231. else if (value == "mean") { params.pooling_type = LLAMA_POOLING_TYPE_MEAN; }
  1232. else if (value == "cls") { params.pooling_type = LLAMA_POOLING_TYPE_CLS; }
  1233. else if (value == "last") { params.pooling_type = LLAMA_POOLING_TYPE_LAST; }
  1234. else if (value == "rank") { params.pooling_type = LLAMA_POOLING_TYPE_RANK; }
  1235. else { throw std::invalid_argument("invalid value"); }
  1236. }
  1237. ).set_examples({LLAMA_EXAMPLE_EMBEDDING, LLAMA_EXAMPLE_RETRIEVAL, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_POOLING"));
  1238. add_opt(common_arg(
  1239. {"--attention"}, "{causal,non-causal}",
  1240. "attention type for embeddings, use model default if unspecified",
  1241. [](common_params & params, const std::string & value) {
  1242. /**/ if (value == "causal") { params.attention_type = LLAMA_ATTENTION_TYPE_CAUSAL; }
  1243. else if (value == "non-causal") { params.attention_type = LLAMA_ATTENTION_TYPE_NON_CAUSAL; }
  1244. else { throw std::invalid_argument("invalid value"); }
  1245. }
  1246. ).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
  1247. add_opt(common_arg(
  1248. {"--rope-scaling"}, "{none,linear,yarn}",
  1249. "RoPE frequency scaling method, defaults to linear unless specified by the model",
  1250. [](common_params & params, const std::string & value) {
  1251. /**/ if (value == "none") { params.rope_scaling_type = LLAMA_ROPE_SCALING_TYPE_NONE; }
  1252. else if (value == "linear") { params.rope_scaling_type = LLAMA_ROPE_SCALING_TYPE_LINEAR; }
  1253. else if (value == "yarn") { params.rope_scaling_type = LLAMA_ROPE_SCALING_TYPE_YARN; }
  1254. else { throw std::invalid_argument("invalid value"); }
  1255. }
  1256. ).set_env("LLAMA_ARG_ROPE_SCALING_TYPE"));
  1257. add_opt(common_arg(
  1258. {"--rope-scale"}, "N",
  1259. "RoPE context scaling factor, expands context by a factor of N",
  1260. [](common_params & params, const std::string & value) {
  1261. params.rope_freq_scale = 1.0f / std::stof(value);
  1262. }
  1263. ).set_env("LLAMA_ARG_ROPE_SCALE"));
  1264. add_opt(common_arg(
  1265. {"--rope-freq-base"}, "N",
  1266. "RoPE base frequency, used by NTK-aware scaling (default: loaded from model)",
  1267. [](common_params & params, const std::string & value) {
  1268. params.rope_freq_base = std::stof(value);
  1269. }
  1270. ).set_env("LLAMA_ARG_ROPE_FREQ_BASE"));
  1271. add_opt(common_arg(
  1272. {"--rope-freq-scale"}, "N",
  1273. "RoPE frequency scaling factor, expands context by a factor of 1/N",
  1274. [](common_params & params, const std::string & value) {
  1275. params.rope_freq_scale = std::stof(value);
  1276. }
  1277. ).set_env("LLAMA_ARG_ROPE_FREQ_SCALE"));
  1278. add_opt(common_arg(
  1279. {"--yarn-orig-ctx"}, "N",
  1280. string_format("YaRN: original context size of model (default: %d = model training context size)", params.yarn_orig_ctx),
  1281. [](common_params & params, int value) {
  1282. params.yarn_orig_ctx = value;
  1283. }
  1284. ).set_env("LLAMA_ARG_YARN_ORIG_CTX"));
  1285. add_opt(common_arg(
  1286. {"--yarn-ext-factor"}, "N",
  1287. string_format("YaRN: extrapolation mix factor (default: %.1f, 0.0 = full interpolation)", (double)params.yarn_ext_factor),
  1288. [](common_params & params, const std::string & value) {
  1289. params.yarn_ext_factor = std::stof(value);
  1290. }
  1291. ).set_env("LLAMA_ARG_YARN_EXT_FACTOR"));
  1292. add_opt(common_arg(
  1293. {"--yarn-attn-factor"}, "N",
  1294. string_format("YaRN: scale sqrt(t) or attention magnitude (default: %.1f)", (double)params.yarn_attn_factor),
  1295. [](common_params & params, const std::string & value) {
  1296. params.yarn_attn_factor = std::stof(value);
  1297. }
  1298. ).set_env("LLAMA_ARG_YARN_ATTN_FACTOR"));
  1299. add_opt(common_arg(
  1300. {"--yarn-beta-slow"}, "N",
  1301. string_format("YaRN: high correction dim or alpha (default: %.1f)", (double)params.yarn_beta_slow),
  1302. [](common_params & params, const std::string & value) {
  1303. params.yarn_beta_slow = std::stof(value);
  1304. }
  1305. ).set_env("LLAMA_ARG_YARN_BETA_SLOW"));
  1306. add_opt(common_arg(
  1307. {"--yarn-beta-fast"}, "N",
  1308. string_format("YaRN: low correction dim or beta (default: %.1f)", (double)params.yarn_beta_fast),
  1309. [](common_params & params, const std::string & value) {
  1310. params.yarn_beta_fast = std::stof(value);
  1311. }
  1312. ).set_env("LLAMA_ARG_YARN_BETA_FAST"));
  1313. add_opt(common_arg(
  1314. {"-gan", "--grp-attn-n"}, "N",
  1315. string_format("group-attention factor (default: %d)", params.grp_attn_n),
  1316. [](common_params & params, int value) {
  1317. params.grp_attn_n = value;
  1318. }
  1319. ).set_env("LLAMA_ARG_GRP_ATTN_N").set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_PASSKEY}));
  1320. add_opt(common_arg(
  1321. {"-gaw", "--grp-attn-w"}, "N",
  1322. string_format("group-attention width (default: %d)", params.grp_attn_w),
  1323. [](common_params & params, int value) {
  1324. params.grp_attn_w = value;
  1325. }
  1326. ).set_env("LLAMA_ARG_GRP_ATTN_W").set_examples({LLAMA_EXAMPLE_MAIN}));
  1327. add_opt(common_arg(
  1328. {"-dkvc", "--dump-kv-cache"},
  1329. "verbose print of the KV cache",
  1330. [](common_params & params) {
  1331. params.dump_kv_cache = true;
  1332. }
  1333. ));
  1334. add_opt(common_arg(
  1335. {"-nkvo", "--no-kv-offload"},
  1336. "disable KV offload",
  1337. [](common_params & params) {
  1338. params.no_kv_offload = true;
  1339. }
  1340. ).set_env("LLAMA_ARG_NO_KV_OFFLOAD"));
  1341. add_opt(common_arg(
  1342. {"-ctk", "--cache-type-k"}, "TYPE",
  1343. string_format(
  1344. "KV cache data type for K\n"
  1345. "allowed values: %s\n"
  1346. "(default: %s)",
  1347. get_all_kv_cache_types().c_str(),
  1348. ggml_type_name(params.cache_type_k)
  1349. ),
  1350. [](common_params & params, const std::string & value) {
  1351. params.cache_type_k = kv_cache_type_from_str(value);
  1352. }
  1353. ).set_env("LLAMA_ARG_CACHE_TYPE_K"));
  1354. add_opt(common_arg(
  1355. {"-ctv", "--cache-type-v"}, "TYPE",
  1356. string_format(
  1357. "KV cache data type for V\n"
  1358. "allowed values: %s\n"
  1359. "(default: %s)",
  1360. get_all_kv_cache_types().c_str(),
  1361. ggml_type_name(params.cache_type_v)
  1362. ),
  1363. [](common_params & params, const std::string & value) {
  1364. params.cache_type_v = kv_cache_type_from_str(value);
  1365. }
  1366. ).set_env("LLAMA_ARG_CACHE_TYPE_V"));
  1367. add_opt(common_arg(
  1368. {"--perplexity", "--all-logits"},
  1369. string_format("return logits for all tokens in the batch (default: %s)", params.logits_all ? "true" : "false"),
  1370. [](common_params & params) {
  1371. params.logits_all = true;
  1372. }
  1373. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1374. add_opt(common_arg(
  1375. {"--hellaswag"},
  1376. "compute HellaSwag score over random tasks from datafile supplied with -f",
  1377. [](common_params & params) {
  1378. params.hellaswag = true;
  1379. }
  1380. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1381. add_opt(common_arg(
  1382. {"--hellaswag-tasks"}, "N",
  1383. string_format("number of tasks to use when computing the HellaSwag score (default: %zu)", params.hellaswag_tasks),
  1384. [](common_params & params, int value) {
  1385. params.hellaswag_tasks = value;
  1386. }
  1387. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1388. add_opt(common_arg(
  1389. {"--winogrande"},
  1390. "compute Winogrande score over random tasks from datafile supplied with -f",
  1391. [](common_params & params) {
  1392. params.winogrande = true;
  1393. }
  1394. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1395. add_opt(common_arg(
  1396. {"--winogrande-tasks"}, "N",
  1397. string_format("number of tasks to use when computing the Winogrande score (default: %zu)", params.winogrande_tasks),
  1398. [](common_params & params, int value) {
  1399. params.winogrande_tasks = value;
  1400. }
  1401. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1402. add_opt(common_arg(
  1403. {"--multiple-choice"},
  1404. "compute multiple choice score over random tasks from datafile supplied with -f",
  1405. [](common_params & params) {
  1406. params.multiple_choice = true;
  1407. }
  1408. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1409. add_opt(common_arg(
  1410. {"--multiple-choice-tasks"}, "N",
  1411. string_format("number of tasks to use when computing the multiple choice score (default: %zu)", params.multiple_choice_tasks),
  1412. [](common_params & params, int value) {
  1413. params.multiple_choice_tasks = value;
  1414. }
  1415. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1416. add_opt(common_arg(
  1417. {"--kl-divergence"},
  1418. "computes KL-divergence to logits provided via --kl-divergence-base",
  1419. [](common_params & params) {
  1420. params.kl_divergence = true;
  1421. }
  1422. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1423. add_opt(common_arg(
  1424. {"--save-all-logits", "--kl-divergence-base"}, "FNAME",
  1425. "set logits file",
  1426. [](common_params & params, const std::string & value) {
  1427. params.logits_file = value;
  1428. }
  1429. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1430. add_opt(common_arg(
  1431. {"--ppl-stride"}, "N",
  1432. string_format("stride for perplexity calculation (default: %d)", params.ppl_stride),
  1433. [](common_params & params, int value) {
  1434. params.ppl_stride = value;
  1435. }
  1436. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1437. add_opt(common_arg(
  1438. {"--ppl-output-type"}, "<0|1>",
  1439. string_format("output type for perplexity calculation (default: %d)", params.ppl_output_type),
  1440. [](common_params & params, int value) {
  1441. params.ppl_output_type = value;
  1442. }
  1443. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1444. add_opt(common_arg(
  1445. {"-dt", "--defrag-thold"}, "N",
  1446. string_format("KV cache defragmentation threshold (default: %.1f, < 0 - disabled)", (double)params.defrag_thold),
  1447. [](common_params & params, const std::string & value) {
  1448. params.defrag_thold = std::stof(value);
  1449. }
  1450. ).set_env("LLAMA_ARG_DEFRAG_THOLD"));
  1451. add_opt(common_arg(
  1452. {"-np", "--parallel"}, "N",
  1453. string_format("number of parallel sequences to decode (default: %d)", params.n_parallel),
  1454. [](common_params & params, int value) {
  1455. params.n_parallel = value;
  1456. }
  1457. ).set_env("LLAMA_ARG_N_PARALLEL"));
  1458. add_opt(common_arg(
  1459. {"-ns", "--sequences"}, "N",
  1460. string_format("number of sequences to decode (default: %d)", params.n_sequences),
  1461. [](common_params & params, int value) {
  1462. params.n_sequences = value;
  1463. }
  1464. ).set_examples({LLAMA_EXAMPLE_PARALLEL}));
  1465. add_opt(common_arg(
  1466. {"-cb", "--cont-batching"},
  1467. string_format("enable continuous batching (a.k.a dynamic batching) (default: %s)", params.cont_batching ? "enabled" : "disabled"),
  1468. [](common_params & params) {
  1469. params.cont_batching = true;
  1470. }
  1471. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CONT_BATCHING"));
  1472. add_opt(common_arg(
  1473. {"-nocb", "--no-cont-batching"},
  1474. "disable continuous batching",
  1475. [](common_params & params) {
  1476. params.cont_batching = false;
  1477. }
  1478. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_NO_CONT_BATCHING"));
  1479. add_opt(common_arg(
  1480. {"--mmproj"}, "FILE",
  1481. "path to a multimodal projector file for LLaVA. see examples/llava/README.md",
  1482. [](common_params & params, const std::string & value) {
  1483. params.mmproj = value;
  1484. }
  1485. ).set_examples({LLAMA_EXAMPLE_LLAVA}));
  1486. add_opt(common_arg(
  1487. {"--image"}, "FILE",
  1488. "path to an image file. use with multimodal models. Specify multiple times for batching",
  1489. [](common_params & params, const std::string & value) {
  1490. params.image.emplace_back(value);
  1491. }
  1492. ).set_examples({LLAMA_EXAMPLE_LLAVA}));
  1493. if (llama_supports_rpc()) {
  1494. add_opt(common_arg(
  1495. {"--rpc"}, "SERVERS",
  1496. "comma separated list of RPC servers",
  1497. [](common_params & params, const std::string & value) {
  1498. add_rpc_devices(value);
  1499. GGML_UNUSED(params);
  1500. }
  1501. ).set_env("LLAMA_ARG_RPC"));
  1502. }
  1503. add_opt(common_arg(
  1504. {"--mlock"},
  1505. "force system to keep model in RAM rather than swapping or compressing",
  1506. [](common_params & params) {
  1507. params.use_mlock = true;
  1508. }
  1509. ).set_env("LLAMA_ARG_MLOCK"));
  1510. add_opt(common_arg(
  1511. {"--no-mmap"},
  1512. "do not memory-map model (slower load but may reduce pageouts if not using mlock)",
  1513. [](common_params & params) {
  1514. params.use_mmap = false;
  1515. }
  1516. ).set_env("LLAMA_ARG_NO_MMAP"));
  1517. add_opt(common_arg(
  1518. {"--numa"}, "TYPE",
  1519. "attempt optimizations that help on some NUMA systems\n"
  1520. "- distribute: spread execution evenly over all nodes\n"
  1521. "- isolate: only spawn threads on CPUs on the node that execution started on\n"
  1522. "- numactl: use the CPU map provided by numactl\n"
  1523. "if run without this previously, it is recommended to drop the system page cache before using this\n"
  1524. "see https://github.com/ggml-org/llama.cpp/issues/1437",
  1525. [](common_params & params, const std::string & value) {
  1526. /**/ if (value == "distribute" || value == "") { params.numa = GGML_NUMA_STRATEGY_DISTRIBUTE; }
  1527. else if (value == "isolate") { params.numa = GGML_NUMA_STRATEGY_ISOLATE; }
  1528. else if (value == "numactl") { params.numa = GGML_NUMA_STRATEGY_NUMACTL; }
  1529. else { throw std::invalid_argument("invalid value"); }
  1530. }
  1531. ).set_env("LLAMA_ARG_NUMA"));
  1532. add_opt(common_arg(
  1533. {"-dev", "--device"}, "<dev1,dev2,..>",
  1534. "comma-separated list of devices to use for offloading (none = don't offload)\n"
  1535. "use --list-devices to see a list of available devices",
  1536. [](common_params & params, const std::string & value) {
  1537. params.devices = parse_device_list(value);
  1538. }
  1539. ).set_env("LLAMA_ARG_DEVICE"));
  1540. add_opt(common_arg(
  1541. {"--list-devices"},
  1542. "print list of available devices and exit",
  1543. [](common_params &) {
  1544. std::vector<ggml_backend_dev_t> rpc_devices;
  1545. std::vector<ggml_backend_dev_t> all_devices;
  1546. for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
  1547. auto * dev = ggml_backend_dev_get(i);
  1548. if (ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_GPU) {
  1549. ggml_backend_reg_t reg = ggml_backend_dev_backend_reg(dev);
  1550. if (ggml_backend_reg_name(reg) == std::string("RPC")) {
  1551. rpc_devices.push_back(dev);
  1552. } else {
  1553. all_devices.push_back(dev);
  1554. }
  1555. }
  1556. }
  1557. // insert RPC devices in front
  1558. all_devices.insert(all_devices.begin(), rpc_devices.begin(), rpc_devices.end());
  1559. printf("Available devices:\n");
  1560. for (size_t i = 0; i < all_devices.size(); ++i) {
  1561. auto * dev = all_devices[i];
  1562. size_t free, total;
  1563. ggml_backend_dev_memory(dev, &free, &total);
  1564. 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);
  1565. }
  1566. exit(0);
  1567. }
  1568. ));
  1569. add_opt(common_arg(
  1570. {"-ngl", "--gpu-layers", "--n-gpu-layers"}, "N",
  1571. "number of layers to store in VRAM",
  1572. [](common_params & params, int value) {
  1573. params.n_gpu_layers = value;
  1574. if (!llama_supports_gpu_offload()) {
  1575. fprintf(stderr, "warning: no usable GPU found, --gpu-layers option will be ignored\n");
  1576. fprintf(stderr, "warning: one possible reason is that llama.cpp was compiled without GPU support\n");
  1577. fprintf(stderr, "warning: consult docs/build.md for compilation instructions\n");
  1578. }
  1579. }
  1580. ).set_env("LLAMA_ARG_N_GPU_LAYERS"));
  1581. add_opt(common_arg(
  1582. {"-sm", "--split-mode"}, "{none,layer,row}",
  1583. "how to split the model across multiple GPUs, one of:\n"
  1584. "- none: use one GPU only\n"
  1585. "- layer (default): split layers and KV across GPUs\n"
  1586. "- row: split rows across GPUs",
  1587. [](common_params & params, const std::string & value) {
  1588. std::string arg_next = value;
  1589. if (arg_next == "none") {
  1590. params.split_mode = LLAMA_SPLIT_MODE_NONE;
  1591. } else if (arg_next == "layer") {
  1592. params.split_mode = LLAMA_SPLIT_MODE_LAYER;
  1593. } else if (arg_next == "row") {
  1594. params.split_mode = LLAMA_SPLIT_MODE_ROW;
  1595. } else {
  1596. throw std::invalid_argument("invalid value");
  1597. }
  1598. if (!llama_supports_gpu_offload()) {
  1599. fprintf(stderr, "warning: llama.cpp was compiled without support for GPU offload. Setting the split mode has no effect.\n");
  1600. }
  1601. }
  1602. ).set_env("LLAMA_ARG_SPLIT_MODE"));
  1603. add_opt(common_arg(
  1604. {"-ts", "--tensor-split"}, "N0,N1,N2,...",
  1605. "fraction of the model to offload to each GPU, comma-separated list of proportions, e.g. 3,1",
  1606. [](common_params & params, const std::string & value) {
  1607. std::string arg_next = value;
  1608. // split string by , and /
  1609. const std::regex regex{ R"([,/]+)" };
  1610. std::sregex_token_iterator it{ arg_next.begin(), arg_next.end(), regex, -1 };
  1611. std::vector<std::string> split_arg{ it, {} };
  1612. if (split_arg.size() >= llama_max_devices()) {
  1613. throw std::invalid_argument(
  1614. string_format("got %d input configs, but system only has %d devices", (int)split_arg.size(), (int)llama_max_devices())
  1615. );
  1616. }
  1617. for (size_t i = 0; i < llama_max_devices(); ++i) {
  1618. if (i < split_arg.size()) {
  1619. params.tensor_split[i] = std::stof(split_arg[i]);
  1620. } else {
  1621. params.tensor_split[i] = 0.0f;
  1622. }
  1623. }
  1624. if (!llama_supports_gpu_offload()) {
  1625. fprintf(stderr, "warning: llama.cpp was compiled without support for GPU offload. Setting a tensor split has no effect.\n");
  1626. }
  1627. }
  1628. ).set_env("LLAMA_ARG_TENSOR_SPLIT"));
  1629. add_opt(common_arg(
  1630. {"-mg", "--main-gpu"}, "INDEX",
  1631. 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),
  1632. [](common_params & params, int value) {
  1633. params.main_gpu = value;
  1634. if (!llama_supports_gpu_offload()) {
  1635. fprintf(stderr, "warning: llama.cpp was compiled without support for GPU offload. Setting the main GPU has no effect.\n");
  1636. }
  1637. }
  1638. ).set_env("LLAMA_ARG_MAIN_GPU"));
  1639. add_opt(common_arg(
  1640. {"--check-tensors"},
  1641. string_format("check model tensor data for invalid values (default: %s)", params.check_tensors ? "true" : "false"),
  1642. [](common_params & params) {
  1643. params.check_tensors = true;
  1644. }
  1645. ));
  1646. add_opt(common_arg(
  1647. {"--override-kv"}, "KEY=TYPE:VALUE",
  1648. "advanced option to override model metadata by key. may be specified multiple times.\n"
  1649. "types: int, float, bool, str. example: --override-kv tokenizer.ggml.add_bos_token=bool:false",
  1650. [](common_params & params, const std::string & value) {
  1651. if (!string_parse_kv_override(value.c_str(), params.kv_overrides)) {
  1652. throw std::runtime_error(string_format("error: Invalid type for KV override: %s\n", value.c_str()));
  1653. }
  1654. }
  1655. ));
  1656. add_opt(common_arg(
  1657. {"--lora"}, "FNAME",
  1658. "path to LoRA adapter (can be repeated to use multiple adapters)",
  1659. [](common_params & params, const std::string & value) {
  1660. params.lora_adapters.push_back({ std::string(value), 1.0, nullptr });
  1661. }
  1662. // 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
  1663. ).set_examples({LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_EXPORT_LORA}));
  1664. add_opt(common_arg(
  1665. {"--lora-scaled"}, "FNAME", "SCALE",
  1666. "path to LoRA adapter with user defined scaling (can be repeated to use multiple adapters)",
  1667. [](common_params & params, const std::string & fname, const std::string & scale) {
  1668. params.lora_adapters.push_back({ fname, std::stof(scale), nullptr });
  1669. }
  1670. // 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
  1671. ).set_examples({LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_EXPORT_LORA}));
  1672. add_opt(common_arg(
  1673. {"--control-vector"}, "FNAME",
  1674. "add a control vector\nnote: this argument can be repeated to add multiple control vectors",
  1675. [](common_params & params, const std::string & value) {
  1676. params.control_vectors.push_back({ 1.0f, value, });
  1677. }
  1678. ));
  1679. add_opt(common_arg(
  1680. {"--control-vector-scaled"}, "FNAME", "SCALE",
  1681. "add a control vector with user defined scaling SCALE\n"
  1682. "note: this argument can be repeated to add multiple scaled control vectors",
  1683. [](common_params & params, const std::string & fname, const std::string & scale) {
  1684. params.control_vectors.push_back({ std::stof(scale), fname });
  1685. }
  1686. ));
  1687. add_opt(common_arg(
  1688. {"--control-vector-layer-range"}, "START", "END",
  1689. "layer range to apply the control vector(s) to, start and end inclusive",
  1690. [](common_params & params, const std::string & start, const std::string & end) {
  1691. params.control_vector_layer_start = std::stoi(start);
  1692. params.control_vector_layer_end = std::stoi(end);
  1693. }
  1694. ));
  1695. add_opt(common_arg(
  1696. {"-a", "--alias"}, "STRING",
  1697. "set alias for model name (to be used by REST API)",
  1698. [](common_params & params, const std::string & value) {
  1699. params.model_alias = value;
  1700. }
  1701. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ALIAS"));
  1702. add_opt(common_arg(
  1703. {"-m", "--model"}, "FNAME",
  1704. ex == LLAMA_EXAMPLE_EXPORT_LORA
  1705. ? std::string("model path from which to load base model")
  1706. : string_format(
  1707. "model path (default: `models/$filename` with filename from `--hf-file` "
  1708. "or `--model-url` if set, otherwise %s)", DEFAULT_MODEL_PATH
  1709. ),
  1710. [](common_params & params, const std::string & value) {
  1711. params.model = value;
  1712. }
  1713. ).set_examples({LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_EXPORT_LORA}).set_env("LLAMA_ARG_MODEL"));
  1714. add_opt(common_arg(
  1715. {"-mu", "--model-url"}, "MODEL_URL",
  1716. "model download url (default: unused)",
  1717. [](common_params & params, const std::string & value) {
  1718. params.model_url = value;
  1719. }
  1720. ).set_env("LLAMA_ARG_MODEL_URL"));
  1721. add_opt(common_arg(
  1722. {"-hf", "-hfr", "--hf-repo"}, "<user>/<model>[:quant]",
  1723. "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"
  1724. "example: unsloth/phi-4-GGUF:q4_k_m\n"
  1725. "(default: unused)",
  1726. [](common_params & params, const std::string & value) {
  1727. params.hf_repo = value;
  1728. }
  1729. ).set_env("LLAMA_ARG_HF_REPO"));
  1730. add_opt(common_arg(
  1731. {"-hfd", "-hfrd", "--hf-repo-draft"}, "<user>/<model>[:quant]",
  1732. "Same as --hf-repo, but for the draft model (default: unused)",
  1733. [](common_params & params, const std::string & value) {
  1734. params.speculative.hf_repo = value;
  1735. }
  1736. ).set_env("LLAMA_ARG_HFD_REPO"));
  1737. add_opt(common_arg(
  1738. {"-hff", "--hf-file"}, "FILE",
  1739. "Hugging Face model file. If specified, it will override the quant in --hf-repo (default: unused)",
  1740. [](common_params & params, const std::string & value) {
  1741. params.hf_file = value;
  1742. }
  1743. ).set_env("LLAMA_ARG_HF_FILE"));
  1744. add_opt(common_arg(
  1745. {"-hfv", "-hfrv", "--hf-repo-v"}, "<user>/<model>[:quant]",
  1746. "Hugging Face model repository for the vocoder model (default: unused)",
  1747. [](common_params & params, const std::string & value) {
  1748. params.vocoder.hf_repo = value;
  1749. }
  1750. ).set_env("LLAMA_ARG_HF_REPO_V"));
  1751. add_opt(common_arg(
  1752. {"-hffv", "--hf-file-v"}, "FILE",
  1753. "Hugging Face model file for the vocoder model (default: unused)",
  1754. [](common_params & params, const std::string & value) {
  1755. params.vocoder.hf_file = value;
  1756. }
  1757. ).set_env("LLAMA_ARG_HF_FILE_V"));
  1758. add_opt(common_arg(
  1759. {"-hft", "--hf-token"}, "TOKEN",
  1760. "Hugging Face access token (default: value from HF_TOKEN environment variable)",
  1761. [](common_params & params, const std::string & value) {
  1762. params.hf_token = value;
  1763. }
  1764. ).set_env("HF_TOKEN"));
  1765. add_opt(common_arg(
  1766. {"--context-file"}, "FNAME",
  1767. "file to load context from (repeat to specify multiple files)",
  1768. [](common_params & params, const std::string & value) {
  1769. std::ifstream file(value, std::ios::binary);
  1770. if (!file) {
  1771. throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
  1772. }
  1773. params.context_files.push_back(value);
  1774. }
  1775. ).set_examples({LLAMA_EXAMPLE_RETRIEVAL}));
  1776. add_opt(common_arg(
  1777. {"--chunk-size"}, "N",
  1778. string_format("minimum length of embedded text chunks (default: %d)", params.chunk_size),
  1779. [](common_params & params, int value) {
  1780. params.chunk_size = value;
  1781. }
  1782. ).set_examples({LLAMA_EXAMPLE_RETRIEVAL}));
  1783. add_opt(common_arg(
  1784. {"--chunk-separator"}, "STRING",
  1785. string_format("separator between chunks (default: '%s')", params.chunk_separator.c_str()),
  1786. [](common_params & params, const std::string & value) {
  1787. params.chunk_separator = value;
  1788. }
  1789. ).set_examples({LLAMA_EXAMPLE_RETRIEVAL}));
  1790. add_opt(common_arg(
  1791. {"--junk"}, "N",
  1792. string_format("number of times to repeat the junk text (default: %d)", params.n_junk),
  1793. [](common_params & params, int value) {
  1794. params.n_junk = value;
  1795. }
  1796. ).set_examples({LLAMA_EXAMPLE_PASSKEY}));
  1797. add_opt(common_arg(
  1798. {"--pos"}, "N",
  1799. string_format("position of the passkey in the junk text (default: %d)", params.i_pos),
  1800. [](common_params & params, int value) {
  1801. params.i_pos = value;
  1802. }
  1803. ).set_examples({LLAMA_EXAMPLE_PASSKEY}));
  1804. add_opt(common_arg(
  1805. {"-o", "--output", "--output-file"}, "FNAME",
  1806. string_format("output file (default: '%s')", params.out_file.c_str()),
  1807. [](common_params & params, const std::string & value) {
  1808. params.out_file = value;
  1809. }
  1810. ).set_examples({LLAMA_EXAMPLE_IMATRIX, LLAMA_EXAMPLE_CVECTOR_GENERATOR, LLAMA_EXAMPLE_EXPORT_LORA}));
  1811. add_opt(common_arg(
  1812. {"-ofreq", "--output-frequency"}, "N",
  1813. string_format("output the imatrix every N iterations (default: %d)", params.n_out_freq),
  1814. [](common_params & params, int value) {
  1815. params.n_out_freq = value;
  1816. }
  1817. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  1818. add_opt(common_arg(
  1819. {"--save-frequency"}, "N",
  1820. string_format("save an imatrix copy every N iterations (default: %d)", params.n_save_freq),
  1821. [](common_params & params, int value) {
  1822. params.n_save_freq = value;
  1823. }
  1824. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  1825. add_opt(common_arg(
  1826. {"--process-output"},
  1827. string_format("collect data for the output tensor (default: %s)", params.process_output ? "true" : "false"),
  1828. [](common_params & params) {
  1829. params.process_output = true;
  1830. }
  1831. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  1832. add_opt(common_arg(
  1833. {"--no-ppl"},
  1834. string_format("do not compute perplexity (default: %s)", params.compute_ppl ? "true" : "false"),
  1835. [](common_params & params) {
  1836. params.compute_ppl = false;
  1837. }
  1838. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  1839. add_opt(common_arg(
  1840. {"--chunk", "--from-chunk"}, "N",
  1841. string_format("start processing the input from chunk N (default: %d)", params.i_chunk),
  1842. [](common_params & params, int value) {
  1843. params.i_chunk = value;
  1844. }
  1845. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  1846. add_opt(common_arg(
  1847. {"-pps"},
  1848. string_format("is the prompt shared across parallel sequences (default: %s)", params.is_pp_shared ? "true" : "false"),
  1849. [](common_params & params) {
  1850. params.is_pp_shared = true;
  1851. }
  1852. ).set_examples({LLAMA_EXAMPLE_BENCH}));
  1853. add_opt(common_arg(
  1854. {"-npp"}, "n0,n1,...",
  1855. "number of prompt tokens",
  1856. [](common_params & params, const std::string & value) {
  1857. auto p = string_split<int>(value, ',');
  1858. params.n_pp.insert(params.n_pp.end(), p.begin(), p.end());
  1859. }
  1860. ).set_examples({LLAMA_EXAMPLE_BENCH}));
  1861. add_opt(common_arg(
  1862. {"-ntg"}, "n0,n1,...",
  1863. "number of text generation tokens",
  1864. [](common_params & params, const std::string & value) {
  1865. auto p = string_split<int>(value, ',');
  1866. params.n_tg.insert(params.n_tg.end(), p.begin(), p.end());
  1867. }
  1868. ).set_examples({LLAMA_EXAMPLE_BENCH}));
  1869. add_opt(common_arg(
  1870. {"-npl"}, "n0,n1,...",
  1871. "number of parallel prompts",
  1872. [](common_params & params, const std::string & value) {
  1873. auto p = string_split<int>(value, ',');
  1874. params.n_pl.insert(params.n_pl.end(), p.begin(), p.end());
  1875. }
  1876. ).set_examples({LLAMA_EXAMPLE_BENCH}));
  1877. add_opt(common_arg(
  1878. {"--embd-normalize"}, "N",
  1879. string_format("normalisation for embeddings (default: %d) (-1=none, 0=max absolute int16, 1=taxicab, 2=euclidean, >2=p-norm)", params.embd_normalize),
  1880. [](common_params & params, int value) {
  1881. params.embd_normalize = value;
  1882. }
  1883. ).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
  1884. add_opt(common_arg(
  1885. {"--embd-output-format"}, "FORMAT",
  1886. "empty = default, \"array\" = [[],[]...], \"json\" = openai style, \"json+\" = same \"json\" + cosine similarity matrix",
  1887. [](common_params & params, const std::string & value) {
  1888. params.embd_out = value;
  1889. }
  1890. ).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
  1891. add_opt(common_arg(
  1892. {"--embd-separator"}, "STRING",
  1893. "separator of embeddings (default \\n) for example \"<#sep#>\"",
  1894. [](common_params & params, const std::string & value) {
  1895. params.embd_sep = value;
  1896. }
  1897. ).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
  1898. add_opt(common_arg(
  1899. {"--host"}, "HOST",
  1900. string_format("ip address to listen (default: %s)", params.hostname.c_str()),
  1901. [](common_params & params, const std::string & value) {
  1902. params.hostname = value;
  1903. }
  1904. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_HOST"));
  1905. add_opt(common_arg(
  1906. {"--port"}, "PORT",
  1907. string_format("port to listen (default: %d)", params.port),
  1908. [](common_params & params, int value) {
  1909. params.port = value;
  1910. }
  1911. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_PORT"));
  1912. add_opt(common_arg(
  1913. {"--path"}, "PATH",
  1914. string_format("path to serve static files from (default: %s)", params.public_path.c_str()),
  1915. [](common_params & params, const std::string & value) {
  1916. params.public_path = value;
  1917. }
  1918. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_STATIC_PATH"));
  1919. add_opt(common_arg(
  1920. {"--no-webui"},
  1921. string_format("Disable the Web UI (default: %s)", params.webui ? "enabled" : "disabled"),
  1922. [](common_params & params) {
  1923. params.webui = false;
  1924. }
  1925. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_NO_WEBUI"));
  1926. add_opt(common_arg(
  1927. {"--embedding", "--embeddings"},
  1928. string_format("restrict to only support embedding use case; use only with dedicated embedding models (default: %s)", params.embedding ? "enabled" : "disabled"),
  1929. [](common_params & params) {
  1930. params.embedding = true;
  1931. }
  1932. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_EMBEDDINGS"));
  1933. add_opt(common_arg(
  1934. {"--reranking", "--rerank"},
  1935. string_format("enable reranking endpoint on server (default: %s)", params.reranking ? "enabled" : "disabled"),
  1936. [](common_params & params) {
  1937. params.reranking = true;
  1938. }
  1939. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_RERANKING"));
  1940. add_opt(common_arg(
  1941. {"--api-key"}, "KEY",
  1942. "API key to use for authentication (default: none)",
  1943. [](common_params & params, const std::string & value) {
  1944. params.api_keys.push_back(value);
  1945. }
  1946. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_API_KEY"));
  1947. add_opt(common_arg(
  1948. {"--api-key-file"}, "FNAME",
  1949. "path to file containing API keys (default: none)",
  1950. [](common_params & params, const std::string & value) {
  1951. std::ifstream key_file(value);
  1952. if (!key_file) {
  1953. throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
  1954. }
  1955. std::string key;
  1956. while (std::getline(key_file, key)) {
  1957. if (!key.empty()) {
  1958. params.api_keys.push_back(key);
  1959. }
  1960. }
  1961. key_file.close();
  1962. }
  1963. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  1964. add_opt(common_arg(
  1965. {"--ssl-key-file"}, "FNAME",
  1966. "path to file a PEM-encoded SSL private key",
  1967. [](common_params & params, const std::string & value) {
  1968. params.ssl_file_key = value;
  1969. }
  1970. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_SSL_KEY_FILE"));
  1971. add_opt(common_arg(
  1972. {"--ssl-cert-file"}, "FNAME",
  1973. "path to file a PEM-encoded SSL certificate",
  1974. [](common_params & params, const std::string & value) {
  1975. params.ssl_file_cert = value;
  1976. }
  1977. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_SSL_CERT_FILE"));
  1978. add_opt(common_arg(
  1979. {"-to", "--timeout"}, "N",
  1980. string_format("server read/write timeout in seconds (default: %d)", params.timeout_read),
  1981. [](common_params & params, int value) {
  1982. params.timeout_read = value;
  1983. params.timeout_write = value;
  1984. }
  1985. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_TIMEOUT"));
  1986. add_opt(common_arg(
  1987. {"--threads-http"}, "N",
  1988. string_format("number of threads used to process HTTP requests (default: %d)", params.n_threads_http),
  1989. [](common_params & params, int value) {
  1990. params.n_threads_http = value;
  1991. }
  1992. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_THREADS_HTTP"));
  1993. add_opt(common_arg(
  1994. {"--cache-reuse"}, "N",
  1995. string_format("min chunk size to attempt reusing from the cache via KV shifting (default: %d)", params.n_cache_reuse),
  1996. [](common_params & params, int value) {
  1997. params.n_cache_reuse = value;
  1998. }
  1999. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CACHE_REUSE"));
  2000. add_opt(common_arg(
  2001. {"--metrics"},
  2002. string_format("enable prometheus compatible metrics endpoint (default: %s)", params.endpoint_metrics ? "enabled" : "disabled"),
  2003. [](common_params & params) {
  2004. params.endpoint_metrics = true;
  2005. }
  2006. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ENDPOINT_METRICS"));
  2007. add_opt(common_arg(
  2008. {"--slots"},
  2009. string_format("enable slots monitoring endpoint (default: %s)", params.endpoint_slots ? "enabled" : "disabled"),
  2010. [](common_params & params) {
  2011. params.endpoint_slots = true;
  2012. }
  2013. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ENDPOINT_SLOTS"));
  2014. add_opt(common_arg(
  2015. {"--props"},
  2016. string_format("enable changing global properties via POST /props (default: %s)", params.endpoint_props ? "enabled" : "disabled"),
  2017. [](common_params & params) {
  2018. params.endpoint_props = true;
  2019. }
  2020. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ENDPOINT_PROPS"));
  2021. add_opt(common_arg(
  2022. {"--no-slots"},
  2023. "disables slots monitoring endpoint",
  2024. [](common_params & params) {
  2025. params.endpoint_slots = false;
  2026. }
  2027. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_NO_ENDPOINT_SLOTS"));
  2028. add_opt(common_arg(
  2029. {"--slot-save-path"}, "PATH",
  2030. "path to save slot kv cache (default: disabled)",
  2031. [](common_params & params, const std::string & value) {
  2032. params.slot_save_path = value;
  2033. // if doesn't end with DIRECTORY_SEPARATOR, add it
  2034. if (!params.slot_save_path.empty() && params.slot_save_path[params.slot_save_path.size() - 1] != DIRECTORY_SEPARATOR) {
  2035. params.slot_save_path += DIRECTORY_SEPARATOR;
  2036. }
  2037. }
  2038. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2039. add_opt(common_arg(
  2040. {"--jinja"},
  2041. "use jinja template for chat (default: disabled)",
  2042. [](common_params & params) {
  2043. params.use_jinja = true;
  2044. }
  2045. ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_MAIN}).set_env("LLAMA_ARG_JINJA"));
  2046. add_opt(common_arg(
  2047. {"--reasoning-format"}, "FORMAT",
  2048. "reasoning format (default: deepseek; allowed values: deepseek, none)\n"
  2049. "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"
  2050. "only supported for non-streamed responses",
  2051. [](common_params & params, const std::string & value) {
  2052. /**/ if (value == "deepseek") { params.reasoning_format = COMMON_REASONING_FORMAT_DEEPSEEK; }
  2053. else if (value == "none") { params.reasoning_format = COMMON_REASONING_FORMAT_NONE; }
  2054. else { std::invalid_argument("invalid value"); }
  2055. }
  2056. ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_MAIN}).set_env("LLAMA_ARG_THINK"));
  2057. add_opt(common_arg(
  2058. {"--chat-template"}, "JINJA_TEMPLATE",
  2059. string_format(
  2060. "set custom jinja chat template (default: template taken from model's metadata)\n"
  2061. "if suffix/prefix are specified, template will be disabled\n"
  2062. "only commonly used templates are accepted (unless --jinja is set before this flag):\n"
  2063. "list of built-in templates:\n%s", list_builtin_chat_templates().c_str()
  2064. ),
  2065. [](common_params & params, const std::string & value) {
  2066. params.chat_template = value;
  2067. }
  2068. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CHAT_TEMPLATE"));
  2069. add_opt(common_arg(
  2070. {"--chat-template-file"}, "JINJA_TEMPLATE_FILE",
  2071. string_format(
  2072. "set custom jinja chat template file (default: template taken from model's metadata)\n"
  2073. "if suffix/prefix are specified, template will be disabled\n"
  2074. "only commonly used templates are accepted (unless --jinja is set before this flag):\n"
  2075. "list of built-in templates:\n%s", list_builtin_chat_templates().c_str()
  2076. ),
  2077. [](common_params & params, const std::string & value) {
  2078. std::ifstream file(value);
  2079. if (!file) {
  2080. throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
  2081. }
  2082. std::copy(
  2083. std::istreambuf_iterator<char>(file),
  2084. std::istreambuf_iterator<char>(),
  2085. std::back_inserter(params.chat_template));
  2086. }
  2087. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CHAT_TEMPLATE_FILE"));
  2088. add_opt(common_arg(
  2089. {"-sps", "--slot-prompt-similarity"}, "SIMILARITY",
  2090. 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),
  2091. [](common_params & params, const std::string & value) {
  2092. params.slot_prompt_similarity = std::stof(value);
  2093. }
  2094. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2095. add_opt(common_arg(
  2096. {"--lora-init-without-apply"},
  2097. string_format("load LoRA adapters without applying them (apply later via POST /lora-adapters) (default: %s)", params.lora_init_without_apply ? "enabled" : "disabled"),
  2098. [](common_params & params) {
  2099. params.lora_init_without_apply = true;
  2100. }
  2101. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2102. add_opt(common_arg(
  2103. {"--simple-io"},
  2104. "use basic IO for better compatibility in subprocesses and limited consoles",
  2105. [](common_params & params) {
  2106. params.simple_io = true;
  2107. }
  2108. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_INFILL}));
  2109. add_opt(common_arg(
  2110. {"--positive-file"}, "FNAME",
  2111. string_format("positive prompts file, one prompt per line (default: '%s')", params.cvector_positive_file.c_str()),
  2112. [](common_params & params, const std::string & value) {
  2113. params.cvector_positive_file = value;
  2114. }
  2115. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  2116. add_opt(common_arg(
  2117. {"--negative-file"}, "FNAME",
  2118. string_format("negative prompts file, one prompt per line (default: '%s')", params.cvector_negative_file.c_str()),
  2119. [](common_params & params, const std::string & value) {
  2120. params.cvector_negative_file = value;
  2121. }
  2122. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  2123. add_opt(common_arg(
  2124. {"--pca-batch"}, "N",
  2125. string_format("batch size used for PCA. Larger batch runs faster, but uses more memory (default: %d)", params.n_pca_batch),
  2126. [](common_params & params, int value) {
  2127. params.n_pca_batch = value;
  2128. }
  2129. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  2130. add_opt(common_arg(
  2131. {"--pca-iter"}, "N",
  2132. string_format("number of iterations used for PCA (default: %d)", params.n_pca_iterations),
  2133. [](common_params & params, int value) {
  2134. params.n_pca_iterations = value;
  2135. }
  2136. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  2137. add_opt(common_arg(
  2138. {"--method"}, "{pca, mean}",
  2139. "dimensionality reduction method to be used (default: pca)",
  2140. [](common_params & params, const std::string & value) {
  2141. /**/ if (value == "pca") { params.cvector_dimre_method = DIMRE_METHOD_PCA; }
  2142. else if (value == "mean") { params.cvector_dimre_method = DIMRE_METHOD_MEAN; }
  2143. else { throw std::invalid_argument("invalid value"); }
  2144. }
  2145. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  2146. add_opt(common_arg(
  2147. {"--output-format"}, "{md,jsonl}",
  2148. "output format for batched-bench results (default: md)",
  2149. [](common_params & params, const std::string & value) {
  2150. /**/ if (value == "jsonl") { params.batched_bench_output_jsonl = true; }
  2151. else if (value == "md") { params.batched_bench_output_jsonl = false; }
  2152. else { std::invalid_argument("invalid value"); }
  2153. }
  2154. ).set_examples({LLAMA_EXAMPLE_BENCH}));
  2155. add_opt(common_arg(
  2156. {"--log-disable"},
  2157. "Log disable",
  2158. [](common_params &) {
  2159. common_log_pause(common_log_main());
  2160. }
  2161. ));
  2162. add_opt(common_arg(
  2163. {"--log-file"}, "FNAME",
  2164. "Log to file",
  2165. [](common_params &, const std::string & value) {
  2166. common_log_set_file(common_log_main(), value.c_str());
  2167. }
  2168. ));
  2169. add_opt(common_arg(
  2170. {"--log-colors"},
  2171. "Enable colored logging",
  2172. [](common_params &) {
  2173. common_log_set_colors(common_log_main(), true);
  2174. }
  2175. ).set_env("LLAMA_LOG_COLORS"));
  2176. add_opt(common_arg(
  2177. {"-v", "--verbose", "--log-verbose"},
  2178. "Set verbosity level to infinity (i.e. log all messages, useful for debugging)",
  2179. [](common_params & params) {
  2180. params.verbosity = INT_MAX;
  2181. common_log_set_verbosity_thold(INT_MAX);
  2182. }
  2183. ));
  2184. add_opt(common_arg(
  2185. {"-lv", "--verbosity", "--log-verbosity"}, "N",
  2186. "Set the verbosity threshold. Messages with a higher verbosity will be ignored.",
  2187. [](common_params & params, int value) {
  2188. params.verbosity = value;
  2189. common_log_set_verbosity_thold(value);
  2190. }
  2191. ).set_env("LLAMA_LOG_VERBOSITY"));
  2192. add_opt(common_arg(
  2193. {"--log-prefix"},
  2194. "Enable prefix in log messages",
  2195. [](common_params &) {
  2196. common_log_set_prefix(common_log_main(), true);
  2197. }
  2198. ).set_env("LLAMA_LOG_PREFIX"));
  2199. add_opt(common_arg(
  2200. {"--log-timestamps"},
  2201. "Enable timestamps in log messages",
  2202. [](common_params &) {
  2203. common_log_set_timestamps(common_log_main(), true);
  2204. }
  2205. ).set_env("LLAMA_LOG_TIMESTAMPS"));
  2206. // speculative parameters
  2207. add_opt(common_arg(
  2208. {"-td", "--threads-draft"}, "N",
  2209. "number of threads to use during generation (default: same as --threads)",
  2210. [](common_params & params, int value) {
  2211. params.speculative.cpuparams.n_threads = value;
  2212. if (params.speculative.cpuparams.n_threads <= 0) {
  2213. params.speculative.cpuparams.n_threads = std::thread::hardware_concurrency();
  2214. }
  2215. }
  2216. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2217. add_opt(common_arg(
  2218. {"-tbd", "--threads-batch-draft"}, "N",
  2219. "number of threads to use during batch and prompt processing (default: same as --threads-draft)",
  2220. [](common_params & params, int value) {
  2221. params.speculative.cpuparams_batch.n_threads = value;
  2222. if (params.speculative.cpuparams_batch.n_threads <= 0) {
  2223. params.speculative.cpuparams_batch.n_threads = std::thread::hardware_concurrency();
  2224. }
  2225. }
  2226. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2227. add_opt(common_arg(
  2228. {"-Cd", "--cpu-mask-draft"}, "M",
  2229. "Draft model CPU affinity mask. Complements cpu-range-draft (default: same as --cpu-mask)",
  2230. [](common_params & params, const std::string & mask) {
  2231. params.speculative.cpuparams.mask_valid = true;
  2232. if (!parse_cpu_mask(mask, params.speculative.cpuparams.cpumask)) {
  2233. throw std::invalid_argument("invalid cpumask");
  2234. }
  2235. }
  2236. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2237. add_opt(common_arg(
  2238. {"-Crd", "--cpu-range-draft"}, "lo-hi",
  2239. "Ranges of CPUs for affinity. Complements --cpu-mask-draft",
  2240. [](common_params & params, const std::string & range) {
  2241. params.speculative.cpuparams.mask_valid = true;
  2242. if (!parse_cpu_range(range, params.speculative.cpuparams.cpumask)) {
  2243. throw std::invalid_argument("invalid range");
  2244. }
  2245. }
  2246. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2247. add_opt(common_arg(
  2248. {"--cpu-strict-draft"}, "<0|1>",
  2249. "Use strict CPU placement for draft model (default: same as --cpu-strict)",
  2250. [](common_params & params, int value) {
  2251. params.speculative.cpuparams.strict_cpu = value;
  2252. }
  2253. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2254. add_opt(common_arg(
  2255. {"--prio-draft"}, "N",
  2256. string_format("set draft process/thread priority : 0-normal, 1-medium, 2-high, 3-realtime (default: %d)\n", params.speculative.cpuparams.priority),
  2257. [](common_params & params, int prio) {
  2258. if (prio < 0 || prio > 3) {
  2259. throw std::invalid_argument("invalid value");
  2260. }
  2261. params.speculative.cpuparams.priority = (enum ggml_sched_priority) prio;
  2262. }
  2263. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2264. add_opt(common_arg(
  2265. {"--poll-draft"}, "<0|1>",
  2266. "Use polling to wait for draft model work (default: same as --poll])",
  2267. [](common_params & params, int value) {
  2268. params.speculative.cpuparams.poll = value;
  2269. }
  2270. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2271. add_opt(common_arg(
  2272. {"-Cbd", "--cpu-mask-batch-draft"}, "M",
  2273. "Draft model CPU affinity mask. Complements cpu-range-draft (default: same as --cpu-mask)",
  2274. [](common_params & params, const std::string & mask) {
  2275. params.speculative.cpuparams_batch.mask_valid = true;
  2276. if (!parse_cpu_mask(mask, params.speculative.cpuparams_batch.cpumask)) {
  2277. throw std::invalid_argument("invalid cpumask");
  2278. }
  2279. }
  2280. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2281. add_opt(common_arg(
  2282. {"-Crbd", "--cpu-range-batch-draft"}, "lo-hi",
  2283. "Ranges of CPUs for affinity. Complements --cpu-mask-draft-batch)",
  2284. [](common_params & params, const std::string & range) {
  2285. params.speculative.cpuparams_batch.mask_valid = true;
  2286. if (!parse_cpu_range(range, params.speculative.cpuparams_batch.cpumask)) {
  2287. throw std::invalid_argument("invalid cpumask");
  2288. }
  2289. }
  2290. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2291. add_opt(common_arg(
  2292. {"--cpu-strict-batch-draft"}, "<0|1>",
  2293. "Use strict CPU placement for draft model (default: --cpu-strict-draft)",
  2294. [](common_params & params, int value) {
  2295. params.speculative.cpuparams_batch.strict_cpu = value;
  2296. }
  2297. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2298. add_opt(common_arg(
  2299. {"--prio-batch-draft"}, "N",
  2300. string_format("set draft process/thread priority : 0-normal, 1-medium, 2-high, 3-realtime (default: %d)\n", params.speculative.cpuparams_batch.priority),
  2301. [](common_params & params, int prio) {
  2302. if (prio < 0 || prio > 3) {
  2303. throw std::invalid_argument("invalid value");
  2304. }
  2305. params.speculative.cpuparams_batch.priority = (enum ggml_sched_priority) prio;
  2306. }
  2307. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2308. add_opt(common_arg(
  2309. {"--poll-batch-draft"}, "<0|1>",
  2310. "Use polling to wait for draft model work (default: --poll-draft)",
  2311. [](common_params & params, int value) {
  2312. params.speculative.cpuparams_batch.poll = value;
  2313. }
  2314. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2315. add_opt(common_arg(
  2316. {"--draft-max", "--draft", "--draft-n"}, "N",
  2317. string_format("number of tokens to draft for speculative decoding (default: %d)", params.speculative.n_max),
  2318. [](common_params & params, int value) {
  2319. params.speculative.n_max = value;
  2320. }
  2321. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_LOOKUP, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_DRAFT_MAX"));
  2322. add_opt(common_arg(
  2323. {"--draft-min", "--draft-n-min"}, "N",
  2324. string_format("minimum number of draft tokens to use for speculative decoding (default: %d)", params.speculative.n_min),
  2325. [](common_params & params, int value) {
  2326. params.speculative.n_min = value;
  2327. }
  2328. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_LOOKUP, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_DRAFT_MIN"));
  2329. add_opt(common_arg(
  2330. {"--draft-p-split"}, "P",
  2331. string_format("speculative decoding split probability (default: %.1f)", (double)params.speculative.p_split),
  2332. [](common_params & params, const std::string & value) {
  2333. params.speculative.p_split = std::stof(value);
  2334. }
  2335. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}).set_env("LLAMA_ARG_DRAFT_P_SPLIT"));
  2336. add_opt(common_arg(
  2337. {"--draft-p-min"}, "P",
  2338. string_format("minimum speculative decoding probability (greedy) (default: %.1f)", (double)params.speculative.p_min),
  2339. [](common_params & params, const std::string & value) {
  2340. params.speculative.p_min = std::stof(value);
  2341. }
  2342. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_DRAFT_P_MIN"));
  2343. add_opt(common_arg(
  2344. {"-cd", "--ctx-size-draft"}, "N",
  2345. string_format("size of the prompt context for the draft model (default: %d, 0 = loaded from model)", params.speculative.n_ctx),
  2346. [](common_params & params, int value) {
  2347. params.speculative.n_ctx = value;
  2348. }
  2349. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CTX_SIZE_DRAFT"));
  2350. add_opt(common_arg(
  2351. {"-devd", "--device-draft"}, "<dev1,dev2,..>",
  2352. "comma-separated list of devices to use for offloading the draft model (none = don't offload)\n"
  2353. "use --list-devices to see a list of available devices",
  2354. [](common_params & params, const std::string & value) {
  2355. params.speculative.devices = parse_device_list(value);
  2356. }
  2357. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}));
  2358. add_opt(common_arg(
  2359. {"-ngld", "--gpu-layers-draft", "--n-gpu-layers-draft"}, "N",
  2360. "number of layers to store in VRAM for the draft model",
  2361. [](common_params & params, int value) {
  2362. params.speculative.n_gpu_layers = value;
  2363. if (!llama_supports_gpu_offload()) {
  2364. fprintf(stderr, "warning: no usable GPU found, --gpu-layers-draft option will be ignored\n");
  2365. fprintf(stderr, "warning: one possible reason is that llama.cpp was compiled without GPU support\n");
  2366. fprintf(stderr, "warning: consult docs/build.md for compilation instructions\n");
  2367. }
  2368. }
  2369. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_N_GPU_LAYERS_DRAFT"));
  2370. add_opt(common_arg(
  2371. {"-md", "--model-draft"}, "FNAME",
  2372. "draft model for speculative decoding (default: unused)",
  2373. [](common_params & params, const std::string & value) {
  2374. params.speculative.model = value;
  2375. }
  2376. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_MODEL_DRAFT"));
  2377. add_opt(common_arg(
  2378. {"-mv", "--model-vocoder"}, "FNAME",
  2379. "vocoder model for audio generation (default: unused)",
  2380. [](common_params & params, const std::string & value) {
  2381. params.vocoder.model = value;
  2382. }
  2383. ).set_examples({LLAMA_EXAMPLE_TTS, LLAMA_EXAMPLE_SERVER}));
  2384. add_opt(common_arg(
  2385. {"--tts-use-guide-tokens"},
  2386. "Use guide tokens to improve TTS word recall",
  2387. [](common_params & params) {
  2388. params.vocoder.use_guide_tokens = true;
  2389. }
  2390. ).set_examples({LLAMA_EXAMPLE_TTS, LLAMA_EXAMPLE_SERVER}));
  2391. add_opt(common_arg(
  2392. {"--tts-speaker-file"}, "FNAME",
  2393. "speaker file path for audio generation",
  2394. [](common_params & params, const std::string & value) {
  2395. params.vocoder.speaker_file = value;
  2396. }
  2397. ).set_examples({LLAMA_EXAMPLE_TTS}));
  2398. // model-specific
  2399. add_opt(common_arg(
  2400. {"--tts-oute-default"},
  2401. string_format("use default OuteTTS models (note: can download weights from the internet)"),
  2402. [](common_params & params) {
  2403. params.hf_repo = "OuteAI/OuteTTS-0.2-500M-GGUF";
  2404. params.hf_file = "OuteTTS-0.2-500M-Q8_0.gguf";
  2405. params.vocoder.hf_repo = "ggml-org/WavTokenizer";
  2406. params.vocoder.hf_file = "WavTokenizer-Large-75-F16.gguf";
  2407. }
  2408. ).set_examples({LLAMA_EXAMPLE_TTS}));
  2409. add_opt(common_arg(
  2410. {"--embd-bge-small-en-default"},
  2411. string_format("use default bge-small-en-v1.5 model (note: can download weights from the internet)"),
  2412. [](common_params & params) {
  2413. params.hf_repo = "ggml-org/bge-small-en-v1.5-Q8_0-GGUF";
  2414. params.hf_file = "bge-small-en-v1.5-q8_0.gguf";
  2415. params.pooling_type = LLAMA_POOLING_TYPE_NONE;
  2416. params.embd_normalize = 2;
  2417. params.n_ctx = 512;
  2418. params.verbose_prompt = true;
  2419. params.embedding = true;
  2420. }
  2421. ).set_examples({LLAMA_EXAMPLE_EMBEDDING, LLAMA_EXAMPLE_SERVER}));
  2422. add_opt(common_arg(
  2423. {"--embd-e5-small-en-default"},
  2424. string_format("use default e5-small-v2 model (note: can download weights from the internet)"),
  2425. [](common_params & params) {
  2426. params.hf_repo = "ggml-org/e5-small-v2-Q8_0-GGUF";
  2427. params.hf_file = "e5-small-v2-q8_0.gguf";
  2428. params.pooling_type = LLAMA_POOLING_TYPE_NONE;
  2429. params.embd_normalize = 2;
  2430. params.n_ctx = 512;
  2431. params.verbose_prompt = true;
  2432. params.embedding = true;
  2433. }
  2434. ).set_examples({LLAMA_EXAMPLE_EMBEDDING, LLAMA_EXAMPLE_SERVER}));
  2435. add_opt(common_arg(
  2436. {"--embd-gte-small-default"},
  2437. string_format("use default gte-small model (note: can download weights from the internet)"),
  2438. [](common_params & params) {
  2439. params.hf_repo = "ggml-org/gte-small-Q8_0-GGUF";
  2440. params.hf_file = "gte-small-q8_0.gguf";
  2441. params.pooling_type = LLAMA_POOLING_TYPE_NONE;
  2442. params.embd_normalize = 2;
  2443. params.n_ctx = 512;
  2444. params.verbose_prompt = true;
  2445. params.embedding = true;
  2446. }
  2447. ).set_examples({LLAMA_EXAMPLE_EMBEDDING, LLAMA_EXAMPLE_SERVER}));
  2448. add_opt(common_arg(
  2449. {"--fim-qwen-1.5b-default"},
  2450. string_format("use default Qwen 2.5 Coder 1.5B (note: can download weights from the internet)"),
  2451. [](common_params & params) {
  2452. params.hf_repo = "ggml-org/Qwen2.5-Coder-1.5B-Q8_0-GGUF";
  2453. params.hf_file = "qwen2.5-coder-1.5b-q8_0.gguf";
  2454. params.port = 8012;
  2455. params.n_gpu_layers = 99;
  2456. params.flash_attn = true;
  2457. params.n_ubatch = 1024;
  2458. params.n_batch = 1024;
  2459. params.n_ctx = 0;
  2460. params.n_cache_reuse = 256;
  2461. }
  2462. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2463. add_opt(common_arg(
  2464. {"--fim-qwen-3b-default"},
  2465. string_format("use default Qwen 2.5 Coder 3B (note: can download weights from the internet)"),
  2466. [](common_params & params) {
  2467. params.hf_repo = "ggml-org/Qwen2.5-Coder-3B-Q8_0-GGUF";
  2468. params.hf_file = "qwen2.5-coder-3b-q8_0.gguf";
  2469. params.port = 8012;
  2470. params.n_gpu_layers = 99;
  2471. params.flash_attn = true;
  2472. params.n_ubatch = 1024;
  2473. params.n_batch = 1024;
  2474. params.n_ctx = 0;
  2475. params.n_cache_reuse = 256;
  2476. }
  2477. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2478. add_opt(common_arg(
  2479. {"--fim-qwen-7b-default"},
  2480. string_format("use default Qwen 2.5 Coder 7B (note: can download weights from the internet)"),
  2481. [](common_params & params) {
  2482. params.hf_repo = "ggml-org/Qwen2.5-Coder-7B-Q8_0-GGUF";
  2483. params.hf_file = "qwen2.5-coder-7b-q8_0.gguf";
  2484. params.port = 8012;
  2485. params.n_gpu_layers = 99;
  2486. params.flash_attn = true;
  2487. params.n_ubatch = 1024;
  2488. params.n_batch = 1024;
  2489. params.n_ctx = 0;
  2490. params.n_cache_reuse = 256;
  2491. }
  2492. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2493. add_opt(common_arg(
  2494. {"--fim-qwen-7b-spec"},
  2495. string_format("use Qwen 2.5 Coder 7B + 0.5B draft for speculative decoding (note: can download weights from the internet)"),
  2496. [](common_params & params) {
  2497. params.hf_repo = "ggml-org/Qwen2.5-Coder-7B-Q8_0-GGUF";
  2498. params.hf_file = "qwen2.5-coder-7b-q8_0.gguf";
  2499. params.speculative.hf_repo = "ggml-org/Qwen2.5-Coder-0.5B-Q8_0-GGUF";
  2500. params.speculative.hf_file = "qwen2.5-coder-0.5b-q8_0.gguf";
  2501. params.speculative.n_gpu_layers = 99;
  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-14b-spec"},
  2513. string_format("use Qwen 2.5 Coder 14B + 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-14B-Q8_0-GGUF";
  2516. params.hf_file = "qwen2.5-coder-14b-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. return ctx_arg;
  2530. }