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.8 kB

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

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