arg.cpp 110 KB

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