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