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.safellamacontexthandle.md 9.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. # SafeLLamaContextHandle
  2. Namespace: LLama.Native
  3. A safe wrapper around a llama_context
  4. ```csharp
  5. public sealed class SafeLLamaContextHandle : 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) → [SafeLLamaContextHandle](./llama.native.safellamacontexthandle.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. ### **EmbeddingSize**
  25. Dimension of embedding vectors
  26. ```csharp
  27. public int EmbeddingSize { get; }
  28. ```
  29. #### Property Value
  30. [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  31. ### **ModelHandle**
  32. Get the model which this context is using
  33. ```csharp
  34. public SafeLlamaModelHandle ModelHandle { get; }
  35. ```
  36. #### Property Value
  37. [SafeLlamaModelHandle](./llama.native.safellamamodelhandle.md)<br>
  38. ### **IsInvalid**
  39. ```csharp
  40. public bool IsInvalid { get; }
  41. ```
  42. #### Property Value
  43. [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)<br>
  44. ### **IsClosed**
  45. ```csharp
  46. public bool IsClosed { get; }
  47. ```
  48. #### Property Value
  49. [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)<br>
  50. ## Constructors
  51. ### **SafeLLamaContextHandle(IntPtr, SafeLlamaModelHandle)**
  52. Create a new SafeLLamaContextHandle
  53. ```csharp
  54. public SafeLLamaContextHandle(IntPtr handle, SafeLlamaModelHandle model)
  55. ```
  56. #### Parameters
  57. `handle` [IntPtr](https://docs.microsoft.com/en-us/dotnet/api/system.intptr)<br>
  58. pointer to an allocated llama_context
  59. `model` [SafeLlamaModelHandle](./llama.native.safellamamodelhandle.md)<br>
  60. the model which this context was created from
  61. ## Methods
  62. ### **ReleaseHandle()**
  63. ```csharp
  64. protected bool ReleaseHandle()
  65. ```
  66. #### Returns
  67. [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)<br>
  68. ### **Create(SafeLlamaModelHandle, LLamaContextParams)**
  69. Create a new llama_state for the given model
  70. ```csharp
  71. public static SafeLLamaContextHandle Create(SafeLlamaModelHandle model, LLamaContextParams lparams)
  72. ```
  73. #### Parameters
  74. `model` [SafeLlamaModelHandle](./llama.native.safellamamodelhandle.md)<br>
  75. `lparams` [LLamaContextParams](./llama.native.llamacontextparams.md)<br>
  76. #### Returns
  77. [SafeLLamaContextHandle](./llama.native.safellamacontexthandle.md)<br>
  78. #### Exceptions
  79. [RuntimeError](./llama.exceptions.runtimeerror.md)<br>
  80. ### **Clone(LLamaContextParams)**
  81. Create a new llama context with a clone of the current llama context state
  82. ```csharp
  83. public SafeLLamaContextHandle Clone(LLamaContextParams lparams)
  84. ```
  85. #### Parameters
  86. `lparams` [LLamaContextParams](./llama.native.llamacontextparams.md)<br>
  87. #### Returns
  88. [SafeLLamaContextHandle](./llama.native.safellamacontexthandle.md)<br>
  89. ### **Tokenize(String, Boolean, Encoding)**
  90. Convert the given text into tokens
  91. ```csharp
  92. public Int32[] Tokenize(string text, bool add_bos, Encoding encoding)
  93. ```
  94. #### Parameters
  95. `text` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>
  96. The text to tokenize
  97. `add_bos` [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)<br>
  98. Whether the "BOS" token should be added
  99. `encoding` [Encoding](https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding)<br>
  100. Encoding to use for the text
  101. #### Returns
  102. [Int32[]](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  103. #### Exceptions
  104. [RuntimeError](./llama.exceptions.runtimeerror.md)<br>
  105. ### **GetLogits()**
  106. Token logits obtained from the last call to llama_eval()
  107. The logits for the last token are stored in the last row
  108. Can be mutated in order to change the probabilities of the next token.<br>
  109. Rows: n_tokens<br>
  110. Cols: n_vocab
  111. ```csharp
  112. public Span<float> GetLogits()
  113. ```
  114. #### Returns
  115. [Span&lt;Single&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.span-1)<br>
  116. ### **TokenToString(Int32, Encoding)**
  117. Convert a token into a string
  118. ```csharp
  119. public string TokenToString(int token, Encoding encoding)
  120. ```
  121. #### Parameters
  122. `token` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  123. Token to decode into a string
  124. `encoding` [Encoding](https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding)<br>
  125. #### Returns
  126. [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>
  127. ### **TokenToString(Int32, Encoding, StringBuilder)**
  128. Append a single llama token to a string builder
  129. ```csharp
  130. public void TokenToString(int token, Encoding encoding, StringBuilder dest)
  131. ```
  132. #### Parameters
  133. `token` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  134. Token to decode
  135. `encoding` [Encoding](https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding)<br>
  136. `dest` [StringBuilder](https://docs.microsoft.com/en-us/dotnet/api/system.text.stringbuilder)<br>
  137. string builder to append the result to
  138. ### **TokenToSpan(Int32, Span&lt;Byte&gt;)**
  139. Convert a single llama token into bytes
  140. ```csharp
  141. public int TokenToSpan(int token, Span<byte> dest)
  142. ```
  143. #### Parameters
  144. `token` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  145. Token to decode
  146. `dest` [Span&lt;Byte&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.span-1)<br>
  147. A span to attempt to write into. If this is too small nothing will be written
  148. #### Returns
  149. [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  150. The size of this token. **nothing will be written** if this is larger than `dest`
  151. ### **Eval(ReadOnlySpan&lt;Int32&gt;, Int32, Int32)**
  152. Run the llama inference to obtain the logits and probabilities for the next token.
  153. ```csharp
  154. public bool Eval(ReadOnlySpan<int> tokens, int n_past, int n_threads)
  155. ```
  156. #### Parameters
  157. `tokens` [ReadOnlySpan&lt;Int32&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.readonlyspan-1)<br>
  158. The provided batch of new tokens to process
  159. `n_past` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  160. the number of tokens to use from previous eval calls
  161. `n_threads` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  162. #### Returns
  163. [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)<br>
  164. Returns true on success
  165. ### **GetStateSize()**
  166. Get the size of the state, when saved as bytes
  167. ```csharp
  168. public ulong GetStateSize()
  169. ```
  170. #### Returns
  171. [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)<br>
  172. ### **GetState(Byte*, UInt64)**
  173. Get the raw state of this context, encoded as bytes. Data is written into the `dest` pointer.
  174. ```csharp
  175. public ulong GetState(Byte* dest, ulong size)
  176. ```
  177. #### Parameters
  178. `dest` [Byte*](https://docs.microsoft.com/en-us/dotnet/api/system.byte*)<br>
  179. Destination to write to
  180. `size` [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)<br>
  181. Number of bytes available to write to in dest (check required size with `GetStateSize()`)
  182. #### Returns
  183. [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)<br>
  184. The number of bytes written to dest
  185. #### Exceptions
  186. [ArgumentOutOfRangeException](https://docs.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception)<br>
  187. Thrown if dest is too small
  188. ### **GetState(IntPtr, UInt64)**
  189. Get the raw state of this context, encoded as bytes. Data is written into the `dest` pointer.
  190. ```csharp
  191. public ulong GetState(IntPtr dest, ulong size)
  192. ```
  193. #### Parameters
  194. `dest` [IntPtr](https://docs.microsoft.com/en-us/dotnet/api/system.intptr)<br>
  195. Destination to write to
  196. `size` [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)<br>
  197. Number of bytes available to write to in dest (check required size with `GetStateSize()`)
  198. #### Returns
  199. [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)<br>
  200. The number of bytes written to dest
  201. #### Exceptions
  202. [ArgumentOutOfRangeException](https://docs.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception)<br>
  203. Thrown if dest is too small
  204. ### **SetState(Byte*)**
  205. Set the raw state of this context
  206. ```csharp
  207. public ulong SetState(Byte* src)
  208. ```
  209. #### Parameters
  210. `src` [Byte*](https://docs.microsoft.com/en-us/dotnet/api/system.byte*)<br>
  211. The pointer to read the state from
  212. #### Returns
  213. [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)<br>
  214. Number of bytes read from the src pointer
  215. ### **SetState(IntPtr)**
  216. Set the raw state of this context
  217. ```csharp
  218. public ulong SetState(IntPtr src)
  219. ```
  220. #### Parameters
  221. `src` [IntPtr](https://docs.microsoft.com/en-us/dotnet/api/system.intptr)<br>
  222. The pointer to read the state from
  223. #### Returns
  224. [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)<br>
  225. Number of bytes read from the src pointer