arg.cpp 110 KB

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