Skip to content

< Back


SafeLlamaModelHandle

Namespace: LLama.Native

A reference to a set of llama model weights

1
public sealed class SafeLlamaModelHandle : SafeLLamaHandleBase, System.IDisposable

Inheritance ObjectCriticalFinalizerObjectSafeHandleSafeLLamaHandleBaseSafeLlamaModelHandle
Implements IDisposable
Attributes NullableContextAttribute, NullableAttribute

Fields

handle

1
protected IntPtr handle;

Properties

RopeType

Get the rope (positional embedding) type for this model

1
public LLamaRopeType RopeType { get; }

Property Value

LLamaRopeType

ContextSize

The number of tokens in the context that this model was trained for

1
public int ContextSize { get; }

Property Value

Int32

RopeFrequency

Get the rope frequency this model was trained with

1
public float RopeFrequency { get; }

Property Value

Single

EmbeddingSize

Dimension of embedding vectors

1
public int EmbeddingSize { get; }

Property Value

Int32

SizeInBytes

Get the size of this model in bytes

1
public ulong SizeInBytes { get; }

Property Value

UInt64

ParameterCount

Get the number of parameters in this model

1
public ulong ParameterCount { get; }

Property Value

UInt64

LayerCount

Get the number of layers in this model

1
public int LayerCount { get; }

Property Value

Int32

HeadCount

Get the number of heads in this model

1
public int HeadCount { get; }

Property Value

Int32

KVHeadCount

Get the number of KV heads in this model

1
public int KVHeadCount { get; }

Property Value

Int32

HasEncoder

Returns true if the model contains an encoder that requires llama_encode() call

1
public bool HasEncoder { get; }

Property Value

Boolean

HasDecoder

Returns true if the model contains a decoder that requires llama_decode() call

1
public bool HasDecoder { get; }

Property Value

Boolean

IsRecurrent

Returns true if the model is recurrent (like Mamba, RWKV, etc.)

1
public bool IsRecurrent { get; }

Property Value

Boolean

Description

Get a description of this model

1
public string Description { get; }

Property Value

String

MetadataCount

Get the number of metadata key/value pairs

1
public int MetadataCount { get; }

Property Value

Int32

Vocab

Get the vocabulary of this model

1
public Vocabulary Vocab { get; }

Property Value

Vocabulary

IsInvalid

1
public bool IsInvalid { get; }

Property Value

Boolean

IsClosed

1
public bool IsClosed { get; }

Property Value

Boolean

Constructors

SafeLlamaModelHandle()

1
public SafeLlamaModelHandle()

Methods

ReleaseHandle()

1
protected bool ReleaseHandle()

Returns

Boolean

LoadFromFile(String, LLamaModelParams)

Load a model from the given file path into memory

1
public static SafeLlamaModelHandle LoadFromFile(string modelPath, LLamaModelParams lparams)

Parameters

modelPath String

lparams LLamaModelParams

Returns

SafeLlamaModelHandle

Exceptions

RuntimeError

LoadLoraFromFile(String)

Load a LoRA adapter from file. The adapter will be associated with this model but will not be applied

1
public LoraAdapter LoadLoraFromFile(string path)

Parameters

path String

Returns

LoraAdapter

Exceptions

InvalidOperationException

TokenToSpan(LLamaToken, Span<Byte>, Int32, Boolean)

Convert a single llama token into bytes

1
public uint TokenToSpan(LLamaToken token, Span<byte> dest, int lstrip, bool special)

Parameters

token LLamaToken
Token to decode

dest Span<Byte>
A span to attempt to write into. If this is too small nothing will be written

lstrip Int32
User can skip up to 'lstrip' leading spaces before copying (useful when encoding/decoding multiple tokens with 'add_space_prefix')

special Boolean
If true, special characters will be converted to text. If false they will be invisible.

Returns

UInt32
The size of this token. nothing will be written if this is larger than dest

Tokenize(String, Boolean, Boolean, Encoding)

Convert a string of text into tokens

1
public LLamaToken[] Tokenize(string text, bool addBos, bool special, Encoding encoding)

Parameters

text String

addBos Boolean

special Boolean
Allow tokenizing special and/or control tokens which otherwise are not exposed and treated as plaintext.

encoding Encoding

Returns

LLamaToken[]

CreateContext(LLamaContextParams)

Create a new context for this model

1
public SafeLLamaContextHandle CreateContext(LLamaContextParams params)

Parameters

params LLamaContextParams

Returns

SafeLLamaContextHandle

MetadataValueByKey(String)

Get the metadata value for the given key

1
public Nullable<Memory<byte>> MetadataValueByKey(string key)

Parameters

key String
The key to fetch

Returns

Nullable<Memory<Byte>>
The value, null if there is no such key

MetadataKeyByIndex(Int32)

Get the metadata key for the given index

1
public Nullable<Memory<byte>> MetadataKeyByIndex(int index)

Parameters

index Int32
The index to get

Returns

Nullable<Memory<Byte>>
The key, null if there is no such key or if the buffer was too small

MetadataValueByIndex(Int32)

Get the metadata value for the given index

1
public Nullable<Memory<byte>> MetadataValueByIndex(int index)

Parameters

index Int32
The index to get

Returns

Nullable<Memory<Byte>>
The value, null if there is no such value or if the buffer was too small

GetTemplate(String, Boolean)

Get the default chat template. Returns nullptr if not available If name is NULL, returns the default chat template

1
public string GetTemplate(string name, bool strict)

Parameters

name String
The name of the template, in case there are many or differently named. Set to 'null' for the default behaviour of finding an appropriate match.

strict Boolean
Setting this to true will cause the call to throw if no valid templates are found.

Returns

String


< Back