Namespace: LLama.Native
A safe wrapper around a llama_context
public sealed class SafeLLamaContextHandle : SafeLLamaHandleBase, System.IDisposable
Inheritance Object → CriticalFinalizerObject → SafeHandle → SafeLLamaHandleBase → SafeLLamaContextHandle
Implements IDisposable
Total number of tokens in vocabulary of this model
public int VocabCount { get; }
Total number of tokens in the context
public int ContextSize { get; }
Dimension of embedding vectors
public int EmbeddingSize { get; }
Get the model which this context is using
public SafeLlamaModelHandle ModelHandle { get; }
public bool IsInvalid { get; }
public bool IsClosed { get; }
Create a new SafeLLamaContextHandle
public SafeLLamaContextHandle(IntPtr handle, SafeLlamaModelHandle model)
handle IntPtr
pointer to an allocated llama_context
model SafeLlamaModelHandle
the model which this context was created from
protected bool ReleaseHandle()
Create a new llama_state for the given model
public static SafeLLamaContextHandle Create(SafeLlamaModelHandle model, LLamaContextParams lparams)
model SafeLlamaModelHandle
lparams LLamaContextParams
Create a new llama context with a clone of the current llama context state
public SafeLLamaContextHandle Clone(LLamaContextParams lparams)
lparams LLamaContextParams
Convert the given text into tokens
public Int32[] Tokenize(string text, bool add_bos, Encoding encoding)
text String
The text to tokenize
add_bos Boolean
Whether the "BOS" token should be added
encoding Encoding
Encoding to use for the text
Token logits obtained from the last call to llama_eval()
The logits for the last token are stored in the last row
Can be mutated in order to change the probabilities of the next token.
Rows: n_tokens
Cols: n_vocab
public Span<float> GetLogits()
Convert a token into a string
public string TokenToString(int token, Encoding encoding)
token Int32
Token to decode into a string
encoding Encoding
Append a single llama token to a string builder
public void TokenToString(int token, Encoding encoding, StringBuilder dest)
token Int32
Token to decode
encoding Encoding
dest StringBuilder
string builder to append the result to
Convert a single llama token into bytes
public int TokenToSpan(int token, Span<byte> dest)
token Int32
Token to decode
dest Span<Byte>
A span to attempt to write into. If this is too small nothing will be written
Int32
The size of this token. nothing will be written if this is larger than dest
Run the llama inference to obtain the logits and probabilities for the next token.
public bool Eval(ReadOnlySpan<int> tokens, int n_past, int n_threads)
tokens ReadOnlySpan<Int32>
The provided batch of new tokens to process
n_past Int32
the number of tokens to use from previous eval calls
n_threads Int32
Boolean
Returns true on success
Get the size of the state, when saved as bytes
public ulong GetStateSize()
Get the raw state of this context, encoded as bytes. Data is written into the dest pointer.
public ulong GetState(Byte* dest, ulong size)
dest Byte*
Destination to write to
size UInt64
Number of bytes available to write to in dest (check required size with GetStateSize())
UInt64
The number of bytes written to dest
ArgumentOutOfRangeException
Thrown if dest is too small
Get the raw state of this context, encoded as bytes. Data is written into the dest pointer.
public ulong GetState(IntPtr dest, ulong size)
dest IntPtr
Destination to write to
size UInt64
Number of bytes available to write to in dest (check required size with GetStateSize())
UInt64
The number of bytes written to dest
ArgumentOutOfRangeException
Thrown if dest is too small
Set the raw state of this context
public ulong SetState(Byte* src)
src Byte*
The pointer to read the state from
UInt64
Number of bytes read from the src pointer
Set the raw state of this context
public ulong SetState(IntPtr src)
src IntPtr
The pointer to read the state from
UInt64
Number of bytes read from the src pointer