You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

LLamaModel.cs 26 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. using LLama.Native;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using LLama.Exceptions;
  7. using System.Linq;
  8. using System.Text.RegularExpressions;
  9. using System.Runtime.InteropServices;
  10. using System.Diagnostics;
  11. namespace LLama
  12. {
  13. using llama_token = Int32;
  14. public class LLamaModel: IChatModel, IDisposable
  15. {
  16. LLamaParams _params;
  17. SafeLLamaContextHandle _ctx;
  18. string _path_session;
  19. List<llama_token> _session_tokens;
  20. List<llama_token> _embed_inp;
  21. int _n_ctx;
  22. List<llama_token> _inp_pfx;
  23. List<llama_token> _inp_sfx;
  24. List<llama_token> _llama_token_newline;
  25. List<llama_token> _last_n_tokens;
  26. bool _is_interacting;
  27. bool _is_antiprompt;
  28. bool _input_echo;
  29. // HACK - because session saving incurs a non-negligible delay, for now skip re-saving session
  30. // if we loaded a session with at least 75% similarity. It's currently just used to speed up the
  31. // initial prompt so it doesn't need to be an exact match.
  32. bool _need_to_save_session;
  33. int _n_past;
  34. int _n_remain;
  35. int _n_consumed;
  36. int _n_session_consumed;
  37. List<llama_token> _embed;
  38. // params related to chat API only
  39. bool _first_time_chat = true;
  40. public string Name { get; set; }
  41. public SafeLLamaContextHandle NativeHandle => _ctx;
  42. public LLamaModel(string model_path, string model_name, bool echo_input = false, bool verbose = false, int seed = 0, int n_threads = -1, int n_predict = -1,
  43. int n_parts = -1, int n_ctx = 512, int n_batch = 512, int n_keep = 0, int n_gpu_layers = 0,
  44. Dictionary<llama_token, float> logit_bias = null, int top_k = 40, float top_p = 0.95f,
  45. float tfs_z = 1.00f, float typical_p = 1.00f, float temp = 0.80f, float repeat_penalty = 1.10f,
  46. int repeat_last_n = 64, float frequency_penalty = 0.00f, float presence_penalty = 0.00f,
  47. int mirostat = 0, float mirostat_tau = 5.00f, float mirostat_eta = 0.10f, string prompt = "",
  48. string path_session = "", string input_prefix = "", string input_suffix = "",
  49. List<string> antiprompt = null, string lora_adapter = "", string lora_base = "",
  50. bool memory_f16 = true, bool random_prompt = false, bool use_color = false, bool interactive = false,
  51. bool embedding = false, bool interactive_first = false, bool prompt_cache_all = false, bool instruct = false, bool penalize_nl = true,
  52. bool perplexity = false, bool use_mmap = true, bool use_mlock = false, bool mem_test = false,
  53. bool verbose_prompt = false) : this(new LLamaParams(seed, n_threads, n_predict, n_parts, n_ctx, n_batch,
  54. n_keep, n_gpu_layers, logit_bias, top_k, top_p, tfs_z, typical_p, temp, repeat_penalty, repeat_last_n, frequency_penalty,
  55. presence_penalty, mirostat, mirostat_tau, mirostat_eta, model_path, prompt, path_session, input_prefix,
  56. input_suffix, antiprompt, lora_adapter, lora_base, memory_f16, random_prompt, use_color, interactive, embedding,
  57. interactive_first, prompt_cache_all, instruct, penalize_nl, perplexity, use_mmap, use_mlock, mem_test, verbose_prompt), model_name, echo_input, verbose)
  58. {
  59. }
  60. public unsafe LLamaModel(LLamaParams @params, string name = "", bool echo_input = false, bool verbose = false, string encoding = "UTF-8")
  61. {
  62. Name = name;
  63. _params = @params;
  64. _ctx = Utils.llama_init_from_gpt_params(ref _params);
  65. // Add a space in front of the first character to match OG llama tokenizer behavior
  66. _session_tokens = new List<llama_token>();
  67. _path_session = @params.path_session;
  68. if (!string.IsNullOrEmpty(_path_session))
  69. {
  70. if (verbose)
  71. {
  72. Logger.Default.Info($"Attempting to load saved session from '{_path_session}'");
  73. }
  74. if (!File.Exists(_path_session))
  75. {
  76. Logger.Default.Warn("Session file does not exist, will create.");
  77. }
  78. llama_token[] session_tokens = new llama_token[@params.n_ctx];
  79. ulong n_token_count_out = 0;
  80. if (!NativeApi.llama_load_session_file(_ctx, _path_session, session_tokens, (ulong)@params.n_ctx, &n_token_count_out))
  81. {
  82. throw new RuntimeError($"Failed to load session file {_path_session}");
  83. }
  84. _session_tokens = session_tokens.Take((int)n_token_count_out).ToList();
  85. if (verbose)
  86. {
  87. Logger.Default.Info($"Loaded a session with prompt size of {_session_tokens.Count} tokens");
  88. }
  89. }
  90. _n_ctx = NativeApi.llama_n_ctx(_ctx);
  91. WithPrompt(_params.prompt);
  92. // prefix & suffix for instruct mode
  93. _inp_pfx = Utils.llama_tokenize(_ctx, "\n\n### Instruction:\n\n", true, encoding);
  94. _inp_sfx = Utils.llama_tokenize(_ctx, "\n\n### Response:\n\n", false, encoding);
  95. // in instruct mode, we inject a prefix and a suffix to each input by the user
  96. if (_params.instruct)
  97. {
  98. _params.interactive_first = true;
  99. _params.antiprompt.Add("### Instruction:\n\n");
  100. }
  101. // enable interactive mode if reverse prompt or interactive start is specified
  102. if (_params.antiprompt.Count != 0 || _params.interactive_first)
  103. {
  104. _params.interactive = true;
  105. }
  106. // determine newline token
  107. _llama_token_newline = Utils.llama_tokenize(_ctx, "\n", false, encoding);
  108. if (_params.verbose_prompt)
  109. {
  110. Logger.Default.Info("\n");
  111. Logger.Default.Info($"prompt: '{_params.prompt}'");
  112. Logger.Default.Info($"number of tokens in prompt = {_embed_inp.Count}");
  113. for (int i = 0; i < _embed_inp.Count; i++)
  114. {
  115. Logger.Default.Info($"{_embed_inp[i]} -> '{NativeApi.llama_token_to_str(_ctx, _embed_inp[i])}'");
  116. }
  117. if (_params.n_keep > 0)
  118. {
  119. Logger.Default.Info($"static prompt based on n_keep: '");
  120. for (int i = 0; i < _params.n_keep; i++)
  121. {
  122. Logger.Default.Info($"{NativeApi.llama_token_to_str(_ctx, _embed_inp[i])}");
  123. }
  124. Logger.Default.Info("\n");
  125. }
  126. Logger.Default.Info("\n");
  127. }
  128. if (_params.interactive && verbose)
  129. {
  130. Logger.Default.Info("interactive mode on.");
  131. }
  132. if (verbose)
  133. {
  134. Logger.Default.Info($"sampling: repeat_last_n = {_params.repeat_last_n}, " +
  135. $"repeat_penalty = {_params.repeat_penalty}, presence_penalty = {_params.presence_penalty}, " +
  136. $"frequency_penalty = {_params.frequency_penalty}, top_k = {_params.top_k}, tfs_z = {_params.tfs_z}," +
  137. $" top_p = {_params.top_p}, typical_p = {_params.typical_p}, temp = {_params.temp}, mirostat = {_params.mirostat}," +
  138. $" mirostat_lr = {_params.mirostat_eta}, mirostat_ent = {_params.mirostat_tau}");
  139. Logger.Default.Info($"generate: n_ctx = {_n_ctx}, n_batch = {_params.n_batch}, n_predict = {_params.n_predict}, " +
  140. $"n_keep = {_params.n_keep}");
  141. Logger.Default.Info("\n");
  142. }
  143. _last_n_tokens = Enumerable.Repeat(0, _n_ctx).ToList();
  144. if (_params.interactive)
  145. {
  146. if (verbose)
  147. {
  148. Logger.Default.Info("== Running in interactive mode. ==");
  149. }
  150. _is_interacting = _params.interactive_first;
  151. }
  152. _is_antiprompt = false;
  153. _input_echo = echo_input;
  154. _n_past = 0;
  155. _n_remain = _params.n_predict;
  156. _n_consumed = 0;
  157. _n_session_consumed = 0;
  158. _embed = new List<llama_token>();
  159. }
  160. public LLamaModel WithPrompt(string prompt, string encoding = "UTF-8")
  161. {
  162. _params.prompt = _params.prompt.Insert(0, " ");
  163. _embed_inp = Utils.llama_tokenize(_ctx, _params.prompt, true, encoding);
  164. if (_embed_inp.Count > _n_ctx - 4)
  165. {
  166. throw new ArgumentException($"prompt is too long ({_embed_inp.Count} tokens, max {_n_ctx - 4})");
  167. }
  168. ulong n_matching_session_tokens = 0;
  169. if (_session_tokens.Count > 0)
  170. {
  171. foreach (var id in _session_tokens)
  172. {
  173. if (n_matching_session_tokens >= (ulong)_embed_inp.Count || id != _embed_inp[(int)n_matching_session_tokens])
  174. {
  175. break;
  176. }
  177. n_matching_session_tokens++;
  178. }
  179. if (n_matching_session_tokens >= (ulong)_embed_inp.Count)
  180. {
  181. Logger.Default.Info("Session file has exact match for prompt!");
  182. }
  183. else if (n_matching_session_tokens < (ulong)(_embed_inp.Count / 2))
  184. {
  185. Logger.Default.Warn($"session file has low similarity to prompt ({n_matching_session_tokens} " +
  186. $"/ {_embed_inp.Count} tokens); will mostly be reevaluated.");
  187. }
  188. else
  189. {
  190. Logger.Default.Info($"Session file matches {n_matching_session_tokens} / {_embed_inp.Count} " +
  191. $"tokens of prompt.");
  192. }
  193. }
  194. // number of tokens to keep when resetting context
  195. if (_params.n_keep < 0 || _params.n_keep > (int)_embed_inp.Count || _params.instruct)
  196. {
  197. _params.n_keep = _embed_inp.Count;
  198. }
  199. if (_embed_inp.Count > _n_ctx - 4)
  200. {
  201. throw new ArgumentException($"prompt is too long ({_embed_inp.Count} tokens, max {_n_ctx - 4})");
  202. }
  203. _need_to_save_session = !string.IsNullOrEmpty(_path_session) && n_matching_session_tokens < (ulong)(_embed_inp.Count * 3 / 4);
  204. return this;
  205. }
  206. public LLamaModel WithPromptFile(string promptFileName)
  207. {
  208. return WithPrompt(File.ReadAllText(promptFileName));
  209. }
  210. private string ProcessTextBeforeInfer(string text, string encoding)
  211. {
  212. if (!string.IsNullOrEmpty(_params.input_prefix))
  213. {
  214. text = _params.input_prefix + text;
  215. }
  216. if (!text.EndsWith("\n"))
  217. {
  218. text += "\n";
  219. }
  220. if (text.Length > 1)
  221. {
  222. // append input suffix if any
  223. if (!string.IsNullOrEmpty(_params.input_suffix))
  224. {
  225. text += _params.input_suffix;
  226. Console.Write(_params.input_suffix);
  227. }
  228. // instruct mode: insert instruction prefix
  229. if (_params.instruct && !_is_antiprompt)
  230. {
  231. _n_consumed = _embed_inp.Count;
  232. _embed_inp.AddRange(_inp_pfx);
  233. }
  234. var line_inp = Utils.llama_tokenize(_ctx, text, false, encoding);
  235. _embed_inp.AddRange(line_inp);
  236. // instruct mode: insert response suffix
  237. if (_params.instruct)
  238. {
  239. _embed_inp.AddRange(_inp_sfx);
  240. }
  241. _n_remain -= line_inp.Count;
  242. }
  243. return text;
  244. }
  245. public void InitChatPrompt(string prompt, string encoding = "UTF-8")
  246. {
  247. WithPrompt(prompt);
  248. }
  249. public void InitChatAntiprompt(string[] antiprompt)
  250. {
  251. _params.antiprompt = antiprompt.ToList();
  252. }
  253. public IEnumerable<string> Chat(string text, string? prompt = null, string encoding = "UTF-8")
  254. {
  255. _params.interactive = true;
  256. _input_echo = false;
  257. if (!string.IsNullOrEmpty(prompt))
  258. {
  259. WithPrompt(prompt);
  260. }
  261. return Call(text, encoding);
  262. }
  263. public IEnumerable<string> Call(string text, string encoding = "UTF-8")
  264. {
  265. _is_interacting = _is_antiprompt = false;
  266. ProcessTextBeforeInfer(text, encoding);
  267. while ((_n_remain != 0 || _params.interactive) && !_is_interacting)
  268. {
  269. if (_embed.Count > 0)
  270. {
  271. // infinite text generation via context swapping
  272. // if we run out of context:
  273. // - take the n_keep first tokens from the original prompt (via n_past)
  274. // - take half of the last (n_ctx - n_keep) tokens and recompute the logits in batches
  275. if (_n_past + _embed.Count > _n_ctx)
  276. {
  277. int n_left = _n_past - _params.n_keep;
  278. _n_past = Math.Max(1, _params.n_keep);
  279. // insert n_left/2 tokens at the start of embed from last_n_tokens
  280. _embed.InsertRange(0, _last_n_tokens.Take(_last_n_tokens.Count - _embed.Count).Skip(_n_ctx - n_left / 2 - _embed.Count));
  281. // stop saving session if we run out of context
  282. _path_session = "";
  283. // Console.WriteLine("\n---\n");
  284. // Console.Write("resetting: '");
  285. // for (int i = 0; i < embed.Count; i++) {
  286. // Console.Write(llama_token_to_str(ctx, embed[i]));
  287. // }
  288. // Console.WriteLine("'\n");
  289. // Console.WriteLine("\n---\n");
  290. }
  291. // try to reuse a matching prefix from the loaded session instead of re-eval (via n_past)
  292. // REVIEW
  293. if (_n_session_consumed < _session_tokens.Count)
  294. {
  295. int i = 0;
  296. for (; i < _embed.Count; i++)
  297. {
  298. if (!_embed[i].Equals(_session_tokens[_n_session_consumed]))
  299. {
  300. _session_tokens.RemoveRange(_n_session_consumed, _session_tokens.Count - _n_session_consumed);
  301. break;
  302. }
  303. _n_past++;
  304. _n_session_consumed++;
  305. if (_n_session_consumed >= _session_tokens.Count)
  306. {
  307. i++;
  308. break;
  309. }
  310. }
  311. if (i > 0)
  312. {
  313. _embed.RemoveRange(0, i);
  314. }
  315. }
  316. // evaluate tokens in batches
  317. // embed is typically prepared beforehand to fit within a batch, but not always
  318. for (int i = 0; i < _embed.Count; i += _params.n_batch)
  319. {
  320. int n_eval = _embed.Count - i;
  321. if (n_eval > _params.n_batch)
  322. {
  323. n_eval = _params.n_batch;
  324. }
  325. var array = _embed.GetRange(i, n_eval).ToArray();
  326. if (NativeApi.llama_eval(_ctx, array, n_eval, _n_past, _params.n_threads) != 0)
  327. {
  328. Logger.Default.Error($"Failed to eval");
  329. throw new RuntimeError("Failed to eval");
  330. }
  331. _n_past += n_eval;
  332. }
  333. if (_embed.Count > 0 && !string.IsNullOrEmpty(_path_session))
  334. {
  335. _session_tokens.AddRange(_embed);
  336. _n_session_consumed = _session_tokens.Count;
  337. }
  338. }
  339. _embed.Clear();
  340. if (_embed_inp.Count <= _n_consumed && !_is_interacting)
  341. {
  342. var temp = _params.temp;
  343. var top_k = _params.top_k <= 0 ? NativeApi.llama_n_vocab(_ctx) : _params.top_k;
  344. var top_p = _params.top_p;
  345. var tfs_z = _params.tfs_z;
  346. var typical_p = _params.typical_p;
  347. var repeat_last_n = _params.repeat_last_n < 0 ? _n_ctx : _params.repeat_last_n;
  348. var repeat_penalty = _params.repeat_penalty;
  349. var alpha_presence = _params.presence_penalty;
  350. var alpha_frequency = _params.frequency_penalty;
  351. var mirostat = _params.mirostat;
  352. var mirostat_tau = _params.mirostat_tau;
  353. var mirostat_eta = _params.mirostat_eta;
  354. var penalize_nl = _params.penalize_nl;
  355. // optionally save the session on first sample (for faster prompt loading next time)
  356. if (!string.IsNullOrEmpty(_path_session) && _need_to_save_session)
  357. {
  358. _need_to_save_session = false;
  359. NativeApi.llama_save_session_file(_ctx, _path_session, _session_tokens.ToArray(), (ulong)_session_tokens.Count);
  360. }
  361. llama_token id = 0;
  362. {
  363. var n_vocab = NativeApi.llama_n_vocab(_ctx);
  364. var logits = Utils.llama_get_logits(_ctx, n_vocab);
  365. // Apply params.logit_bias map
  366. foreach (KeyValuePair<int, float> it in _params.logit_bias)
  367. {
  368. logits[it.Key] += it.Value;
  369. }
  370. var candidates = new List<LLamaTokenData>();
  371. candidates.Capacity = n_vocab;
  372. for (llama_token token_id = 0; token_id < n_vocab; token_id++)
  373. {
  374. candidates.Add(new LLamaTokenData(token_id, logits[token_id], 0.0f));
  375. }
  376. LLamaTokenDataArray candidates_p = new LLamaTokenDataArray(candidates.ToArray(), (ulong)candidates.Count, false);
  377. // Apply penalties
  378. float nl_logit = logits[NativeApi.llama_token_nl()];
  379. var last_n_repeat = Math.Min(Math.Min(_last_n_tokens.Count, repeat_last_n), _n_ctx);
  380. SamplingApi.llama_sample_repetition_penalty(_ctx, candidates_p,
  381. _last_n_tokens.GetRange(_last_n_tokens.Count - last_n_repeat, last_n_repeat).ToArray(),
  382. (ulong)last_n_repeat, repeat_penalty);
  383. SamplingApi.llama_sample_frequency_and_presence_penalties(_ctx, candidates_p,
  384. _last_n_tokens.GetRange(_last_n_tokens.Count - last_n_repeat, last_n_repeat).ToArray(),
  385. (ulong)last_n_repeat, alpha_frequency, alpha_presence);
  386. if (!penalize_nl)
  387. {
  388. logits[NativeApi.llama_token_nl()] = nl_logit;
  389. }
  390. if (temp <= 0)
  391. {
  392. // Greedy sampling
  393. id = SamplingApi.llama_sample_token_greedy(_ctx, candidates_p);
  394. }
  395. else
  396. {
  397. if (mirostat == 1)
  398. {
  399. float mirostat_mu = 2.0f * mirostat_tau;
  400. const int mirostat_m = 100;
  401. SamplingApi.llama_sample_temperature(_ctx, candidates_p, temp);
  402. id = SamplingApi.llama_sample_token_mirostat(_ctx, candidates_p, mirostat_tau, mirostat_eta, mirostat_m, mirostat_mu);
  403. }
  404. else if (mirostat == 2)
  405. {
  406. float mirostat_mu = 2.0f * mirostat_tau;
  407. SamplingApi.llama_sample_temperature(_ctx, candidates_p, temp);
  408. id = SamplingApi.llama_sample_token_mirostat_v2(_ctx, candidates_p, mirostat_tau, mirostat_eta, mirostat_mu);
  409. }
  410. else
  411. {
  412. // Temperature sampling
  413. SamplingApi.llama_sample_top_k(_ctx, candidates_p, top_k, 1);
  414. SamplingApi.llama_sample_tail_free(_ctx, candidates_p, tfs_z, 1);
  415. SamplingApi.llama_sample_typical(_ctx, candidates_p, typical_p, 1);
  416. SamplingApi.llama_sample_top_p(_ctx, candidates_p, top_p, 1);
  417. SamplingApi.llama_sample_temperature(_ctx, candidates_p, temp);
  418. id = SamplingApi.llama_sample_token(_ctx, candidates_p);
  419. }
  420. }
  421. _last_n_tokens.RemoveAt(0);
  422. _last_n_tokens.Add(id);
  423. }
  424. // replace end of text token with newline token when in interactive mode
  425. if (id == NativeApi.llama_token_eos() && _params.interactive && !_params.instruct)
  426. {
  427. id = _llama_token_newline[0];
  428. if (_params.antiprompt.Count != 0)
  429. {
  430. // tokenize and inject first reverse prompt
  431. var first_antiprompt = Utils.llama_tokenize(_ctx, _params.antiprompt[0], false, encoding);
  432. _embed_inp.AddRange(first_antiprompt);
  433. }
  434. }
  435. // add it to the context
  436. _embed.Add(id);
  437. // echo this to console
  438. _input_echo = true;
  439. // decrement remaining sampling budget
  440. _n_remain--;
  441. }
  442. else
  443. {
  444. // Assuming that the necessary variables have been defined and initialized,
  445. // the C# equivalent code could be:
  446. while (_embed_inp.Count > _n_consumed)
  447. {
  448. _embed.Add(_embed_inp[_n_consumed]);
  449. _last_n_tokens.RemoveAt(0);
  450. _last_n_tokens.Add(_embed_inp[_n_consumed]);
  451. _n_consumed++;
  452. if (_embed.Count >= _params.n_batch)
  453. {
  454. break;
  455. }
  456. }
  457. }
  458. if (_input_echo)
  459. {
  460. foreach (var id in _embed)
  461. {
  462. yield return Utils.PtrToStringUTF8(NativeApi.llama_token_to_str(_ctx, id));
  463. }
  464. }
  465. if (_params.interactive && _embed_inp.Count <= _n_consumed)
  466. {
  467. if (_params.antiprompt.Count > 0)
  468. {
  469. string last_output = "";
  470. foreach (var id in _last_n_tokens)
  471. {
  472. last_output += Utils.PtrToStringUTF8(NativeApi.llama_token_to_str(_ctx, id));
  473. }
  474. _is_antiprompt = false;
  475. foreach (var antiprompt in _params.antiprompt)
  476. {
  477. if (last_output.EndsWith(antiprompt))
  478. {
  479. _is_interacting = true;
  480. _is_antiprompt = true;
  481. break;
  482. }
  483. }
  484. }
  485. if(_n_past > 0 && _is_interacting)
  486. {
  487. _input_echo = false;
  488. break;
  489. }
  490. if (_embed.Count > 0 && _embed.Last() == NativeApi.llama_token_eos())
  491. {
  492. if (_params.instruct) {
  493. _is_interacting = true;
  494. } else
  495. {
  496. Logger.Default.Info(" [end of text]");
  497. }
  498. }
  499. if (_params.interactive && _n_remain <= 0 && _params.n_predict != -1) {
  500. _n_remain = _params.n_predict;
  501. _is_interacting = true;
  502. }
  503. }
  504. }
  505. if(!string.IsNullOrEmpty(_path_session) && _params.prompt_cache_all)
  506. {
  507. Logger.Default.Info($"saving final output to session file {_path_session}");
  508. var session_token_array = _session_tokens.ToArray();
  509. NativeApi.llama_save_session_file(_ctx, _path_session, session_token_array, (ulong)session_token_array.Length);
  510. }
  511. }
  512. public void Dispose()
  513. {
  514. _ctx.Dispose();
  515. }
  516. }
  517. }

C#/.NET上易用的LLM高性能推理框架,支持LLaMA和LLaVA系列模型。