arg.cpp 107 KB

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