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.

llama.native.safellamamodelhandle.md 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. # SafeLlamaModelHandle
  2. Namespace: LLama.Native
  3. A reference to a set of llama model weights
  4. ```csharp
  5. public sealed class SafeLlamaModelHandle : SafeLLamaHandleBase, System.IDisposable
  6. ```
  7. Inheritance [Object](https://docs.microsoft.com/en-us/dotnet/api/system.object) → [CriticalFinalizerObject](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.constrainedexecution.criticalfinalizerobject) → [SafeHandle](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.safehandle) → [SafeLLamaHandleBase](./llama.native.safellamahandlebase.md) → [SafeLlamaModelHandle](./llama.native.safellamamodelhandle.md)<br>
  8. Implements [IDisposable](https://docs.microsoft.com/en-us/dotnet/api/system.idisposable)
  9. ## Properties
  10. ### **VocabCount**
  11. Total number of tokens in vocabulary of this model
  12. ```csharp
  13. public int VocabCount { get; }
  14. ```
  15. #### Property Value
  16. [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  17. ### **ContextSize**
  18. Total number of tokens in the context
  19. ```csharp
  20. public int ContextSize { get; }
  21. ```
  22. #### Property Value
  23. [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  24. ### **RopeFrequency**
  25. Get the rope frequency this model was trained with
  26. ```csharp
  27. public float RopeFrequency { get; }
  28. ```
  29. #### Property Value
  30. [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  31. ### **EmbeddingSize**
  32. Dimension of embedding vectors
  33. ```csharp
  34. public int EmbeddingSize { get; }
  35. ```
  36. #### Property Value
  37. [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  38. ### **SizeInBytes**
  39. Get the size of this model in bytes
  40. ```csharp
  41. public ulong SizeInBytes { get; }
  42. ```
  43. #### Property Value
  44. [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)<br>
  45. ### **ParameterCount**
  46. Get the number of parameters in this model
  47. ```csharp
  48. public ulong ParameterCount { get; }
  49. ```
  50. #### Property Value
  51. [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)<br>
  52. ### **Description**
  53. Get a description of this model
  54. ```csharp
  55. public string Description { get; }
  56. ```
  57. #### Property Value
  58. [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>
  59. ### **MetadataCount**
  60. Get the number of metadata key/value pairs
  61. ```csharp
  62. public int MetadataCount { get; }
  63. ```
  64. #### Property Value
  65. [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  66. ### **IsInvalid**
  67. ```csharp
  68. public bool IsInvalid { get; }
  69. ```
  70. #### Property Value
  71. [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)<br>
  72. ### **IsClosed**
  73. ```csharp
  74. public bool IsClosed { get; }
  75. ```
  76. #### Property Value
  77. [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)<br>
  78. ## Constructors
  79. ### **SafeLlamaModelHandle()**
  80. ```csharp
  81. public SafeLlamaModelHandle()
  82. ```
  83. ## Methods
  84. ### **ReleaseHandle()**
  85. ```csharp
  86. protected bool ReleaseHandle()
  87. ```
  88. #### Returns
  89. [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)<br>
  90. ### **LoadFromFile(String, LLamaModelParams)**
  91. Load a model from the given file path into memory
  92. ```csharp
  93. public static SafeLlamaModelHandle LoadFromFile(string modelPath, LLamaModelParams lparams)
  94. ```
  95. #### Parameters
  96. `modelPath` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>
  97. `lparams` [LLamaModelParams](./llama.native.llamamodelparams.md)<br>
  98. #### Returns
  99. [SafeLlamaModelHandle](./llama.native.safellamamodelhandle.md)<br>
  100. #### Exceptions
  101. [RuntimeError](./llama.exceptions.runtimeerror.md)<br>
  102. ### **llama_model_apply_lora_from_file(SafeLlamaModelHandle, String, Single, String, Int32)**
  103. Apply a LoRA adapter to a loaded model
  104. path_base_model is the path to a higher quality model to use as a base for
  105. the layers modified by the adapter. Can be NULL to use the current loaded model.
  106. The model needs to be reloaded before applying a new adapter, otherwise the adapter
  107. will be applied on top of the previous one
  108. ```csharp
  109. public static int llama_model_apply_lora_from_file(SafeLlamaModelHandle model_ptr, string path_lora, float scale, string path_base_model, int n_threads)
  110. ```
  111. #### Parameters
  112. `model_ptr` [SafeLlamaModelHandle](./llama.native.safellamamodelhandle.md)<br>
  113. `path_lora` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>
  114. `scale` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  115. `path_base_model` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>
  116. `n_threads` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  117. #### Returns
  118. [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  119. Returns 0 on success
  120. ### **llama_model_meta_val_str(SafeLlamaModelHandle, Byte*, Byte*, Int64)**
  121. Get metadata value as a string by key name
  122. ```csharp
  123. public static int llama_model_meta_val_str(SafeLlamaModelHandle model, Byte* key, Byte* buf, long buf_size)
  124. ```
  125. #### Parameters
  126. `model` [SafeLlamaModelHandle](./llama.native.safellamamodelhandle.md)<br>
  127. `key` [Byte*](https://docs.microsoft.com/en-us/dotnet/api/system.byte*)<br>
  128. `buf` [Byte*](https://docs.microsoft.com/en-us/dotnet/api/system.byte*)<br>
  129. `buf_size` [Int64](https://docs.microsoft.com/en-us/dotnet/api/system.int64)<br>
  130. #### Returns
  131. [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  132. The length of the string on success, or -1 on failure
  133. ### **ApplyLoraFromFile(String, Single, String, Nullable&lt;Int32&gt;)**
  134. Apply a LoRA adapter to a loaded model
  135. ```csharp
  136. public void ApplyLoraFromFile(string lora, float scale, string modelBase, Nullable<int> threads)
  137. ```
  138. #### Parameters
  139. `lora` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>
  140. `scale` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  141. `modelBase` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>
  142. A path to a higher quality model to use as a base for the layers modified by the
  143. adapter. Can be NULL to use the current loaded model.
  144. `threads` [Nullable&lt;Int32&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.nullable-1)<br>
  145. #### Exceptions
  146. [RuntimeError](./llama.exceptions.runtimeerror.md)<br>
  147. ### **TokenToSpan(LLamaToken, Span&lt;Byte&gt;)**
  148. Convert a single llama token into bytes
  149. ```csharp
  150. public uint TokenToSpan(LLamaToken token, Span<byte> dest)
  151. ```
  152. #### Parameters
  153. `token` [LLamaToken](./llama.native.llamatoken.md)<br>
  154. Token to decode
  155. `dest` [Span&lt;Byte&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.span-1)<br>
  156. A span to attempt to write into. If this is too small nothing will be written
  157. #### Returns
  158. [UInt32](https://docs.microsoft.com/en-us/dotnet/api/system.uint32)<br>
  159. The size of this token. **nothing will be written** if this is larger than `dest`
  160. ### **TokensToSpan(IReadOnlyList&lt;LLamaToken&gt;, Span&lt;Char&gt;, Encoding)**
  161. #### Caution
  162. Use a StreamingTokenDecoder instead
  163. ---
  164. Convert a sequence of tokens into characters.
  165. ```csharp
  166. internal Span<char> TokensToSpan(IReadOnlyList<LLamaToken> tokens, Span<char> dest, Encoding encoding)
  167. ```
  168. #### Parameters
  169. `tokens` [IReadOnlyList&lt;LLamaToken&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ireadonlylist-1)<br>
  170. `dest` [Span&lt;Char&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.span-1)<br>
  171. `encoding` [Encoding](https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding)<br>
  172. #### Returns
  173. [Span&lt;Char&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.span-1)<br>
  174. The section of the span which has valid data in it.
  175. If there was insufficient space in the output span this will be
  176. filled with as many characters as possible, starting from the _last_ token.
  177. ### **Tokenize(String, Boolean, Boolean, Encoding)**
  178. Convert a string of text into tokens
  179. ```csharp
  180. public LLamaToken[] Tokenize(string text, bool add_bos, bool special, Encoding encoding)
  181. ```
  182. #### Parameters
  183. `text` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>
  184. `add_bos` [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)<br>
  185. `special` [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)<br>
  186. Allow tokenizing special and/or control tokens which otherwise are not exposed and treated as plaintext.
  187. `encoding` [Encoding](https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding)<br>
  188. #### Returns
  189. [LLamaToken[]](./llama.native.llamatoken.md)<br>
  190. ### **CreateContext(LLamaContextParams)**
  191. Create a new context for this model
  192. ```csharp
  193. public SafeLLamaContextHandle CreateContext(LLamaContextParams params)
  194. ```
  195. #### Parameters
  196. `params` [LLamaContextParams](./llama.native.llamacontextparams.md)<br>
  197. #### Returns
  198. [SafeLLamaContextHandle](./llama.native.safellamacontexthandle.md)<br>
  199. ### **MetadataKeyByIndex(Int32)**
  200. Get the metadata key for the given index
  201. ```csharp
  202. public Nullable<Memory<byte>> MetadataKeyByIndex(int index)
  203. ```
  204. #### Parameters
  205. `index` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  206. The index to get
  207. #### Returns
  208. [Nullable&lt;Memory&lt;Byte&gt;&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.nullable-1)<br>
  209. The key, null if there is no such key or if the buffer was too small
  210. ### **MetadataValueByIndex(Int32)**
  211. Get the metadata value for the given index
  212. ```csharp
  213. public Nullable<Memory<byte>> MetadataValueByIndex(int index)
  214. ```
  215. #### Parameters
  216. `index` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  217. The index to get
  218. #### Returns
  219. [Nullable&lt;Memory&lt;Byte&gt;&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.nullable-1)<br>
  220. The value, null if there is no such value or if the buffer was too small
  221. ### **ReadMetadata()**
  222. ```csharp
  223. internal IReadOnlyDictionary<string, string> ReadMetadata()
  224. ```
  225. #### Returns
  226. [IReadOnlyDictionary&lt;String, String&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ireadonlydictionary-2)<br>
  227. ### **&lt;llama_model_meta_key_by_index&gt;g__llama_model_meta_key_by_index_native|23_0(SafeLlamaModelHandle, Int32, Byte*, Int64)**
  228. ```csharp
  229. internal static int <llama_model_meta_key_by_index>g__llama_model_meta_key_by_index_native|23_0(SafeLlamaModelHandle model, int index, Byte* buf, long buf_size)
  230. ```
  231. #### Parameters
  232. `model` [SafeLlamaModelHandle](./llama.native.safellamamodelhandle.md)<br>
  233. `index` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  234. `buf` [Byte*](https://docs.microsoft.com/en-us/dotnet/api/system.byte*)<br>
  235. `buf_size` [Int64](https://docs.microsoft.com/en-us/dotnet/api/system.int64)<br>
  236. #### Returns
  237. [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  238. ### **&lt;llama_model_meta_val_str_by_index&gt;g__llama_model_meta_val_str_by_index_native|24_0(SafeLlamaModelHandle, Int32, Byte*, Int64)**
  239. ```csharp
  240. internal static int <llama_model_meta_val_str_by_index>g__llama_model_meta_val_str_by_index_native|24_0(SafeLlamaModelHandle model, int index, Byte* buf, long buf_size)
  241. ```
  242. #### Parameters
  243. `model` [SafeLlamaModelHandle](./llama.native.safellamamodelhandle.md)<br>
  244. `index` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  245. `buf` [Byte*](https://docs.microsoft.com/en-us/dotnet/api/system.byte*)<br>
  246. `buf_size` [Int64](https://docs.microsoft.com/en-us/dotnet/api/system.int64)<br>
  247. #### Returns
  248. [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>