Browse Source

Replaced `nint` with `float[]?` in Model params, which is much more user friendly!

tags/v0.5.1
Martin Evans 2 years ago
parent
commit
685eb3b9c2
3 changed files with 21 additions and 23 deletions
  1. +1
    -1
      LLama/Abstractions/IModelParams.cs
  2. +3
    -4
      LLama/Common/ModelParams.cs
  3. +17
    -18
      LLama/Utils.cs

+ 1
- 1
LLama/Abstractions/IModelParams.cs View File

@@ -93,7 +93,7 @@ namespace LLama.Abstractions
/// <summary>
/// how split tensors should be distributed across GPUs
/// </summary>
nint TensorSplits { get; set; }
float[]? TensorSplits { get; set; }

/// <summary>
/// Grouped-Query Attention


+ 3
- 4
LLama/Common/ModelParams.cs View File

@@ -1,14 +1,13 @@
using LLama.Abstractions;
using System;
using System.Collections.Generic;
using System.Text;

namespace LLama.Common
{
/// <summary>
/// The parameters for initializing a LLama model.
/// </summary>
public class ModelParams : IModelParams
public class ModelParams
: IModelParams
{
/// <summary>
/// Model context size (n_ctx)
@@ -85,7 +84,7 @@ namespace LLama.Common
/// <summary>
/// how split tensors should be distributed across GPUs
/// </summary>
public nint TensorSplits { get; set; }
public float[]? TensorSplits { get; set; }

/// <summary>
/// Grouped-Query Attention


+ 17
- 18
LLama/Utils.cs View File

@@ -15,8 +15,13 @@ namespace LLama
{
public static SafeLLamaContextHandle InitLLamaContextFromModelParams(IModelParams @params)
{
var lparams = NativeApi.llama_context_default_params();
if (!File.Exists(@params.ModelPath))
throw new FileNotFoundException($"The model file does not exist: {@params.ModelPath}");

if (@params.TensorSplits != null && @params.TensorSplits.Length != 1)
throw new ArgumentException("Currently multi-gpu support is not supported by both llama.cpp and LLamaSharp.");

var lparams = NativeApi.llama_context_default_params();
lparams.n_ctx = @params.ContextSize;
lparams.n_batch = @params.BatchSize;
lparams.main_gpu = @params.MainGpu;
@@ -34,27 +39,21 @@ namespace LLama
lparams.rope_freq_scale = @params.RopeFrequencyScale;
lparams.mul_mat_q = @params.MulMatQ;

/*
if (@params.TensorSplits.Length != 1)
{
throw new ArgumentException("Currently multi-gpu support is not supported by " +
"both llama.cpp and LLamaSharp.");
}*/

lparams.tensor_split = @params.TensorSplits;

if (!File.Exists(@params.ModelPath))
unsafe
{
throw new FileNotFoundException($"The model file does not exist: {@params.ModelPath}");
}
fixed (float* splits = @params.TensorSplits)
{
lparams.tensor_split = (nint)splits;

var model = SafeLlamaModelHandle.LoadFromFile(@params.ModelPath, lparams);
var ctx = SafeLLamaContextHandle.Create(model, 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);
if (!string.IsNullOrEmpty(@params.LoraAdapter))
model.ApplyLoraFromFile(@params.LoraAdapter, @params.LoraBase, @params.Threads);

return ctx;
return ctx;
}
}
}

public static IEnumerable<llama_token> Tokenize(SafeLLamaContextHandle ctx, string text, bool add_bos, Encoding encoding)


Loading…
Cancel
Save