arg.cpp 95 KB

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