arg.cpp 91 KB

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