arg.cpp 93 KB

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