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 29 kB

2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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 byte* llama_token_get_text(SafeLlamaModelHandle model, llama_token token);
  231. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  232. public static extern float llama_token_get_score(SafeLlamaModelHandle model, llama_token token);
  233. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  234. public static extern LLamaTokenType llama_token_get_type(SafeLlamaModelHandle model, llama_token token);
  235. /// <summary>
  236. /// Get the size of the context window for the model for this context
  237. /// </summary>
  238. /// <param name="ctx"></param>
  239. /// <returns></returns>
  240. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  241. public static extern int llama_n_ctx(SafeLLamaContextHandle ctx);
  242. /// <summary>
  243. /// Token logits obtained from the last call to llama_eval()
  244. /// The logits for the last token are stored in the last row
  245. /// Can be mutated in order to change the probabilities of the next token.<br />
  246. /// Rows: n_tokens<br />
  247. /// Cols: n_vocab
  248. /// </summary>
  249. /// <param name="ctx"></param>
  250. /// <returns></returns>
  251. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  252. public static extern float* llama_get_logits(SafeLLamaContextHandle ctx);
  253. /// <summary>
  254. /// Logits for the ith token. Equivalent to: llama_get_logits(ctx) + i*n_vocab
  255. /// </summary>
  256. /// <param name="ctx"></param>
  257. /// <param name="i"></param>
  258. /// <returns></returns>
  259. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  260. public static extern float* llama_get_logits_ith(SafeLLamaContextHandle ctx, int i);
  261. /// <summary>
  262. /// Get the embeddings for the input
  263. /// shape: [n_embd] (1-dimensional)
  264. /// </summary>
  265. /// <param name="ctx"></param>
  266. /// <returns></returns>
  267. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  268. public static extern float* llama_get_embeddings(SafeLLamaContextHandle ctx);
  269. /// <summary>
  270. /// Get the "Beginning of sentence" token
  271. /// </summary>
  272. /// <returns></returns>
  273. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  274. public static extern llama_token llama_token_bos(SafeLlamaModelHandle model);
  275. /// <summary>
  276. /// Get the "End of sentence" token
  277. /// </summary>
  278. /// <returns></returns>
  279. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  280. public static extern llama_token llama_token_eos(SafeLlamaModelHandle model);
  281. /// <summary>
  282. /// Get the "new line" token
  283. /// </summary>
  284. /// <returns></returns>
  285. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  286. public static extern llama_token llama_token_nl(SafeLlamaModelHandle model);
  287. /// <summary>
  288. /// Returns -1 if unknown, 1 for true or 0 for false.
  289. /// </summary>
  290. /// <returns></returns>
  291. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  292. public static extern int llama_add_bos_token(SafeLlamaModelHandle model);
  293. /// <summary>
  294. /// Returns -1 if unknown, 1 for true or 0 for false.
  295. /// </summary>
  296. /// <returns></returns>
  297. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  298. public static extern int llama_add_eos_token(SafeLlamaModelHandle model);
  299. /// <summary>
  300. /// codellama infill tokens, Beginning of infill prefix
  301. /// </summary>
  302. /// <returns></returns>
  303. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  304. public static extern int llama_token_prefix(SafeLlamaModelHandle model);
  305. /// <summary>
  306. /// codellama infill tokens, Beginning of infill middle
  307. /// </summary>
  308. /// <returns></returns>
  309. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  310. public static extern int llama_token_middle(SafeLlamaModelHandle model);
  311. /// <summary>
  312. /// codellama infill tokens, Beginning of infill suffix
  313. /// </summary>
  314. /// <returns></returns>
  315. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  316. public static extern int llama_token_suffix(SafeLlamaModelHandle model);
  317. /// <summary>
  318. /// codellama infill tokens, End of infill middle
  319. /// </summary>
  320. /// <returns></returns>
  321. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  322. public static extern int llama_token_eot(SafeLlamaModelHandle model);
  323. /// <summary>
  324. /// Print out timing information for this context
  325. /// </summary>
  326. /// <param name="ctx"></param>
  327. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  328. public static extern void llama_print_timings(SafeLLamaContextHandle ctx);
  329. /// <summary>
  330. /// Reset all collected timing information for this context
  331. /// </summary>
  332. /// <param name="ctx"></param>
  333. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  334. public static extern void llama_reset_timings(SafeLLamaContextHandle ctx);
  335. /// <summary>
  336. /// Print system information
  337. /// </summary>
  338. /// <returns></returns>
  339. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  340. public static extern IntPtr llama_print_system_info();
  341. /// <summary>
  342. /// Get the number of tokens in the model vocabulary
  343. /// </summary>
  344. /// <param name="model"></param>
  345. /// <returns></returns>
  346. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  347. public static extern int llama_n_vocab(SafeLlamaModelHandle model);
  348. /// <summary>
  349. /// Get the size of the context window for the model
  350. /// </summary>
  351. /// <param name="model"></param>
  352. /// <returns></returns>
  353. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  354. public static extern int llama_n_ctx_train(SafeLlamaModelHandle model);
  355. /// <summary>
  356. /// Get the dimension of embedding vectors from this model
  357. /// </summary>
  358. /// <param name="model"></param>
  359. /// <returns></returns>
  360. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  361. public static extern int llama_n_embd(SafeLlamaModelHandle model);
  362. /// <summary>
  363. /// Get the model's RoPE frequency scaling factor
  364. /// </summary>
  365. /// <param name="model"></param>
  366. /// <returns></returns>
  367. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  368. public static extern float llama_rope_freq_scale_train(SafeLlamaModelHandle model);
  369. /// <summary>
  370. /// Get metadata value as a string by key name
  371. /// </summary>
  372. /// <param name="model"></param>
  373. /// <param name="key"></param>
  374. /// <param name="buf"></param>
  375. /// <param name="buf_size"></param>
  376. /// <returns>The length of the string on success, or -1 on failure</returns>
  377. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  378. public static extern int llama_model_meta_val_str(SafeLlamaModelHandle model, byte* key, byte* buf, long buf_size);
  379. /// <summary>
  380. /// Get the number of metadata key/value pairs
  381. /// </summary>
  382. /// <param name="model"></param>
  383. /// <returns></returns>
  384. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  385. public static extern int llama_model_meta_count(SafeLlamaModelHandle model);
  386. /// <summary>
  387. /// Get metadata key name by index
  388. /// </summary>
  389. /// <param name="model"></param>
  390. /// <param name="index"></param>
  391. /// <param name="buf"></param>
  392. /// <param name="buf_size"></param>
  393. /// <returns>The length of the string on success, or -1 on failure</returns>
  394. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  395. public static extern int llama_model_meta_key_by_index(SafeLlamaModelHandle model, int index, byte* buf, long buf_size);
  396. /// <summary>
  397. /// Get metadata value as a string by index
  398. /// </summary>
  399. /// <param name="model"></param>
  400. /// <param name="index"></param>
  401. /// <param name="buf"></param>
  402. /// <param name="buf_size"></param>
  403. /// <returns>The length of the string on success, or -1 on failure</returns>
  404. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  405. public static extern int llama_model_meta_val_str_by_index(SafeLlamaModelHandle model, int index, byte* buf, long buf_size);
  406. /// <summary>
  407. /// Get a string describing the model type
  408. /// </summary>
  409. /// <param name="model"></param>
  410. /// <param name="buf"></param>
  411. /// <param name="buf_size"></param>
  412. /// <returns>The length of the string on success, or -1 on failure</returns>
  413. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  414. public static extern int llama_model_desc(SafeLlamaModelHandle model, byte* buf, long buf_size);
  415. /// <summary>
  416. /// Get the size of the model in bytes
  417. /// </summary>
  418. /// <param name="model"></param>
  419. /// <returns>The size of the model</returns>
  420. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  421. public static extern ulong llama_model_size(SafeLlamaModelHandle model);
  422. /// <summary>
  423. /// Get the number of parameters in this model
  424. /// </summary>
  425. /// <param name="model"></param>
  426. /// <returns>The functions return the length of the string on success, or -1 on failure</returns>
  427. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  428. public static extern ulong llama_model_n_params(SafeLlamaModelHandle model);
  429. /// <summary>
  430. /// Convert a single token into text
  431. /// </summary>
  432. /// <param name="model"></param>
  433. /// <param name="llamaToken"></param>
  434. /// <param name="buffer">buffer to write string into</param>
  435. /// <param name="length">size of the buffer</param>
  436. /// <returns>The length written, or if the buffer is too small a negative that indicates the length required</returns>
  437. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  438. public static extern int llama_token_to_piece(SafeLlamaModelHandle model, int llamaToken, byte* buffer, int length);
  439. /// <summary>
  440. /// Convert text into tokens
  441. /// </summary>
  442. /// <param name="model"></param>
  443. /// <param name="text"></param>
  444. /// <param name="text_len"></param>
  445. /// <param name="tokens"></param>
  446. /// <param name="n_max_tokens"></param>
  447. /// <param name="add_bos"></param>
  448. /// <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>
  449. /// <returns>Returns the number of tokens on success, no more than n_max_tokens.
  450. /// Returns a negative number on failure - the number of tokens that would have been returned
  451. /// </returns>
  452. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  453. public static extern int llama_tokenize(SafeLlamaModelHandle model, byte* text, int text_len, int* tokens, int n_max_tokens, bool add_bos, bool special);
  454. /// <summary>
  455. /// Register a callback to receive llama log messages
  456. /// </summary>
  457. /// <param name="logCallback"></param>
  458. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  459. public static extern void llama_log_set(LLamaLogCallback logCallback);
  460. /// <summary>
  461. /// Clear the KV cache
  462. /// </summary>
  463. /// <param name="ctx"></param>
  464. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  465. public static extern void llama_kv_cache_clear(SafeLLamaContextHandle ctx);
  466. /// <summary>
  467. /// Removes all tokens that belong to the specified sequence and have positions in [p0, p1)
  468. /// </summary>
  469. /// <param name="ctx"></param>
  470. /// <param name="seq"></param>
  471. /// <param name="p0"></param>
  472. /// <param name="p1"></param>
  473. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  474. public static extern void llama_kv_cache_seq_rm(SafeLLamaContextHandle ctx, LLamaSeqId seq, LLamaPos p0, LLamaPos p1);
  475. /// <summary>
  476. /// Copy all tokens that belong to the specified sequence to another sequence
  477. /// Note that this does not allocate extra KV cache memory - it simply assigns the tokens to the new sequence
  478. /// </summary>
  479. /// <param name="ctx"></param>
  480. /// <param name="src"></param>
  481. /// <param name="dest"></param>
  482. /// <param name="p0"></param>
  483. /// <param name="p1"></param>
  484. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  485. public static extern void llama_kv_cache_seq_cp(SafeLLamaContextHandle ctx, LLamaSeqId src, LLamaSeqId dest, LLamaPos p0, LLamaPos p1);
  486. /// <summary>
  487. /// Removes all tokens that do not belong to the specified sequence
  488. /// </summary>
  489. /// <param name="ctx"></param>
  490. /// <param name="seq"></param>
  491. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  492. public static extern void llama_kv_cache_seq_keep(SafeLLamaContextHandle ctx, LLamaSeqId seq);
  493. /// <summary>
  494. /// Adds relative position "delta" to all tokens that belong to the specified sequence and have positions in [p0, p1)
  495. /// If the KV cache is RoPEd, the KV data is updated accordingly
  496. /// </summary>
  497. /// <param name="ctx"></param>
  498. /// <param name="seq"></param>
  499. /// <param name="p0"></param>
  500. /// <param name="p1"></param>
  501. /// <param name="delta"></param>
  502. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  503. public static extern void llama_kv_cache_seq_shift(SafeLLamaContextHandle ctx, LLamaSeqId seq, LLamaPos p0, LLamaPos p1, LLamaPos delta);
  504. /// <summary>
  505. /// Allocates a batch of tokens on the heap
  506. /// Each token can be assigned up to n_seq_max sequence ids
  507. /// The batch has to be freed with llama_batch_free()
  508. /// If embd != 0, llama_batch.embd will be allocated with size of n_tokens * embd * sizeof(float)
  509. /// Otherwise, llama_batch.token will be allocated to store n_tokens llama_token
  510. /// The rest of the llama_batch members are allocated with size n_tokens
  511. /// All members are left uninitialized
  512. /// </summary>
  513. /// <param name="n_tokens"></param>
  514. /// <param name="embd"></param>
  515. /// <param name="n_seq_max">Each token can be assigned up to n_seq_max sequence ids</param>
  516. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  517. public static extern LLamaNativeBatch llama_batch_init(int n_tokens, int embd, int n_seq_max);
  518. /// <summary>
  519. /// Frees a batch of tokens allocated with llama_batch_init()
  520. /// </summary>
  521. /// <param name="batch"></param>
  522. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  523. public static extern void llama_batch_free(LLamaNativeBatch batch);
  524. /// <summary>
  525. /// </summary>
  526. /// <param name="ctx"></param>
  527. /// <param name="batch"></param>
  528. /// <returns>Positive return values does not mean a fatal error, but rather a warning:<br />
  529. /// - 0: success<br />
  530. /// - 1: could not find a KV slot for the batch (try reducing the size of the batch or increase the context)<br />
  531. /// - &lt; 0: error<br />
  532. /// </returns>
  533. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  534. public static extern int llama_decode(SafeLLamaContextHandle ctx, LLamaNativeBatch batch);
  535. /// <summary>
  536. /// Set the number of threads used for decoding
  537. /// </summary>
  538. /// <param name="ctx"></param>
  539. /// <param name="n_threads">n_threads is the number of threads used for generation (single token)</param>
  540. /// <param name="n_threads_batch">n_threads_batch is the number of threads used for prompt and batch processing (multiple tokens)</param>
  541. /// <returns></returns>
  542. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  543. public static extern int llama_set_n_threads(SafeLLamaContextHandle ctx, uint n_threads, uint n_threads_batch);
  544. }
  545. }