arg.cpp 96 KB

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