arg.cpp 137 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151
  1. #include "arg.h"
  2. #include "chat.h"
  3. #include "common.h"
  4. #include "json-schema-to-grammar.h"
  5. #include "log.h"
  6. #include "sampling.h"
  7. #include "download.h"
  8. // fix problem with std::min and std::max
  9. #if defined(_WIN32)
  10. #define WIN32_LEAN_AND_MEAN
  11. #ifndef NOMINMAX
  12. # define NOMINMAX
  13. #endif
  14. #include <windows.h>
  15. #endif
  16. #define JSON_ASSERT GGML_ASSERT
  17. #include <nlohmann/json.hpp>
  18. #include <algorithm>
  19. #include <climits>
  20. #include <cstdarg>
  21. #include <fstream>
  22. #include <list>
  23. #include <regex>
  24. #include <set>
  25. #include <string>
  26. #include <thread> // for hardware_concurrency
  27. #include <vector>
  28. #ifdef __linux__
  29. #include <linux/limits.h>
  30. #elif defined(_WIN32)
  31. # if !defined(PATH_MAX)
  32. # define PATH_MAX MAX_PATH
  33. # endif
  34. #elif defined(_AIX)
  35. #include <sys/limits.h>
  36. #else
  37. #include <sys/syslimits.h>
  38. #endif
  39. #define LLAMA_MAX_URL_LENGTH 2084 // Maximum URL Length in Chrome: 2083
  40. using json = nlohmann::ordered_json;
  41. static std::initializer_list<enum llama_example> mmproj_examples = {
  42. LLAMA_EXAMPLE_MTMD,
  43. LLAMA_EXAMPLE_SERVER,
  44. };
  45. static std::string read_file(const std::string & fname) {
  46. std::ifstream file(fname);
  47. if (!file) {
  48. throw std::runtime_error(string_format("error: failed to open file '%s'\n", fname.c_str()));
  49. }
  50. std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
  51. file.close();
  52. return content;
  53. }
  54. common_arg & common_arg::set_examples(std::initializer_list<enum llama_example> examples) {
  55. this->examples = examples;
  56. return *this;
  57. }
  58. common_arg & common_arg::set_excludes(std::initializer_list<enum llama_example> excludes) {
  59. this->excludes = excludes;
  60. return *this;
  61. }
  62. common_arg & common_arg::set_env(const char * env) {
  63. help = help + "\n(env: " + env + ")";
  64. this->env = env;
  65. return *this;
  66. }
  67. common_arg & common_arg::set_sparam() {
  68. is_sparam = true;
  69. return *this;
  70. }
  71. bool common_arg::in_example(enum llama_example ex) {
  72. return examples.find(ex) != examples.end();
  73. }
  74. bool common_arg::is_exclude(enum llama_example ex) {
  75. return excludes.find(ex) != excludes.end();
  76. }
  77. bool common_arg::get_value_from_env(std::string & output) const {
  78. if (env == nullptr) return false;
  79. char * value = std::getenv(env);
  80. if (value) {
  81. output = value;
  82. return true;
  83. }
  84. return false;
  85. }
  86. bool common_arg::has_value_from_env() const {
  87. return env != nullptr && std::getenv(env);
  88. }
  89. static std::vector<std::string> break_str_into_lines(std::string input, size_t max_char_per_line) {
  90. std::vector<std::string> result;
  91. std::istringstream iss(input);
  92. std::string line;
  93. auto add_line = [&](const std::string& l) {
  94. if (l.length() <= max_char_per_line) {
  95. result.push_back(l);
  96. } else {
  97. std::istringstream line_stream(l);
  98. std::string word, current_line;
  99. while (line_stream >> word) {
  100. if (current_line.length() + !current_line.empty() + word.length() > max_char_per_line) {
  101. if (!current_line.empty()) result.push_back(current_line);
  102. current_line = word;
  103. } else {
  104. current_line += (!current_line.empty() ? " " : "") + word;
  105. }
  106. }
  107. if (!current_line.empty()) result.push_back(current_line);
  108. }
  109. };
  110. while (std::getline(iss, line)) {
  111. add_line(line);
  112. }
  113. return result;
  114. }
  115. std::string common_arg::to_string() {
  116. // params for printing to console
  117. const static int n_leading_spaces = 40;
  118. const static int n_char_per_line_help = 70; // TODO: detect this based on current console
  119. std::string leading_spaces(n_leading_spaces, ' ');
  120. std::ostringstream ss;
  121. for (const auto arg : args) {
  122. if (arg == args.front()) {
  123. if (args.size() == 1) {
  124. ss << arg;
  125. } else {
  126. // first arg is usually abbreviation, we need padding to make it more beautiful
  127. auto tmp = std::string(arg) + ", ";
  128. auto spaces = std::string(std::max(0, 7 - (int)tmp.size()), ' ');
  129. ss << tmp << spaces;
  130. }
  131. } else {
  132. ss << arg << (arg != args.back() ? ", " : "");
  133. }
  134. }
  135. if (value_hint) ss << " " << value_hint;
  136. if (value_hint_2) ss << " " << value_hint_2;
  137. if (ss.tellp() > n_leading_spaces - 3) {
  138. // current line is too long, add new line
  139. ss << "\n" << leading_spaces;
  140. } else {
  141. // padding between arg and help, same line
  142. ss << std::string(leading_spaces.size() - ss.tellp(), ' ');
  143. }
  144. const auto help_lines = break_str_into_lines(help, n_char_per_line_help);
  145. for (const auto & line : help_lines) {
  146. ss << (&line == &help_lines.front() ? "" : leading_spaces) << line << "\n";
  147. }
  148. return ss.str();
  149. }
  150. //
  151. // utils
  152. //
  153. // Helper function to parse tensor buffer override strings
  154. static void parse_tensor_buffer_overrides(const std::string & value, std::vector<llama_model_tensor_buft_override> & overrides) {
  155. std::map<std::string, ggml_backend_buffer_type_t> buft_list;
  156. for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
  157. auto * dev = ggml_backend_dev_get(i);
  158. auto * buft = ggml_backend_dev_buffer_type(dev);
  159. if (buft) {
  160. buft_list[ggml_backend_buft_name(buft)] = buft;
  161. }
  162. }
  163. for (const auto & override : string_split<std::string>(value, ',')) {
  164. std::string::size_type pos = override.find('=');
  165. if (pos == std::string::npos) {
  166. throw std::invalid_argument("invalid value");
  167. }
  168. std::string tensor_name = override.substr(0, pos);
  169. std::string buffer_type = override.substr(pos + 1);
  170. if (buft_list.find(buffer_type) == buft_list.end()) {
  171. printf("Available buffer types:\n");
  172. for (const auto & it : buft_list) {
  173. printf(" %s\n", ggml_backend_buft_name(it.second));
  174. }
  175. throw std::invalid_argument("unknown buffer type");
  176. }
  177. // keep strings alive and avoid leaking memory by storing them in a static vector
  178. static std::list<std::string> buft_overrides;
  179. buft_overrides.push_back(tensor_name);
  180. overrides.push_back({buft_overrides.back().c_str(), buft_list.at(buffer_type)});
  181. }
  182. }
  183. struct handle_model_result {
  184. bool found_mmproj = false;
  185. common_params_model mmproj;
  186. };
  187. static handle_model_result common_params_handle_model(
  188. struct common_params_model & model,
  189. const std::string & bearer_token,
  190. const std::string & model_path_default,
  191. bool offline) {
  192. handle_model_result result;
  193. // handle pre-fill default model path and url based on hf_repo and hf_file
  194. {
  195. if (!model.docker_repo.empty()) { // Handle Docker URLs by resolving them to local paths
  196. model.path = common_docker_resolve_model(model.docker_repo);
  197. } else if (!model.hf_repo.empty()) {
  198. // short-hand to avoid specifying --hf-file -> default it to --model
  199. if (model.hf_file.empty()) {
  200. if (model.path.empty()) {
  201. auto auto_detected = common_get_hf_file(model.hf_repo, bearer_token, offline);
  202. if (auto_detected.repo.empty() || auto_detected.ggufFile.empty()) {
  203. exit(1); // built without CURL, error message already printed
  204. }
  205. model.hf_repo = auto_detected.repo;
  206. model.hf_file = auto_detected.ggufFile;
  207. if (!auto_detected.mmprojFile.empty()) {
  208. result.found_mmproj = true;
  209. result.mmproj.hf_repo = model.hf_repo;
  210. result.mmproj.hf_file = auto_detected.mmprojFile;
  211. }
  212. } else {
  213. model.hf_file = model.path;
  214. }
  215. }
  216. std::string model_endpoint = get_model_endpoint();
  217. model.url = model_endpoint + model.hf_repo + "/resolve/main/" + model.hf_file;
  218. // make sure model path is present (for caching purposes)
  219. if (model.path.empty()) {
  220. // this is to avoid different repo having same file name, or same file name in different subdirs
  221. std::string filename = model.hf_repo + "_" + model.hf_file;
  222. // to make sure we don't have any slashes in the filename
  223. string_replace_all(filename, "/", "_");
  224. model.path = fs_get_cache_file(filename);
  225. }
  226. } else if (!model.url.empty()) {
  227. if (model.path.empty()) {
  228. auto f = string_split<std::string>(model.url, '#').front();
  229. f = string_split<std::string>(f, '?').front();
  230. model.path = fs_get_cache_file(string_split<std::string>(f, '/').back());
  231. }
  232. } else if (model.path.empty()) {
  233. model.path = model_path_default;
  234. }
  235. }
  236. // then, download it if needed
  237. if (!model.url.empty()) {
  238. bool ok = common_download_model(model, bearer_token, offline);
  239. if (!ok) {
  240. LOG_ERR("error: failed to download model from %s\n", model.url.c_str());
  241. exit(1);
  242. }
  243. }
  244. return result;
  245. }
  246. const std::vector<ggml_type> kv_cache_types = {
  247. GGML_TYPE_F32,
  248. GGML_TYPE_F16,
  249. GGML_TYPE_BF16,
  250. GGML_TYPE_Q8_0,
  251. GGML_TYPE_Q4_0,
  252. GGML_TYPE_Q4_1,
  253. GGML_TYPE_IQ4_NL,
  254. GGML_TYPE_Q5_0,
  255. GGML_TYPE_Q5_1,
  256. };
  257. static ggml_type kv_cache_type_from_str(const std::string & s) {
  258. for (const auto & type : kv_cache_types) {
  259. if (ggml_type_name(type) == s) {
  260. return type;
  261. }
  262. }
  263. throw std::runtime_error("Unsupported cache type: " + s);
  264. }
  265. static std::string get_all_kv_cache_types() {
  266. std::ostringstream msg;
  267. for (const auto & type : kv_cache_types) {
  268. msg << ggml_type_name(type) << (&type == &kv_cache_types.back() ? "" : ", ");
  269. }
  270. return msg.str();
  271. }
  272. //
  273. // CLI argument parsing functions
  274. //
  275. static bool common_params_parse_ex(int argc, char ** argv, common_params_context & ctx_arg) {
  276. common_params & params = ctx_arg.params;
  277. std::unordered_map<std::string, common_arg *> arg_to_options;
  278. for (auto & opt : ctx_arg.options) {
  279. for (const auto & arg : opt.args) {
  280. arg_to_options[arg] = &opt;
  281. }
  282. }
  283. // handle environment variables
  284. for (auto & opt : ctx_arg.options) {
  285. std::string value;
  286. if (opt.get_value_from_env(value)) {
  287. try {
  288. if (opt.handler_void && (value == "1" || value == "true")) {
  289. opt.handler_void(params);
  290. }
  291. if (opt.handler_int) {
  292. opt.handler_int(params, std::stoi(value));
  293. }
  294. if (opt.handler_string) {
  295. opt.handler_string(params, value);
  296. continue;
  297. }
  298. } catch (std::exception & e) {
  299. throw std::invalid_argument(string_format(
  300. "error while handling environment variable \"%s\": %s\n\n", opt.env, e.what()));
  301. }
  302. }
  303. }
  304. // handle command line arguments
  305. auto check_arg = [&](int i) {
  306. if (i+1 >= argc) {
  307. throw std::invalid_argument("expected value for argument");
  308. }
  309. };
  310. for (int i = 1; i < argc; i++) {
  311. const std::string arg_prefix = "--";
  312. std::string arg = argv[i];
  313. if (arg.compare(0, arg_prefix.size(), arg_prefix) == 0) {
  314. std::replace(arg.begin(), arg.end(), '_', '-');
  315. }
  316. if (arg_to_options.find(arg) == arg_to_options.end()) {
  317. throw std::invalid_argument(string_format("error: invalid argument: %s", arg.c_str()));
  318. }
  319. auto opt = *arg_to_options[arg];
  320. if (opt.has_value_from_env()) {
  321. fprintf(stderr, "warn: %s environment variable is set, but will be overwritten by command line argument %s\n", opt.env, arg.c_str());
  322. }
  323. try {
  324. if (opt.handler_void) {
  325. opt.handler_void(params);
  326. continue;
  327. }
  328. // arg with single value
  329. check_arg(i);
  330. std::string val = argv[++i];
  331. if (opt.handler_int) {
  332. opt.handler_int(params, std::stoi(val));
  333. continue;
  334. }
  335. if (opt.handler_string) {
  336. opt.handler_string(params, val);
  337. continue;
  338. }
  339. // arg with 2 values
  340. check_arg(i);
  341. std::string val2 = argv[++i];
  342. if (opt.handler_str_str) {
  343. opt.handler_str_str(params, val, val2);
  344. continue;
  345. }
  346. } catch (std::exception & e) {
  347. throw std::invalid_argument(string_format(
  348. "error while handling argument \"%s\": %s\n\n"
  349. "usage:\n%s\n\nto show complete usage, run with -h",
  350. arg.c_str(), e.what(), arg_to_options[arg]->to_string().c_str()));
  351. }
  352. }
  353. postprocess_cpu_params(params.cpuparams, nullptr);
  354. postprocess_cpu_params(params.cpuparams_batch, &params.cpuparams);
  355. postprocess_cpu_params(params.speculative.cpuparams, &params.cpuparams);
  356. postprocess_cpu_params(params.speculative.cpuparams_batch, &params.cpuparams_batch);
  357. if (params.prompt_cache_all && (params.interactive || params.interactive_first)) {
  358. throw std::invalid_argument("error: --prompt-cache-all not supported in interactive mode yet\n");
  359. }
  360. // handle model and download
  361. {
  362. auto res = common_params_handle_model(params.model, params.hf_token, DEFAULT_MODEL_PATH, params.offline);
  363. if (params.no_mmproj) {
  364. params.mmproj = {};
  365. } else if (res.found_mmproj && params.mmproj.path.empty() && params.mmproj.url.empty()) {
  366. // optionally, handle mmproj model when -hf is specified
  367. params.mmproj = res.mmproj;
  368. }
  369. // only download mmproj if the current example is using it
  370. for (auto & ex : mmproj_examples) {
  371. if (ctx_arg.ex == ex) {
  372. common_params_handle_model(params.mmproj, params.hf_token, "", params.offline);
  373. break;
  374. }
  375. }
  376. common_params_handle_model(params.speculative.model, params.hf_token, "", params.offline);
  377. common_params_handle_model(params.vocoder.model, params.hf_token, "", params.offline);
  378. }
  379. if (params.escape) {
  380. string_process_escapes(params.prompt);
  381. string_process_escapes(params.input_prefix);
  382. string_process_escapes(params.input_suffix);
  383. for (auto & antiprompt : params.antiprompt) {
  384. string_process_escapes(antiprompt);
  385. }
  386. for (auto & seq_breaker : params.sampling.dry_sequence_breakers) {
  387. string_process_escapes(seq_breaker);
  388. }
  389. for (auto & pair : params.speculative.replacements) {
  390. string_process_escapes(pair.first);
  391. string_process_escapes(pair.second);
  392. }
  393. }
  394. if (!params.kv_overrides.empty()) {
  395. params.kv_overrides.emplace_back();
  396. params.kv_overrides.back().key[0] = 0;
  397. }
  398. if (!params.tensor_buft_overrides.empty()) {
  399. params.tensor_buft_overrides.push_back({nullptr, nullptr});
  400. }
  401. if (!params.speculative.tensor_buft_overrides.empty()) {
  402. params.speculative.tensor_buft_overrides.push_back({nullptr, nullptr});
  403. }
  404. if (!params.chat_template.empty() && !common_chat_verify_template(params.chat_template, params.use_jinja)) {
  405. throw std::runtime_error(string_format(
  406. "error: the supplied chat template is not supported: %s%s\n",
  407. params.chat_template.c_str(),
  408. params.use_jinja ? "" : "\nnote: llama.cpp was started without --jinja, we only support commonly used templates"
  409. ));
  410. }
  411. return true;
  412. }
  413. static void common_params_print_usage(common_params_context & ctx_arg) {
  414. auto print_options = [](std::vector<common_arg *> & options) {
  415. for (common_arg * opt : options) {
  416. printf("%s", opt->to_string().c_str());
  417. }
  418. };
  419. std::vector<common_arg *> common_options;
  420. std::vector<common_arg *> sparam_options;
  421. std::vector<common_arg *> specific_options;
  422. for (auto & opt : ctx_arg.options) {
  423. // in case multiple LLAMA_EXAMPLE_* are set, we prioritize the LLAMA_EXAMPLE_* matching current example
  424. if (opt.is_sparam) {
  425. sparam_options.push_back(&opt);
  426. } else if (opt.in_example(ctx_arg.ex)) {
  427. specific_options.push_back(&opt);
  428. } else {
  429. common_options.push_back(&opt);
  430. }
  431. }
  432. printf("----- common params -----\n\n");
  433. print_options(common_options);
  434. printf("\n\n----- sampling params -----\n\n");
  435. print_options(sparam_options);
  436. // TODO: maybe convert enum llama_example to string
  437. printf("\n\n----- example-specific params -----\n\n");
  438. print_options(specific_options);
  439. }
  440. static void common_params_print_completion(common_params_context & ctx_arg) {
  441. std::vector<common_arg *> common_options;
  442. std::vector<common_arg *> sparam_options;
  443. std::vector<common_arg *> specific_options;
  444. for (auto & opt : ctx_arg.options) {
  445. if (opt.is_sparam) {
  446. sparam_options.push_back(&opt);
  447. } else if (opt.in_example(ctx_arg.ex)) {
  448. specific_options.push_back(&opt);
  449. } else {
  450. common_options.push_back(&opt);
  451. }
  452. }
  453. printf("_llama_completions() {\n");
  454. printf(" local cur prev opts\n");
  455. printf(" COMPREPLY=()\n");
  456. printf(" cur=\"${COMP_WORDS[COMP_CWORD]}\"\n");
  457. printf(" prev=\"${COMP_WORDS[COMP_CWORD-1]}\"\n\n");
  458. printf(" opts=\"");
  459. auto print_options = [](const std::vector<common_arg *> & options) {
  460. for (const common_arg * opt : options) {
  461. for (const char * arg : opt->args) {
  462. printf("%s ", arg);
  463. }
  464. }
  465. };
  466. print_options(common_options);
  467. print_options(sparam_options);
  468. print_options(specific_options);
  469. printf("\"\n\n");
  470. printf(" case \"$prev\" in\n");
  471. printf(" --model|-m)\n");
  472. printf(" COMPREPLY=( $(compgen -f -X '!*.gguf' -- \"$cur\") $(compgen -d -- \"$cur\") )\n");
  473. printf(" return 0\n");
  474. printf(" ;;\n");
  475. printf(" --grammar-file)\n");
  476. printf(" COMPREPLY=( $(compgen -f -X '!*.gbnf' -- \"$cur\") $(compgen -d -- \"$cur\") )\n");
  477. printf(" return 0\n");
  478. printf(" ;;\n");
  479. printf(" --chat-template-file)\n");
  480. printf(" COMPREPLY=( $(compgen -f -X '!*.jinja' -- \"$cur\") $(compgen -d -- \"$cur\") )\n");
  481. printf(" return 0\n");
  482. printf(" ;;\n");
  483. printf(" *)\n");
  484. printf(" COMPREPLY=( $(compgen -W \"${opts}\" -- \"$cur\") )\n");
  485. printf(" return 0\n");
  486. printf(" ;;\n");
  487. printf(" esac\n");
  488. printf("}\n\n");
  489. std::set<std::string> executables = {
  490. "llama-batched",
  491. "llama-batched-bench",
  492. "llama-bench",
  493. "llama-cli",
  494. "llama-convert-llama2c-to-ggml",
  495. "llama-cvector-generator",
  496. "llama-embedding",
  497. "llama-eval-callback",
  498. "llama-export-lora",
  499. "llama-gen-docs",
  500. "llama-gguf",
  501. "llama-gguf-hash",
  502. "llama-gguf-split",
  503. "llama-gritlm",
  504. "llama-imatrix",
  505. "llama-infill",
  506. "llama-mtmd-cli",
  507. "llama-llava-clip-quantize-cli",
  508. "llama-lookahead",
  509. "llama-lookup",
  510. "llama-lookup-create",
  511. "llama-lookup-merge",
  512. "llama-lookup-stats",
  513. "llama-parallel",
  514. "llama-passkey",
  515. "llama-perplexity",
  516. "llama-q8dot",
  517. "llama-quantize",
  518. "llama-qwen2vl-cli",
  519. "llama-retrieval",
  520. "llama-run",
  521. "llama-save-load-state",
  522. "llama-server",
  523. "llama-simple",
  524. "llama-simple-chat",
  525. "llama-speculative",
  526. "llama-speculative-simple",
  527. "llama-tokenize",
  528. "llama-tts",
  529. "llama-vdot"
  530. };
  531. for (const auto& exe : executables) {
  532. printf("complete -F _llama_completions %s\n", exe.c_str());
  533. }
  534. }
  535. static std::vector<ggml_backend_dev_t> parse_device_list(const std::string & value) {
  536. std::vector<ggml_backend_dev_t> devices;
  537. auto dev_names = string_split<std::string>(value, ',');
  538. if (dev_names.empty()) {
  539. throw std::invalid_argument("no devices specified");
  540. }
  541. if (dev_names.size() == 1 && dev_names[0] == "none") {
  542. devices.push_back(nullptr);
  543. } else {
  544. for (const auto & device : dev_names) {
  545. auto * dev = ggml_backend_dev_by_name(device.c_str());
  546. if (!dev || ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_CPU) {
  547. throw std::invalid_argument(string_format("invalid device: %s", device.c_str()));
  548. }
  549. devices.push_back(dev);
  550. }
  551. devices.push_back(nullptr);
  552. }
  553. return devices;
  554. }
  555. static void add_rpc_devices(const std::string & servers) {
  556. auto rpc_servers = string_split<std::string>(servers, ',');
  557. if (rpc_servers.empty()) {
  558. throw std::invalid_argument("no RPC servers specified");
  559. }
  560. ggml_backend_reg_t rpc_reg = ggml_backend_reg_by_name("RPC");
  561. if (!rpc_reg) {
  562. throw std::invalid_argument("failed to find RPC backend");
  563. }
  564. typedef ggml_backend_reg_t (*ggml_backend_rpc_add_server_t)(const char * endpoint);
  565. ggml_backend_rpc_add_server_t ggml_backend_rpc_add_server_fn = (ggml_backend_rpc_add_server_t) ggml_backend_reg_get_proc_address(rpc_reg, "ggml_backend_rpc_add_server");
  566. if (!ggml_backend_rpc_add_server_fn) {
  567. throw std::invalid_argument("failed to find RPC add server function");
  568. }
  569. for (const auto & server : rpc_servers) {
  570. auto reg = ggml_backend_rpc_add_server_fn(server.c_str());
  571. ggml_backend_register(reg);
  572. }
  573. }
  574. bool common_params_parse(int argc, char ** argv, common_params & params, llama_example ex, void(*print_usage)(int, char **)) {
  575. auto ctx_arg = common_params_parser_init(params, ex, print_usage);
  576. const common_params params_org = ctx_arg.params; // the example can modify the default params
  577. try {
  578. if (!common_params_parse_ex(argc, argv, ctx_arg)) {
  579. ctx_arg.params = params_org;
  580. return false;
  581. }
  582. if (ctx_arg.params.usage) {
  583. common_params_print_usage(ctx_arg);
  584. if (ctx_arg.print_usage) {
  585. ctx_arg.print_usage(argc, argv);
  586. }
  587. exit(0);
  588. }
  589. if (ctx_arg.params.completion) {
  590. common_params_print_completion(ctx_arg);
  591. exit(0);
  592. }
  593. params.lr.init();
  594. } catch (const std::invalid_argument & ex) {
  595. fprintf(stderr, "%s\n", ex.what());
  596. ctx_arg.params = params_org;
  597. return false;
  598. } catch (std::exception & ex) {
  599. fprintf(stderr, "%s\n", ex.what());
  600. exit(1); // for other exceptions, we exit with status code 1
  601. }
  602. return true;
  603. }
  604. static std::string list_builtin_chat_templates() {
  605. std::vector<const char *> supported_tmpl;
  606. int32_t res = llama_chat_builtin_templates(nullptr, 0);
  607. supported_tmpl.resize(res);
  608. res = llama_chat_builtin_templates(supported_tmpl.data(), supported_tmpl.size());
  609. std::ostringstream msg;
  610. for (auto & tmpl : supported_tmpl) {
  611. msg << tmpl << (&tmpl == &supported_tmpl.back() ? "" : ", ");
  612. }
  613. return msg.str();
  614. }
  615. static bool is_truthy(const std::string & value) {
  616. return value == "on" || value == "enabled" || value == "1";
  617. }
  618. static bool is_falsey(const std::string & value) {
  619. return value == "off" || value == "disabled" || value == "0";
  620. }
  621. static bool is_autoy(const std::string & value) {
  622. return value == "auto" || value == "-1";
  623. }
  624. common_params_context common_params_parser_init(common_params & params, llama_example ex, void(*print_usage)(int, char **)) {
  625. // load dynamic backends
  626. ggml_backend_load_all();
  627. common_params_context ctx_arg(params);
  628. ctx_arg.print_usage = print_usage;
  629. ctx_arg.ex = ex;
  630. std::string sampler_type_chars;
  631. std::string sampler_type_names;
  632. for (const auto & sampler : params.sampling.samplers) {
  633. sampler_type_chars += common_sampler_type_to_chr(sampler);
  634. sampler_type_names += common_sampler_type_to_str(sampler) + ";";
  635. }
  636. sampler_type_names.pop_back();
  637. /**
  638. * filter options by example
  639. * rules:
  640. * - all examples inherit options from LLAMA_EXAMPLE_COMMON
  641. * - if LLAMA_EXAMPLE_* is set (other than COMMON), we only show the option in the corresponding example
  642. * - if both {LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_*,} are set, we will prioritize the LLAMA_EXAMPLE_* matching current example
  643. */
  644. auto add_opt = [&](common_arg arg) {
  645. if ((arg.in_example(ex) || arg.in_example(LLAMA_EXAMPLE_COMMON)) && !arg.is_exclude(ex)) {
  646. ctx_arg.options.push_back(std::move(arg));
  647. }
  648. };
  649. add_opt(common_arg(
  650. {"-h", "--help", "--usage"},
  651. "print usage and exit",
  652. [](common_params & params) {
  653. params.usage = true;
  654. }
  655. ));
  656. add_opt(common_arg(
  657. {"--version"},
  658. "show version and build info",
  659. [](common_params &) {
  660. fprintf(stderr, "version: %d (%s)\n", LLAMA_BUILD_NUMBER, LLAMA_COMMIT);
  661. fprintf(stderr, "built with %s for %s\n", LLAMA_COMPILER, LLAMA_BUILD_TARGET);
  662. exit(0);
  663. }
  664. ));
  665. add_opt(common_arg(
  666. {"-cl", "--cache-list"},
  667. "show list of models in cache",
  668. [](common_params &) {
  669. printf("model cache directory: %s\n", fs_get_cache_directory().c_str());
  670. auto models = common_list_cached_models();
  671. printf("number of models in cache: %zu\n", models.size());
  672. for (size_t i = 0; i < models.size(); i++) {
  673. auto & model = models[i];
  674. printf("%4d. %s\n", (int) i + 1, model.to_string().c_str());
  675. }
  676. exit(0);
  677. }
  678. ));
  679. add_opt(common_arg(
  680. {"--completion-bash"},
  681. "print source-able bash completion script for llama.cpp",
  682. [](common_params & params) {
  683. params.completion = true;
  684. }
  685. ));
  686. add_opt(common_arg(
  687. {"--verbose-prompt"},
  688. string_format("print a verbose prompt before generation (default: %s)", params.verbose_prompt ? "true" : "false"),
  689. [](common_params & params) {
  690. params.verbose_prompt = true;
  691. }
  692. ));
  693. add_opt(common_arg(
  694. {"--no-display-prompt"},
  695. string_format("don't print prompt at generation (default: %s)", !params.display_prompt ? "true" : "false"),
  696. [](common_params & params) {
  697. params.display_prompt = false;
  698. }
  699. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  700. add_opt(common_arg(
  701. {"-co", "--color"},
  702. string_format("colorise output to distinguish prompt and user input from generations (default: %s)", params.use_color ? "true" : "false"),
  703. [](common_params & params) {
  704. params.use_color = true;
  705. }
  706. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_LOOKUP}));
  707. add_opt(common_arg(
  708. {"-t", "--threads"}, "N",
  709. string_format("number of CPU threads to use during generation (default: %d)", params.cpuparams.n_threads),
  710. [](common_params & params, int value) {
  711. params.cpuparams.n_threads = value;
  712. if (params.cpuparams.n_threads <= 0) {
  713. params.cpuparams.n_threads = std::thread::hardware_concurrency();
  714. }
  715. }
  716. ).set_env("LLAMA_ARG_THREADS"));
  717. add_opt(common_arg(
  718. {"-tb", "--threads-batch"}, "N",
  719. "number of threads to use during batch and prompt processing (default: same as --threads)",
  720. [](common_params & params, int value) {
  721. params.cpuparams_batch.n_threads = value;
  722. if (params.cpuparams_batch.n_threads <= 0) {
  723. params.cpuparams_batch.n_threads = std::thread::hardware_concurrency();
  724. }
  725. }
  726. ));
  727. add_opt(common_arg(
  728. {"-C", "--cpu-mask"}, "M",
  729. "CPU affinity mask: arbitrarily long hex. Complements cpu-range (default: \"\")",
  730. [](common_params & params, const std::string & mask) {
  731. params.cpuparams.mask_valid = true;
  732. if (!parse_cpu_mask(mask, params.cpuparams.cpumask)) {
  733. throw std::invalid_argument("invalid cpumask");
  734. }
  735. }
  736. ));
  737. add_opt(common_arg(
  738. {"-Cr", "--cpu-range"}, "lo-hi",
  739. "range of CPUs for affinity. Complements --cpu-mask",
  740. [](common_params & params, const std::string & range) {
  741. params.cpuparams.mask_valid = true;
  742. if (!parse_cpu_range(range, params.cpuparams.cpumask)) {
  743. throw std::invalid_argument("invalid range");
  744. }
  745. }
  746. ));
  747. add_opt(common_arg(
  748. {"--cpu-strict"}, "<0|1>",
  749. string_format("use strict CPU placement (default: %u)\n", (unsigned) params.cpuparams.strict_cpu),
  750. [](common_params & params, const std::string & value) {
  751. params.cpuparams.strict_cpu = std::stoul(value);
  752. }
  753. ));
  754. add_opt(common_arg(
  755. {"--prio"}, "N",
  756. string_format("set process/thread priority : low(-1), normal(0), medium(1), high(2), realtime(3) (default: %d)\n", params.cpuparams.priority),
  757. [](common_params & params, int prio) {
  758. if (prio < GGML_SCHED_PRIO_LOW || prio > GGML_SCHED_PRIO_REALTIME) {
  759. throw std::invalid_argument("invalid value");
  760. }
  761. params.cpuparams.priority = (enum ggml_sched_priority) prio;
  762. }
  763. ));
  764. add_opt(common_arg(
  765. {"--poll"}, "<0...100>",
  766. string_format("use polling level to wait for work (0 - no polling, default: %u)\n", (unsigned) params.cpuparams.poll),
  767. [](common_params & params, const std::string & value) {
  768. params.cpuparams.poll = std::stoul(value);
  769. }
  770. ));
  771. add_opt(common_arg(
  772. {"-Cb", "--cpu-mask-batch"}, "M",
  773. "CPU affinity mask: arbitrarily long hex. Complements cpu-range-batch (default: same as --cpu-mask)",
  774. [](common_params & params, const std::string & mask) {
  775. params.cpuparams_batch.mask_valid = true;
  776. if (!parse_cpu_mask(mask, params.cpuparams_batch.cpumask)) {
  777. throw std::invalid_argument("invalid cpumask");
  778. }
  779. }
  780. ));
  781. add_opt(common_arg(
  782. {"-Crb", "--cpu-range-batch"}, "lo-hi",
  783. "ranges of CPUs for affinity. Complements --cpu-mask-batch",
  784. [](common_params & params, const std::string & range) {
  785. params.cpuparams_batch.mask_valid = true;
  786. if (!parse_cpu_range(range, params.cpuparams_batch.cpumask)) {
  787. throw std::invalid_argument("invalid range");
  788. }
  789. }
  790. ));
  791. add_opt(common_arg(
  792. {"--cpu-strict-batch"}, "<0|1>",
  793. "use strict CPU placement (default: same as --cpu-strict)",
  794. [](common_params & params, int value) {
  795. params.cpuparams_batch.strict_cpu = value;
  796. }
  797. ));
  798. add_opt(common_arg(
  799. {"--prio-batch"}, "N",
  800. string_format("set process/thread priority : 0-normal, 1-medium, 2-high, 3-realtime (default: %d)\n", params.cpuparams_batch.priority),
  801. [](common_params & params, int prio) {
  802. if (prio < 0 || prio > 3) {
  803. throw std::invalid_argument("invalid value");
  804. }
  805. params.cpuparams_batch.priority = (enum ggml_sched_priority) prio;
  806. }
  807. ));
  808. add_opt(common_arg(
  809. {"--poll-batch"}, "<0|1>",
  810. "use polling to wait for work (default: same as --poll)",
  811. [](common_params & params, int value) {
  812. params.cpuparams_batch.poll = value;
  813. }
  814. ));
  815. add_opt(common_arg(
  816. {"-lcs", "--lookup-cache-static"}, "FNAME",
  817. "path to static lookup cache to use for lookup decoding (not updated by generation)",
  818. [](common_params & params, const std::string & value) {
  819. params.lookup_cache_static = value;
  820. }
  821. ).set_examples({LLAMA_EXAMPLE_LOOKUP}));
  822. add_opt(common_arg(
  823. {"-lcd", "--lookup-cache-dynamic"}, "FNAME",
  824. "path to dynamic lookup cache to use for lookup decoding (updated by generation)",
  825. [](common_params & params, const std::string & value) {
  826. params.lookup_cache_dynamic = value;
  827. }
  828. ).set_examples({LLAMA_EXAMPLE_LOOKUP}));
  829. add_opt(common_arg(
  830. {"-c", "--ctx-size"}, "N",
  831. string_format("size of the prompt context (default: %d, 0 = loaded from model)", params.n_ctx),
  832. [](common_params & params, int value) {
  833. params.n_ctx = value;
  834. }
  835. ).set_env("LLAMA_ARG_CTX_SIZE"));
  836. add_opt(common_arg(
  837. {"-n", "--predict", "--n-predict"}, "N",
  838. string_format(
  839. ex == LLAMA_EXAMPLE_MAIN
  840. ? "number of tokens to predict (default: %d, -1 = infinity, -2 = until context filled)"
  841. : "number of tokens to predict (default: %d, -1 = infinity)",
  842. params.n_predict),
  843. [](common_params & params, int value) {
  844. params.n_predict = value;
  845. }
  846. ).set_env("LLAMA_ARG_N_PREDICT"));
  847. add_opt(common_arg(
  848. {"-b", "--batch-size"}, "N",
  849. string_format("logical maximum batch size (default: %d)", params.n_batch),
  850. [](common_params & params, int value) {
  851. params.n_batch = value;
  852. }
  853. ).set_env("LLAMA_ARG_BATCH"));
  854. add_opt(common_arg(
  855. {"-ub", "--ubatch-size"}, "N",
  856. string_format("physical maximum batch size (default: %d)", params.n_ubatch),
  857. [](common_params & params, int value) {
  858. params.n_ubatch = value;
  859. }
  860. ).set_env("LLAMA_ARG_UBATCH"));
  861. add_opt(common_arg(
  862. {"--keep"}, "N",
  863. string_format("number of tokens to keep from the initial prompt (default: %d, -1 = all)", params.n_keep),
  864. [](common_params & params, int value) {
  865. params.n_keep = value;
  866. }
  867. ));
  868. add_opt(common_arg(
  869. {"--swa-full"},
  870. string_format("use full-size SWA cache (default: %s)\n"
  871. "[(more info)](https://github.com/ggml-org/llama.cpp/pull/13194#issuecomment-2868343055)", params.swa_full ? "true" : "false"),
  872. [](common_params & params) {
  873. params.swa_full = true;
  874. }
  875. ).set_env("LLAMA_ARG_SWA_FULL"));
  876. add_opt(common_arg(
  877. {"--ctx-checkpoints", "--swa-checkpoints"}, "N",
  878. string_format("max number of context checkpoints to create per slot (default: %d)\n"
  879. "[(more info)](https://github.com/ggml-org/llama.cpp/pull/15293)", params.n_ctx_checkpoints),
  880. [](common_params & params, int value) {
  881. params.n_ctx_checkpoints = value;
  882. }
  883. ).set_env("LLAMA_ARG_CTX_CHECKPOINTS").set_examples({LLAMA_EXAMPLE_SERVER}));
  884. add_opt(common_arg(
  885. {"--cache-ram", "-cram"}, "N",
  886. string_format("set the maximum cache size in MiB (default: %d, -1 - no limit, 0 - disable)\n"
  887. "[(more info)](https://github.com/ggml-org/llama.cpp/pull/16391)", params.cache_ram_mib),
  888. [](common_params & params, int value) {
  889. params.cache_ram_mib = value;
  890. }
  891. ).set_env("LLAMA_ARG_CACHE_RAM").set_examples({LLAMA_EXAMPLE_SERVER}));
  892. add_opt(common_arg(
  893. {"--kv-unified", "-kvu"},
  894. string_format("use single unified KV buffer for the KV cache of all sequences (default: %s)\n"
  895. "[(more info)](https://github.com/ggml-org/llama.cpp/pull/14363)", params.kv_unified ? "true" : "false"),
  896. [](common_params & params) {
  897. params.kv_unified = true;
  898. }
  899. ).set_env("LLAMA_ARG_KV_SPLIT"));
  900. add_opt(common_arg(
  901. {"--no-context-shift"},
  902. string_format("disables context shift on infinite text generation (default: %s)", params.ctx_shift ? "disabled" : "enabled"),
  903. [](common_params & params) {
  904. params.ctx_shift = false;
  905. }
  906. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_IMATRIX, LLAMA_EXAMPLE_PERPLEXITY}).set_env("LLAMA_ARG_NO_CONTEXT_SHIFT"));
  907. add_opt(common_arg(
  908. {"--context-shift"},
  909. string_format("enables context shift on infinite text generation (default: %s)", params.ctx_shift ? "enabled" : "disabled"),
  910. [](common_params & params) {
  911. params.ctx_shift = true;
  912. }
  913. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_IMATRIX, LLAMA_EXAMPLE_PERPLEXITY}).set_env("LLAMA_ARG_CONTEXT_SHIFT"));
  914. add_opt(common_arg(
  915. {"--chunks"}, "N",
  916. string_format("max number of chunks to process (default: %d, -1 = all)", params.n_chunks),
  917. [](common_params & params, int value) {
  918. params.n_chunks = value;
  919. }
  920. ).set_examples({LLAMA_EXAMPLE_IMATRIX, LLAMA_EXAMPLE_PERPLEXITY, LLAMA_EXAMPLE_RETRIEVAL}));
  921. add_opt(common_arg({ "-fa", "--flash-attn" }, "[on|off|auto]",
  922. string_format("set Flash Attention use ('on', 'off', or 'auto', default: '%s')",
  923. llama_flash_attn_type_name(params.flash_attn_type)),
  924. [](common_params & params, const std::string & value) {
  925. if (is_truthy(value)) {
  926. params.flash_attn_type = LLAMA_FLASH_ATTN_TYPE_ENABLED;
  927. } else if (is_falsey(value)) {
  928. params.flash_attn_type = LLAMA_FLASH_ATTN_TYPE_DISABLED;
  929. } else if (is_autoy(value)) {
  930. params.flash_attn_type = LLAMA_FLASH_ATTN_TYPE_AUTO;
  931. } else {
  932. throw std::runtime_error(
  933. string_format("error: unkown value for --flash-attn: '%s'\n", value.c_str()));
  934. }
  935. }).set_env("LLAMA_ARG_FLASH_ATTN"));
  936. add_opt(common_arg(
  937. {"-p", "--prompt"}, "PROMPT",
  938. "prompt to start generation with; for system message, use -sys",
  939. [](common_params & params, const std::string & value) {
  940. params.prompt = value;
  941. }
  942. ).set_excludes({LLAMA_EXAMPLE_SERVER}));
  943. add_opt(common_arg(
  944. {"-sys", "--system-prompt"}, "PROMPT",
  945. "system prompt to use with model (if applicable, depending on chat template)",
  946. [](common_params & params, const std::string & value) {
  947. params.system_prompt = value;
  948. }
  949. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_DIFFUSION}));
  950. add_opt(common_arg(
  951. {"--no-perf"},
  952. string_format("disable internal libllama performance timings (default: %s)", params.no_perf ? "true" : "false"),
  953. [](common_params & params) {
  954. params.no_perf = true;
  955. params.sampling.no_perf = true;
  956. }
  957. ).set_env("LLAMA_ARG_NO_PERF"));
  958. add_opt(common_arg(
  959. {"-f", "--file"}, "FNAME",
  960. "a file containing the prompt (default: none)",
  961. [](common_params & params, const std::string & value) {
  962. params.prompt = read_file(value);
  963. // store the external file name in params
  964. params.prompt_file = value;
  965. if (!params.prompt.empty() && params.prompt.back() == '\n') {
  966. params.prompt.pop_back();
  967. }
  968. }
  969. ).set_excludes({LLAMA_EXAMPLE_SERVER}));
  970. add_opt(common_arg(
  971. {"-sysf", "--system-prompt-file"}, "FNAME",
  972. "a file containing the system prompt (default: none)",
  973. [](common_params & params, const std::string & value) {
  974. params.system_prompt = read_file(value);
  975. if (!params.system_prompt.empty() && params.system_prompt.back() == '\n') {
  976. params.system_prompt.pop_back();
  977. }
  978. }
  979. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_DIFFUSION}));
  980. add_opt(common_arg(
  981. {"--in-file"}, "FNAME",
  982. "an input file (repeat to specify multiple files)",
  983. [](common_params & params, const std::string & value) {
  984. std::ifstream file(value);
  985. if (!file) {
  986. throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
  987. }
  988. params.in_files.push_back(value);
  989. }
  990. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  991. add_opt(common_arg(
  992. {"-bf", "--binary-file"}, "FNAME",
  993. "binary file containing the prompt (default: none)",
  994. [](common_params & params, const std::string & value) {
  995. std::ifstream file(value, std::ios::binary);
  996. if (!file) {
  997. throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
  998. }
  999. // store the external file name in params
  1000. params.prompt_file = value;
  1001. std::ostringstream ss;
  1002. ss << file.rdbuf();
  1003. params.prompt = ss.str();
  1004. fprintf(stderr, "Read %zu bytes from binary file %s\n", params.prompt.size(), value.c_str());
  1005. }
  1006. ).set_excludes({LLAMA_EXAMPLE_SERVER}));
  1007. add_opt(common_arg(
  1008. {"-e", "--escape"},
  1009. string_format("process escapes sequences (\\n, \\r, \\t, \\', \\\", \\\\) (default: %s)", params.escape ? "true" : "false"),
  1010. [](common_params & params) {
  1011. params.escape = true;
  1012. }
  1013. ));
  1014. add_opt(common_arg(
  1015. {"--no-escape"},
  1016. "do not process escape sequences",
  1017. [](common_params & params) {
  1018. params.escape = false;
  1019. }
  1020. ));
  1021. add_opt(common_arg(
  1022. {"-ptc", "--print-token-count"}, "N",
  1023. string_format("print token count every N tokens (default: %d)", params.n_print),
  1024. [](common_params & params, int value) {
  1025. params.n_print = value;
  1026. }
  1027. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  1028. add_opt(common_arg(
  1029. {"--prompt-cache"}, "FNAME",
  1030. "file to cache prompt state for faster startup (default: none)",
  1031. [](common_params & params, const std::string & value) {
  1032. params.path_prompt_cache = value;
  1033. }
  1034. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  1035. add_opt(common_arg(
  1036. {"--prompt-cache-all"},
  1037. "if specified, saves user input and generations to cache as well\n",
  1038. [](common_params & params) {
  1039. params.prompt_cache_all = true;
  1040. }
  1041. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  1042. add_opt(common_arg(
  1043. {"--prompt-cache-ro"},
  1044. "if specified, uses the prompt cache but does not update it",
  1045. [](common_params & params) {
  1046. params.prompt_cache_ro = true;
  1047. }
  1048. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  1049. add_opt(common_arg(
  1050. {"-r", "--reverse-prompt"}, "PROMPT",
  1051. "halt generation at PROMPT, return control in interactive mode\n",
  1052. [](common_params & params, const std::string & value) {
  1053. params.antiprompt.emplace_back(value);
  1054. }
  1055. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_SERVER}));
  1056. add_opt(common_arg(
  1057. {"-sp", "--special"},
  1058. string_format("special tokens output enabled (default: %s)", params.special ? "true" : "false"),
  1059. [](common_params & params) {
  1060. params.special = true;
  1061. }
  1062. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_SERVER}));
  1063. add_opt(common_arg(
  1064. {"-cnv", "--conversation"},
  1065. "run in conversation mode:\n"
  1066. "- does not print special tokens and suffix/prefix\n"
  1067. "- interactive mode is also enabled\n"
  1068. "(default: auto enabled if chat template is available)",
  1069. [](common_params & params) {
  1070. params.conversation_mode = COMMON_CONVERSATION_MODE_ENABLED;
  1071. }
  1072. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  1073. add_opt(common_arg(
  1074. {"-no-cnv", "--no-conversation"},
  1075. "force disable conversation mode (default: false)",
  1076. [](common_params & params) {
  1077. params.conversation_mode = COMMON_CONVERSATION_MODE_DISABLED;
  1078. }
  1079. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  1080. add_opt(common_arg(
  1081. {"-st", "--single-turn"},
  1082. "run conversation for a single turn only, then exit when done\n"
  1083. "will not be interactive if first turn is predefined with --prompt\n"
  1084. "(default: false)",
  1085. [](common_params & params) {
  1086. params.single_turn = true;
  1087. }
  1088. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  1089. add_opt(common_arg(
  1090. {"-i", "--interactive"},
  1091. string_format("run in interactive mode (default: %s)", params.interactive ? "true" : "false"),
  1092. [](common_params & params) {
  1093. params.interactive = true;
  1094. }
  1095. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  1096. add_opt(common_arg(
  1097. {"-if", "--interactive-first"},
  1098. string_format("run in interactive mode and wait for input right away (default: %s)", params.interactive_first ? "true" : "false"),
  1099. [](common_params & params) {
  1100. params.interactive_first = true;
  1101. }
  1102. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  1103. add_opt(common_arg(
  1104. {"-mli", "--multiline-input"},
  1105. "allows you to write or paste multiple lines without ending each in '\\'",
  1106. [](common_params & params) {
  1107. params.multiline_input = true;
  1108. }
  1109. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  1110. add_opt(common_arg(
  1111. {"--in-prefix-bos"},
  1112. "prefix BOS to user inputs, preceding the `--in-prefix` string",
  1113. [](common_params & params) {
  1114. params.input_prefix_bos = true;
  1115. params.enable_chat_template = false;
  1116. }
  1117. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  1118. add_opt(common_arg(
  1119. {"--in-prefix"}, "STRING",
  1120. "string to prefix user inputs with (default: empty)",
  1121. [](common_params & params, const std::string & value) {
  1122. params.input_prefix = value;
  1123. params.enable_chat_template = false;
  1124. }
  1125. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  1126. add_opt(common_arg(
  1127. {"--in-suffix"}, "STRING",
  1128. "string to suffix after user inputs with (default: empty)",
  1129. [](common_params & params, const std::string & value) {
  1130. params.input_suffix = value;
  1131. params.enable_chat_template = false;
  1132. }
  1133. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  1134. add_opt(common_arg(
  1135. {"--no-warmup"},
  1136. "skip warming up the model with an empty run",
  1137. [](common_params & params) {
  1138. params.warmup = false;
  1139. }
  1140. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_EMBEDDING, LLAMA_EXAMPLE_RETRIEVAL, LLAMA_EXAMPLE_PERPLEXITY}));
  1141. add_opt(common_arg(
  1142. {"--spm-infill"},
  1143. string_format(
  1144. "use Suffix/Prefix/Middle pattern for infill (instead of Prefix/Suffix/Middle) as some models prefer this. (default: %s)",
  1145. params.spm_infill ? "enabled" : "disabled"
  1146. ),
  1147. [](common_params & params) {
  1148. params.spm_infill = true;
  1149. }
  1150. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  1151. add_opt(common_arg(
  1152. {"--samplers"}, "SAMPLERS",
  1153. string_format("samplers that will be used for generation in the order, separated by \';\'\n(default: %s)", sampler_type_names.c_str()),
  1154. [](common_params & params, const std::string & value) {
  1155. const auto sampler_names = string_split<std::string>(value, ';');
  1156. params.sampling.samplers = common_sampler_types_from_names(sampler_names, true);
  1157. }
  1158. ).set_sparam());
  1159. add_opt(common_arg(
  1160. {"-s", "--seed"}, "SEED",
  1161. string_format("RNG seed (default: %d, use random seed for %d)", params.sampling.seed, LLAMA_DEFAULT_SEED),
  1162. [](common_params & params, const std::string & value) {
  1163. params.sampling.seed = std::stoul(value);
  1164. }
  1165. ).set_sparam());
  1166. add_opt(common_arg(
  1167. {"--sampling-seq", "--sampler-seq"}, "SEQUENCE",
  1168. string_format("simplified sequence for samplers that will be used (default: %s)", sampler_type_chars.c_str()),
  1169. [](common_params & params, const std::string & value) {
  1170. params.sampling.samplers = common_sampler_types_from_chars(value);
  1171. }
  1172. ).set_sparam());
  1173. add_opt(common_arg(
  1174. {"--ignore-eos"},
  1175. "ignore end of stream token and continue generating (implies --logit-bias EOS-inf)",
  1176. [](common_params & params) {
  1177. params.sampling.ignore_eos = true;
  1178. }
  1179. ).set_sparam());
  1180. add_opt(common_arg(
  1181. {"--temp"}, "N",
  1182. string_format("temperature (default: %.1f)", (double)params.sampling.temp),
  1183. [](common_params & params, const std::string & value) {
  1184. params.sampling.temp = std::stof(value);
  1185. params.sampling.temp = std::max(params.sampling.temp, 0.0f);
  1186. }
  1187. ).set_sparam());
  1188. add_opt(common_arg(
  1189. {"--top-k"}, "N",
  1190. string_format("top-k sampling (default: %d, 0 = disabled)", params.sampling.top_k),
  1191. [](common_params & params, int value) {
  1192. params.sampling.top_k = value;
  1193. }
  1194. ).set_sparam());
  1195. add_opt(common_arg(
  1196. {"--top-p"}, "N",
  1197. string_format("top-p sampling (default: %.1f, 1.0 = disabled)", (double)params.sampling.top_p),
  1198. [](common_params & params, const std::string & value) {
  1199. params.sampling.top_p = std::stof(value);
  1200. }
  1201. ).set_sparam());
  1202. add_opt(common_arg(
  1203. {"--min-p"}, "N",
  1204. string_format("min-p sampling (default: %.1f, 0.0 = disabled)", (double)params.sampling.min_p),
  1205. [](common_params & params, const std::string & value) {
  1206. params.sampling.min_p = std::stof(value);
  1207. }
  1208. ).set_sparam());
  1209. add_opt(common_arg(
  1210. {"--top-nsigma"}, "N",
  1211. string_format("top-n-sigma sampling (default: %.1f, -1.0 = disabled)", params.sampling.top_n_sigma),
  1212. [](common_params & params, const std::string & value) {
  1213. params.sampling.top_n_sigma = std::stof(value);
  1214. }
  1215. ).set_sparam());
  1216. add_opt(common_arg(
  1217. {"--xtc-probability"}, "N",
  1218. string_format("xtc probability (default: %.1f, 0.0 = disabled)", (double)params.sampling.xtc_probability),
  1219. [](common_params & params, const std::string & value) {
  1220. params.sampling.xtc_probability = std::stof(value);
  1221. }
  1222. ).set_sparam());
  1223. add_opt(common_arg(
  1224. {"--xtc-threshold"}, "N",
  1225. string_format("xtc threshold (default: %.1f, 1.0 = disabled)", (double)params.sampling.xtc_threshold),
  1226. [](common_params & params, const std::string & value) {
  1227. params.sampling.xtc_threshold = std::stof(value);
  1228. }
  1229. ).set_sparam());
  1230. add_opt(common_arg(
  1231. {"--typical"}, "N",
  1232. string_format("locally typical sampling, parameter p (default: %.1f, 1.0 = disabled)", (double)params.sampling.typ_p),
  1233. [](common_params & params, const std::string & value) {
  1234. params.sampling.typ_p = std::stof(value);
  1235. }
  1236. ).set_sparam());
  1237. add_opt(common_arg(
  1238. {"--repeat-last-n"}, "N",
  1239. string_format("last n tokens to consider for penalize (default: %d, 0 = disabled, -1 = ctx_size)", params.sampling.penalty_last_n),
  1240. [](common_params & params, int value) {
  1241. if (value < -1) {
  1242. throw std::runtime_error(string_format("error: invalid repeat-last-n = %d\n", value));
  1243. }
  1244. params.sampling.penalty_last_n = value;
  1245. params.sampling.n_prev = std::max(params.sampling.n_prev, params.sampling.penalty_last_n);
  1246. }
  1247. ).set_sparam());
  1248. add_opt(common_arg(
  1249. {"--repeat-penalty"}, "N",
  1250. string_format("penalize repeat sequence of tokens (default: %.1f, 1.0 = disabled)", (double)params.sampling.penalty_repeat),
  1251. [](common_params & params, const std::string & value) {
  1252. params.sampling.penalty_repeat = std::stof(value);
  1253. }
  1254. ).set_sparam());
  1255. add_opt(common_arg(
  1256. {"--presence-penalty"}, "N",
  1257. string_format("repeat alpha presence penalty (default: %.1f, 0.0 = disabled)", (double)params.sampling.penalty_present),
  1258. [](common_params & params, const std::string & value) {
  1259. params.sampling.penalty_present = std::stof(value);
  1260. }
  1261. ).set_sparam());
  1262. add_opt(common_arg(
  1263. {"--frequency-penalty"}, "N",
  1264. string_format("repeat alpha frequency penalty (default: %.1f, 0.0 = disabled)", (double)params.sampling.penalty_freq),
  1265. [](common_params & params, const std::string & value) {
  1266. params.sampling.penalty_freq = std::stof(value);
  1267. }
  1268. ).set_sparam());
  1269. add_opt(common_arg(
  1270. {"--dry-multiplier"}, "N",
  1271. string_format("set DRY sampling multiplier (default: %.1f, 0.0 = disabled)", (double)params.sampling.dry_multiplier),
  1272. [](common_params & params, const std::string & value) {
  1273. params.sampling.dry_multiplier = std::stof(value);
  1274. }
  1275. ).set_sparam());
  1276. add_opt(common_arg(
  1277. {"--dry-base"}, "N",
  1278. string_format("set DRY sampling base value (default: %.2f)", (double)params.sampling.dry_base),
  1279. [](common_params & params, const std::string & value) {
  1280. float potential_base = std::stof(value);
  1281. if (potential_base >= 1.0f)
  1282. {
  1283. params.sampling.dry_base = potential_base;
  1284. }
  1285. }
  1286. ).set_sparam());
  1287. add_opt(common_arg(
  1288. {"--dry-allowed-length"}, "N",
  1289. string_format("set allowed length for DRY sampling (default: %d)", params.sampling.dry_allowed_length),
  1290. [](common_params & params, int value) {
  1291. params.sampling.dry_allowed_length = value;
  1292. }
  1293. ).set_sparam());
  1294. add_opt(common_arg(
  1295. {"--dry-penalty-last-n"}, "N",
  1296. string_format("set DRY penalty for the last n tokens (default: %d, 0 = disable, -1 = context size)", params.sampling.dry_penalty_last_n),
  1297. [](common_params & params, int value) {
  1298. if (value < -1) {
  1299. throw std::runtime_error(string_format("error: invalid dry-penalty-last-n = %d\n", value));
  1300. }
  1301. params.sampling.dry_penalty_last_n = value;
  1302. }
  1303. ).set_sparam());
  1304. add_opt(common_arg(
  1305. {"--dry-sequence-breaker"}, "STRING",
  1306. 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",
  1307. params.sampling.dry_sequence_breakers.empty() ? "none" :
  1308. std::accumulate(std::next(params.sampling.dry_sequence_breakers.begin()),
  1309. params.sampling.dry_sequence_breakers.end(),
  1310. std::string("'") + (params.sampling.dry_sequence_breakers[0] == "\n" ? "\\n" : params.sampling.dry_sequence_breakers[0]) + "'",
  1311. [](const std::string& a, const std::string& b) {
  1312. std::string formatted_b = (b == "\n") ? "\\n" : b;
  1313. return a + ", '" + formatted_b + "'";
  1314. }).c_str()),
  1315. [](common_params & params, const std::string & value) {
  1316. static bool defaults_cleared = false;
  1317. if (!defaults_cleared) {
  1318. params.sampling.dry_sequence_breakers.clear();
  1319. defaults_cleared = true;
  1320. }
  1321. if (value == "none") {
  1322. params.sampling.dry_sequence_breakers.clear();
  1323. } else {
  1324. params.sampling.dry_sequence_breakers.emplace_back(value);
  1325. }
  1326. }
  1327. ).set_sparam());
  1328. add_opt(common_arg(
  1329. {"--dynatemp-range"}, "N",
  1330. string_format("dynamic temperature range (default: %.1f, 0.0 = disabled)", (double)params.sampling.dynatemp_range),
  1331. [](common_params & params, const std::string & value) {
  1332. params.sampling.dynatemp_range = std::stof(value);
  1333. }
  1334. ).set_sparam());
  1335. add_opt(common_arg(
  1336. {"--dynatemp-exp"}, "N",
  1337. string_format("dynamic temperature exponent (default: %.1f)", (double)params.sampling.dynatemp_exponent),
  1338. [](common_params & params, const std::string & value) {
  1339. params.sampling.dynatemp_exponent = std::stof(value);
  1340. }
  1341. ).set_sparam());
  1342. add_opt(common_arg(
  1343. {"--mirostat"}, "N",
  1344. string_format("use Mirostat sampling.\nTop K, Nucleus and Locally Typical samplers are ignored if used.\n"
  1345. "(default: %d, 0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0)", params.sampling.mirostat),
  1346. [](common_params & params, int value) {
  1347. params.sampling.mirostat = value;
  1348. }
  1349. ).set_sparam());
  1350. add_opt(common_arg(
  1351. {"--mirostat-lr"}, "N",
  1352. string_format("Mirostat learning rate, parameter eta (default: %.1f)", (double)params.sampling.mirostat_eta),
  1353. [](common_params & params, const std::string & value) {
  1354. params.sampling.mirostat_eta = std::stof(value);
  1355. }
  1356. ).set_sparam());
  1357. add_opt(common_arg(
  1358. {"--mirostat-ent"}, "N",
  1359. string_format("Mirostat target entropy, parameter tau (default: %.1f)", (double)params.sampling.mirostat_tau),
  1360. [](common_params & params, const std::string & value) {
  1361. params.sampling.mirostat_tau = std::stof(value);
  1362. }
  1363. ).set_sparam());
  1364. add_opt(common_arg(
  1365. {"-l", "--logit-bias"}, "TOKEN_ID(+/-)BIAS",
  1366. "modifies the likelihood of token appearing in the completion,\n"
  1367. "i.e. `--logit-bias 15043+1` to increase likelihood of token ' Hello',\n"
  1368. "or `--logit-bias 15043-1` to decrease likelihood of token ' Hello'",
  1369. [](common_params & params, const std::string & value) {
  1370. std::stringstream ss(value);
  1371. llama_token key;
  1372. char sign;
  1373. std::string value_str;
  1374. try {
  1375. if (ss >> key && ss >> sign && std::getline(ss, value_str) && (sign == '+' || sign == '-')) {
  1376. const float bias = std::stof(value_str) * ((sign == '-') ? -1.0f : 1.0f);
  1377. params.sampling.logit_bias.push_back({key, bias});
  1378. } else {
  1379. throw std::invalid_argument("invalid input format");
  1380. }
  1381. } catch (const std::exception&) {
  1382. throw std::invalid_argument("invalid input format");
  1383. }
  1384. }
  1385. ).set_sparam());
  1386. add_opt(common_arg(
  1387. {"--grammar"}, "GRAMMAR",
  1388. string_format("BNF-like grammar to constrain generations (see samples in grammars/ dir) (default: '%s')", params.sampling.grammar.c_str()),
  1389. [](common_params & params, const std::string & value) {
  1390. params.sampling.grammar = value;
  1391. }
  1392. ).set_sparam());
  1393. add_opt(common_arg(
  1394. {"--grammar-file"}, "FNAME",
  1395. "file to read grammar from",
  1396. [](common_params & params, const std::string & value) {
  1397. params.sampling.grammar = read_file(value);
  1398. }
  1399. ).set_sparam());
  1400. add_opt(common_arg(
  1401. {"-j", "--json-schema"}, "SCHEMA",
  1402. "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",
  1403. [](common_params & params, const std::string & value) {
  1404. params.sampling.grammar = json_schema_to_grammar(json::parse(value));
  1405. }
  1406. ).set_sparam());
  1407. add_opt(common_arg(
  1408. {"-jf", "--json-schema-file"}, "FILE",
  1409. "File containing a 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",
  1410. [](common_params & params, const std::string & value) {
  1411. std::ifstream file(value);
  1412. if (!file) {
  1413. throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
  1414. }
  1415. std::string schema;
  1416. std::copy(
  1417. std::istreambuf_iterator<char>(file),
  1418. std::istreambuf_iterator<char>(),
  1419. std::back_inserter(schema)
  1420. );
  1421. params.sampling.grammar = json_schema_to_grammar(json::parse(schema));
  1422. }
  1423. ).set_sparam());
  1424. add_opt(common_arg(
  1425. {"--pooling"}, "{none,mean,cls,last,rank}",
  1426. "pooling type for embeddings, use model default if unspecified",
  1427. [](common_params & params, const std::string & value) {
  1428. /**/ if (value == "none") { params.pooling_type = LLAMA_POOLING_TYPE_NONE; }
  1429. else if (value == "mean") { params.pooling_type = LLAMA_POOLING_TYPE_MEAN; }
  1430. else if (value == "cls") { params.pooling_type = LLAMA_POOLING_TYPE_CLS; }
  1431. else if (value == "last") { params.pooling_type = LLAMA_POOLING_TYPE_LAST; }
  1432. else if (value == "rank") { params.pooling_type = LLAMA_POOLING_TYPE_RANK; }
  1433. else { throw std::invalid_argument("invalid value"); }
  1434. }
  1435. ).set_examples({LLAMA_EXAMPLE_EMBEDDING, LLAMA_EXAMPLE_RETRIEVAL, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_POOLING"));
  1436. add_opt(common_arg(
  1437. {"--attention"}, "{causal,non-causal}",
  1438. "attention type for embeddings, use model default if unspecified",
  1439. [](common_params & params, const std::string & value) {
  1440. /**/ if (value == "causal") { params.attention_type = LLAMA_ATTENTION_TYPE_CAUSAL; }
  1441. else if (value == "non-causal") { params.attention_type = LLAMA_ATTENTION_TYPE_NON_CAUSAL; }
  1442. else { throw std::invalid_argument("invalid value"); }
  1443. }
  1444. ).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
  1445. add_opt(common_arg(
  1446. {"--rope-scaling"}, "{none,linear,yarn}",
  1447. "RoPE frequency scaling method, defaults to linear unless specified by the model",
  1448. [](common_params & params, const std::string & value) {
  1449. /**/ if (value == "none") { params.rope_scaling_type = LLAMA_ROPE_SCALING_TYPE_NONE; }
  1450. else if (value == "linear") { params.rope_scaling_type = LLAMA_ROPE_SCALING_TYPE_LINEAR; }
  1451. else if (value == "yarn") { params.rope_scaling_type = LLAMA_ROPE_SCALING_TYPE_YARN; }
  1452. else { throw std::invalid_argument("invalid value"); }
  1453. }
  1454. ).set_env("LLAMA_ARG_ROPE_SCALING_TYPE"));
  1455. add_opt(common_arg(
  1456. {"--rope-scale"}, "N",
  1457. "RoPE context scaling factor, expands context by a factor of N",
  1458. [](common_params & params, const std::string & value) {
  1459. params.rope_freq_scale = 1.0f / std::stof(value);
  1460. }
  1461. ).set_env("LLAMA_ARG_ROPE_SCALE"));
  1462. add_opt(common_arg(
  1463. {"--rope-freq-base"}, "N",
  1464. "RoPE base frequency, used by NTK-aware scaling (default: loaded from model)",
  1465. [](common_params & params, const std::string & value) {
  1466. params.rope_freq_base = std::stof(value);
  1467. }
  1468. ).set_env("LLAMA_ARG_ROPE_FREQ_BASE"));
  1469. add_opt(common_arg(
  1470. {"--rope-freq-scale"}, "N",
  1471. "RoPE frequency scaling factor, expands context by a factor of 1/N",
  1472. [](common_params & params, const std::string & value) {
  1473. params.rope_freq_scale = std::stof(value);
  1474. }
  1475. ).set_env("LLAMA_ARG_ROPE_FREQ_SCALE"));
  1476. add_opt(common_arg(
  1477. {"--yarn-orig-ctx"}, "N",
  1478. string_format("YaRN: original context size of model (default: %d = model training context size)", params.yarn_orig_ctx),
  1479. [](common_params & params, int value) {
  1480. params.yarn_orig_ctx = value;
  1481. }
  1482. ).set_env("LLAMA_ARG_YARN_ORIG_CTX"));
  1483. add_opt(common_arg(
  1484. {"--yarn-ext-factor"}, "N",
  1485. string_format("YaRN: extrapolation mix factor (default: %.1f, 0.0 = full interpolation)", (double)params.yarn_ext_factor),
  1486. [](common_params & params, const std::string & value) {
  1487. params.yarn_ext_factor = std::stof(value);
  1488. }
  1489. ).set_env("LLAMA_ARG_YARN_EXT_FACTOR"));
  1490. add_opt(common_arg(
  1491. {"--yarn-attn-factor"}, "N",
  1492. string_format("YaRN: scale sqrt(t) or attention magnitude (default: %.1f)", (double)params.yarn_attn_factor),
  1493. [](common_params & params, const std::string & value) {
  1494. params.yarn_attn_factor = std::stof(value);
  1495. }
  1496. ).set_env("LLAMA_ARG_YARN_ATTN_FACTOR"));
  1497. add_opt(common_arg(
  1498. {"--yarn-beta-slow"}, "N",
  1499. string_format("YaRN: high correction dim or alpha (default: %.1f)", (double)params.yarn_beta_slow),
  1500. [](common_params & params, const std::string & value) {
  1501. params.yarn_beta_slow = std::stof(value);
  1502. }
  1503. ).set_env("LLAMA_ARG_YARN_BETA_SLOW"));
  1504. add_opt(common_arg(
  1505. {"--yarn-beta-fast"}, "N",
  1506. string_format("YaRN: low correction dim or beta (default: %.1f)", (double)params.yarn_beta_fast),
  1507. [](common_params & params, const std::string & value) {
  1508. params.yarn_beta_fast = std::stof(value);
  1509. }
  1510. ).set_env("LLAMA_ARG_YARN_BETA_FAST"));
  1511. add_opt(common_arg(
  1512. {"-gan", "--grp-attn-n"}, "N",
  1513. string_format("group-attention factor (default: %d)", params.grp_attn_n),
  1514. [](common_params & params, int value) {
  1515. params.grp_attn_n = value;
  1516. }
  1517. ).set_env("LLAMA_ARG_GRP_ATTN_N").set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_PASSKEY}));
  1518. add_opt(common_arg(
  1519. {"-gaw", "--grp-attn-w"}, "N",
  1520. string_format("group-attention width (default: %d)", params.grp_attn_w),
  1521. [](common_params & params, int value) {
  1522. params.grp_attn_w = value;
  1523. }
  1524. ).set_env("LLAMA_ARG_GRP_ATTN_W").set_examples({LLAMA_EXAMPLE_MAIN}));
  1525. add_opt(common_arg(
  1526. {"-nkvo", "--no-kv-offload"},
  1527. "disable KV offload",
  1528. [](common_params & params) {
  1529. params.no_kv_offload = true;
  1530. }
  1531. ).set_env("LLAMA_ARG_NO_KV_OFFLOAD"));
  1532. add_opt(common_arg(
  1533. {"-nr", "--no-repack"},
  1534. "disable weight repacking",
  1535. [](common_params & params) {
  1536. params.no_extra_bufts = true;
  1537. }
  1538. ).set_env("LLAMA_ARG_NO_REPACK"));
  1539. add_opt(common_arg(
  1540. {"--no-host"},
  1541. "bypass host buffer allowing extra buffers to be used",
  1542. [](common_params & params) {
  1543. params.no_host = true;
  1544. }
  1545. ).set_env("LLAMA_ARG_NO_HOST"));
  1546. add_opt(common_arg(
  1547. {"-ctk", "--cache-type-k"}, "TYPE",
  1548. string_format(
  1549. "KV cache data type for K\n"
  1550. "allowed values: %s\n"
  1551. "(default: %s)",
  1552. get_all_kv_cache_types().c_str(),
  1553. ggml_type_name(params.cache_type_k)
  1554. ),
  1555. [](common_params & params, const std::string & value) {
  1556. params.cache_type_k = kv_cache_type_from_str(value);
  1557. }
  1558. ).set_env("LLAMA_ARG_CACHE_TYPE_K"));
  1559. add_opt(common_arg(
  1560. {"-ctv", "--cache-type-v"}, "TYPE",
  1561. string_format(
  1562. "KV cache data type for V\n"
  1563. "allowed values: %s\n"
  1564. "(default: %s)",
  1565. get_all_kv_cache_types().c_str(),
  1566. ggml_type_name(params.cache_type_v)
  1567. ),
  1568. [](common_params & params, const std::string & value) {
  1569. params.cache_type_v = kv_cache_type_from_str(value);
  1570. }
  1571. ).set_env("LLAMA_ARG_CACHE_TYPE_V"));
  1572. add_opt(common_arg(
  1573. {"--hellaswag"},
  1574. "compute HellaSwag score over random tasks from datafile supplied with -f",
  1575. [](common_params & params) {
  1576. params.hellaswag = true;
  1577. }
  1578. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1579. add_opt(common_arg(
  1580. {"--hellaswag-tasks"}, "N",
  1581. string_format("number of tasks to use when computing the HellaSwag score (default: %zu)", params.hellaswag_tasks),
  1582. [](common_params & params, int value) {
  1583. params.hellaswag_tasks = value;
  1584. }
  1585. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1586. add_opt(common_arg(
  1587. {"--winogrande"},
  1588. "compute Winogrande score over random tasks from datafile supplied with -f",
  1589. [](common_params & params) {
  1590. params.winogrande = true;
  1591. }
  1592. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1593. add_opt(common_arg(
  1594. {"--winogrande-tasks"}, "N",
  1595. string_format("number of tasks to use when computing the Winogrande score (default: %zu)", params.winogrande_tasks),
  1596. [](common_params & params, int value) {
  1597. params.winogrande_tasks = value;
  1598. }
  1599. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1600. add_opt(common_arg(
  1601. {"--multiple-choice"},
  1602. "compute multiple choice score over random tasks from datafile supplied with -f",
  1603. [](common_params & params) {
  1604. params.multiple_choice = true;
  1605. }
  1606. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1607. add_opt(common_arg(
  1608. {"--multiple-choice-tasks"}, "N",
  1609. string_format("number of tasks to use when computing the multiple choice score (default: %zu)", params.multiple_choice_tasks),
  1610. [](common_params & params, int value) {
  1611. params.multiple_choice_tasks = value;
  1612. }
  1613. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1614. add_opt(common_arg(
  1615. {"--kl-divergence"},
  1616. "computes KL-divergence to logits provided via --kl-divergence-base",
  1617. [](common_params & params) {
  1618. params.kl_divergence = true;
  1619. }
  1620. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1621. add_opt(common_arg(
  1622. {"--save-all-logits", "--kl-divergence-base"}, "FNAME",
  1623. "set logits file",
  1624. [](common_params & params, const std::string & value) {
  1625. params.logits_file = value;
  1626. }
  1627. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1628. add_opt(common_arg(
  1629. {"--ppl-stride"}, "N",
  1630. string_format("stride for perplexity calculation (default: %d)", params.ppl_stride),
  1631. [](common_params & params, int value) {
  1632. params.ppl_stride = value;
  1633. }
  1634. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1635. add_opt(common_arg(
  1636. {"--ppl-output-type"}, "<0|1>",
  1637. string_format("output type for perplexity calculation (default: %d)", params.ppl_output_type),
  1638. [](common_params & params, int value) {
  1639. params.ppl_output_type = value;
  1640. }
  1641. ).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
  1642. add_opt(common_arg(
  1643. {"-dt", "--defrag-thold"}, "N",
  1644. string_format("KV cache defragmentation threshold (DEPRECATED)"),
  1645. [](common_params & params, const std::string & value) {
  1646. GGML_UNUSED(params);
  1647. GGML_UNUSED(value);
  1648. LOG_WRN("DEPRECATED: --defrag-thold is deprecated and no longer necessary to specify\n");
  1649. }
  1650. ).set_env("LLAMA_ARG_DEFRAG_THOLD"));
  1651. add_opt(common_arg(
  1652. {"-np", "--parallel"}, "N",
  1653. string_format("number of parallel sequences to decode (default: %d)", params.n_parallel),
  1654. [](common_params & params, int value) {
  1655. params.n_parallel = value;
  1656. }
  1657. ).set_env("LLAMA_ARG_N_PARALLEL"));
  1658. add_opt(common_arg(
  1659. {"-ns", "--sequences"}, "N",
  1660. string_format("number of sequences to decode (default: %d)", params.n_sequences),
  1661. [](common_params & params, int value) {
  1662. params.n_sequences = value;
  1663. }
  1664. ).set_examples({LLAMA_EXAMPLE_PARALLEL}));
  1665. add_opt(common_arg(
  1666. {"-cb", "--cont-batching"},
  1667. string_format("enable continuous batching (a.k.a dynamic batching) (default: %s)", params.cont_batching ? "enabled" : "disabled"),
  1668. [](common_params & params) {
  1669. params.cont_batching = true;
  1670. }
  1671. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CONT_BATCHING"));
  1672. add_opt(common_arg(
  1673. {"-nocb", "--no-cont-batching"},
  1674. "disable continuous batching",
  1675. [](common_params & params) {
  1676. params.cont_batching = false;
  1677. }
  1678. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_NO_CONT_BATCHING"));
  1679. add_opt(common_arg(
  1680. {"--mmproj"}, "FILE",
  1681. "path to a multimodal projector file. see tools/mtmd/README.md\n"
  1682. "note: if -hf is used, this argument can be omitted",
  1683. [](common_params & params, const std::string & value) {
  1684. params.mmproj.path = value;
  1685. }
  1686. ).set_examples(mmproj_examples).set_env("LLAMA_ARG_MMPROJ"));
  1687. add_opt(common_arg(
  1688. {"--mmproj-url"}, "URL",
  1689. "URL to a multimodal projector file. see tools/mtmd/README.md",
  1690. [](common_params & params, const std::string & value) {
  1691. params.mmproj.url = value;
  1692. }
  1693. ).set_examples(mmproj_examples).set_env("LLAMA_ARG_MMPROJ_URL"));
  1694. add_opt(common_arg(
  1695. {"--no-mmproj"},
  1696. "explicitly disable multimodal projector, useful when using -hf",
  1697. [](common_params & params) {
  1698. params.no_mmproj = true;
  1699. }
  1700. ).set_examples(mmproj_examples).set_env("LLAMA_ARG_NO_MMPROJ"));
  1701. add_opt(common_arg(
  1702. {"--no-mmproj-offload"},
  1703. "do not offload multimodal projector to GPU",
  1704. [](common_params & params) {
  1705. params.mmproj_use_gpu = false;
  1706. }
  1707. ).set_examples(mmproj_examples).set_env("LLAMA_ARG_NO_MMPROJ_OFFLOAD"));
  1708. add_opt(common_arg(
  1709. {"--image", "--audio"}, "FILE",
  1710. "path to an image or audio file. use with multimodal models, can be repeated if you have multiple files\n",
  1711. [](common_params & params, const std::string & value) {
  1712. params.image.emplace_back(value);
  1713. }
  1714. ).set_examples({LLAMA_EXAMPLE_MTMD}));
  1715. add_opt(common_arg(
  1716. {"--image-min-tokens"}, "N",
  1717. "minimum number of tokens each image can take, only used by vision models with dynamic resolution (default: read from model)",
  1718. [](common_params & params, int value) {
  1719. params.image_min_tokens = value;
  1720. }
  1721. ).set_examples(mmproj_examples).set_env("LLAMA_ARG_IMAGE_MIN_TOKENS"));
  1722. add_opt(common_arg(
  1723. {"--image-max-tokens"}, "N",
  1724. "maximum number of tokens each image can take, only used by vision models with dynamic resolution (default: read from model)",
  1725. [](common_params & params, int value) {
  1726. params.image_max_tokens = value;
  1727. }
  1728. ).set_examples(mmproj_examples).set_env("LLAMA_ARG_IMAGE_MAX_TOKENS"));
  1729. if (llama_supports_rpc()) {
  1730. add_opt(common_arg(
  1731. {"--rpc"}, "SERVERS",
  1732. "comma separated list of RPC servers",
  1733. [](common_params & params, const std::string & value) {
  1734. add_rpc_devices(value);
  1735. GGML_UNUSED(params);
  1736. }
  1737. ).set_env("LLAMA_ARG_RPC"));
  1738. }
  1739. add_opt(common_arg(
  1740. {"--mlock"},
  1741. "force system to keep model in RAM rather than swapping or compressing",
  1742. [](common_params & params) {
  1743. params.use_mlock = true;
  1744. }
  1745. ).set_env("LLAMA_ARG_MLOCK"));
  1746. add_opt(common_arg(
  1747. {"--no-mmap"},
  1748. "do not memory-map model (slower load but may reduce pageouts if not using mlock)",
  1749. [](common_params & params) {
  1750. params.use_mmap = false;
  1751. }
  1752. ).set_env("LLAMA_ARG_NO_MMAP"));
  1753. add_opt(common_arg(
  1754. {"--numa"}, "TYPE",
  1755. "attempt optimizations that help on some NUMA systems\n"
  1756. "- distribute: spread execution evenly over all nodes\n"
  1757. "- isolate: only spawn threads on CPUs on the node that execution started on\n"
  1758. "- numactl: use the CPU map provided by numactl\n"
  1759. "if run without this previously, it is recommended to drop the system page cache before using this\n"
  1760. "see https://github.com/ggml-org/llama.cpp/issues/1437",
  1761. [](common_params & params, const std::string & value) {
  1762. /**/ if (value == "distribute" || value == "") { params.numa = GGML_NUMA_STRATEGY_DISTRIBUTE; }
  1763. else if (value == "isolate") { params.numa = GGML_NUMA_STRATEGY_ISOLATE; }
  1764. else if (value == "numactl") { params.numa = GGML_NUMA_STRATEGY_NUMACTL; }
  1765. else { throw std::invalid_argument("invalid value"); }
  1766. }
  1767. ).set_env("LLAMA_ARG_NUMA"));
  1768. add_opt(common_arg(
  1769. {"-dev", "--device"}, "<dev1,dev2,..>",
  1770. "comma-separated list of devices to use for offloading (none = don't offload)\n"
  1771. "use --list-devices to see a list of available devices",
  1772. [](common_params & params, const std::string & value) {
  1773. params.devices = parse_device_list(value);
  1774. }
  1775. ).set_env("LLAMA_ARG_DEVICE"));
  1776. add_opt(common_arg(
  1777. {"--list-devices"},
  1778. "print list of available devices and exit",
  1779. [](common_params &) {
  1780. std::vector<ggml_backend_dev_t> devices;
  1781. for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
  1782. auto * dev = ggml_backend_dev_get(i);
  1783. if (ggml_backend_dev_type(dev) != GGML_BACKEND_DEVICE_TYPE_CPU) {
  1784. devices.push_back(dev);
  1785. }
  1786. }
  1787. printf("Available devices:\n");
  1788. for (auto * dev : devices) {
  1789. size_t free, total;
  1790. ggml_backend_dev_memory(dev, &free, &total);
  1791. 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);
  1792. }
  1793. exit(0);
  1794. }
  1795. ));
  1796. add_opt(common_arg(
  1797. {"--override-tensor", "-ot"}, "<tensor name pattern>=<buffer type>,...",
  1798. "override tensor buffer type", [](common_params & params, const std::string & value) {
  1799. parse_tensor_buffer_overrides(value, params.tensor_buft_overrides);
  1800. }
  1801. ));
  1802. add_opt(common_arg(
  1803. {"--override-tensor-draft", "-otd"}, "<tensor name pattern>=<buffer type>,...",
  1804. "override tensor buffer type for draft model", [](common_params & params, const std::string & value) {
  1805. parse_tensor_buffer_overrides(value, params.speculative.tensor_buft_overrides);
  1806. }
  1807. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}));
  1808. add_opt(common_arg(
  1809. {"--cpu-moe", "-cmoe"},
  1810. "keep all Mixture of Experts (MoE) weights in the CPU",
  1811. [](common_params & params) {
  1812. params.tensor_buft_overrides.push_back(llm_ffn_exps_cpu_override());
  1813. }
  1814. ).set_env("LLAMA_ARG_CPU_MOE"));
  1815. add_opt(common_arg(
  1816. {"--n-cpu-moe", "-ncmoe"}, "N",
  1817. "keep the Mixture of Experts (MoE) weights of the first N layers in the CPU",
  1818. [](common_params & params, int value) {
  1819. if (value < 0) {
  1820. throw std::invalid_argument("invalid value");
  1821. }
  1822. for (int i = 0; i < value; ++i) {
  1823. // keep strings alive and avoid leaking memory by storing them in a static vector
  1824. static std::list<std::string> buft_overrides;
  1825. buft_overrides.push_back(llm_ffn_exps_block_regex(i));
  1826. params.tensor_buft_overrides.push_back({buft_overrides.back().c_str(), ggml_backend_cpu_buffer_type()});
  1827. }
  1828. }
  1829. ).set_env("LLAMA_ARG_N_CPU_MOE"));
  1830. add_opt(common_arg(
  1831. {"--cpu-moe-draft", "-cmoed"},
  1832. "keep all Mixture of Experts (MoE) weights in the CPU for the draft model",
  1833. [](common_params & params) {
  1834. params.speculative.tensor_buft_overrides.push_back(llm_ffn_exps_cpu_override());
  1835. }
  1836. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CPU_MOE_DRAFT"));
  1837. add_opt(common_arg(
  1838. {"--n-cpu-moe-draft", "-ncmoed"}, "N",
  1839. "keep the Mixture of Experts (MoE) weights of the first N layers in the CPU for the draft model",
  1840. [](common_params & params, int value) {
  1841. if (value < 0) {
  1842. throw std::invalid_argument("invalid value");
  1843. }
  1844. for (int i = 0; i < value; ++i) {
  1845. static std::list<std::string> buft_overrides_draft;
  1846. buft_overrides_draft.push_back(llm_ffn_exps_block_regex(i));
  1847. params.speculative.tensor_buft_overrides.push_back({buft_overrides_draft.back().c_str(), ggml_backend_cpu_buffer_type()});
  1848. }
  1849. }
  1850. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_N_CPU_MOE_DRAFT"));
  1851. add_opt(common_arg(
  1852. {"-ngl", "--gpu-layers", "--n-gpu-layers"}, "N",
  1853. string_format("max. number of layers to store in VRAM (default: %d)", params.n_gpu_layers),
  1854. [](common_params & params, int value) {
  1855. params.n_gpu_layers = value;
  1856. if (!llama_supports_gpu_offload()) {
  1857. fprintf(stderr, "warning: no usable GPU found, --gpu-layers option will be ignored\n");
  1858. fprintf(stderr, "warning: one possible reason is that llama.cpp was compiled without GPU support\n");
  1859. fprintf(stderr, "warning: consult docs/build.md for compilation instructions\n");
  1860. }
  1861. }
  1862. ).set_env("LLAMA_ARG_N_GPU_LAYERS"));
  1863. add_opt(common_arg(
  1864. {"-sm", "--split-mode"}, "{none,layer,row}",
  1865. "how to split the model across multiple GPUs, one of:\n"
  1866. "- none: use one GPU only\n"
  1867. "- layer (default): split layers and KV across GPUs\n"
  1868. "- row: split rows across GPUs",
  1869. [](common_params & params, const std::string & value) {
  1870. std::string arg_next = value;
  1871. if (arg_next == "none") {
  1872. params.split_mode = LLAMA_SPLIT_MODE_NONE;
  1873. } else if (arg_next == "layer") {
  1874. params.split_mode = LLAMA_SPLIT_MODE_LAYER;
  1875. } else if (arg_next == "row") {
  1876. params.split_mode = LLAMA_SPLIT_MODE_ROW;
  1877. } else {
  1878. throw std::invalid_argument("invalid value");
  1879. }
  1880. if (!llama_supports_gpu_offload()) {
  1881. fprintf(stderr, "warning: llama.cpp was compiled without support for GPU offload. Setting the split mode has no effect.\n");
  1882. }
  1883. }
  1884. ).set_env("LLAMA_ARG_SPLIT_MODE"));
  1885. add_opt(common_arg(
  1886. {"-ts", "--tensor-split"}, "N0,N1,N2,...",
  1887. "fraction of the model to offload to each GPU, comma-separated list of proportions, e.g. 3,1",
  1888. [](common_params & params, const std::string & value) {
  1889. std::string arg_next = value;
  1890. // split string by , and /
  1891. const std::regex regex{ R"([,/]+)" };
  1892. std::sregex_token_iterator it{ arg_next.begin(), arg_next.end(), regex, -1 };
  1893. std::vector<std::string> split_arg{ it, {} };
  1894. if (split_arg.size() >= llama_max_devices()) {
  1895. throw std::invalid_argument(
  1896. string_format("got %d input configs, but system only has %d devices", (int)split_arg.size(), (int)llama_max_devices())
  1897. );
  1898. }
  1899. for (size_t i = 0; i < llama_max_devices(); ++i) {
  1900. if (i < split_arg.size()) {
  1901. params.tensor_split[i] = std::stof(split_arg[i]);
  1902. } else {
  1903. params.tensor_split[i] = 0.0f;
  1904. }
  1905. }
  1906. if (!llama_supports_gpu_offload()) {
  1907. fprintf(stderr, "warning: llama.cpp was compiled without support for GPU offload. Setting a tensor split has no effect.\n");
  1908. }
  1909. }
  1910. ).set_env("LLAMA_ARG_TENSOR_SPLIT"));
  1911. add_opt(common_arg(
  1912. {"-mg", "--main-gpu"}, "INDEX",
  1913. 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),
  1914. [](common_params & params, int value) {
  1915. params.main_gpu = value;
  1916. if (!llama_supports_gpu_offload()) {
  1917. fprintf(stderr, "warning: llama.cpp was compiled without support for GPU offload. Setting the main GPU has no effect.\n");
  1918. }
  1919. }
  1920. ).set_env("LLAMA_ARG_MAIN_GPU"));
  1921. add_opt(common_arg(
  1922. {"--check-tensors"},
  1923. string_format("check model tensor data for invalid values (default: %s)", params.check_tensors ? "true" : "false"),
  1924. [](common_params & params) {
  1925. params.check_tensors = true;
  1926. }
  1927. ));
  1928. add_opt(common_arg(
  1929. {"--override-kv"}, "KEY=TYPE:VALUE",
  1930. "advanced option to override model metadata by key. may be specified multiple times.\n"
  1931. "types: int, float, bool, str. example: --override-kv tokenizer.ggml.add_bos_token=bool:false",
  1932. [](common_params & params, const std::string & value) {
  1933. if (!string_parse_kv_override(value.c_str(), params.kv_overrides)) {
  1934. throw std::runtime_error(string_format("error: Invalid type for KV override: %s\n", value.c_str()));
  1935. }
  1936. }
  1937. ));
  1938. add_opt(common_arg(
  1939. {"--no-op-offload"},
  1940. string_format("disable offloading host tensor operations to device (default: %s)", params.no_op_offload ? "true" : "false"),
  1941. [](common_params & params) {
  1942. params.no_op_offload = true;
  1943. }
  1944. ));
  1945. add_opt(common_arg(
  1946. {"--lora"}, "FNAME",
  1947. "path to LoRA adapter (can be repeated to use multiple adapters)",
  1948. [](common_params & params, const std::string & value) {
  1949. params.lora_adapters.push_back({ std::string(value), 1.0, "", "", nullptr });
  1950. }
  1951. // 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
  1952. ).set_examples({LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_EXPORT_LORA}));
  1953. add_opt(common_arg(
  1954. {"--lora-scaled"}, "FNAME", "SCALE",
  1955. "path to LoRA adapter with user defined scaling (can be repeated to use multiple adapters)",
  1956. [](common_params & params, const std::string & fname, const std::string & scale) {
  1957. params.lora_adapters.push_back({ fname, std::stof(scale), "", "", nullptr });
  1958. }
  1959. // 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
  1960. ).set_examples({LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_EXPORT_LORA}));
  1961. add_opt(common_arg(
  1962. {"--control-vector"}, "FNAME",
  1963. "add a control vector\nnote: this argument can be repeated to add multiple control vectors",
  1964. [](common_params & params, const std::string & value) {
  1965. params.control_vectors.push_back({ 1.0f, value, });
  1966. }
  1967. ));
  1968. add_opt(common_arg(
  1969. {"--control-vector-scaled"}, "FNAME", "SCALE",
  1970. "add a control vector with user defined scaling SCALE\n"
  1971. "note: this argument can be repeated to add multiple scaled control vectors",
  1972. [](common_params & params, const std::string & fname, const std::string & scale) {
  1973. params.control_vectors.push_back({ std::stof(scale), fname });
  1974. }
  1975. ));
  1976. add_opt(common_arg(
  1977. {"--control-vector-layer-range"}, "START", "END",
  1978. "layer range to apply the control vector(s) to, start and end inclusive",
  1979. [](common_params & params, const std::string & start, const std::string & end) {
  1980. params.control_vector_layer_start = std::stoi(start);
  1981. params.control_vector_layer_end = std::stoi(end);
  1982. }
  1983. ));
  1984. add_opt(common_arg(
  1985. {"-a", "--alias"}, "STRING",
  1986. "set alias for model name (to be used by REST API)",
  1987. [](common_params & params, const std::string & value) {
  1988. params.model_alias = value;
  1989. }
  1990. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ALIAS"));
  1991. add_opt(common_arg(
  1992. {"-m", "--model"}, "FNAME",
  1993. ex == LLAMA_EXAMPLE_EXPORT_LORA
  1994. ? std::string("model path from which to load base model")
  1995. : string_format(
  1996. "model path (default: `models/$filename` with filename from `--hf-file` "
  1997. "or `--model-url` if set, otherwise %s)", DEFAULT_MODEL_PATH
  1998. ),
  1999. [](common_params & params, const std::string & value) {
  2000. params.model.path = value;
  2001. }
  2002. ).set_examples({LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_EXPORT_LORA}).set_env("LLAMA_ARG_MODEL"));
  2003. add_opt(common_arg(
  2004. {"-mu", "--model-url"}, "MODEL_URL",
  2005. "model download url (default: unused)",
  2006. [](common_params & params, const std::string & value) {
  2007. params.model.url = value;
  2008. }
  2009. ).set_env("LLAMA_ARG_MODEL_URL"));
  2010. add_opt(common_arg(
  2011. { "-dr", "--docker-repo" }, "[<repo>/]<model>[:quant]",
  2012. "Docker Hub model repository. repo is optional, default to ai/. quant is optional, default to :latest.\n"
  2013. "example: gemma3\n"
  2014. "(default: unused)",
  2015. [](common_params & params, const std::string & value) {
  2016. params.model.docker_repo = value;
  2017. }
  2018. ).set_env("LLAMA_ARG_DOCKER_REPO"));
  2019. add_opt(common_arg(
  2020. {"-hf", "-hfr", "--hf-repo"}, "<user>/<model>[:quant]",
  2021. "Hugging Face model repository; quant is optional, case-insensitive, default to Q4_K_M, or falls back to the first file in the repo if Q4_K_M doesn't exist.\n"
  2022. "mmproj is also downloaded automatically if available. to disable, add --no-mmproj\n"
  2023. "example: unsloth/phi-4-GGUF:q4_k_m\n"
  2024. "(default: unused)",
  2025. [](common_params & params, const std::string & value) {
  2026. params.model.hf_repo = value;
  2027. }
  2028. ).set_env("LLAMA_ARG_HF_REPO"));
  2029. add_opt(common_arg(
  2030. {"-hfd", "-hfrd", "--hf-repo-draft"}, "<user>/<model>[:quant]",
  2031. "Same as --hf-repo, but for the draft model (default: unused)",
  2032. [](common_params & params, const std::string & value) {
  2033. params.speculative.model.hf_repo = value;
  2034. }
  2035. ).set_env("LLAMA_ARG_HFD_REPO"));
  2036. add_opt(common_arg(
  2037. {"-hff", "--hf-file"}, "FILE",
  2038. "Hugging Face model file. If specified, it will override the quant in --hf-repo (default: unused)",
  2039. [](common_params & params, const std::string & value) {
  2040. params.model.hf_file = value;
  2041. }
  2042. ).set_env("LLAMA_ARG_HF_FILE"));
  2043. add_opt(common_arg(
  2044. {"-hfv", "-hfrv", "--hf-repo-v"}, "<user>/<model>[:quant]",
  2045. "Hugging Face model repository for the vocoder model (default: unused)",
  2046. [](common_params & params, const std::string & value) {
  2047. params.vocoder.model.hf_repo = value;
  2048. }
  2049. ).set_env("LLAMA_ARG_HF_REPO_V"));
  2050. add_opt(common_arg(
  2051. {"-hffv", "--hf-file-v"}, "FILE",
  2052. "Hugging Face model file for the vocoder model (default: unused)",
  2053. [](common_params & params, const std::string & value) {
  2054. params.vocoder.model.hf_file = value;
  2055. }
  2056. ).set_env("LLAMA_ARG_HF_FILE_V"));
  2057. add_opt(common_arg(
  2058. {"-hft", "--hf-token"}, "TOKEN",
  2059. "Hugging Face access token (default: value from HF_TOKEN environment variable)",
  2060. [](common_params & params, const std::string & value) {
  2061. params.hf_token = value;
  2062. }
  2063. ).set_env("HF_TOKEN"));
  2064. add_opt(common_arg(
  2065. {"--context-file"}, "FNAME",
  2066. "file to load context from (repeat to specify multiple files)",
  2067. [](common_params & params, const std::string & value) {
  2068. std::ifstream file(value, std::ios::binary);
  2069. if (!file) {
  2070. throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
  2071. }
  2072. params.context_files.push_back(value);
  2073. }
  2074. ).set_examples({LLAMA_EXAMPLE_RETRIEVAL}));
  2075. add_opt(common_arg(
  2076. {"--chunk-size"}, "N",
  2077. string_format("minimum length of embedded text chunks (default: %d)", params.chunk_size),
  2078. [](common_params & params, int value) {
  2079. params.chunk_size = value;
  2080. }
  2081. ).set_examples({LLAMA_EXAMPLE_RETRIEVAL}));
  2082. add_opt(common_arg(
  2083. {"--chunk-separator"}, "STRING",
  2084. string_format("separator between chunks (default: '%s')", params.chunk_separator.c_str()),
  2085. [](common_params & params, const std::string & value) {
  2086. params.chunk_separator = value;
  2087. }
  2088. ).set_examples({LLAMA_EXAMPLE_RETRIEVAL}));
  2089. add_opt(common_arg(
  2090. {"--junk"}, "N",
  2091. string_format("number of times to repeat the junk text (default: %d)", params.n_junk),
  2092. [](common_params & params, int value) {
  2093. params.n_junk = value;
  2094. }
  2095. ).set_examples({LLAMA_EXAMPLE_PASSKEY, LLAMA_EXAMPLE_PARALLEL}));
  2096. add_opt(common_arg(
  2097. {"--pos"}, "N",
  2098. string_format("position of the passkey in the junk text (default: %d)", params.i_pos),
  2099. [](common_params & params, int value) {
  2100. params.i_pos = value;
  2101. }
  2102. ).set_examples({LLAMA_EXAMPLE_PASSKEY}));
  2103. add_opt(common_arg(
  2104. {"-o", "--output", "--output-file"}, "FNAME",
  2105. string_format("output file (default: '%s')", params.out_file.c_str()),
  2106. [](common_params & params, const std::string & value) {
  2107. params.out_file = value;
  2108. }
  2109. ).set_examples({LLAMA_EXAMPLE_IMATRIX, LLAMA_EXAMPLE_CVECTOR_GENERATOR, LLAMA_EXAMPLE_EXPORT_LORA, LLAMA_EXAMPLE_TTS, LLAMA_EXAMPLE_FINETUNE}));
  2110. add_opt(common_arg(
  2111. {"-ofreq", "--output-frequency"}, "N",
  2112. string_format("output the imatrix every N iterations (default: %d)", params.n_out_freq),
  2113. [](common_params & params, int value) {
  2114. params.n_out_freq = value;
  2115. }
  2116. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  2117. add_opt(common_arg(
  2118. {"--output-format"}, "{gguf,dat}",
  2119. string_format("output format for imatrix file (default: %s)", params.imat_dat > 0 ? "dat" : "gguf"),
  2120. [](common_params & params, const std::string & value) {
  2121. /**/ if (value == "gguf") { params.imat_dat = -1; }
  2122. else if (value == "dat") { params.imat_dat = 1; }
  2123. else { throw std::invalid_argument("invalid output format"); }
  2124. }
  2125. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  2126. add_opt(common_arg(
  2127. {"--save-frequency"}, "N",
  2128. string_format("save an imatrix copy every N iterations (default: %d)", params.n_save_freq),
  2129. [](common_params & params, int value) {
  2130. params.n_save_freq = value;
  2131. }
  2132. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  2133. add_opt(common_arg(
  2134. {"--process-output"},
  2135. string_format("collect data for the output tensor (default: %s)", params.process_output ? "true" : "false"),
  2136. [](common_params & params) {
  2137. params.process_output = true;
  2138. }
  2139. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  2140. add_opt(common_arg(
  2141. {"--no-ppl"},
  2142. string_format("do not compute perplexity (default: %s)", params.compute_ppl ? "true" : "false"),
  2143. [](common_params & params) {
  2144. params.compute_ppl = false;
  2145. }
  2146. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  2147. add_opt(common_arg(
  2148. {"--chunk", "--from-chunk"}, "N",
  2149. string_format("start processing the input from chunk N (default: %d)", params.i_chunk),
  2150. [](common_params & params, int value) {
  2151. params.i_chunk = value;
  2152. }
  2153. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  2154. add_opt(common_arg(
  2155. {"--show-statistics"},
  2156. string_format("show imatrix statistics and then exit (default: %s)", params.show_statistics ? "true" : "false"),
  2157. [](common_params & params) {
  2158. params.show_statistics = true;
  2159. }
  2160. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  2161. add_opt(common_arg(
  2162. {"--parse-special"},
  2163. string_format("parse special tokens (chat, tool, etc) (default: %s)", params.parse_special ? "true" : "false"),
  2164. [](common_params & params) {
  2165. params.parse_special = true;
  2166. }
  2167. ).set_examples({LLAMA_EXAMPLE_IMATRIX}));
  2168. add_opt(common_arg(
  2169. {"-pps"},
  2170. string_format("is the prompt shared across parallel sequences (default: %s)", params.is_pp_shared ? "true" : "false"),
  2171. [](common_params & params) {
  2172. params.is_pp_shared = true;
  2173. }
  2174. ).set_examples({LLAMA_EXAMPLE_BENCH, LLAMA_EXAMPLE_PARALLEL}));
  2175. add_opt(common_arg(
  2176. {"-npp"}, "n0,n1,...",
  2177. "number of prompt tokens",
  2178. [](common_params & params, const std::string & value) {
  2179. auto p = string_split<int>(value, ',');
  2180. params.n_pp.insert(params.n_pp.end(), p.begin(), p.end());
  2181. }
  2182. ).set_examples({LLAMA_EXAMPLE_BENCH}));
  2183. add_opt(common_arg(
  2184. {"-ntg"}, "n0,n1,...",
  2185. "number of text generation tokens",
  2186. [](common_params & params, const std::string & value) {
  2187. auto p = string_split<int>(value, ',');
  2188. params.n_tg.insert(params.n_tg.end(), p.begin(), p.end());
  2189. }
  2190. ).set_examples({LLAMA_EXAMPLE_BENCH}));
  2191. add_opt(common_arg(
  2192. {"-npl"}, "n0,n1,...",
  2193. "number of parallel prompts",
  2194. [](common_params & params, const std::string & value) {
  2195. auto p = string_split<int>(value, ',');
  2196. params.n_pl.insert(params.n_pl.end(), p.begin(), p.end());
  2197. }
  2198. ).set_examples({LLAMA_EXAMPLE_BENCH}));
  2199. add_opt(common_arg(
  2200. {"--embd-normalize"}, "N",
  2201. string_format("normalisation for embeddings (default: %d) (-1=none, 0=max absolute int16, 1=taxicab, 2=euclidean, >2=p-norm)", params.embd_normalize),
  2202. [](common_params & params, int value) {
  2203. params.embd_normalize = value;
  2204. }
  2205. ).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
  2206. add_opt(common_arg(
  2207. {"--embd-output-format"}, "FORMAT",
  2208. "empty = default, \"array\" = [[],[]...], \"json\" = openai style, \"json+\" = same \"json\" + cosine similarity matrix, \"raw\" = plain whitespace-delimited output (one embedding per line)",
  2209. [](common_params & params, const std::string & value) {
  2210. params.embd_out = value;
  2211. }
  2212. ).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
  2213. add_opt(common_arg(
  2214. {"--embd-separator"}, "STRING",
  2215. "separator of embeddings (default \\n) for example \"<#sep#>\"",
  2216. [](common_params & params, const std::string & value) {
  2217. params.embd_sep = value;
  2218. }
  2219. ).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
  2220. add_opt(common_arg(
  2221. {"--cls-separator"}, "STRING",
  2222. "separator of classification sequences (default \\t) for example \"<#seq#>\"",
  2223. [](common_params & params, const std::string & value) {
  2224. params.cls_sep = value;
  2225. }
  2226. ).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
  2227. add_opt(common_arg(
  2228. {"--host"}, "HOST",
  2229. string_format("ip address to listen, or bind to an UNIX socket if the address ends with .sock (default: %s)", params.hostname.c_str()),
  2230. [](common_params & params, const std::string & value) {
  2231. params.hostname = value;
  2232. }
  2233. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_HOST"));
  2234. add_opt(common_arg(
  2235. {"--port"}, "PORT",
  2236. string_format("port to listen (default: %d)", params.port),
  2237. [](common_params & params, int value) {
  2238. params.port = value;
  2239. }
  2240. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_PORT"));
  2241. add_opt(common_arg(
  2242. {"--path"}, "PATH",
  2243. string_format("path to serve static files from (default: %s)", params.public_path.c_str()),
  2244. [](common_params & params, const std::string & value) {
  2245. params.public_path = value;
  2246. }
  2247. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_STATIC_PATH"));
  2248. add_opt(common_arg(
  2249. {"--api-prefix"}, "PREFIX",
  2250. string_format("prefix path the server serves from, without the trailing slash (default: %s)", params.api_prefix.c_str()),
  2251. [](common_params & params, const std::string & value) {
  2252. params.api_prefix = value;
  2253. }
  2254. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_API_PREFIX"));
  2255. add_opt(common_arg(
  2256. {"--no-webui"},
  2257. string_format("Disable the Web UI (default: %s)", params.webui ? "enabled" : "disabled"),
  2258. [](common_params & params) {
  2259. params.webui = false;
  2260. }
  2261. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_NO_WEBUI"));
  2262. add_opt(common_arg(
  2263. {"--embedding", "--embeddings"},
  2264. string_format("restrict to only support embedding use case; use only with dedicated embedding models (default: %s)", params.embedding ? "enabled" : "disabled"),
  2265. [](common_params & params) {
  2266. params.embedding = true;
  2267. }
  2268. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_EMBEDDINGS"));
  2269. add_opt(common_arg(
  2270. {"--reranking", "--rerank"},
  2271. string_format("enable reranking endpoint on server (default: %s)", "disabled"),
  2272. [](common_params & params) {
  2273. params.embedding = true;
  2274. params.pooling_type = LLAMA_POOLING_TYPE_RANK;
  2275. }
  2276. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_RERANKING"));
  2277. add_opt(common_arg(
  2278. {"--api-key"}, "KEY",
  2279. "API key to use for authentication (default: none)",
  2280. [](common_params & params, const std::string & value) {
  2281. params.api_keys.push_back(value);
  2282. }
  2283. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_API_KEY"));
  2284. add_opt(common_arg(
  2285. {"--api-key-file"}, "FNAME",
  2286. "path to file containing API keys (default: none)",
  2287. [](common_params & params, const std::string & value) {
  2288. std::ifstream key_file(value);
  2289. if (!key_file) {
  2290. throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
  2291. }
  2292. std::string key;
  2293. while (std::getline(key_file, key)) {
  2294. if (!key.empty()) {
  2295. params.api_keys.push_back(key);
  2296. }
  2297. }
  2298. key_file.close();
  2299. }
  2300. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2301. add_opt(common_arg(
  2302. {"--ssl-key-file"}, "FNAME",
  2303. "path to file a PEM-encoded SSL private key",
  2304. [](common_params & params, const std::string & value) {
  2305. params.ssl_file_key = value;
  2306. }
  2307. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_SSL_KEY_FILE"));
  2308. add_opt(common_arg(
  2309. {"--ssl-cert-file"}, "FNAME",
  2310. "path to file a PEM-encoded SSL certificate",
  2311. [](common_params & params, const std::string & value) {
  2312. params.ssl_file_cert = value;
  2313. }
  2314. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_SSL_CERT_FILE"));
  2315. add_opt(common_arg(
  2316. {"--chat-template-kwargs"}, "STRING",
  2317. string_format("sets additional params for the json template parser"),
  2318. [](common_params & params, const std::string & value) {
  2319. auto parsed = json::parse(value);
  2320. for (const auto & item : parsed.items()) {
  2321. params.default_template_kwargs[item.key()] = item.value().dump();
  2322. }
  2323. }
  2324. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_CHAT_TEMPLATE_KWARGS"));
  2325. add_opt(common_arg(
  2326. {"-to", "--timeout"}, "N",
  2327. string_format("server read/write timeout in seconds (default: %d)", params.timeout_read),
  2328. [](common_params & params, int value) {
  2329. params.timeout_read = value;
  2330. params.timeout_write = value;
  2331. }
  2332. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_TIMEOUT"));
  2333. add_opt(common_arg(
  2334. {"--threads-http"}, "N",
  2335. string_format("number of threads used to process HTTP requests (default: %d)", params.n_threads_http),
  2336. [](common_params & params, int value) {
  2337. params.n_threads_http = value;
  2338. }
  2339. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_THREADS_HTTP"));
  2340. add_opt(common_arg(
  2341. {"--cache-reuse"}, "N",
  2342. string_format(
  2343. "min chunk size to attempt reusing from the cache via KV shifting (default: %d)\n"
  2344. "[(card)](https://ggml.ai/f0.png)", params.n_cache_reuse
  2345. ),
  2346. [](common_params & params, int value) {
  2347. params.n_cache_reuse = value;
  2348. }
  2349. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CACHE_REUSE"));
  2350. add_opt(common_arg(
  2351. {"--metrics"},
  2352. string_format("enable prometheus compatible metrics endpoint (default: %s)", params.endpoint_metrics ? "enabled" : "disabled"),
  2353. [](common_params & params) {
  2354. params.endpoint_metrics = true;
  2355. }
  2356. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ENDPOINT_METRICS"));
  2357. add_opt(common_arg(
  2358. {"--props"},
  2359. string_format("enable changing global properties via POST /props (default: %s)", params.endpoint_props ? "enabled" : "disabled"),
  2360. [](common_params & params) {
  2361. params.endpoint_props = true;
  2362. }
  2363. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ENDPOINT_PROPS"));
  2364. add_opt(common_arg(
  2365. {"--slots"},
  2366. string_format("enable slots monitoring endpoint (default: %s)", params.endpoint_slots ? "enabled" : "disabled"),
  2367. [](common_params & params) {
  2368. params.endpoint_slots = true;
  2369. }
  2370. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ENDPOINT_SLOTS"));
  2371. add_opt(common_arg(
  2372. {"--no-slots"},
  2373. "disables slots monitoring endpoint",
  2374. [](common_params & params) {
  2375. params.endpoint_slots = false;
  2376. }
  2377. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_NO_ENDPOINT_SLOTS"));
  2378. add_opt(common_arg(
  2379. {"--slot-save-path"}, "PATH",
  2380. "path to save slot kv cache (default: disabled)",
  2381. [](common_params & params, const std::string & value) {
  2382. params.slot_save_path = value;
  2383. // if doesn't end with DIRECTORY_SEPARATOR, add it
  2384. if (!params.slot_save_path.empty() && params.slot_save_path[params.slot_save_path.size() - 1] != DIRECTORY_SEPARATOR) {
  2385. params.slot_save_path += DIRECTORY_SEPARATOR;
  2386. }
  2387. }
  2388. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2389. add_opt(common_arg(
  2390. {"--jinja"},
  2391. "use jinja template for chat (default: disabled)",
  2392. [](common_params & params) {
  2393. params.use_jinja = true;
  2394. }
  2395. ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_MTMD}).set_env("LLAMA_ARG_JINJA"));
  2396. add_opt(common_arg(
  2397. {"--reasoning-format"}, "FORMAT",
  2398. "controls whether thought tags are allowed and/or extracted from the response, and in which format they're returned; one of:\n"
  2399. "- none: leaves thoughts unparsed in `message.content`\n"
  2400. "- deepseek: puts thoughts in `message.reasoning_content`\n"
  2401. "- deepseek-legacy: keeps `<think>` tags in `message.content` while also populating `message.reasoning_content`\n"
  2402. "(default: auto)",
  2403. [](common_params & params, const std::string & value) {
  2404. params.reasoning_format = common_reasoning_format_from_name(value);
  2405. }
  2406. ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_MAIN}).set_env("LLAMA_ARG_THINK"));
  2407. add_opt(common_arg(
  2408. {"--reasoning-budget"}, "N",
  2409. "controls the amount of thinking allowed; currently only one of: -1 for unrestricted thinking budget, or 0 to disable thinking (default: -1)",
  2410. [](common_params & params, int value) {
  2411. if (value != 0 && value != -1) { throw std::invalid_argument("invalid value"); }
  2412. params.reasoning_budget = value;
  2413. }
  2414. ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_MAIN}).set_env("LLAMA_ARG_THINK_BUDGET"));
  2415. add_opt(common_arg(
  2416. {"--chat-template"}, "JINJA_TEMPLATE",
  2417. string_format(
  2418. "set custom jinja chat template (default: template taken from model's metadata)\n"
  2419. "if suffix/prefix are specified, template will be disabled\n"
  2420. "only commonly used templates are accepted (unless --jinja is set before this flag):\n"
  2421. "list of built-in templates:\n%s", list_builtin_chat_templates().c_str()
  2422. ),
  2423. [](common_params & params, const std::string & value) {
  2424. params.chat_template = value;
  2425. }
  2426. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_MTMD}).set_env("LLAMA_ARG_CHAT_TEMPLATE"));
  2427. add_opt(common_arg(
  2428. {"--chat-template-file"}, "JINJA_TEMPLATE_FILE",
  2429. string_format(
  2430. "set custom jinja chat template file (default: template taken from model's metadata)\n"
  2431. "if suffix/prefix are specified, template will be disabled\n"
  2432. "only commonly used templates are accepted (unless --jinja is set before this flag):\n"
  2433. "list of built-in templates:\n%s", list_builtin_chat_templates().c_str()
  2434. ),
  2435. [](common_params & params, const std::string & value) {
  2436. params.chat_template = read_file(value);
  2437. }
  2438. ).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CHAT_TEMPLATE_FILE"));
  2439. add_opt(common_arg(
  2440. {"--no-prefill-assistant"},
  2441. string_format(
  2442. "whether to prefill the assistant's response if the last message is an assistant message (default: prefill enabled)\n"
  2443. "when this flag is set, if the last message is an assistant message then it will be treated as a full message and not prefilled\n"
  2444. ),
  2445. [](common_params & params) {
  2446. params.prefill_assistant = false;
  2447. }
  2448. ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_NO_PREFILL_ASSISTANT"));
  2449. add_opt(common_arg(
  2450. {"-sps", "--slot-prompt-similarity"}, "SIMILARITY",
  2451. 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),
  2452. [](common_params & params, const std::string & value) {
  2453. params.slot_prompt_similarity = std::stof(value);
  2454. }
  2455. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2456. add_opt(common_arg(
  2457. {"--lora-init-without-apply"},
  2458. string_format("load LoRA adapters without applying them (apply later via POST /lora-adapters) (default: %s)", params.lora_init_without_apply ? "enabled" : "disabled"),
  2459. [](common_params & params) {
  2460. params.lora_init_without_apply = true;
  2461. }
  2462. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2463. add_opt(common_arg(
  2464. {"--simple-io"},
  2465. "use basic IO for better compatibility in subprocesses and limited consoles",
  2466. [](common_params & params) {
  2467. params.simple_io = true;
  2468. }
  2469. ).set_examples({LLAMA_EXAMPLE_MAIN}));
  2470. add_opt(common_arg(
  2471. {"--positive-file"}, "FNAME",
  2472. string_format("positive prompts file, one prompt per line (default: '%s')", params.cvector_positive_file.c_str()),
  2473. [](common_params & params, const std::string & value) {
  2474. params.cvector_positive_file = value;
  2475. }
  2476. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  2477. add_opt(common_arg(
  2478. {"--negative-file"}, "FNAME",
  2479. string_format("negative prompts file, one prompt per line (default: '%s')", params.cvector_negative_file.c_str()),
  2480. [](common_params & params, const std::string & value) {
  2481. params.cvector_negative_file = value;
  2482. }
  2483. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  2484. add_opt(common_arg(
  2485. {"--pca-batch"}, "N",
  2486. string_format("batch size used for PCA. Larger batch runs faster, but uses more memory (default: %d)", params.n_pca_batch),
  2487. [](common_params & params, int value) {
  2488. params.n_pca_batch = value;
  2489. }
  2490. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  2491. add_opt(common_arg(
  2492. {"--pca-iter"}, "N",
  2493. string_format("number of iterations used for PCA (default: %d)", params.n_pca_iterations),
  2494. [](common_params & params, int value) {
  2495. params.n_pca_iterations = value;
  2496. }
  2497. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  2498. add_opt(common_arg(
  2499. {"--method"}, "{pca, mean}",
  2500. "dimensionality reduction method to be used (default: pca)",
  2501. [](common_params & params, const std::string & value) {
  2502. /**/ if (value == "pca") { params.cvector_dimre_method = DIMRE_METHOD_PCA; }
  2503. else if (value == "mean") { params.cvector_dimre_method = DIMRE_METHOD_MEAN; }
  2504. else { throw std::invalid_argument("invalid value"); }
  2505. }
  2506. ).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
  2507. add_opt(common_arg(
  2508. {"--output-format"}, "{md,jsonl}",
  2509. "output format for batched-bench results (default: md)",
  2510. [](common_params & params, const std::string & value) {
  2511. /**/ if (value == "jsonl") { params.batched_bench_output_jsonl = true; }
  2512. else if (value == "md") { params.batched_bench_output_jsonl = false; }
  2513. else { throw std::invalid_argument("invalid value"); }
  2514. }
  2515. ).set_examples({LLAMA_EXAMPLE_BENCH}));
  2516. add_opt(common_arg(
  2517. {"--log-disable"},
  2518. "Log disable",
  2519. [](common_params &) {
  2520. common_log_pause(common_log_main());
  2521. }
  2522. ));
  2523. add_opt(common_arg(
  2524. {"--log-file"}, "FNAME",
  2525. "Log to file",
  2526. [](common_params &, const std::string & value) {
  2527. common_log_set_file(common_log_main(), value.c_str());
  2528. }
  2529. ));
  2530. add_opt(common_arg(
  2531. {"--log-colors"}, "[on|off|auto]",
  2532. "Set colored logging ('on', 'off', or 'auto', default: 'auto')\n"
  2533. "'auto' enables colors when output is to a terminal",
  2534. [](common_params &, const std::string & value) {
  2535. if (is_truthy(value)) {
  2536. common_log_set_colors(common_log_main(), LOG_COLORS_ENABLED);
  2537. } else if (is_falsey(value)) {
  2538. common_log_set_colors(common_log_main(), LOG_COLORS_DISABLED);
  2539. } else if (is_autoy(value)) {
  2540. common_log_set_colors(common_log_main(), LOG_COLORS_AUTO);
  2541. } else {
  2542. throw std::invalid_argument(
  2543. string_format("error: unkown value for --log-colors: '%s'\n", value.c_str()));
  2544. }
  2545. }
  2546. ).set_env("LLAMA_LOG_COLORS"));
  2547. add_opt(common_arg(
  2548. {"-v", "--verbose", "--log-verbose"},
  2549. "Set verbosity level to infinity (i.e. log all messages, useful for debugging)",
  2550. [](common_params & params) {
  2551. params.verbosity = INT_MAX;
  2552. common_log_set_verbosity_thold(INT_MAX);
  2553. }
  2554. ));
  2555. add_opt(common_arg(
  2556. {"--offline"},
  2557. "Offline mode: forces use of cache, prevents network access",
  2558. [](common_params & params) {
  2559. params.offline = true;
  2560. }
  2561. ).set_env("LLAMA_OFFLINE"));
  2562. add_opt(common_arg(
  2563. {"-lv", "--verbosity", "--log-verbosity"}, "N",
  2564. "Set the verbosity threshold. Messages with a higher verbosity will be ignored.",
  2565. [](common_params & params, int value) {
  2566. params.verbosity = value;
  2567. common_log_set_verbosity_thold(value);
  2568. }
  2569. ).set_env("LLAMA_LOG_VERBOSITY"));
  2570. add_opt(common_arg(
  2571. {"--log-prefix"},
  2572. "Enable prefix in log messages",
  2573. [](common_params &) {
  2574. common_log_set_prefix(common_log_main(), true);
  2575. }
  2576. ).set_env("LLAMA_LOG_PREFIX"));
  2577. add_opt(common_arg(
  2578. {"--log-timestamps"},
  2579. "Enable timestamps in log messages",
  2580. [](common_params &) {
  2581. common_log_set_timestamps(common_log_main(), true);
  2582. }
  2583. ).set_env("LLAMA_LOG_TIMESTAMPS"));
  2584. // speculative parameters
  2585. add_opt(common_arg(
  2586. {"-td", "--threads-draft"}, "N",
  2587. "number of threads to use during generation (default: same as --threads)",
  2588. [](common_params & params, int value) {
  2589. params.speculative.cpuparams.n_threads = value;
  2590. if (params.speculative.cpuparams.n_threads <= 0) {
  2591. params.speculative.cpuparams.n_threads = std::thread::hardware_concurrency();
  2592. }
  2593. }
  2594. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}));
  2595. add_opt(common_arg(
  2596. {"-tbd", "--threads-batch-draft"}, "N",
  2597. "number of threads to use during batch and prompt processing (default: same as --threads-draft)",
  2598. [](common_params & params, int value) {
  2599. params.speculative.cpuparams_batch.n_threads = value;
  2600. if (params.speculative.cpuparams_batch.n_threads <= 0) {
  2601. params.speculative.cpuparams_batch.n_threads = std::thread::hardware_concurrency();
  2602. }
  2603. }
  2604. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}));
  2605. add_opt(common_arg(
  2606. {"-Cd", "--cpu-mask-draft"}, "M",
  2607. "Draft model CPU affinity mask. Complements cpu-range-draft (default: same as --cpu-mask)",
  2608. [](common_params & params, const std::string & mask) {
  2609. params.speculative.cpuparams.mask_valid = true;
  2610. if (!parse_cpu_mask(mask, params.speculative.cpuparams.cpumask)) {
  2611. throw std::invalid_argument("invalid cpumask");
  2612. }
  2613. }
  2614. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2615. add_opt(common_arg(
  2616. {"-Crd", "--cpu-range-draft"}, "lo-hi",
  2617. "Ranges of CPUs for affinity. Complements --cpu-mask-draft",
  2618. [](common_params & params, const std::string & range) {
  2619. params.speculative.cpuparams.mask_valid = true;
  2620. if (!parse_cpu_range(range, params.speculative.cpuparams.cpumask)) {
  2621. throw std::invalid_argument("invalid range");
  2622. }
  2623. }
  2624. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2625. add_opt(common_arg(
  2626. {"--cpu-strict-draft"}, "<0|1>",
  2627. "Use strict CPU placement for draft model (default: same as --cpu-strict)",
  2628. [](common_params & params, int value) {
  2629. params.speculative.cpuparams.strict_cpu = value;
  2630. }
  2631. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2632. add_opt(common_arg(
  2633. {"--prio-draft"}, "N",
  2634. string_format("set draft process/thread priority : 0-normal, 1-medium, 2-high, 3-realtime (default: %d)\n", params.speculative.cpuparams.priority),
  2635. [](common_params & params, int prio) {
  2636. if (prio < 0 || prio > 3) {
  2637. throw std::invalid_argument("invalid value");
  2638. }
  2639. params.speculative.cpuparams.priority = (enum ggml_sched_priority) prio;
  2640. }
  2641. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2642. add_opt(common_arg(
  2643. {"--poll-draft"}, "<0|1>",
  2644. "Use polling to wait for draft model work (default: same as --poll])",
  2645. [](common_params & params, int value) {
  2646. params.speculative.cpuparams.poll = value;
  2647. }
  2648. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2649. add_opt(common_arg(
  2650. {"-Cbd", "--cpu-mask-batch-draft"}, "M",
  2651. "Draft model CPU affinity mask. Complements cpu-range-draft (default: same as --cpu-mask)",
  2652. [](common_params & params, const std::string & mask) {
  2653. params.speculative.cpuparams_batch.mask_valid = true;
  2654. if (!parse_cpu_mask(mask, params.speculative.cpuparams_batch.cpumask)) {
  2655. throw std::invalid_argument("invalid cpumask");
  2656. }
  2657. }
  2658. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2659. add_opt(common_arg(
  2660. {"-Crbd", "--cpu-range-batch-draft"}, "lo-hi",
  2661. "Ranges of CPUs for affinity. Complements --cpu-mask-draft-batch)",
  2662. [](common_params & params, const std::string & range) {
  2663. params.speculative.cpuparams_batch.mask_valid = true;
  2664. if (!parse_cpu_range(range, params.speculative.cpuparams_batch.cpumask)) {
  2665. throw std::invalid_argument("invalid cpumask");
  2666. }
  2667. }
  2668. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2669. add_opt(common_arg(
  2670. {"--cpu-strict-batch-draft"}, "<0|1>",
  2671. "Use strict CPU placement for draft model (default: --cpu-strict-draft)",
  2672. [](common_params & params, int value) {
  2673. params.speculative.cpuparams_batch.strict_cpu = value;
  2674. }
  2675. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2676. add_opt(common_arg(
  2677. {"--prio-batch-draft"}, "N",
  2678. string_format("set draft process/thread priority : 0-normal, 1-medium, 2-high, 3-realtime (default: %d)\n", params.speculative.cpuparams_batch.priority),
  2679. [](common_params & params, int prio) {
  2680. if (prio < 0 || prio > 3) {
  2681. throw std::invalid_argument("invalid value");
  2682. }
  2683. params.speculative.cpuparams_batch.priority = (enum ggml_sched_priority) prio;
  2684. }
  2685. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2686. add_opt(common_arg(
  2687. {"--poll-batch-draft"}, "<0|1>",
  2688. "Use polling to wait for draft model work (default: --poll-draft)",
  2689. [](common_params & params, int value) {
  2690. params.speculative.cpuparams_batch.poll = value;
  2691. }
  2692. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
  2693. add_opt(common_arg(
  2694. {"--draft-max", "--draft", "--draft-n"}, "N",
  2695. string_format("number of tokens to draft for speculative decoding (default: %d)", params.speculative.n_max),
  2696. [](common_params & params, int value) {
  2697. params.speculative.n_max = value;
  2698. }
  2699. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_LOOKUP, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_DRAFT_MAX"));
  2700. add_opt(common_arg(
  2701. {"--draft-min", "--draft-n-min"}, "N",
  2702. string_format("minimum number of draft tokens to use for speculative decoding (default: %d)", params.speculative.n_min),
  2703. [](common_params & params, int value) {
  2704. params.speculative.n_min = value;
  2705. }
  2706. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_LOOKUP, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_DRAFT_MIN"));
  2707. add_opt(common_arg(
  2708. {"--draft-p-split"}, "P",
  2709. string_format("speculative decoding split probability (default: %.1f)", (double)params.speculative.p_split),
  2710. [](common_params & params, const std::string & value) {
  2711. params.speculative.p_split = std::stof(value);
  2712. }
  2713. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE}).set_env("LLAMA_ARG_DRAFT_P_SPLIT"));
  2714. add_opt(common_arg(
  2715. {"--draft-p-min"}, "P",
  2716. string_format("minimum speculative decoding probability (greedy) (default: %.1f)", (double)params.speculative.p_min),
  2717. [](common_params & params, const std::string & value) {
  2718. params.speculative.p_min = std::stof(value);
  2719. }
  2720. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_DRAFT_P_MIN"));
  2721. add_opt(common_arg(
  2722. {"-cd", "--ctx-size-draft"}, "N",
  2723. string_format("size of the prompt context for the draft model (default: %d, 0 = loaded from model)", params.speculative.n_ctx),
  2724. [](common_params & params, int value) {
  2725. params.speculative.n_ctx = value;
  2726. }
  2727. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CTX_SIZE_DRAFT"));
  2728. add_opt(common_arg(
  2729. {"-devd", "--device-draft"}, "<dev1,dev2,..>",
  2730. "comma-separated list of devices to use for offloading the draft model (none = don't offload)\n"
  2731. "use --list-devices to see a list of available devices",
  2732. [](common_params & params, const std::string & value) {
  2733. params.speculative.devices = parse_device_list(value);
  2734. }
  2735. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}));
  2736. add_opt(common_arg(
  2737. {"-ngld", "--gpu-layers-draft", "--n-gpu-layers-draft"}, "N",
  2738. "number of layers to store in VRAM for the draft model",
  2739. [](common_params & params, int value) {
  2740. params.speculative.n_gpu_layers = value;
  2741. if (!llama_supports_gpu_offload()) {
  2742. fprintf(stderr, "warning: no usable GPU found, --gpu-layers-draft option will be ignored\n");
  2743. fprintf(stderr, "warning: one possible reason is that llama.cpp was compiled without GPU support\n");
  2744. fprintf(stderr, "warning: consult docs/build.md for compilation instructions\n");
  2745. }
  2746. }
  2747. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_N_GPU_LAYERS_DRAFT"));
  2748. add_opt(common_arg(
  2749. {"-md", "--model-draft"}, "FNAME",
  2750. "draft model for speculative decoding (default: unused)",
  2751. [](common_params & params, const std::string & value) {
  2752. params.speculative.model.path = value;
  2753. }
  2754. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_MODEL_DRAFT"));
  2755. add_opt(common_arg(
  2756. {"--spec-replace"}, "TARGET", "DRAFT",
  2757. "translate the string in TARGET into DRAFT if the draft model and main model are not compatible",
  2758. [](common_params & params, const std::string & tgt, const std::string & dft) {
  2759. params.speculative.replacements.push_back({ tgt, dft });
  2760. }
  2761. ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}));
  2762. add_opt(common_arg(
  2763. {"-ctkd", "--cache-type-k-draft"}, "TYPE",
  2764. string_format(
  2765. "KV cache data type for K for the draft model\n"
  2766. "allowed values: %s\n"
  2767. "(default: %s)",
  2768. get_all_kv_cache_types().c_str(),
  2769. ggml_type_name(params.speculative.cache_type_k)
  2770. ),
  2771. [](common_params & params, const std::string & value) {
  2772. params.speculative.cache_type_k = kv_cache_type_from_str(value);
  2773. }
  2774. ).set_env("LLAMA_ARG_CACHE_TYPE_K_DRAFT"));
  2775. add_opt(common_arg(
  2776. {"-ctvd", "--cache-type-v-draft"}, "TYPE",
  2777. string_format(
  2778. "KV cache data type for V for the draft model\n"
  2779. "allowed values: %s\n"
  2780. "(default: %s)",
  2781. get_all_kv_cache_types().c_str(),
  2782. ggml_type_name(params.speculative.cache_type_v)
  2783. ),
  2784. [](common_params & params, const std::string & value) {
  2785. params.speculative.cache_type_v = kv_cache_type_from_str(value);
  2786. }
  2787. ).set_env("LLAMA_ARG_CACHE_TYPE_V_DRAFT"));
  2788. add_opt(common_arg(
  2789. {"-mv", "--model-vocoder"}, "FNAME",
  2790. "vocoder model for audio generation (default: unused)",
  2791. [](common_params & params, const std::string & value) {
  2792. params.vocoder.model.path = value;
  2793. }
  2794. ).set_examples({LLAMA_EXAMPLE_TTS, LLAMA_EXAMPLE_SERVER}));
  2795. add_opt(common_arg(
  2796. {"--tts-use-guide-tokens"},
  2797. "Use guide tokens to improve TTS word recall",
  2798. [](common_params & params) {
  2799. params.vocoder.use_guide_tokens = true;
  2800. }
  2801. ).set_examples({LLAMA_EXAMPLE_TTS, LLAMA_EXAMPLE_SERVER}));
  2802. add_opt(common_arg(
  2803. {"--tts-speaker-file"}, "FNAME",
  2804. "speaker file path for audio generation",
  2805. [](common_params & params, const std::string & value) {
  2806. params.vocoder.speaker_file = value;
  2807. }
  2808. ).set_examples({LLAMA_EXAMPLE_TTS}));
  2809. add_opt(common_arg(
  2810. {"--diffusion-steps"}, "N",
  2811. string_format("number of diffusion steps (default: %d)", params.diffusion.steps),
  2812. [](common_params & params, int value) { params.diffusion.steps = value; }
  2813. ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
  2814. add_opt(common_arg(
  2815. {"--diffusion-visual"},
  2816. string_format("enable visual diffusion mode (show progressive generation) (default: %s)", params.diffusion.visual_mode ? "true" : "false"),
  2817. [](common_params & params) { params.diffusion.visual_mode = true; }
  2818. ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
  2819. add_opt(common_arg(
  2820. {"--diffusion-eps"}, "F",
  2821. string_format("epsilon for timesteps (default: %.6f)", (double) params.diffusion.eps),
  2822. [](common_params & params, const std::string & value) { params.diffusion.eps = std::stof(value); }
  2823. ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
  2824. add_opt(common_arg(
  2825. {"--diffusion-algorithm"}, "N",
  2826. string_format("diffusion algorithm: 0=ORIGIN, 1=ENTROPY_BASED, 2=MARGIN_BASED, 3=RANDOM, 4=LOW_CONFIDENCE (default: %d)", params.diffusion.algorithm),
  2827. [](common_params & params, int value) { params.diffusion.algorithm = value; }
  2828. ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
  2829. add_opt(common_arg(
  2830. {"--diffusion-alg-temp"}, "F",
  2831. string_format("dream algorithm temperature (default: %.3f)", (double) params.diffusion.alg_temp),
  2832. [](common_params & params, const std::string & value) { params.diffusion.alg_temp = std::stof(value); }
  2833. ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
  2834. add_opt(common_arg(
  2835. {"--diffusion-block-length"}, "N",
  2836. string_format("llada block length for generation (default: %d)", params.diffusion.block_length),
  2837. [](common_params & params, int value) { params.diffusion.block_length = value; }
  2838. ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
  2839. add_opt(common_arg(
  2840. {"--diffusion-cfg-scale"}, "F",
  2841. string_format("llada classifier-free guidance scale (default: %.3f)", (double) params.diffusion.cfg_scale),
  2842. [](common_params & params, const std::string & value) { params.diffusion.cfg_scale = std::stof(value); }
  2843. ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
  2844. add_opt(common_arg(
  2845. {"--diffusion-add-gumbel-noise"}, "F",
  2846. string_format("add gumbel noise to the logits if temp > 0.0 (default: %s)", params.diffusion.add_gumbel_noise ? "true" : "false"),
  2847. [](common_params & params, const std::string & value) { params.diffusion.add_gumbel_noise = std::stof(value); }
  2848. ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
  2849. add_opt(common_arg(
  2850. { "-lr", "--learning-rate" }, "ALPHA",
  2851. string_format("adamw or sgd optimizer alpha (default: %.2g); note: sgd alpha recommended ~10x (no momentum)", (double) params.lr.lr0),
  2852. [](common_params & params, const std::string & value) { params.lr.lr0 = std::stof(value); }
  2853. ).set_examples({ LLAMA_EXAMPLE_FINETUNE }));
  2854. add_opt(common_arg({ "-lr-min", "--learning-rate-min" }, "ALPHA",
  2855. string_format("(if >0) final learning rate after decay (if -decay-epochs is set, default=%.2g)",
  2856. (double) params.lr.lr_min),
  2857. [](common_params & params, const std::string & value) { params.lr.lr_min = std::stof(value); }
  2858. ).set_examples({ LLAMA_EXAMPLE_FINETUNE }));
  2859. add_opt(common_arg(
  2860. {"-decay-epochs", "--learning-rate-decay-epochs"}, "ALPHA",
  2861. string_format("(if >0) decay learning rate to -lr-min after this many epochs (exponential decay, default=%.2g)", (double) params.lr.decay_epochs),
  2862. [](common_params & params, const std::string & value) { params.lr.decay_epochs = std::stof(value); }
  2863. ).set_examples({ LLAMA_EXAMPLE_FINETUNE }));
  2864. add_opt(common_arg(
  2865. {"-wd", "--weight-decay"}, "WD",
  2866. string_format("adamw or sgd optimizer weight decay (0 is off; recommend very small e.g. 1e-9) (default: %.2g).", (double) params.lr.wd),
  2867. [](common_params & params, const std::string & value) { params.lr.wd = std::stof(value); }
  2868. ).set_examples({ LLAMA_EXAMPLE_FINETUNE }));
  2869. add_opt(common_arg(
  2870. {"-val-split", "--val-split"}, "FRACTION",
  2871. string_format("fraction of data to use as validation set for training (default: %.2g).", (double) params.val_split),
  2872. [](common_params & params, const std::string & value) { params.val_split = std::stof(value); }
  2873. ).set_examples({ LLAMA_EXAMPLE_FINETUNE }));
  2874. add_opt(common_arg(
  2875. {"-epochs", "--epochs"}, "N",
  2876. string_format("optimizer max # of epochs (default: %d)", params.lr.epochs),
  2877. [](common_params & params, int epochs) { params.lr.epochs = epochs; }
  2878. ).set_examples({ LLAMA_EXAMPLE_FINETUNE }));
  2879. add_opt(common_arg(
  2880. {"-opt", "--optimizer"}, "sgd|adamw", "adamw or sgd",
  2881. [](common_params & params, const std::string & name) {
  2882. params.optimizer = common_opt_get_optimizer(name.c_str());
  2883. if (params.optimizer == GGML_OPT_OPTIMIZER_TYPE_COUNT) {
  2884. throw std::invalid_argument("invalid --optimizer, valid options: adamw, sgd");
  2885. }
  2886. }
  2887. ).set_examples({ LLAMA_EXAMPLE_FINETUNE }));
  2888. // presets
  2889. add_opt(common_arg(
  2890. {"--tts-oute-default"},
  2891. string_format("use default OuteTTS models (note: can download weights from the internet)"),
  2892. [](common_params & params) {
  2893. params.model.hf_repo = "OuteAI/OuteTTS-0.2-500M-GGUF";
  2894. params.model.hf_file = "OuteTTS-0.2-500M-Q8_0.gguf";
  2895. params.vocoder.model.hf_repo = "ggml-org/WavTokenizer";
  2896. params.vocoder.model.hf_file = "WavTokenizer-Large-75-F16.gguf";
  2897. }
  2898. ).set_examples({LLAMA_EXAMPLE_TTS}));
  2899. add_opt(common_arg(
  2900. {"--embd-gemma-default"},
  2901. string_format("use default EmbeddingGemma model (note: can download weights from the internet)"),
  2902. [](common_params & params) {
  2903. params.model.hf_repo = "ggml-org/embeddinggemma-300M-qat-q4_0-GGUF";
  2904. params.model.hf_file = "embeddinggemma-300M-qat-Q4_0.gguf";
  2905. params.port = 8011;
  2906. params.n_ubatch = 2048;
  2907. params.n_batch = 2048;
  2908. params.n_parallel = 32;
  2909. params.n_ctx = 2048*params.n_parallel;
  2910. params.verbose_prompt = true;
  2911. params.embedding = true;
  2912. }
  2913. ).set_examples({LLAMA_EXAMPLE_EMBEDDING, LLAMA_EXAMPLE_SERVER}));
  2914. add_opt(common_arg(
  2915. {"--fim-qwen-1.5b-default"},
  2916. string_format("use default Qwen 2.5 Coder 1.5B (note: can download weights from the internet)"),
  2917. [](common_params & params) {
  2918. params.model.hf_repo = "ggml-org/Qwen2.5-Coder-1.5B-Q8_0-GGUF";
  2919. params.model.hf_file = "qwen2.5-coder-1.5b-q8_0.gguf";
  2920. params.port = 8012;
  2921. params.n_ubatch = 1024;
  2922. params.n_batch = 1024;
  2923. params.n_ctx = 0;
  2924. params.n_cache_reuse = 256;
  2925. }
  2926. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2927. add_opt(common_arg(
  2928. {"--fim-qwen-3b-default"},
  2929. string_format("use default Qwen 2.5 Coder 3B (note: can download weights from the internet)"),
  2930. [](common_params & params) {
  2931. params.model.hf_repo = "ggml-org/Qwen2.5-Coder-3B-Q8_0-GGUF";
  2932. params.model.hf_file = "qwen2.5-coder-3b-q8_0.gguf";
  2933. params.port = 8012;
  2934. params.n_ubatch = 1024;
  2935. params.n_batch = 1024;
  2936. params.n_ctx = 0;
  2937. params.n_cache_reuse = 256;
  2938. }
  2939. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2940. add_opt(common_arg(
  2941. {"--fim-qwen-7b-default"},
  2942. string_format("use default Qwen 2.5 Coder 7B (note: can download weights from the internet)"),
  2943. [](common_params & params) {
  2944. params.model.hf_repo = "ggml-org/Qwen2.5-Coder-7B-Q8_0-GGUF";
  2945. params.model.hf_file = "qwen2.5-coder-7b-q8_0.gguf";
  2946. params.port = 8012;
  2947. params.n_ubatch = 1024;
  2948. params.n_batch = 1024;
  2949. params.n_ctx = 0;
  2950. params.n_cache_reuse = 256;
  2951. }
  2952. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2953. add_opt(common_arg(
  2954. {"--fim-qwen-7b-spec"},
  2955. string_format("use Qwen 2.5 Coder 7B + 0.5B draft for speculative decoding (note: can download weights from the internet)"),
  2956. [](common_params & params) {
  2957. params.model.hf_repo = "ggml-org/Qwen2.5-Coder-7B-Q8_0-GGUF";
  2958. params.model.hf_file = "qwen2.5-coder-7b-q8_0.gguf";
  2959. params.speculative.model.hf_repo = "ggml-org/Qwen2.5-Coder-0.5B-Q8_0-GGUF";
  2960. params.speculative.model.hf_file = "qwen2.5-coder-0.5b-q8_0.gguf";
  2961. params.port = 8012;
  2962. params.n_ubatch = 1024;
  2963. params.n_batch = 1024;
  2964. params.n_ctx = 0;
  2965. params.n_cache_reuse = 256;
  2966. }
  2967. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2968. add_opt(common_arg(
  2969. {"--fim-qwen-14b-spec"},
  2970. string_format("use Qwen 2.5 Coder 14B + 0.5B draft for speculative decoding (note: can download weights from the internet)"),
  2971. [](common_params & params) {
  2972. params.model.hf_repo = "ggml-org/Qwen2.5-Coder-14B-Q8_0-GGUF";
  2973. params.model.hf_file = "qwen2.5-coder-14b-q8_0.gguf";
  2974. params.speculative.model.hf_repo = "ggml-org/Qwen2.5-Coder-0.5B-Q8_0-GGUF";
  2975. params.speculative.model.hf_file = "qwen2.5-coder-0.5b-q8_0.gguf";
  2976. params.port = 8012;
  2977. params.n_ubatch = 1024;
  2978. params.n_batch = 1024;
  2979. params.n_ctx = 0;
  2980. params.n_cache_reuse = 256;
  2981. }
  2982. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2983. add_opt(common_arg(
  2984. {"--fim-qwen-30b-default"},
  2985. string_format("use default Qwen 3 Coder 30B A3B Instruct (note: can download weights from the internet)"),
  2986. [](common_params & params) {
  2987. params.model.hf_repo = "ggml-org/Qwen3-Coder-30B-A3B-Instruct-Q8_0-GGUF";
  2988. params.model.hf_file = "qwen3-coder-30b-a3b-instruct-q8_0.gguf";
  2989. params.port = 8012;
  2990. params.n_ubatch = 1024;
  2991. params.n_batch = 1024;
  2992. params.n_ctx = 0;
  2993. params.n_cache_reuse = 256;
  2994. }
  2995. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  2996. add_opt(common_arg(
  2997. {"--gpt-oss-20b-default"},
  2998. string_format("use gpt-oss-20b (note: can download weights from the internet)"),
  2999. [](common_params & params) {
  3000. params.model.hf_repo = "ggml-org/gpt-oss-20b-GGUF";
  3001. params.model.hf_file = "gpt-oss-20b-mxfp4.gguf";
  3002. params.port = 8013;
  3003. params.n_ubatch = 2048;
  3004. params.n_batch = 32768;
  3005. params.n_parallel = 2;
  3006. params.n_ctx = 131072*params.n_parallel;
  3007. params.sampling.temp = 1.0f;
  3008. params.sampling.top_p = 1.0f;
  3009. params.sampling.top_k = 0;
  3010. params.sampling.min_p = 0.01f;
  3011. params.use_jinja = true;
  3012. //params.default_template_kwargs["reasoning_effort"] = "\"high\"";
  3013. }
  3014. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  3015. add_opt(common_arg(
  3016. {"--gpt-oss-120b-default"},
  3017. string_format("use gpt-oss-120b (note: can download weights from the internet)"),
  3018. [](common_params & params) {
  3019. params.model.hf_repo = "ggml-org/gpt-oss-120b-GGUF";
  3020. params.port = 8013;
  3021. params.n_ubatch = 2048;
  3022. params.n_batch = 32768;
  3023. params.n_parallel = 2;
  3024. params.n_ctx = 131072*params.n_parallel;
  3025. params.sampling.temp = 1.0f;
  3026. params.sampling.top_p = 1.0f;
  3027. params.sampling.top_k = 0;
  3028. params.sampling.min_p = 0.01f;
  3029. params.use_jinja = true;
  3030. //params.default_template_kwargs["reasoning_effort"] = "\"high\"";
  3031. }
  3032. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  3033. add_opt(common_arg(
  3034. {"--vision-gemma-4b-default"},
  3035. string_format("use Gemma 3 4B QAT (note: can download weights from the internet)"),
  3036. [](common_params & params) {
  3037. params.model.hf_repo = "ggml-org/gemma-3-4b-it-qat-GGUF";
  3038. params.port = 8014;
  3039. params.n_ctx = 0;
  3040. params.use_jinja = true;
  3041. }
  3042. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  3043. add_opt(common_arg(
  3044. {"--vision-gemma-12b-default"},
  3045. string_format("use Gemma 3 12B QAT (note: can download weights from the internet)"),
  3046. [](common_params & params) {
  3047. params.model.hf_repo = "ggml-org/gemma-3-12b-it-qat-GGUF";
  3048. params.port = 8014;
  3049. params.n_ctx = 0;
  3050. params.use_jinja = true;
  3051. }
  3052. ).set_examples({LLAMA_EXAMPLE_SERVER}));
  3053. return ctx_arg;
  3054. }