arg.cpp 98 KB

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