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.

NativeApi.cs 27 kB

2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. using System;
  2. using System.Buffers;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. #pragma warning disable IDE1006 // Naming Styles
  6. namespace LLama.Native
  7. {
  8. using llama_token = Int32;
  9. public enum LLamaTokenType
  10. {
  11. LLAMA_TOKEN_TYPE_UNDEFINED = 0,
  12. LLAMA_TOKEN_TYPE_NORMAL = 1,
  13. LLAMA_TOKEN_TYPE_UNKNOWN = 2,
  14. LLAMA_TOKEN_TYPE_CONTROL = 3,
  15. LLAMA_TOKEN_TYPE_USER_DEFINED = 4,
  16. LLAMA_TOKEN_TYPE_UNUSED = 5,
  17. LLAMA_TOKEN_TYPE_BYTE = 6,
  18. }
  19. /// <summary>
  20. /// Callback from llama.cpp with log messages
  21. /// </summary>
  22. /// <param name="level"></param>
  23. /// <param name="message"></param>
  24. public delegate void LLamaLogCallback(LLamaLogLevel level, string message);
  25. /// <summary>
  26. /// Direct translation of the llama.cpp API
  27. /// </summary>
  28. public unsafe partial class NativeApi
  29. {
  30. /// <summary>
  31. /// A method that does nothing. This is a native method, calling it will force the llama native dependencies to be loaded.
  32. /// </summary>
  33. /// <returns></returns>
  34. [DllImport(libraryName, EntryPoint = "llama_mmap_supported", CallingConvention = CallingConvention.Cdecl)]
  35. public static extern bool llama_empty_call();
  36. /// <summary>
  37. /// Get the maximum number of devices supported by llama.cpp
  38. /// </summary>
  39. /// <returns></returns>
  40. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  41. public static extern int llama_max_devices();
  42. /// <summary>
  43. /// Create a LLamaModelParams with default values
  44. /// </summary>
  45. /// <returns></returns>
  46. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  47. public static extern LLamaModelParams llama_model_default_params();
  48. /// <summary>
  49. /// Create a LLamaContextParams with default values
  50. /// </summary>
  51. /// <returns></returns>
  52. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  53. public static extern LLamaContextParams llama_context_default_params();
  54. /// <summary>
  55. /// Create a LLamaModelQuantizeParams with default values
  56. /// </summary>
  57. /// <returns></returns>
  58. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  59. public static extern LLamaModelQuantizeParams llama_model_quantize_default_params();
  60. /// <summary>
  61. /// Check if memory mapping is supported
  62. /// </summary>
  63. /// <returns></returns>
  64. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  65. public static extern bool llama_mmap_supported();
  66. /// <summary>
  67. /// Check if memory lockingis supported
  68. /// </summary>
  69. /// <returns></returns>
  70. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  71. public static extern bool llama_mlock_supported();
  72. /// <summary>
  73. /// Various functions for loading a ggml llama model.
  74. /// Allocate (almost) all memory needed for the model.
  75. /// Return NULL on failure
  76. /// </summary>
  77. /// <param name="path_model"></param>
  78. /// <param name="params"></param>
  79. /// <returns></returns>
  80. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  81. public static extern IntPtr llama_load_model_from_file(string path_model, LLamaModelParams @params);
  82. /// <summary>
  83. /// Create a new llama_context with the given model.
  84. /// Return value should always be wrapped in SafeLLamaContextHandle!
  85. /// </summary>
  86. /// <param name="model"></param>
  87. /// <param name="params"></param>
  88. /// <returns></returns>
  89. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  90. public static extern IntPtr llama_new_context_with_model(SafeLlamaModelHandle model, LLamaContextParams @params);
  91. /// <summary>
  92. /// not great API - very likely to change.
  93. /// Initialize the llama + ggml backend
  94. /// Call once at the start of the program
  95. /// </summary>
  96. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  97. public static extern void llama_backend_init(bool numa);
  98. /// <summary>
  99. /// Frees all allocated memory in the given llama_context
  100. /// </summary>
  101. /// <param name="ctx"></param>
  102. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  103. public static extern void llama_free(IntPtr ctx);
  104. /// <summary>
  105. /// Frees all allocated memory associated with a model
  106. /// </summary>
  107. /// <param name="model"></param>
  108. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  109. public static extern void llama_free_model(IntPtr model);
  110. /// <summary>
  111. /// Apply a LoRA adapter to a loaded model
  112. /// path_base_model is the path to a higher quality model to use as a base for
  113. /// the layers modified by the adapter. Can be NULL to use the current loaded model.
  114. /// The model needs to be reloaded before applying a new adapter, otherwise the adapter
  115. /// will be applied on top of the previous one
  116. /// </summary>
  117. /// <param name="model_ptr"></param>
  118. /// <param name="path_lora"></param>
  119. /// <param name="scale"></param>
  120. /// <param name="path_base_model"></param>
  121. /// <param name="n_threads"></param>
  122. /// <returns>Returns 0 on success</returns>
  123. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  124. public static extern int llama_model_apply_lora_from_file(SafeLlamaModelHandle model_ptr, string path_lora, float scale, string? path_base_model, int n_threads);
  125. /// <summary>
  126. /// Sets the current rng seed.
  127. /// </summary>
  128. /// <param name="ctx"></param>
  129. /// <param name="seed"></param>
  130. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  131. public static extern void llama_set_rng_seed(SafeLLamaContextHandle ctx, uint seed);
  132. /// <summary>
  133. /// Returns the maximum size in bytes of the state (rng, logits, embedding
  134. /// and kv_cache) - will often be smaller after compacting tokens
  135. /// </summary>
  136. /// <param name="ctx"></param>
  137. /// <returns></returns>
  138. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  139. public static extern ulong llama_get_state_size(SafeLLamaContextHandle ctx);
  140. /// <summary>
  141. /// Copies the state to the specified destination address.
  142. /// Destination needs to have allocated enough memory.
  143. /// </summary>
  144. /// <param name="ctx"></param>
  145. /// <param name="dest"></param>
  146. /// <returns>the number of bytes copied</returns>
  147. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  148. public static extern ulong llama_copy_state_data(SafeLLamaContextHandle ctx, byte* dest);
  149. /// <summary>
  150. /// Set the state reading from the specified address
  151. /// </summary>
  152. /// <param name="ctx"></param>
  153. /// <param name="src"></param>
  154. /// <returns>the number of bytes read</returns>
  155. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  156. public static extern ulong llama_set_state_data(SafeLLamaContextHandle ctx, byte* src);
  157. /// <summary>
  158. /// Load session file
  159. /// </summary>
  160. /// <param name="ctx"></param>
  161. /// <param name="path_session"></param>
  162. /// <param name="tokens_out"></param>
  163. /// <param name="n_token_capacity"></param>
  164. /// <param name="n_token_count_out"></param>
  165. /// <returns></returns>
  166. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  167. public static extern bool llama_load_session_file(SafeLLamaContextHandle ctx, string path_session, llama_token[] tokens_out, ulong n_token_capacity, ulong* n_token_count_out);
  168. /// <summary>
  169. /// Save session file
  170. /// </summary>
  171. /// <param name="ctx"></param>
  172. /// <param name="path_session"></param>
  173. /// <param name="tokens"></param>
  174. /// <param name="n_token_count"></param>
  175. /// <returns></returns>
  176. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  177. public static extern bool llama_save_session_file(SafeLLamaContextHandle ctx, string path_session, llama_token[] tokens, ulong n_token_count);
  178. /// <summary>
  179. /// Run the llama inference to obtain the logits and probabilities for the next token.
  180. /// tokens + n_tokens is the provided batch of new tokens to process
  181. /// n_past is the number of tokens to use from previous eval calls
  182. /// </summary>
  183. /// <param name="ctx"></param>
  184. /// <param name="tokens"></param>
  185. /// <param name="n_tokens"></param>
  186. /// <param name="n_past"></param>
  187. /// <returns>Returns 0 on success</returns>
  188. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  189. [Obsolete("use llama_decode() instead")]
  190. public static extern int llama_eval(SafeLLamaContextHandle ctx, llama_token* tokens, int n_tokens, int n_past);
  191. /// <summary>
  192. /// Convert the provided text into tokens.
  193. /// </summary>
  194. /// <param name="ctx"></param>
  195. /// <param name="text"></param>
  196. /// <param name="encoding"></param>
  197. /// <param name="tokens"></param>
  198. /// <param name="n_max_tokens"></param>
  199. /// <param name="add_bos"></param>
  200. /// <param name="special">Allow tokenizing special and/or control tokens which otherwise are not exposed and treated as plaintext. Does not insert a leading space.</param>
  201. /// <returns>Returns the number of tokens on success, no more than n_max_tokens.
  202. /// Returns a negative number on failure - the number of tokens that would have been returned
  203. /// </returns>
  204. public static int llama_tokenize(SafeLLamaContextHandle ctx, string text, Encoding encoding, llama_token[] tokens, int n_max_tokens, bool add_bos, bool special)
  205. {
  206. // Calculate number of bytes in text and borrow an array that large (+1 for nul byte)
  207. var byteCount = encoding.GetByteCount(text);
  208. var array = ArrayPool<byte>.Shared.Rent(byteCount + 1);
  209. try
  210. {
  211. // Convert to bytes
  212. fixed (char* textPtr = text)
  213. fixed (byte* arrayPtr = array)
  214. {
  215. encoding.GetBytes(textPtr, text.Length, arrayPtr, array.Length);
  216. }
  217. // Add a zero byte to the end to terminate the string
  218. array[byteCount] = 0;
  219. // Do the actual tokenization
  220. fixed (byte* arrayPtr = array)
  221. fixed (llama_token* tokensPtr = tokens)
  222. return llama_tokenize(ctx.ModelHandle, arrayPtr, byteCount, tokensPtr, n_max_tokens, add_bos, special);
  223. }
  224. finally
  225. {
  226. ArrayPool<byte>.Shared.Return(array);
  227. }
  228. }
  229. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  230. public static extern LLamaTokenType llama_token_get_type(SafeLlamaModelHandle model, llama_token token);
  231. /// <summary>
  232. /// Get the size of the context window for the model for this context
  233. /// </summary>
  234. /// <param name="ctx"></param>
  235. /// <returns></returns>
  236. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  237. public static extern int llama_n_ctx(SafeLLamaContextHandle ctx);
  238. /// <summary>
  239. /// Token logits obtained from the last call to llama_eval()
  240. /// The logits for the last token are stored in the last row
  241. /// Can be mutated in order to change the probabilities of the next token.<br />
  242. /// Rows: n_tokens<br />
  243. /// Cols: n_vocab
  244. /// </summary>
  245. /// <param name="ctx"></param>
  246. /// <returns></returns>
  247. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  248. public static extern float* llama_get_logits(SafeLLamaContextHandle ctx);
  249. /// <summary>
  250. /// Logits for the ith token. Equivalent to: llama_get_logits(ctx) + i*n_vocab
  251. /// </summary>
  252. /// <param name="ctx"></param>
  253. /// <param name="i"></param>
  254. /// <returns></returns>
  255. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  256. public static extern float* llama_get_logits_ith(SafeLLamaContextHandle ctx, int i);
  257. /// <summary>
  258. /// Get the embeddings for the input
  259. /// shape: [n_embd] (1-dimensional)
  260. /// </summary>
  261. /// <param name="ctx"></param>
  262. /// <returns></returns>
  263. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  264. public static extern float* llama_get_embeddings(SafeLLamaContextHandle ctx);
  265. /// <summary>
  266. /// Get the "Beginning of sentence" token
  267. /// </summary>
  268. /// <returns></returns>
  269. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  270. public static extern llama_token llama_token_bos(SafeLlamaModelHandle model);
  271. /// <summary>
  272. /// Get the "End of sentence" token
  273. /// </summary>
  274. /// <returns></returns>
  275. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  276. public static extern llama_token llama_token_eos(SafeLlamaModelHandle model);
  277. /// <summary>
  278. /// Get the "new line" token
  279. /// </summary>
  280. /// <returns></returns>
  281. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  282. public static extern llama_token llama_token_nl(SafeLlamaModelHandle model);
  283. /// <summary>
  284. /// Returns -1 if unknown, 1 for true or 0 for false.
  285. /// </summary>
  286. /// <returns></returns>
  287. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  288. public static extern int llama_add_bos_token(SafeLlamaModelHandle model);
  289. /// <summary>
  290. /// Returns -1 if unknown, 1 for true or 0 for false.
  291. /// </summary>
  292. /// <returns></returns>
  293. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  294. public static extern int llama_add_eos_token(SafeLlamaModelHandle model);
  295. /// <summary>
  296. /// Print out timing information for this context
  297. /// </summary>
  298. /// <param name="ctx"></param>
  299. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  300. public static extern void llama_print_timings(SafeLLamaContextHandle ctx);
  301. /// <summary>
  302. /// Reset all collected timing information for this context
  303. /// </summary>
  304. /// <param name="ctx"></param>
  305. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  306. public static extern void llama_reset_timings(SafeLLamaContextHandle ctx);
  307. /// <summary>
  308. /// Print system information
  309. /// </summary>
  310. /// <returns></returns>
  311. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  312. public static extern IntPtr llama_print_system_info();
  313. /// <summary>
  314. /// Get the number of tokens in the model vocabulary
  315. /// </summary>
  316. /// <param name="model"></param>
  317. /// <returns></returns>
  318. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  319. public static extern int llama_n_vocab(SafeLlamaModelHandle model);
  320. /// <summary>
  321. /// Get the size of the context window for the model
  322. /// </summary>
  323. /// <param name="model"></param>
  324. /// <returns></returns>
  325. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  326. public static extern int llama_n_ctx_train(SafeLlamaModelHandle model);
  327. /// <summary>
  328. /// Get the dimension of embedding vectors from this model
  329. /// </summary>
  330. /// <param name="model"></param>
  331. /// <returns></returns>
  332. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  333. public static extern int llama_n_embd(SafeLlamaModelHandle model);
  334. /// <summary>
  335. /// Get the model's RoPE frequency scaling factor
  336. /// </summary>
  337. /// <param name="model"></param>
  338. /// <returns></returns>
  339. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  340. public static extern float llama_rope_freq_scale_train(SafeLlamaModelHandle model);
  341. /// <summary>
  342. /// Get metadata value as a string by key name
  343. /// </summary>
  344. /// <param name="model"></param>
  345. /// <param name="key"></param>
  346. /// <param name="buf"></param>
  347. /// <param name="buf_size"></param>
  348. /// <returns>The length of the string on success, or -1 on failure</returns>
  349. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  350. public static extern int llama_model_meta_val_str(SafeLlamaModelHandle model, byte* key, byte* buf, long buf_size);
  351. /// <summary>
  352. /// Get the number of metadata key/value pairs
  353. /// </summary>
  354. /// <param name="model"></param>
  355. /// <returns></returns>
  356. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  357. public static extern int llama_model_meta_count(SafeLlamaModelHandle model);
  358. /// <summary>
  359. /// Get metadata key name by index
  360. /// </summary>
  361. /// <param name="model"></param>
  362. /// <param name="index"></param>
  363. /// <param name="buf"></param>
  364. /// <param name="buf_size"></param>
  365. /// <returns>The length of the string on success, or -1 on failure</returns>
  366. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  367. public static extern int llama_model_meta_key_by_index(SafeLlamaModelHandle model, int index, byte* buf, long buf_size);
  368. /// <summary>
  369. /// Get metadata value as a string by index
  370. /// </summary>
  371. /// <param name="model"></param>
  372. /// <param name="index"></param>
  373. /// <param name="buf"></param>
  374. /// <param name="buf_size"></param>
  375. /// <returns>The length of the string on success, or -1 on failure</returns>
  376. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  377. public static extern int llama_model_meta_val_str_by_index(SafeLlamaModelHandle model, int index, byte* buf, long buf_size);
  378. /// <summary>
  379. /// Get a string describing the model type
  380. /// </summary>
  381. /// <param name="model"></param>
  382. /// <param name="buf"></param>
  383. /// <param name="buf_size"></param>
  384. /// <returns>The length of the string on success, or -1 on failure</returns>
  385. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  386. public static extern int llama_model_desc(SafeLlamaModelHandle model, byte* buf, long buf_size);
  387. /// <summary>
  388. /// Get the size of the model in bytes
  389. /// </summary>
  390. /// <param name="model"></param>
  391. /// <returns>The size of the model</returns>
  392. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  393. public static extern ulong llama_model_size(SafeLlamaModelHandle model);
  394. /// <summary>
  395. /// Get the number of parameters in this model
  396. /// </summary>
  397. /// <param name="model"></param>
  398. /// <returns>The functions return the length of the string on success, or -1 on failure</returns>
  399. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  400. public static extern ulong llama_model_n_params(SafeLlamaModelHandle model);
  401. /// <summary>
  402. /// Convert a single token into text
  403. /// </summary>
  404. /// <param name="model"></param>
  405. /// <param name="llamaToken"></param>
  406. /// <param name="buffer">buffer to write string into</param>
  407. /// <param name="length">size of the buffer</param>
  408. /// <returns>The length written, or if the buffer is too small a negative that indicates the length required</returns>
  409. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  410. public static extern int llama_token_to_piece(SafeLlamaModelHandle model, int llamaToken, byte* buffer, int length);
  411. /// <summary>
  412. /// Convert text into tokens
  413. /// </summary>
  414. /// <param name="model"></param>
  415. /// <param name="text"></param>
  416. /// <param name="text_len"></param>
  417. /// <param name="tokens"></param>
  418. /// <param name="n_max_tokens"></param>
  419. /// <param name="add_bos"></param>
  420. /// <param name="special">Allow tokenizing special and/or control tokens which otherwise are not exposed and treated as plaintext. Does not insert a leading space.</param>
  421. /// <returns>Returns the number of tokens on success, no more than n_max_tokens.
  422. /// Returns a negative number on failure - the number of tokens that would have been returned
  423. /// </returns>
  424. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  425. public static extern int llama_tokenize(SafeLlamaModelHandle model, byte* text, int text_len, int* tokens, int n_max_tokens, bool add_bos, bool special);
  426. /// <summary>
  427. /// Register a callback to receive llama log messages
  428. /// </summary>
  429. /// <param name="logCallback"></param>
  430. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  431. public static extern void llama_log_set(LLamaLogCallback logCallback);
  432. /// <summary>
  433. /// Remove all tokens data of cells in [c0, c1)
  434. /// </summary>
  435. /// <param name="ctx"></param>
  436. /// <param name="c0"></param>
  437. /// <param name="c1"></param>
  438. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  439. public static extern void llama_kv_cache_tokens_rm(SafeLLamaContextHandle ctx, int c0, int c1);
  440. /// <summary>
  441. /// Removes all tokens that belong to the specified sequence and have positions in [p0, p1)
  442. /// </summary>
  443. /// <param name="ctx"></param>
  444. /// <param name="seq"></param>
  445. /// <param name="p0"></param>
  446. /// <param name="p1"></param>
  447. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  448. public static extern void llama_kv_cache_seq_rm(SafeLLamaContextHandle ctx, LLamaSeqId seq, LLamaPos p0, LLamaPos p1);
  449. /// <summary>
  450. /// Copy all tokens that belong to the specified sequence to another sequence
  451. /// Note that this does not allocate extra KV cache memory - it simply assigns the tokens to the new sequence
  452. /// </summary>
  453. /// <param name="ctx"></param>
  454. /// <param name="src"></param>
  455. /// <param name="dest"></param>
  456. /// <param name="p0"></param>
  457. /// <param name="p1"></param>
  458. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  459. public static extern void llama_kv_cache_seq_cp(SafeLLamaContextHandle ctx, LLamaSeqId src, LLamaSeqId dest, LLamaPos p0, LLamaPos p1);
  460. /// <summary>
  461. /// Removes all tokens that do not belong to the specified sequence
  462. /// </summary>
  463. /// <param name="ctx"></param>
  464. /// <param name="seq"></param>
  465. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  466. public static extern void llama_kv_cache_seq_keep(SafeLLamaContextHandle ctx, LLamaSeqId seq);
  467. /// <summary>
  468. /// Adds relative position "delta" to all tokens that belong to the specified sequence and have positions in [p0, p1)
  469. /// If the KV cache is RoPEd, the KV data is updated accordingly
  470. /// </summary>
  471. /// <param name="ctx"></param>
  472. /// <param name="seq"></param>
  473. /// <param name="p0"></param>
  474. /// <param name="p1"></param>
  475. /// <param name="delta"></param>
  476. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  477. public static extern void llama_kv_cache_seq_shift(SafeLLamaContextHandle ctx, LLamaSeqId seq, LLamaPos p0, LLamaPos p1, LLamaPos delta);
  478. /// <summary>
  479. /// Allocates a batch of tokens on the heap
  480. /// Each token can be assigned up to n_seq_max sequence ids
  481. /// The batch has to be freed with llama_batch_free()
  482. /// If embd != 0, llama_batch.embd will be allocated with size of n_tokens * embd * sizeof(float)
  483. /// Otherwise, llama_batch.token will be allocated to store n_tokens llama_token
  484. /// The rest of the llama_batch members are allocated with size n_tokens
  485. /// All members are left uninitialized
  486. /// </summary>
  487. /// <param name="n_tokens"></param>
  488. /// <param name="embd"></param>
  489. /// <param name="n_seq_max">Each token can be assigned up to n_seq_max sequence ids</param>
  490. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  491. public static extern LLamaNativeBatch llama_batch_init(int n_tokens, int embd, int n_seq_max);
  492. /// <summary>
  493. /// Frees a batch of tokens allocated with llama_batch_init()
  494. /// </summary>
  495. /// <param name="batch"></param>
  496. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  497. public static extern void llama_batch_free(LLamaNativeBatch batch);
  498. /// <summary>
  499. /// </summary>
  500. /// <param name="ctx"></param>
  501. /// <param name="batch"></param>
  502. /// <returns>Positive return values does not mean a fatal error, but rather a warning:<br />
  503. /// - 0: success<br />
  504. /// - 1: could not find a KV slot for the batch (try reducing the size of the batch or increase the context)<br />
  505. /// - &lt; 0: error<br />
  506. /// </returns>
  507. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  508. public static extern int llama_decode(SafeLLamaContextHandle ctx, LLamaNativeBatch batch);
  509. /// <summary>
  510. /// Set the number of threads used for decoding
  511. /// </summary>
  512. /// <param name="ctx"></param>
  513. /// <param name="n_threads">n_threads is the number of threads used for generation (single token)</param>
  514. /// <param name="n_threads_batch">n_threads_batch is the number of threads used for prompt and batch processing (multiple tokens)</param>
  515. /// <returns></returns>
  516. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  517. public static extern int llama_set_n_threads(SafeLLamaContextHandle ctx, uint n_threads, uint n_threads_batch);
  518. }
  519. }