using System; using System.Collections.Generic; using System.Text; using LLama.Abstractions; using LLama.Extensions; using LLama.Native; using Microsoft.Extensions.Logging; namespace LLama { /// /// A set of model weights, loaded into memory. /// public sealed class LLamaWeights : IDisposable { /// /// The native handle, which is used in the native APIs /// /// Be careful how you use this! public SafeLlamaModelHandle NativeHandle { get; } /// /// Total number of tokens in vocabulary of this model /// public int VocabCount => NativeHandle.VocabCount; /// /// Total number of tokens in the context /// public int ContextSize => NativeHandle.ContextSize; /// /// Get the size of this model in bytes /// public ulong SizeInBytes => NativeHandle.SizeInBytes; /// /// Get the number of parameters in this model /// public ulong ParameterCount => NativeHandle.ParameterCount; /// /// Get the newline token for this model /// public LLamaToken NewlineToken => NativeApi.llama_token_nl(NativeHandle); /// /// Get the "end of sentence" token for this model /// public LLamaToken EndOfSentenceToken => NativeApi.llama_token_eos(NativeHandle); /// /// Get the "beginning of sentence" token for this model /// public LLamaToken BeginningOfSentenceToken => NativeApi.llama_token_bos(NativeHandle); /// /// Dimension of embedding vectors /// public int EmbeddingSize => NativeHandle.EmbeddingSize; /// /// All metadata keys in this model /// public IReadOnlyDictionary Metadata { get; set; } private LLamaWeights(SafeLlamaModelHandle weights) { NativeHandle = weights; Metadata = weights.ReadMetadata(); } /// /// Load weights into memory /// /// /// public static LLamaWeights LoadFromFile(IModelParams @params) { using var pin = @params.ToLlamaModelParams(out var lparams); var weights = SafeLlamaModelHandle.LoadFromFile(@params.ModelPath, lparams); foreach (var adapter in @params.LoraAdapters) { if (string.IsNullOrEmpty(adapter.Path)) continue; if (adapter.Scale <= 0) continue; weights.ApplyLoraFromFile(adapter.Path, adapter.Scale, @params.LoraBase); } return new LLamaWeights(weights); } /// public void Dispose() { NativeHandle.Dispose(); } /// /// Create a llama_context using this model /// /// /// /// public LLamaContext CreateContext(IContextParams @params, ILogger? logger = null) { return new LLamaContext(this, @params, logger); } /// /// Convert a string of text into tokens /// /// /// /// /// Allow tokenizing special and/or control tokens which otherwise are not exposed and treated as plaintext. /// public LLamaToken[] Tokenize(string text, bool add_bos, bool special, Encoding encoding) { return NativeHandle.Tokenize(text, add_bos, special, encoding); } } }