Browse Source

- Apply LoRA in `LLamaWeights.LoadFromFile`

- Sanity checking that weights are not disposed when creating a context from them
 - Further simplified `Utils.InitLLamaContextFromModelParams`
tags/v0.5.1
Martin Evans 2 years ago
parent
commit
20bdc2ec6f
3 changed files with 16 additions and 16 deletions
  1. +4
    -2
      LLama/LLamaContext.cs
  2. +9
    -5
      LLama/LLamaWeights.cs
  3. +3
    -9
      LLama/Utils.cs

+ 4
- 2
LLama/LLamaContext.cs View File

@@ -74,8 +74,7 @@ namespace LLama
_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;

@@ -86,6 +85,9 @@ namespace LLama

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;

_logger = logger;


+ 9
- 5
LLama/LLamaWeights.cs View File

@@ -1,6 +1,6 @@
using System;
using System.Text;
using LLama.Common;
using LLama.Abstractions;
using LLama.Extensions;
using LLama.Native;

@@ -30,10 +30,14 @@ namespace LLama
/// </summary>
/// <param name="params"></param>
/// <returns></returns>
public static LLamaWeights LoadFromFile(ModelParams @params)
public static LLamaWeights LoadFromFile(IModelParams @params)
{
using var pin = @params.ToLlamaContextParams(out var 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);
}

@@ -47,11 +51,11 @@ namespace LLama
/// Create a llama_context using this model
/// </summary>
/// <param name="params"></param>
/// <param name="utf8"></param>
/// <param name="encoding"></param>
/// <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);
}
}
}

+ 3
- 9
LLama/Utils.cs View File

@@ -13,16 +13,10 @@ namespace LLama
{
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")]


Loading…
Cancel
Save