From 44b1e93609b6bc21a97e5fffa947c64cc690a3e2 Mon Sep 17 00:00:00 2001 From: Martin Evans Date: Tue, 25 Jul 2023 16:35:24 +0100 Subject: [PATCH] Moved LoRA loading into `SafeLlamaModelHandle` --- LLama/Native/SafeLlamaModelHandle.cs | 21 +++++++++++++++++++++ LLama/OldVersion/Utils.cs | 10 ++-------- LLama/Utils.cs | 13 +------------ 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/LLama/Native/SafeLlamaModelHandle.cs b/LLama/Native/SafeLlamaModelHandle.cs index 5607ccee..4e3b8044 100644 --- a/LLama/Native/SafeLlamaModelHandle.cs +++ b/LLama/Native/SafeLlamaModelHandle.cs @@ -37,5 +37,26 @@ namespace LLama.Native return new SafeLlamaModelHandle(model_ptr); } + + /// + /// Apply a LoRA adapter to a loaded model + /// + /// + /// A path to a higher quality model to use as a base for the layers modified by the + /// adapter. Can be NULL to use the current loaded model. + /// + /// + public void ApplyLoraFromFile(string lora, string? modelBase = null, int threads = -1) + { + var err = NativeApi.llama_model_apply_lora_from_file( + this, + lora, + string.IsNullOrEmpty(modelBase) ? null : modelBase, + threads + ); + + if (err != 0) + throw new RuntimeError("Failed to apply lora adapter."); + } } } diff --git a/LLama/OldVersion/Utils.cs b/LLama/OldVersion/Utils.cs index 646ce365..df8adddd 100644 --- a/LLama/OldVersion/Utils.cs +++ b/LLama/OldVersion/Utils.cs @@ -35,14 +35,8 @@ namespace LLama.OldVersion var ctx = SafeLLamaContextHandle.Create(model, lparams); if (!string.IsNullOrEmpty(@params.lora_adapter)) - { - int err = NativeApi.llama_model_apply_lora_from_file(model, @params.lora_adapter, - string.IsNullOrEmpty(@params.lora_base) ? null : @params.lora_base, @params.n_threads); - if (err != 0) - { - throw new RuntimeError("Failed to apply lora adapter."); - } - } + model.ApplyLoraFromFile(@params.lora_adapter, @params.lora_base, @params.n_threads); + return ctx; } diff --git a/LLama/Utils.cs b/LLama/Utils.cs index 8ee084ec..c2dbf7aa 100644 --- a/LLama/Utils.cs +++ b/LLama/Utils.cs @@ -48,19 +48,8 @@ namespace LLama var ctx = SafeLLamaContextHandle.Create(model, lparams); if (!string.IsNullOrEmpty(@params.LoraAdapter)) - { - var err = NativeApi.llama_model_apply_lora_from_file( - model, - @params.LoraAdapter, - string.IsNullOrEmpty(@params.LoraBase) ? null : @params.LoraBase, - @params.Threads - ); + model.ApplyLoraFromFile(@params.LoraAdapter, @params.LoraBase, @params.Threads); - if (err != 0) - { - throw new RuntimeError("Failed to apply lora adapter."); - } - } return ctx; }