- Sanity checking that weights are not disposed when creating a context from them - Further simplified `Utils.InitLLamaContextFromModelParams`tags/v0.5.1
| @@ -74,8 +74,7 @@ namespace LLama | |||||
| _ctx = Utils.InitLLamaContextFromModelParams(Params); | _ctx = Utils.InitLLamaContextFromModelParams(Params); | ||||
| } | } | ||||
| //todo make this internal | |||||
| public LLamaContext(SafeLLamaContextHandle nativeContext, IModelParams @params, Encoding encoding, ILLamaLogger? logger = null) | |||||
| internal LLamaContext(SafeLLamaContextHandle nativeContext, IModelParams @params, Encoding encoding, ILLamaLogger? logger = null) | |||||
| { | { | ||||
| Params = @params; | Params = @params; | ||||
| @@ -86,6 +85,9 @@ namespace LLama | |||||
| public LLamaContext(LLamaWeights model, IModelParams @params, Encoding encoding, ILLamaLogger? logger = null) | public LLamaContext(LLamaWeights model, IModelParams @params, Encoding encoding, ILLamaLogger? logger = null) | ||||
| { | { | ||||
| if (model.NativeHandle.IsClosed) | |||||
| throw new ObjectDisposedException("Cannot create context, model weights have been disposed"); | |||||
| Params = @params; | Params = @params; | ||||
| _logger = logger; | _logger = logger; | ||||
| @@ -1,6 +1,6 @@ | |||||
| using System; | using System; | ||||
| using System.Text; | using System.Text; | ||||
| using LLama.Common; | |||||
| using LLama.Abstractions; | |||||
| using LLama.Extensions; | using LLama.Extensions; | ||||
| using LLama.Native; | using LLama.Native; | ||||
| @@ -30,10 +30,14 @@ namespace LLama | |||||
| /// </summary> | /// </summary> | ||||
| /// <param name="params"></param> | /// <param name="params"></param> | ||||
| /// <returns></returns> | /// <returns></returns> | ||||
| public static LLamaWeights LoadFromFile(ModelParams @params) | |||||
| public static LLamaWeights LoadFromFile(IModelParams @params) | |||||
| { | { | ||||
| using var pin = @params.ToLlamaContextParams(out var lparams); | using var pin = @params.ToLlamaContextParams(out var lparams); | ||||
| var weights = SafeLlamaModelHandle.LoadFromFile(@params.ModelPath, lparams); | var weights = SafeLlamaModelHandle.LoadFromFile(@params.ModelPath, lparams); | ||||
| if (!string.IsNullOrEmpty(@params.LoraAdapter)) | |||||
| weights.ApplyLoraFromFile(@params.LoraAdapter, @params.LoraBase, @params.Threads); | |||||
| return new LLamaWeights(weights); | return new LLamaWeights(weights); | ||||
| } | } | ||||
| @@ -47,11 +51,11 @@ namespace LLama | |||||
| /// Create a llama_context using this model | /// Create a llama_context using this model | ||||
| /// </summary> | /// </summary> | ||||
| /// <param name="params"></param> | /// <param name="params"></param> | ||||
| /// <param name="utf8"></param> | |||||
| /// <param name="encoding"></param> | |||||
| /// <returns></returns> | /// <returns></returns> | ||||
| public LLamaContext CreateContext(ModelParams @params, Encoding utf8) | |||||
| public LLamaContext CreateContext(IModelParams @params, Encoding encoding) | |||||
| { | { | ||||
| return new LLamaContext(this, @params, Encoding.UTF8); | |||||
| return new LLamaContext(this, @params, encoding); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| @@ -13,16 +13,10 @@ namespace LLama | |||||
| { | { | ||||
| public static SafeLLamaContextHandle InitLLamaContextFromModelParams(IModelParams @params) | public static SafeLLamaContextHandle InitLLamaContextFromModelParams(IModelParams @params) | ||||
| { | { | ||||
| using (@params.ToLlamaContextParams(out var lparams)) | |||||
| { | |||||
| var model = SafeLlamaModelHandle.LoadFromFile(@params.ModelPath, lparams); | |||||
| var ctx = SafeLLamaContextHandle.Create(model, lparams); | |||||
| if (!string.IsNullOrEmpty(@params.LoraAdapter)) | |||||
| model.ApplyLoraFromFile(@params.LoraAdapter, @params.LoraBase, @params.Threads); | |||||
| using var weights = LLamaWeights.LoadFromFile(@params); | |||||
| return ctx; | |||||
| } | |||||
| using (@params.ToLlamaContextParams(out var lparams)) | |||||
| return SafeLLamaContextHandle.Create(weights.NativeHandle, lparams); | |||||
| } | } | ||||
| [Obsolete("Use SafeLLamaContextHandle Tokenize method instead")] | [Obsolete("Use SafeLLamaContextHandle Tokenize method instead")] | ||||