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

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

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

Contributors (1)