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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. namespace LLama.Native
  7. {
  8. using llama_token = Int32;
  9. internal unsafe partial class NativeApi
  10. {
  11. static NativeApi()
  12. {
  13. }
  14. private const string libraryName = "libllama";
  15. [DllImport(libraryName)]
  16. public static extern LLamaContextParams llama_context_default_params();
  17. [DllImport(libraryName)]
  18. public static extern bool llama_mmap_supported();
  19. [DllImport(libraryName)]
  20. public static extern bool llama_mlock_supported();
  21. /// <summary>
  22. /// Various functions for loading a ggml llama model.
  23. /// Allocate (almost) all memory needed for the model.
  24. /// Return NULL on failure
  25. /// </summary>
  26. /// <param name="path_model"></param>
  27. /// <param name="params_"></param>
  28. /// <returns></returns>
  29. [DllImport(libraryName)]
  30. public static extern IntPtr llama_init_from_file(string path_model, LLamaContextParams params_);
  31. /// <summary>
  32. /// Frees all allocated memory
  33. /// </summary>
  34. /// <param name="ctx"></param>
  35. [DllImport(libraryName)]
  36. public static extern void llama_free(IntPtr ctx);
  37. /// <summary>
  38. /// Returns 0 on success
  39. /// </summary>
  40. /// <param name="fname_inp"></param>
  41. /// <param name="fname_out"></param>
  42. /// <param name="ftype"></param>
  43. /// <param name="nthread">how many threads to use. If <=0, will use std::thread::hardware_concurrency(), else the number given</param>
  44. /// <remarks>not great API - very likely to change</remarks>
  45. /// <returns>Returns 0 on success</returns>
  46. [DllImport(libraryName)]
  47. public static extern int llama_model_quantize(string fname_inp, string fname_out, LLamaFtype ftype, int nthread);
  48. /// <summary>
  49. /// Apply a LoRA adapter to a loaded model
  50. /// path_base_model is the path to a higher quality model to use as a base for
  51. /// the layers modified by the adapter. Can be NULL to use the current loaded model.
  52. /// The model needs to be reloaded before applying a new adapter, otherwise the adapter
  53. /// will be applied on top of the previous one
  54. /// </summary>
  55. /// <param name="ctx"></param>
  56. /// <param name="path_lora"></param>
  57. /// <param name="path_base_model"></param>
  58. /// <param name="n_threads"></param>
  59. /// <returns>Returns 0 on success</returns>
  60. [DllImport(libraryName)]
  61. public static extern int llama_apply_lora_from_file(SafeLLamaContextHandle ctx, string path_lora, string path_base_model, int n_threads);
  62. /// <summary>
  63. /// Returns the number of tokens in the KV cache
  64. /// </summary>
  65. /// <param name="ctx"></param>
  66. /// <returns></returns>
  67. [DllImport(libraryName)]
  68. public static extern int llama_get_kv_cache_token_count(SafeLLamaContextHandle ctx);
  69. /// <summary>
  70. /// Sets the current rng seed.
  71. /// </summary>
  72. /// <param name="ctx"></param>
  73. /// <param name="seed"></param>
  74. [DllImport(libraryName)]
  75. public static extern void llama_set_rng_seed(SafeLLamaContextHandle ctx, int seed);
  76. /// <summary>
  77. /// Returns the maximum size in bytes of the state (rng, logits, embedding
  78. /// and kv_cache) - will often be smaller after compacting tokens
  79. /// </summary>
  80. /// <param name="ctx"></param>
  81. /// <returns></returns>
  82. [DllImport(libraryName)]
  83. public static extern ulong llama_get_state_size(SafeLLamaContextHandle ctx);
  84. /// <summary>
  85. /// Copies the state to the specified destination address.
  86. /// Destination needs to have allocated enough memory.
  87. /// Returns the number of bytes copied
  88. /// </summary>
  89. /// <param name="ctx"></param>
  90. /// <param name="dest"></param>
  91. /// <returns></returns>
  92. [DllImport(libraryName)]
  93. public static extern ulong llama_copy_state_data(SafeLLamaContextHandle ctx, byte[] dest);
  94. /// <summary>
  95. /// Set the state reading from the specified address
  96. /// Returns the number of bytes read
  97. /// </summary>
  98. /// <param name="ctx"></param>
  99. /// <param name="src"></param>
  100. /// <returns></returns>
  101. [DllImport(libraryName)]
  102. public static extern ulong llama_set_state_data(SafeLLamaContextHandle ctx, byte[] src);
  103. /// <summary>
  104. /// Load session file
  105. /// </summary>
  106. /// <param name="ctx"></param>
  107. /// <param name="path_session"></param>
  108. /// <param name="tokens_out"></param>
  109. /// <param name="n_token_capacity"></param>
  110. /// <param name="n_token_count_out"></param>
  111. /// <returns></returns>
  112. [DllImport(libraryName)]
  113. 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);
  114. /// <summary>
  115. /// Save session file
  116. /// </summary>
  117. /// <param name="ctx"></param>
  118. /// <param name="path_session"></param>
  119. /// <param name="tokens"></param>
  120. /// <param name="n_token_count"></param>
  121. /// <returns></returns>
  122. [DllImport(libraryName)]
  123. public static extern bool llama_save_session_file(SafeLLamaContextHandle ctx, string path_session, llama_token[] tokens, ulong n_token_count);
  124. /// <summary>
  125. /// Run the llama inference to obtain the logits and probabilities for the next token.
  126. /// tokens + n_tokens is the provided batch of new tokens to process
  127. /// n_past is the number of tokens to use from previous eval calls
  128. /// </summary>
  129. /// <param name="ctx"></param>
  130. /// <param name="tokens"></param>
  131. /// <param name="n_tokens"></param>
  132. /// <param name="n_past"></param>
  133. /// <param name="n_threads"></param>
  134. /// <returns>Returns 0 on success</returns>
  135. [DllImport(libraryName)]
  136. public static extern int llama_eval(SafeLLamaContextHandle ctx, llama_token[] tokens, int n_tokens, int n_past, int n_threads);
  137. /// <summary>
  138. /// Convert the provided text into tokens.
  139. /// The tokens pointer must be large enough to hold the resulting tokens.
  140. /// Returns the number of tokens on success, no more than n_max_tokens
  141. /// Returns a negative number on failure - the number of tokens that would have been returned
  142. /// </summary>
  143. /// <param name="ctx"></param>
  144. /// <param name="text"></param>
  145. /// <param name="tokens"></param>
  146. /// <param name="n_max_tokens"></param>
  147. /// <param name="add_bos"></param>
  148. /// <returns></returns>
  149. [DllImport(libraryName)]
  150. public static extern int llama_tokenize(SafeLLamaContextHandle ctx, string text, llama_token[] tokens, int n_max_tokens, bool add_bos);
  151. [DllImport(libraryName)]
  152. public static extern int llama_n_vocab(SafeLLamaContextHandle ctx);
  153. [DllImport(libraryName)]
  154. public static extern int llama_n_ctx(SafeLLamaContextHandle ctx);
  155. [DllImport(libraryName)]
  156. public static extern int llama_n_embd(SafeLLamaContextHandle ctx);
  157. /// <summary>
  158. /// Token logits obtained from the last call to llama_eval()
  159. /// The logits for the last token are stored in the last row
  160. /// Can be mutated in order to change the probabilities of the next token
  161. /// Rows: n_tokens
  162. /// Cols: n_vocab
  163. /// </summary>
  164. /// <param name="ctx"></param>
  165. /// <returns></returns>
  166. [DllImport(libraryName)]
  167. public static extern float* llama_get_logits(SafeLLamaContextHandle ctx);
  168. /// <summary>
  169. /// Get the embeddings for the input
  170. /// shape: [n_embd] (1-dimensional)
  171. /// </summary>
  172. /// <param name="ctx"></param>
  173. /// <returns></returns>
  174. [DllImport(libraryName)]
  175. public static extern float* llama_get_embeddings(SafeLLamaContextHandle ctx);
  176. /// <summary>
  177. /// Token Id -> String. Uses the vocabulary in the provided context
  178. /// </summary>
  179. /// <param name="ctx"></param>
  180. /// <param name="token"></param>
  181. /// <returns>Pointer to a string.</returns>
  182. [DllImport(libraryName)]
  183. public static extern IntPtr llama_token_to_str(SafeLLamaContextHandle ctx, llama_token token);
  184. [DllImport(libraryName)]
  185. public static extern llama_token llama_token_bos();
  186. [DllImport(libraryName)]
  187. public static extern llama_token llama_token_eos();
  188. [DllImport(libraryName)]
  189. public static extern llama_token llama_token_nl();
  190. [DllImport(libraryName)]
  191. public static extern void llama_print_timings(SafeLLamaContextHandle ctx);
  192. [DllImport(libraryName)]
  193. public static extern void llama_reset_timings(SafeLLamaContextHandle ctx);
  194. /// <summary>
  195. /// Print system information
  196. /// </summary>
  197. /// <returns></returns>
  198. [DllImport(libraryName)]
  199. public static extern IntPtr llama_print_system_info();
  200. }
  201. }

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

Contributors (1)